Skip to content

googlesql: type inference via the new analysis core (analyze only) - #4533

Merged
kyleconroy merged 1 commit into
mainfrom
claude/core-analyzer-pr-4522-w6yt1t
Jul 30, 2026
Merged

googlesql: type inference via the new analysis core (analyze only)#4533
kyleconroy merged 1 commit into
mainfrom
claude/core-analyzer-pr-4522-w6yt1t

Conversation

@kyleconroy

Copy link
Copy Markdown
Collaborator

Overview

Redo of #4522 on top of the core catalog and analyzer landed in #4521. The original wired GoogleSQL into the legacy compiler (googlesql.NewCatalog() + analyzeQuery/outputColumns); this wires it the way ClickHouse is wired — sqlc analyze --dialect googlesql resolves result columns and parameters entirely through internal/core, with the legacy analyze path never entered.

Engine wiring

  • internal/engine/googlesql/seed.go — a dialect seed registering the built-in GoogleSQL types, comparison and arithmetic operators over them, and COUNT as an int64 aggregate.
  • internal/compiler/engine.go — the googlesql engine builds a core.Catalog; schema DDL is applied via internal/core/schema and queries are resolved by internal/core/analyzer.
  • sqlc analyze accepts googlesql as a dialect.

Core analyzer

Engine-neutral work the GoogleSQL fixtures required, which ClickHouse gets too:

  • internal/core/analyzer/dml.go — INSERT, UPDATE, and DELETE support alongside SELECT: bind the target relation, infer parameter types from the column each value is assigned to or compared against, and project RETURNING / THEN RETURN lists.
  • COUNT(*) resolves through a catalog-registered aggregate when the dialect provides one, falling back to int8 as before. This closes the data_type: "any" limitation Add analyze support for GoogleSQL via zetajones #4522 flagged as a follow-up.
  • The core compile path rewrites named parameters before analysis, so @name resolves to a numbered parameter that carries its name.

GoogleSQL engine

  • Include a statement's leading comment in its reported location so the -- name: annotation is captured. Previously StmtLocation pointed past the comment, so analyze found no named queries. This also fixes name extraction for sqlc parse --dialect googlesql.
  • Tag operator A_Expr nodes with A_Expr_Kind_OP, matching the other engines, so the analyzer recognizes them as operator expressions.
  • Keep named parameters in their native @name form when rewriting, since GoogleSQL supports them natively and Spanner requires them.

Differences from #4522

Dropped, because they only mattered on the legacy path:

  • The nil-list initializations in the converter (ReturningList, TargetList, FromClause). The panics they fixed were in legacy compiler code the core path does not run.
  • The backtick quoteIdent change, used only by legacy star expansion.

Tests

End-to-end fixtures under internal/endtoend/testdata/, run by TestReplay:

  • analyze_basic/googlesql and analyze_dml/googlesql — reproduce Add analyze support for GoogleSQL via zetajones #4522's fixtures byte for byte (the DML case also covers UPDATE ... THEN RETURN and DELETE ... THEN RETURN).
  • analyze_select/googlesql — new: star expansion, joins with aliases, and COUNT(*).
  • Regenerated the parse_basic/googlesql golden, which picks up the corrected statement spans and the now-detected query name and command.

go test --tags=examples -timeout 25m ./... is green with PostgreSQL and MySQL running; gofmt and go vet are clean on the touched files.

Known limitations

  • SELECT * expands in the analyzer's column output but the SQL text is not rewritten (same as ClickHouse — fine for analyze, needs work before generate).
  • INSERT ... SELECT returns an explicit "not supported" error rather than silently producing no columns or parameters.

🤖 Generated with Claude Code


Generated by Claude Code

Redo GoogleSQL analyze support on top of the core catalog and analyzer
landed in #4521, rather than the legacy compiler catalog the original
attempt used.

- A googlesql dialect seed registers the built-in GoogleSQL types, the
  comparison/arithmetic operators over them, and COUNT as an int64
  aggregate, so the core catalog can resolve schema types and predicates.
- The compiler builds a core.Catalog for the googlesql engine; schema DDL
  is applied through internal/core/schema and queries are resolved by
  internal/core/analyzer, never entering the legacy analyze path.
- `sqlc analyze --dialect googlesql` accepts the new engine.

Core analyzer:
- Support INSERT, UPDATE, and DELETE in addition to SELECT: bind the
  target relation, infer parameter types from the columns they are
  assigned to or compared against, and project RETURNING lists.
- Resolve COUNT(*) through a catalog-registered aggregate when the
  dialect provides one, falling back to int8 as before.
- The core compile path now rewrites named parameters before analysis, so
  "@name" resolves to a numbered parameter carrying its name.

GoogleSQL engine:
- Include a statement's leading comment in its reported location so the
  "-- name:" annotation is captured. This also fixes name extraction for
  `sqlc parse --dialect googlesql`.
- Tag operator A_Expr nodes with A_Expr_Kind_OP, matching the other
  engines, so the analyzer recognizes them as operator expressions.
- Keep named parameters in their native "@name" form when rewriting,
  since GoogleSQL (and Spanner in particular) requires them.

End-to-end coverage lands as analyze_basic, analyze_select (star
expansion, joins, aggregates), and analyze_dml cases; the
parse_basic/googlesql golden picks up the corrected statement spans and
the now-detected query name and command.
@kyleconroy
kyleconroy merged commit 673f56d into main Jul 30, 2026
13 checks passed
@kyleconroy
kyleconroy deleted the claude/core-analyzer-pr-4522-w6yt1t branch July 30, 2026 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants