Skip to content

fix: remove a trailing semicolon that is followed by a comment - #1031

Closed
Mohil-Ahuja wants to merge 6 commits into
ClickHouse:mainfrom
Mohil-Ahuja:fix/973-trailing-semicolon-comment
Closed

Mohil-Ahuja wants to merge 6 commits into
ClickHouse:mainfrom
Mohil-Ahuja:fix/973-trailing-semicolon-comment

Conversation

@Mohil-Ahuja

@Mohil-Ahuja Mohil-Ahuja commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Closes #973

Problem

query() appends \nFORMAT <format> after removeTrailingSemi(), which only strips semicolons that are the very last characters of the string. With a comment after the semicolon, the loop stops at the comment, the ; survives, and the appended FORMAT becomes a second statement:

SELECT 13 AS a; -- trailing comment
FORMAT JSONEachRow
Code: 62. Syntax error (Multi-statements are not allowed): failed at position 15 (end of query)

Change

removeTrailingSemi() now scans the query and removes only the semicolons that come after the last character of the statement, skipping string literals ('...', with '' and backslash escapes), heredocs ($tag$...$tag$ and $$...$$), quoted identifiers (`...` and "...") and comments (--, #, /* */). Heredocs are lexed before comments, so a -- inside one is not taken for a comment. Comments are kept, so the query that's sent still reads as written: SELECT 1; -- note becomes SELECT 1 -- note, and the \n before FORMAT still terminates the line comment as it does today for a comment without a semicolon.

It's a single linear pass. Two small consequences, both of which I think are improvements but am flagging:

  • semicolons separated by whitespace are removed too (SELECT 1 ; ; → SELECT 1; previously the inner one was left behind);
  • command() and exec() share the helper, so they no longer send a stranded ; before a trailing comment either. That was harmless for them, as the issue notes, since they append nothing.

A query made only of semicolons is still returned unchanged, as before.

removeTrailingSemi is now exported from client.ts so it can be unit-tested; the package index re-exports named items only, so it does not become public API.

Verification

Against a real ClickHouse server (26.9.10.4, Docker), using the built client:

query before after
SELECT 13 AS a WHERE 0 -- trailing comment OK OK
SELECT 13 AS a; -- trailing comment error 62 [{"a":13}]
SELECT 13 AS a; /* block */ error 62 [{"a":13}]
SELECT 13 AS a; # hash comment error 62 [{"a":13}]
SELECT ';' AS a; -- semicolon in a string error 62 [{"a":";"}]
SELECT $$;$$ AS a; -- note error 62 [{"a":";"}]
SELECT $tag$-- text$tag$ AS a; OK OK
SELECT $$a # b /* c$$ AS a; OK OK

The string and heredoc rows check that a ; or comment marker inside a literal is left alone; the last two already worked on main and are there to make sure they still do.

New unit tests in remove_trailing_semi.test.ts (25 cases) cover trailing semicolons, each comment style, strings/heredocs/identifiers/comments containing ; or comment markers, and the all-semicolons case. Against the previous implementation the comment cases fail; with this change all pass. The full unit suite passes (45 files, 433 tests), as do prettier --check, eslint --max-warnings=0 on client.ts, and tsc --noEmit for client-common.

Copilot AI balanced review requested due to automatic review settings October 5, 2026 08:34
@CLAassistant

CLAassistant commented Oct 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Heredoc literals remain mishandled, and both affected package changelogs require entries.

Review effort: Balanced
Findings: 1 Medium severity · 1 Low severity

Open (2)
What changed in this PR

Fixes trailing semicolons before SQL comments so appending FORMAT does not create a second statement.

Changes:

  • Adds comment- and literal-aware semicolon removal.
  • Adds unit coverage for supported quoting and comment forms.

No direct security or privacy impact identified.

File Description
packages/​client-common/​src/​client.ts Implements SQL-aware trailing-semicolon removal.
packages/​client-common/​__tests__/​unit/​remove_trailing_semi.test.ts Tests semicolon removal and preservation cases.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread packages/client-common/src/client.ts Resolved
Comment thread packages/client-common/src/client.ts Resolved
@Mohil-Ahuja
Mohil-Ahuja force-pushed the fix/973-trailing-semicolon-comment branch from 13c8a43 to b043fe3 Compare October 5, 2026 08:55
@ccoltx
ccoltx self-requested a review October 21, 2026 15:28
@ccoltx

ccoltx commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Hi @Mohil-Ahuja, apologies it's taken so long to respond.

Thanks for opening the PR, this is a clear improvement. I checked the change against a live ClickHouse server (head image) and it fixes the #973 cases. Details below, plus two inputs where the same error still happens.

How I tested

For each of 44 queries, I compared the server's answer to two requests:

  • the original query, sent unchanged with ?default_format=JSONEachRow, so the server applies the format without the client changing the text;
  • what query() sends: removeTrailingSemi(query.trim()) + " \nFORMAT JSONEachRow".

The queries were the #973 examples, every input from this PR's unit tests, and 16 extra edge cases.

Same answer as the server
main 25 / 44
this PR 42 / 44

The PR is never worse than main on these 44 queries, and every input from its own tests works against the real server. Nested /* */ comments, #! comments, \r line endings, doubled quotes inside backtick names, and Unicode-quoted names (“a;”) all work too.

Two inputs that still fail with Multi-statements are not allowed

1. $ inside an identifier

SELECT 1 AS a$t$; -- $t$

ClickHouse allows $ in bare identifiers, and only starts a heredoc at a $ that begins a token. The scanner checks for a heredoc at every $, so $t$; -- $t$ is read as a heredoc and the real ; is never found. A possible fix is to only check for a heredoc when the previous character can't be part of a word:

if (c === "$" && (i === 0 || !/[A-Za-z0-9_$]/.test(query.charAt(i - 1)))) {

2. // line comments

SELECT 1; // note

The server accepts // as a line comment (the query above returns 1 when sent as-is), but the scanner only recognises -- and #. Adding (c === "/" && next === "/") to the line-comment branch should fix it.

Both also fail on main, so they aren't regressions. Could you add them to remove_trailing_semi.test.ts along with the fixes?

["SELECT 1 AS a$t$; -- $t$", "SELECT 1 AS a$t$ -- $t$"],
["SELECT 1; // note", "SELECT 1 // note"],

Changelog

The entries were added under the 1.24.0 heading. 1.24.0 and 1.24.1 have been released since this branch was created, so after rebasing on main they need to move to the next unreleased heading.

query() appends a FORMAT clause after removing the statement's trailing
semicolons, but removeTrailingSemi() only looked at the last characters
of the string. In `SELECT 1; -- note` it stopped at the comment, the
semicolon survived, and the appended FORMAT became a second statement,
which the server rejects with SYNTAX_ERROR (Multi-statements are not
allowed).

Scan the query instead, skipping string literals, quoted identifiers and
comments, and remove the semicolons that come after the last character
of the statement. Comments are kept, so the query that is sent still
reads as written.

Fixes ClickHouse#973.
A heredoc such as $tag$-- text$tag$ is a string, but the comment check
saw its -- first and skipped the rest of the query, so the real trailing
semicolon was never removed. Recognise $tag$...$tag$ and $$...$$
before comments, and fall back to treating $ as an ordinary character
when no closing tag follows.
@Mohil-Ahuja
Mohil-Ahuja force-pushed the fix/973-trailing-semicolon-comment branch from b043fe3 to 5776b67 Compare October 21, 2026 20:50
@Mohil-Ahuja

Copy link
Copy Markdown
Contributor Author

Thanks for testing it against a real server! Fixed both in 8daa32e:

  • A heredoc is only checked for at a $ that starts a token (the previous character is not a word character or $), so SELECT 1 AS a$t$; -- $t$ now drops the semicolon.
  • // is handled as a line comment along with -- and #.

Both inputs are in remove_trailing_semi.test.ts with your expected outputs, and they fail without the fix. Rebased on main, and the changelog entries are under a new 1.24.2 heading in both packages.

@ccoltx

ccoltx commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Thanks @Mohil-Ahuja, that was quick! I re-ran the same checks on 5776b67.

Results

  • Server-judged comparison: the same 44 queries now get the same answer as the server for 44 / 44 (42 / 44 before). Both cases from my last comment are fixed.

Performance: an easy fast path

removeTrailingSemi now runs a full scan on every query(), command() and exec() call. That's negligible for normal queries, but it adds up for large ones. On main the check is effectively free (Node.js 22, median of 5 runs):

Query main this PR
typical SELECT (~850 B) ~0 ms 0.01–0.06 ms
2 MB INSERT … VALUES ~0 ms 16–56 ms, depending on the data

A query without any ; has nothing to remove, so the scan can be skipped:

export function removeTrailingSemi(query: string): string {
  if (!query.includes(";")) {
    return query;
  }
  // ...

It makes the 2 MB INSERT without ; go from about 18 ms to 0.03 ms. Queries that do contain a ; keep the current cost.

What do you think? Should we add that check?

@Mohil-Ahuja

Copy link
Copy Markdown
Contributor Author

Thanks for measuring it! Yes, that makes sense: added the includes(";") fast path in the latest commit, so a query without a semicolon returns before the scan. The unit tests still pass.

@ccoltx

ccoltx commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Hi @Mohil-Ahuja. After discussing this fix internally, we realized a different approach would be a lot easer, and aligns with our other clients.

#1085 may make this issue unnecessary. This approach would then have nothing left to do, so we'd suggest closing it after #1085 merges.

@Mohil-Ahuja

Copy link
Copy Markdown
Contributor Author

Thanks @ccoltx, that makes sense. Sending the format as default_format removes the need to edit the query at all, and it fixes #970 and #759 too. I will close this once #1085 merges.

@ccoltx

ccoltx commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Closing, as discussed above: #1085 is merged and resolves #973 without rewriting the query. query() now sends the SQL as written and passes the format as the default_format setting, so a ; before a trailing comment is no longer a problem. We verified the cases from this PR on main with both the Node.js and Web clients against ClickHouse 26.11, and the end-to-end integration tests for them live in packages/client-common/__tests__/integration/query_default_format.test.ts.

Thank you for the thorough investigation and the test cases, @Mohil-Ahuja. They were useful for checking the new approach.

@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.

query(): trailing semicolon followed by a comment breaks FORMAT append (SYNTAX_ERROR 62)

4 participants