Add analyze command for static query analysis#4476
Open
kyleconroy wants to merge 2 commits into
Open
Conversation
Add a new `sqlc analyze` command that analyzes a query file against a schema file and outputs the inferred result columns and parameters as JSON. Unlike `sqlc generate`, this command does not require a configuration file and does not connect to a database. It drives sqlc's native static analysis (the catalog-based compiler) to infer types directly from the provided schema, supporting the postgresql, mysql, and sqlite dialects. Usage: sqlc analyze --dialect postgresql --schema schema.sql query.sql
Add stdin support to `sqlc analyze`: when no query file argument is given, the query is read from stdin (written to a temporary file so the compiler can read it), mirroring `sqlc parse`. Align the two commands on a single-document JSON output. `parse` previously emitted one JSON object per statement (newline-delimited), which is not parseable as a single document; it now emits a single JSON array of statements, matching the array `analyze` already produces.
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.
Summary
Adds a new
sqlc analyzecommand that analyzes a query against a schema and outputs the inferred result columns and parameters as JSON.Like
sqlc parse, it's a standalone command — no configuration file required and no database connection. It drives sqlc's native static analysis (the catalog-based compiler) to infer types directly from the provided schema, supporting thepostgresql,mysql, andsqlitedialects.--dialect/-d—postgresql,mysql, orsqlite(required)--schema/-s— path to the schema file (required)Example
Given
schema.sql:and the query
-- name: GetAuthor :one/SELECT id, name FROM authors WHERE id = $1;:[ { "name": "GetAuthor", "cmd": ":one", "columns": [ { "name": "id", "data_type": "bigserial", "not_null": true, "is_array": false, "table": "authors" }, { "name": "name", "data_type": "text", "not_null": true, "is_array": false, "table": "authors" } ], "params": [ { "number": 1, "column": { "name": "id", "data_type": "bigserial", "not_null": true, "is_array": false, "table": "authors" } } ] } ]Output format alignment
This also aligns
sqlc parseandsqlc analyzeon a single-document JSON output.parsepreviously emitted one JSON object per statement (newline-delimited), which is not parseable as a single document; it now emits a single JSON array of statements, matching the arrayanalyzeproduces.Note: changing
parse's output is a behavior change to an existing command, but it has no tests or golden files, and the previous newline-delimited form was not valid as a single JSON document.Implementation
internal/cmd/analyze.go(new) — the command, with small serializable output structs (analyzedQuery/analyzedColumn/analyzedParam) so internal AST types aren't dumped raw. Schema/query parse errors are unwrapped frommultierr.Errorand reported withfile:line:collocations. Stdin (when no file arg is given) is written to a temp file so the compiler can read it, mirroringparse.internal/cmd/parse.go— emit a single JSON array of statements.internal/cmd/cmd.go— registeranalyzeCmdand its--dialect/--schemaflags.Testing
Manually verified across all three engines and the error/stdin paths:
SELECT *expansion,RETURNING *, params, and nullability resolved from the schemaNOT NULLinferred correctlybad.sql:2:8: column "nonexistent" does not exist(exit 1); missing--dialectgives a clear messageparseoutput now loads as a single JSON arraygo build ./...andgo vet ./internal/cmd/are clean.https://claude.ai/code/session_01Wyz9n2TDrUMXT2LMk7qjgV
Generated by Claude Code