Add scope graph + bidirectional type checking for SQL analysis#4361
Open
kyleconroy wants to merge 1 commit intomainfrom
Open
Add scope graph + bidirectional type checking for SQL analysis#4361kyleconroy wants to merge 1 commit intomainfrom
kyleconroy wants to merge 1 commit intomainfrom
Conversation
Implement a new SQL analysis system based on two concepts from programming language theory: 1. Scope graphs (internal/analysis/scope/): Model SQL name resolution as path-finding in a labeled graph. Each scope contains declarations (columns, tables, aliases) connected by edges (PARENT, ALIAS, LATERAL, OUTER). This handles joins, subqueries, CTEs, and aliases compositionally — the resolution algorithm doesn't change, only the graph structure does. 2. Bidirectional type checking (internal/analysis/typecheck/): Type information flows in two directions — synthesis (bottom-up: "what type does this expression have?") and checking (top-down: "does this match the expected type?"). This naturally handles parameter type inference: when $1 appears in `WHERE age > $1`, checking mode infers $1's type from the column's type. 3. SQL analyzer (internal/analysis/sqlanalyze/): Combines both systems by walking the sqlc AST, building scope graphs from FROM/JOIN/CTE clauses, and running bidirectional type checking on expressions. Supports SELECT, INSERT, UPDATE, DELETE with parameter inference, output column resolution, and engine-specific operator rules for PostgreSQL and MySQL. https://claude.ai/code/session_01VFJemaXKRZ2NfxYkpwXSbD
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.
Implement a new SQL analysis system based on two concepts from
programming language theory:
Scope graphs (internal/analysis/scope/): Model SQL name resolution
as path-finding in a labeled graph. Each scope contains declarations
(columns, tables, aliases) connected by edges (PARENT, ALIAS,
LATERAL, OUTER). This handles joins, subqueries, CTEs, and aliases
compositionally — the resolution algorithm doesn't change, only the
graph structure does.
Bidirectional type checking (internal/analysis/typecheck/): Type
information flows in two directions — synthesis (bottom-up: "what
type does this expression have?") and checking (top-down: "does this
match the expected type?"). This naturally handles parameter type
inference: when $1 appears in
WHERE age > $1, checking mode infers$1's type from the column's type.
SQL analyzer (internal/analysis/sqlanalyze/): Combines both systems
by walking the sqlc AST, building scope graphs from FROM/JOIN/CTE
clauses, and running bidirectional type checking on expressions.
Supports SELECT, INSERT, UPDATE, DELETE with parameter inference,
output column resolution, and engine-specific operator rules for
PostgreSQL and MySQL.
https://claude.ai/code/session_01VFJemaXKRZ2NfxYkpwXSbD