Skip to content

Add scope graph + bidirectional type checking for SQL analysis#4361

Open
kyleconroy wants to merge 1 commit intomainfrom
claude/sql-analysis-types-zDtcB
Open

Add scope graph + bidirectional type checking for SQL analysis#4361
kyleconroy wants to merge 1 commit intomainfrom
claude/sql-analysis-types-zDtcB

Conversation

@kyleconroy
Copy link
Copy Markdown
Collaborator

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

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
@dosubot dosubot bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Mar 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants