Skip to content

feat: Unified Query AST + Multi-Language Execution Engine#9

Merged
muk2 merged 1 commit intomainfrom
feature/unified-ast-engine
Feb 19, 2026
Merged

feat: Unified Query AST + Multi-Language Execution Engine#9
muk2 merged 1 commit intomainfrom
feature/unified-ast-engine

Conversation

@muk2
Copy link
Owner

@muk2 muk2 commented Feb 18, 2026

Summary

Implements the foundational architecture from RFC #7 — the Unified Query AST and Multi-Language Execution Engine for pgrsql.

This PR introduces a complete src/ast/ module with the following layers:

Core AST Types (types.rs)

  • Query enum — top-level representation for SELECT, INSERT, UPDATE, DELETE, CTEs, and raw SQL passthrough
  • SelectQuery — full SELECT support including DISTINCT, projections, FROM, JOINs, WHERE, GROUP BY, HAVING, WINDOW, ORDER BY, LIMIT/OFFSET, and set operations (UNION/INTERSECT/EXCEPT)
  • Expression enum — recursive expression tree supporting columns, literals, binary/unary ops, function calls, aggregates, window functions, CASE, subqueries, EXISTS, IN, BETWEEN, IS NULL, CAST, arrays, JSON access, type casts, and parameters
  • Join, TableRef, WindowSpec, CTE — complete relational algebra primitives

SQL Parser (parser.rs)

  • Translates SQL text → unified AST using sqlparser crate with PostgreSQL dialect
  • Supports all major SQL constructs: CTEs (including recursive), window functions with PARTITION BY, subqueries, set operations, LATERAL joins, JSON operators, aggregate functions with DISTINCT/FILTER
  • Decoupled from sqlparser's AST — our representation can evolve independently

SQL Compiler (compiler.rs)

  • Compiles AST → PostgreSQL SQL string
  • Enables round-trip: SQL → AST → SQL (verified by tests)
  • Foundation for DSL-to-SQL compilation paths

Language Adapter Layer (adapter.rs)

  • QueryLanguageAdapter trait — for parsing raw query languages (SQL dialects)
  • DSLAdapter trait — for Domain-Specific Languages that compile to our AST
  • AdapterRegistry — manages multiple adapters with auto-detection
  • Built-in PostgresAdapter registered by default

Optimization Infrastructure (optimizer.rs)

  • OptimizationPass trait — pure transformation passes over the AST
  • Optimizer — composable pipeline of ordered passes
  • analyze_query() — structural introspection returning QueryAnalysis (detects joins, CTEs, aggregation, window functions, subqueries, set operations, JSON ops)
  • Built-in RemoveRedundantNesting pass

Plugin Architecture (plugin.rs)

  • QueryPlugin trait — register capabilities at startup
  • PluginRegistry — central registry for adapters, DSLs, and optimization passes
  • Clean extension point for future language integrations (Python DSL, OCaml, Lean, WASM)

Architecture

Input (SQL / DSL)
      ↓
Language Adapter Layer  (adapter.rs)
      ↓
Unified Query AST       (types.rs)
      ↓
Analysis / Optimization (optimizer.rs)
      ↓
SQL Compiler            (compiler.rs)
      ↓
Execution Engine        (existing db/ module)

Test plan

  • 65 unit tests covering all modules
  • Parser tests: simple SELECT, WHERE, JOIN, GROUP BY, CTE, recursive CTE, window functions, INSERT, UPDATE, DELETE, subqueries, UNION, ORDER BY, BETWEEN, CASE, IS NULL, CAST, DISTINCT, multiple statements, invalid SQL
  • Compiler round-trip tests: parse SQL → compile → verify re-parseable
  • Optimizer tests: empty pipeline, default passes, nesting removal, query analysis
  • Adapter tests: PostgreSQL adapter detection, registry auto-dispatch
  • Plugin tests: loading, registration, capability discovery
  • All clippy warnings resolved
  • cargo fmt applied

🤖 Generated with Claude Code

…ation

Implements the core architecture from RFC issue #7:

- Unified AST types (Query, SelectQuery, Expression, etc.) supporting
  SELECT, INSERT, UPDATE, DELETE, CTEs, window functions, subqueries,
  set operations, JSON access, and all standard SQL constructs
- SQL parser (SQL → AST) powered by sqlparser crate with PostgreSQL dialect
- SQL compiler (AST → SQL) for round-trip compilation back to PostgreSQL
- Language adapter traits (QueryLanguageAdapter, DSLAdapter) for
  multi-language input support with built-in PostgreSQL adapter
- Optimization pass infrastructure with composable transform pipeline
  and built-in redundant nesting removal pass
- Query analysis engine for structural introspection (joins, CTEs,
  aggregations, window functions, subqueries, etc.)
- Plugin architecture (QueryPlugin trait, PluginRegistry) for
  extensible adapters, optimizers, and DSL integrations
- 65 unit tests covering parsing, compilation, round-trips,
  optimization, analysis, adapters, and plugin system

Closes #7

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@muk2 muk2 merged commit f30096f into main Feb 19, 2026
8 checks passed
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.

1 participant