Skip to content

transpiler: three-tier intercept to minimize transpilation#192

Draft
EDsCODE wants to merge 3 commits intomainfrom
transpiler/three-tier-intercept
Draft

transpiler: three-tier intercept to minimize transpilation#192
EDsCODE wants to merge 3 commits intomainfrom
transpiler/three-tier-intercept

Conversation

@EDsCODE
Copy link
Contributor

@EDsCODE EDsCODE commented Feb 13, 2026

Summary

  • Adds a pre-parse classifier (Tier 0) that skips transpilation entirely for plain queries with no PostgreSQL-specific patterns — 25x faster (1.6μs vs 41μs, 2 allocs vs 48)
  • When PG patterns are detected, runs only the relevant transforms (Tier 1) via bitmask-based selective execution instead of all 18
  • Zero changes to server/conn.go — all 4 call sites still call tr.Transpile(query) which now internally classifies

Benchmark results

Path ns/op allocs
Tier 0 (plain SELECT) 1,618 2
Full pipeline (same query) 41,225 48
Classifier only 1,552 1

Architecture

Tier Mechanism When
0 Classify() string scan → return original SQL No PG patterns (most queries)
1 Parse → selective transforms → deparse PG patterns detected
2 Error-driven retry (unchanged) ALTER TABLE→VIEW, DROP TABLE→VIEW

Classifier patterns

Conservative rule: false positives (unnecessary transform runs) are fine. False negatives would be bugs.

Detects: SET/SHOW/RESET, pg_catalog.*, information_schema, public., version(), PG types (JSONB, BYTEA, INET, etc.), type casts (::regtype, ::regclass), PG functions (array_agg, regexp_matches, etc.), operators (->, ~), FOR UPDATE, ctid, _pg_expandarray, ON CONFLICT, DDL patterns (DuckLake only), writable CTEs, $N placeholders.

Test plan

  • All existing transpiler tests pass (no regressions)
  • New TestClassify_Direct — plain queries return Direct=true
  • New TestClassify_NeedsTransform — PG-specific queries return correct flags
  • New TestClassify_DuckLakeMode — DDL patterns flagged only in DuckLake mode
  • New TestTier0MatchesTier1 — Direct queries produce equivalent results to full pipeline
  • New benchmarks: BenchmarkTranspile_Direct vs BenchmarkTranspile_OldAllTransforms
  • Integration test: build, restart duckgres, connect with psql, run plain + PG-specific queries

🤖 Generated with Claude Code

Sprite and others added 3 commits February 13, 2026 21:07
Most queries (plain SELECT/INSERT/UPDATE/DELETE) need zero transforms —
DuckDB handles them natively. The parse/transform/deparse cycle was
wasted work for these queries.

This adds a pre-parse classifier (Tier 0) that does fast case-insensitive
substring matching. If no PG-specific patterns are found, the query goes
straight to DuckDB without any parsing — 25x faster than the full pipeline
(1.6μs vs 41μs, 2 allocs vs 48).

When patterns are detected, only the relevant transforms run (Tier 1)
via bitmask-based selective execution, skipping unneeded transforms.

Architecture:
- Tier 0: Classify() does string scan, returns Direct for plain queries
- Tier 1: transpileWithFlags() parses and runs only flagged transforms
- Tier 2: Error-driven retry (unchanged, handled by conn.go)

All changes encapsulated in transpiler package. The 4 call sites in
conn.go remain unchanged — they still call tr.Transpile(query).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…, FlagAll sentinel

- Fix hasAnyPrefix to skip line comments (-- ...) in addition to block
  comments, preventing false negatives like "-- comment\nSET ..."
- Replace brittle FlagAll magic number with flagSentinel pattern
- Add explanatory comments to classifier for known false-positive patterns:
  ~ operator, CTID substring, writable CTE, UNIQUE/REFERENCES in DDL
- Document FallbackToNative behavioral change for Tier 0 queries
- Add test cases for line comment handling in SET/SHOW classification

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…NT ON, REFRESH

The DDL transform marks these as no-ops (IsNoOp=true) but the classifier
was missing them, causing Tier 0 to send them directly to DuckDB where
they would fail instead of being silently acknowledged in DuckLake mode.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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