Repository navigation
fix: remove a trailing semicolon that is followed by a comment - #1031
Mohil-Ahuja wants to merge 6 commits into
Conversation
|
|
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Heredoc literals remain mishandled, and both affected package changelogs require entries.
Review effort: Balanced
Findings: 1
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.
13c8a43 to
b043fe3
Compare
|
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 ( How I testedFor each of 44 queries, I compared the server's answer to two requests:
The queries were the #973 examples, every input from this PR's unit tests, and 16 extra edge cases.
The PR is never worse than Two inputs that still fail with
|
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.
b043fe3 to
5776b67
Compare
|
Thanks for testing it against a real server! Fixed both in 8daa32e:
Both inputs are in |
|
Thanks @Mohil-Ahuja, that was quick! I re-ran the same checks on 5776b67. Results
Performance: an easy fast path
A query without any export function removeTrailingSemi(query: string): string {
if (!query.includes(";")) {
return query;
}
// ...It makes the 2 MB What do you think? Should we add that check? |
|
Thanks for measuring it! Yes, that makes sense: added the |
|
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. |
|
Closing, as discussed above: #1085 is merged and resolves #973 without rewriting the query. Thank you for the thorough investigation and the test cases, @Mohil-Ahuja. They were useful for checking the new approach. |


Closes #973
Problem
query()appends\nFORMAT <format>afterremoveTrailingSemi(), 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 appendedFORMATbecomes a second statement: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; -- notebecomesSELECT 1 -- note, and the\nbeforeFORMATstill 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:
SELECT 1 ; ;→SELECT 1; previously the inner one was left behind);command()andexec()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.
removeTrailingSemiis now exported fromclient.tsso 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:
SELECT 13 AS a WHERE 0 -- trailing commentSELECT 13 AS a; -- trailing comment[{"a":13}]SELECT 13 AS a; /* block */[{"a":13}]SELECT 13 AS a; # hash comment[{"a":13}]SELECT ';' AS a; -- semicolon in a string[{"a":";"}]SELECT $$;$$ AS a; -- note[{"a":";"}]SELECT $tag$-- text$tag$ AS a;SELECT $$a # b /* c$$ AS a;The string and heredoc rows check that a
;or comment marker inside a literal is left alone; the last two already worked onmainand 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 doprettier --check,eslint --max-warnings=0onclient.ts, andtsc --noEmitforclient-common.