Skip to content

refactor(vnext): share bounded SQL lexer - #187

Merged
Light2Dark merged 2 commits into
dev-refactorfrom
codex/vnext-bounded-sql-lexer
Jul 25, 2026
Merged

refactor(vnext): share bounded SQL lexer#187
Light2Dark merged 2 commits into
dev-refactorfrom
codex/vnext-bounded-sql-lexer

Conversation

@Light2Dark

@Light2Dark Light2Dark commented Jul 25, 2026

Copy link
Copy Markdown
Member

Summary

  • extract the streaming bounded SQL lexer from the relation-site state machine into one package-private module
  • preserve the exact PostgreSQL, DuckDB, BigQuery, and Dremio lexical profiles, UTF-16 offsets, embedded-region barriers, quote/comment behavior, one-token pushback, and 16,384-lexeme ceiling
  • keep query-site keyword, comment-cursor, region, and resource semantics local to the consumer
  • translate generic lexer resource evidence through an exhaustive package-owned map
  • add direct boundary tests without exposing tokens or lexer APIs from the package

This is a zero-semantics prerequisite for the separate bounded CTE layout/visibility recognizer. The recognizers will initially use separate streaming traversals over the same lexical implementation; any traversal fusion remains benchmark-driven.

Performance

An initial broader helper extraction caused a reproducible Vite SSR namespace-call regression on the hot path. The boundary was narrowed before commit.

Stable means at the exact head are back at the PR #186 baseline:

  • exact 10 KiB statement: about 0.56–0.59 ms
  • 1,000 classified aliases: about 0.36–0.38 ms
  • 1,000 authenticated USING columns: about 0.21 ms

The lexer remains streaming and does not allocate a token tape.

Verification

  • 1,543 tests passed plus 1 expected failure
  • changed coverage: 97.05% statements, 95.79% branches, 100% functions, 97.04% lines
  • bounded lexer coverage: 98.92% statements/lines, 98.78% branches, 100% functions
  • repository coverage: 95.34% statements, 92.05% branches, 96.15% functions, 95.33% lines
  • source, test, loose-optional, and demo typechecks pass
  • repository oxlint, test-integrity, and diff checks pass
  • browser, package, worker-placement, demo, and benchmark gates pass
  • 20,000 deterministic differential lexer comparisons passed across dialects, masked regions, subranges, UTF-16, comments, quotes, punctuation, and pushback
  • independent SQL/API and concurrency/performance reviewers approved exact commit ed079df0f68aadaf8957a7ca5e6c497e91b74860

Part of #169.


Summary by cubic

Refactored vNext to share a streaming bounded SQL lexer and the embedded-region lookup, and switched query-site to use them. Behavior is unchanged across PostgreSQL, DuckDB, BigQuery, and Dremio; this unblocks the bounded CTE recognizer in #169.

  • Refactors
    • Moved the lexer into package-private src/vnext/bounded-sql-lexer.ts.
    • Centralized findSqlEmbeddedRegionAtOrAfter in src/vnext/source.ts and reused it in the lexer and query-site (with tests).
    • Preserves lexical profiles, UTF-16 offsets, embedded-region barriers, quote/comment rules, one-token pushback, and the 16,384-lexeme cap.
    • Kept consumer-specific semantics in query-site; mapped lexer resource signals to local query-site resources.
    • Removed duplicated lexer and region-lookup code from src/vnext/query-site.ts and wired it to the shared modules.

Written for commit 76190f9. Summary will update on new commits.

Review in cubic

@github-actions

github-actions Bot commented Jul 25, 2026

Copy link
Copy Markdown

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 97.39% (🎯 91%) 709 / 728
🔵 Statements 97.4% (🎯 91%) 714 / 733
🔵 Functions 100% (🎯 90%) 52 / 52
🔵 Branches 96.12% (🎯 85%) 720 / 749
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/vnext/bounded-sql-lexer.ts 100% 100% 100% 100%
src/vnext/query-site.ts 96.69% 95.36% 100% 96.67% 386, 539-540, 566, 653, 772, 858, 944-947, 999, 1002, 1007-1010, 1105, 1196, 1282-1285, 1323, 1331-1334
src/vnext/source.ts 99.04% 98.5% 100% 99.01% 180-183
Generated in workflow #724 for commit 76190f9 by the Vitest Coverage Report Action

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the vNext relation query-site recognizer to reuse a shared streaming, bounded SQL lexer implementation, preserving dialect-specific lexical behavior and resource ceilings while reducing duplicated lexer code.

Changes:

  • Extracted the bounded, streaming SQL lexer into src/vnext/bounded-sql-lexer.ts with explicit resource-limit signaling.
  • Updated src/vnext/query-site.ts to consume BoundedSqlLexer and translate lexer resource limits through a local exhaustive map.
  • Added direct unit tests that validate token boundaries, embedded-region barriers, pushback semantics, and resource-limit behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/vnext/query-site.ts Replaces the inlined query-site lexer with BoundedSqlLexer and maps lexer resource signals into query-site resources.
src/vnext/bounded-sql-lexer.ts New shared, bounded streaming lexer extracted from the prior query-site implementation.
src/vnext/tests/bounded-sql-lexer.test.ts Adds focused boundary/resource tests for the shared bounded lexer across key dialect behaviors and masking.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread src/vnext/bounded-sql-lexer.ts Outdated
@Light2Dark

Copy link
Copy Markdown
Member Author

Resolved Cubic’s P3 at exact head 76190f9: the allocation-free lower-bound lookup now lives in the private source-snapshot module and both the lexer and query-site consumer use it. Half-open boundary probes, the 20,000-case lexer differential, focused 312-test suite, full typechecks/lint, and query-site benchmarks are green. Two independent exact-head reviewers approved the change. Copilot was not re-requested per the one-request-per-PR policy.

@Light2Dark
Light2Dark merged commit 7397e60 into dev-refactor Jul 25, 2026
9 checks passed
@Light2Dark
Light2Dark deleted the codex/vnext-bounded-sql-lexer branch July 25, 2026 00:41
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.

2 participants