You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
clickhouse: type inference via the new analysis core (analyze only) (#4521)
* core: land xqlc catalog + analyzer on sqlc's AST
Port xqlc's core catalog (SQLite-backed sql_* catalog) and its
dialect-neutral query analyzer into internal/core, repointing the
analyzer from xqlc's copy of the AST onto sqlc's internal/sql/ast so
there is a single AST. No converter and no second AST package.
A smoke test drives the analyzer with sqlc's own PostgreSQL parser to
prove the repointed analyzer resolves columns, types, star expansion,
and aliases end-to-end against internal/sql/ast.
This is the first step of merging xqlc back into sqlc as the future
analysis core; ClickHouse will be the first engine wired onto it.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XTGxNHW6v1S1YyC9FDSgrK
* clickhouse: analyze queries through the core catalog + analyzer
Wire ClickHouse onto the merged core: a dialect seed registering the
built-in ClickHouse types, and a DDL handler that populates the core
catalog from CREATE TABLE using sqlc's existing ClickHouse parser. A
smoke test proves the full vertical path — ClickHouse SQL -> sqlc's
ClickHouse parser -> internal/sql/ast -> core catalog + analyzer ->
PrepareResult — resolving column names, types, nullability, source
bindings, and star expansion, with none of the legacy compiler analyze
step involved.
Also fix the ClickHouse converter to render nested type parameters
(the inner type of Nullable(T)/Array(T), Decimal precision, etc.) into
TypeName.Name instead of dropping them as TODO nodes, so wrapped types
resolve to their effective scalar type.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XTGxNHW6v1S1YyC9FDSgrK
* clickhouse: wire end-to-end sqlc generate onto the core
Add the EngineClickHouse engine and a dedicated compile path so
`sqlc generate` produces Go for ClickHouse entirely through the xqlc
core, bypassing the legacy compiler analyze step and the in-memory
sql/catalog:
- config: add the "clickhouse" engine constant.
- compiler: NewCompiler builds a core.Catalog seeded with the ClickHouse
dialect; parseCatalog applies schema DDL to it; a new parseQueryCore
resolves each query's columns and parameters via core/analyzer and
assembles *compiler.Query, reusing only the shared query-metadata
parsing. The legacy analyzeQuery/inferQuery/outputColumns path and the
analyzer.Analyzer seam are never entered.
- codegen: project the core catalog into plugin.Catalog for model/enum
generation, and add a ClickHouse -> Go type map (Nullable(T) -> *T,
the integer ladder, Float32/64, String, DateTime -> time.Time, ...).
- clickhouse parser: compute statement byte-spans with a running offset
and a semicolon scan (doubleclick reports statement starts but not
ends), so leading "-- name:" annotations fall inside each statement.
An endtoend case (clickhouse_select) exercises the full pipeline and its
golden Go output is committed. Updating parse_basic/clickhouse's golden
reflects the corrected statement spans and now-detected query name/cmd.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XTGxNHW6v1S1YyC9FDSgrK
* core: generate catalog accessors with sqlc (sqlc building sqlc)
Introduce internal/core/catalogdb, a sqlc-generated SQLite query layer
over the core catalog's own sql_* tables — sqlc analyzing sqlc's
catalog. The catalog's schema.sql is the sqlc schema; catalogdb/query.sql
holds the queries; //go:generate runs sqlc against them. Generation is
offline and the output is committed, so there is no build-time cycle, and
because the SQLite engine runs on the legacy compiler (not the core
catalog) there is no analysis recursion.
Vertical slice: types.go (CreateType/TypeOID/TypeName/LookupType) now
delegates to the generated Queries. FindProcs is split into two generated
queries — FindProcsAnyNamespace and FindProcsInNamespaces (sqlc.slice) —
with Go choosing based on whether namespaces were supplied, replacing the
hand-built IN-list. Remaining core files still use raw SQL and will be
swept over incrementally.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XTGxNHW6v1S1YyC9FDSgrK
* core: migrate all catalog queries to sqlc-generated code
Sweep the remaining hand-written SQL against the sql_* catalog tables
onto the generated catalogdb layer, completing the conversion started in
the types.go/FindProcs slice. Namespace, dialect, type, class,
attribute, constraint, proc, operator, and cast queries now all run
through catalogdb.Queries.
Notable rewrites:
- FindOperators: the conditionally-appended type filters become a single
static query using (@arg = 0 OR col = @arg) sentinels.
- Nullable OID columns round-trip through sql.NullInt64 (orZero maps
NULL to the 0 sentinel the Go API uses).
- New typed catalog accessors back the cross-package call sites that
previously reached into the raw handle: DropClass (ClickHouse DROP
TABLE), ClassColumns (analyzer scope), and Namespaces /
TablesInNamespace / ClassCodegenColumns (codegen catalog projection).
Only the schema bootstrap (db.Exec(ddl)) and the DB()/Close() handles
remain as raw database/sql; every catalog query is now sqlc-generated.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XTGxNHW6v1S1YyC9FDSgrK
* core: move catalog SQL into a catalogdef package, export Schema
Extract schema.sql and query.sql from internal/core (and catalogdb) into
a dedicated internal/core/catalogdef package that owns the SQL defining
sqlc's catalog and embeds the schema (catalogdef.Schema). sqlc reads its
schema and queries from catalogdef and still generates the querier into
catalogdb, keeping SQL sources separate from generated code.
core/catalog.go now builds its in-memory catalog from catalogdef.Schema
instead of embedding schema.sql itself, so the DDL used at runtime and the
schema fed to codegen are one source of truth.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XTGxNHW6v1S1YyC9FDSgrK
* clickhouse/core: strip comments and remove xqlc references
Remove comments from the Go files authored for this work (catalog core,
analyzer, ClickHouse engine, compile path, codegen type map) and drop the
comments added to the files touched along the way, keeping //go:generate
and //go:embed directives. No remaining references to xqlc.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XTGxNHW6v1S1YyC9FDSgrK
* core: address review — move shim/DDL apply out of engine and compiler
Move pluginCatalogFromCore into a new internal/core/shim package as
PluginCatalog. Move the engine-neutral DDL apply into a new
internal/core/schema package and have the compiler call it directly, so
no clickhouse-specific code remains in the schema loading loop. Push the
ClickHouse type unwrapping into the engine's column conversion so the
core schema apply only sees canonical scalar types. Rename the core
compile path file to parse_core.go. Remove the unit tests in favor of
end-to-end coverage.
* clickhouse: expose core analysis via `sqlc analyze`, drop codegen
Wire the ClickHouse engine into `sqlc analyze` so the core catalog and
analyzer can be exercised end to end without committing to code
generation yet. The analyze command builds the compiler, applies the
schema DDL to the core catalog, and reports inferred result columns as
JSON — the same static-analysis path `generate` would use.
Remove the not-yet-ready code generation surface: the ClickHouse Go
type map, the core-catalog to plugin.Catalog projection, and the
generate golden test. ClickHouse type inference is now covered by an
analyze_basic end-to-end case, matching how the other engines are
tested.
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
0 commit comments