Skip to content

feat: add an internal lexer-based SQL rewriter - #1067

Closed
ccoltx wants to merge 1 commit into
lexer/2-sql-rewriter-seamfrom
lexer/3-lexer-adapter
Closed

ccoltx wants to merge 1 commit into
lexer/2-sql-rewriter-seamfrom
lexer/3-lexer-adapter

Conversation

@ccoltx

@ccoltx ccoltx commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Tracking issue: #1074. Stack: #1065 → #1066 → #1067 → #1068 → #1069 → #1070 → #1071 → #1072

Part 3 of the lexer stack. Depends on #1066. No user-visible change: nothing calls the new code yet.

Why

The client's string-based handling of trailing semicolons can't tell a ; inside a comment, string or heredoc from a real one (#973). The official lexer, @clickhouse/lexer, is ClickHouse's own Lexer.cpp compiled to WASM, so it splits a query exactly as the server does. This PR adds the code that uses it, on its own, so it can be reviewed before any public API is involved.

What

  • sql/tokens.ts:
    • ClickHouseSqlLexer: our own description of the four lexer members we use (loadLexer, tokenizeSync, isError, TOKEN_NAMES). import * as lexer from "@clickhouse/lexer" fits it as-is, and the client doesn't depend on the package's types.
    • createSqlTokenizer: maps the lexer's token types to a few kinds, by name, because the numbers change between lexer releases. Unknown names map to other.
    • It returns null when:
      • the lexer reports an error, such as an unclosed quote;
      • the lexer throws;
      • the tokens don't add up to the query exactly, which happens with a lone UTF-16 surrogate.
  • sql/lexer_rewriter.ts:
    • stripTrailingSemi removes the semicolons after the last statement token and keeps comments, so SELECT 1; -- note becomes SELECT 1 -- note.
    • When the tokenizer returns null, it falls back to the existing string-based logic.
    • withFormat is unchanged for now; part 5 switches it over.
  • @clickhouse/lexer@1.0.1: a root dev dependency, pinned exactly and used only by tests. The lockfile diff is only the new entry.
  • ESLint no-restricted-imports blocks @clickhouse/lexer in the clients' source. The package is ESM-only, and an import there would compile to a require() that fails on Node versions before 20.19. Users pass it in instead (part 4).

Testing

New sql_lexer_rewriter.test.ts runs against the real WASM lexer in both the Node and browser suites. It covers:

Full Node unit suite, typecheck and lint pass.

🤖 Generated with Claude Code

@ccoltx
ccoltx added this pull request to stack #1073 October 21, 2026 16:07
@codecov

codecov Bot commented Oct 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Adds sql/tokens.ts, which maps the official ClickHouse lexer's tokens to
a few kinds by name, and sql/lexer_rewriter.ts, which removes trailing
semicolons with tokens so that comments, strings and heredocs are
handled correctly. Nothing uses it yet.

@clickhouse/lexer is a dev dependency, used only by tests. An ESLint
rule stops the clients' source from importing it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ccoltx
ccoltx force-pushed the lexer/3-lexer-adapter branch from dbdc0d1 to cd667a1 Compare October 21, 2026 18:43
@ccoltx

ccoltx commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Closing: #1085 is merged. query(), command() and exec() now send the SQL as written, with the format passed to the server as the default_format setting, so the client no longer rewrites SQL and this stack has nothing left to do. #973 and #970 are closed as fixed. See #1074 for the wrap-up.

@ccoltx ccoltx closed this Oct 31, 2026
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