Expose source_tables on the plugin Query message#4473
Open
Prateeks16 wants to merge 2 commits into
Open
Conversation
Plugins receive Query.columns[].table, which only covers tables that contribute a column to a query's output. Tables referenced only in a JOIN, a subquery, or a common table expression body are invisible to plugins, even though the query depends on them (issue sqlc-dev#4434). Add a source_tables field to the plugin Query message listing every base table a query reads from, deduplicated and sorted. Common table expression names and the target relations of INSERT, UPDATE, DELETE, and TRUNCATE statements are excluded.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds “source table” dependency information to compiled queries and exposes it through the plugin/codegen JSON output.
Changes:
- Extend the plugin
Queryschema withsource_tables. - Compute source table names from the AST during compilation and pass them into the plugin shim.
- Update end-to-end JSON fixtures and add unit tests for source table extraction.
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| protos/plugin/codegen.proto | Adds source_tables field to the plugin Query message. |
| internal/compiler/source_tables.go | Introduces AST walker to collect/dedupe/sort read tables and exclude write targets/CTEs. |
| internal/compiler/source_tables_test.go | Adds unit tests covering CTEs, aliases, INSERT read-vs-write, and no-table queries. |
| internal/compiler/query.go | Adds SourceTables field to compiler Query model with doc comment. |
| internal/compiler/parse.go | Populates Query.SourceTables during parse. |
| internal/cmd/shim.go | Copies SourceTables into the plugin Query output. |
| internal/endtoend/testdata/**/codegen.json | Updates golden JSON to include source_tables. |
Files not reviewed (1)
- internal/plugin/codegen.pb.go: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Report schema-qualified names when a schema is present, and deduplicate on the qualified form so same-named tables in different schemas stay distinct. - Return an empty slice rather than nil so the field serializes as []. - Document the field on the proto message and the compiler Query model.
Author
|
Thanks for the review. Addressed in the follow-up commit:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a
source_tablesfield to the pluginQuerymessage: the list of base tables a query reads from, deduplicated and sorted ascending.Why
Plugins currently learn a query's tables only through
Query.columns[].table, which covers only tables that contribute a column to the output. A table referenced solely in aJOIN, a subquery, or a common table expression body is invisible to plugins even though the query depends on it. Plugins that build reactive/refetch logic (for example keyed off SQLite'supdate_hook) need a query's full read set, not just its output columns.What's included
source_tablesfield on theQueryproto (and regeneratedcodegen.pb.go).FROM, aJOIN, or a subquery in any clause, including CTE bodies. Common table expression names and the target relations ofINSERT/UPDATE/DELETE/TRUNCATEare excluded.internal/compiler) covering the example below plus deduplication, write-target exclusion, and the no-tables case. The existing JSON codegen goldens are regenerated to include the new field.For this query:
source_tablesis["account_tags", "accounts", "transactions"]:accountsappears once (read in the CTE body and the outer query),account_tagsappears though it is read only inside aNOT EXISTSsubquery,transactionsappears though it projects no column, andfiltered_accountsis excluded as a CTE name.Closes #4434.