Expose source_tables on the plugin Query message#4472
Closed
Prateeks16 wants to merge 1 commit into
Closed
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. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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 tables” metadata to compiled queries and exposes it through the plugin/codegen JSON output.
Changes:
- Extends
plugin.Queryproto + JSON fixtures with a newsource_tablesfield. - Implements
sourceTableNamesin the compiler to extract read-dependencies (CTEs included, write targets excluded). - Adds unit tests covering key dependency patterns (CTEs, joins, dedup across aliases, INSERT read vs write target).
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| protos/plugin/codegen.proto | Adds source_tables to Query message for plugin consumers |
| internal/compiler/source_tables.go | Implements dependency extraction for base tables read by a statement |
| internal/compiler/source_tables_test.go | Adds unit tests for sourceTableNames behavior |
| internal/compiler/query.go | Adds SourceTables field to compiler Query model |
| internal/compiler/parse.go | Populates SourceTables when parsing queries |
| internal/cmd/shim.go | Plumbs compiler SourceTables into plugin Query |
| internal/endtoend/testdata/process_plugin_sqlc_gen_json/gen/codegen.json | Updates golden JSON outputs to include source_tables |
| internal/endtoend/testdata/codegen_json/gen/codegen.json | Updates golden JSON outputs 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.
Comment on lines
+10
to
+12
| // sourceTableNames returns the sorted, deduplicated names of the base tables a | ||
| // statement reads from. It covers every table referenced in a FROM, a JOIN, or | ||
| // a subquery in any clause, including the bodies of common table expressions. |
Comment on lines
+55
to
+62
| if _, ok := cteNames[table.Name]; ok { | ||
| continue | ||
| } | ||
| if _, ok := seen[table.Name]; ok { | ||
| continue | ||
| } | ||
| seen[table.Name] = struct{}{} | ||
| names = append(names, table.Name) |
Comment on lines
+11
to
+44
| func TestSourceTableNames(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| name string | ||
| sql string | ||
| want []string | ||
| }{ | ||
| { | ||
| name: "cte, join and subquery dependencies", | ||
| sql: `WITH filtered_accounts AS ( | ||
| SELECT account_id FROM accounts WHERE accounts.space_id = $1 | ||
| AND NOT EXISTS ( | ||
| SELECT 1 FROM account_tags t WHERE t.account_id = accounts.account_id | ||
| ) | ||
| ) | ||
| SELECT acc.* FROM accounts acc | ||
| JOIN filtered_accounts fa ON acc.account_id = fa.account_id | ||
| LEFT JOIN transactions t ON t.debit_account_id = acc.account_id`, | ||
| want: []string{"account_tags", "accounts", "transactions"}, | ||
| }, | ||
| { | ||
| name: "deduplicated across aliases", | ||
| sql: `SELECT a1.account_id FROM accounts a1 JOIN accounts a2 ON a1.space_id = a2.space_id`, | ||
| want: []string{"accounts"}, | ||
| }, | ||
| { | ||
| name: "insert excludes write target, includes read", | ||
| sql: `INSERT INTO audit_log (account_id) SELECT account_id FROM accounts`, | ||
| want: []string{"accounts"}, | ||
| }, | ||
| { | ||
| name: "no base tables", | ||
| sql: `SELECT 1`, | ||
| want: nil, | ||
| }, |
Author
|
Replaced by a clean branch. |
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.🤖 Generated with Claude Code