Skip to content

fix: with sql_lexer, query() puts FORMAT before a trailing SETTINGS clause - #1070

Closed
ccoltx wants to merge 1 commit into
lexer/5-query-uses-lexerfrom
lexer/6-format-before-settings
Closed

ccoltx wants to merge 1 commit into
lexer/5-query-uses-lexerfrom
lexer/6-format-before-settings

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 6 of the lexer stack. Depends on #1069. Addresses #970 for users who set sql_lexer. Supersedes the hand-written scanner in #972 on this path.

Why

query() always appends FORMAT <format> at the end. When the user's query ends with a SETTINGS clause, the result is ... SETTINGS k = v FORMAT JSON. Older servers (e.g. 23.3, for DESCRIBE format(...)) require SETTINGS to come last and reject this. Current servers accept both orders (#970).

What

In the lexer rewriter's withFormat, FORMAT goes right before the SETTINGS keyword when all of these hold:

  • the keyword is outside any brackets;
  • it's the last such SETTINGS;
  • everything after it is exactly a name = value, ... list. A value may be several tokens, such as -1, 'x', true or [1, 2].

Otherwise FORMAT is appended at the end as before. That's the case for:

  • SETTINGS in a subquery, a string, a comment or a heredoc;
  • a column or alias named settings;
  • a backtick-quoted `SETTINGS`;
  • ... SETTINGS a = 1 UNION ALL SELECT 2;
  • incomplete lists.

Also:

  • The rewriter now works on tokens throughout, so withFormat tokenizes the query only once. New token kinds: equals, comma.
  • Updated:
    • the sql_lexer JSDoc;
    • the query() JSDoc, which no longer says FORMAT always goes at the end;
    • docs/howto/sql_lexer.md;
    • both changelogs.

Testing

🤖 Generated with Claude Code

@codecov

codecov Bot commented Oct 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.07843% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
packages/client-common/src/sql/lexer_rewriter.ts 96.07% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@ccoltx
ccoltx force-pushed the lexer/6-format-before-settings branch from c07bdb3 to cfa3798 Compare October 21, 2026 16:19
@workflow-authentication-public workflow-authentication-public Bot added the needs-docs PR has user-visible changes without matching docs updates label Oct 21, 2026
@workflow-authentication-public

workflow-authentication-public Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Docs drift check: this PR looks like it needs doc updates.

  • skills/clickhouse-js-node-troubleshooting/reference/query-format-clause.md: the intro states query() "always appends FORMAT <format> to the end of the query string" (and the SHOW [ROW] POLICIES section repeats "the FORMAT suffix that query() always appends"). With sql_lexer set, FORMAT is now placed before a trailing top-level SETTINGS clause instead of at the end — the same claim the PR corrected in the query() JSDoc.

@ccoltx
ccoltx force-pushed the lexer/6-format-before-settings branch from cfa3798 to 93dec69 Compare October 21, 2026 18:32
@ccoltx
ccoltx removed this pull request from stack #1073 October 21, 2026 18:35
@ccoltx
ccoltx added this pull request to stack #1077 October 21, 2026 18:35
@ccoltx
ccoltx removed this pull request from stack #1077 October 21, 2026 18:36
@ccoltx
ccoltx added this pull request to stack #1078 October 21, 2026 18:36
…lause

When the query ends with a top-level SETTINGS name = value list, query()
now sends `... FORMAT x SETTINGS ...` instead of `... SETTINGS ... FORMAT
x`, which some older servers reject (#970). SETTINGS inside brackets,
strings or comments, a column named settings, and a SETTINGS clause
followed by more of the query are left alone. Without sql_lexer nothing
changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ccoltx
ccoltx force-pushed the lexer/6-format-before-settings branch from 93dec69 to 5e17136 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

needs-docs PR has user-visible changes without matching docs updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant