Strip trailing FORMAT clauses from SQL queries before sending#11
Conversation
The Formo query API wraps submitted SQL in a paginating subquery with its
own FORMAT JSON:
SELECT * FROM (<query>) LIMIT 100 FORMAT JSON
ClickHouse forbids a FORMAT clause inside a subquery, so a user query that
ends in its own FORMAT clause (e.g. `... FORMAT CSV`) produced a nested
FORMAT and a 400 from the API. A trailing FORMAT can never take effect
through this endpoint anyway -- the outer FORMAT JSON always wins, and
output shaping is the CLI's `--format` job.
Add stripTrailingFormatClause in src/lib/sql.ts to remove a trailing
top-level FORMAT clause and trailing semicolons before the query is sent.
The scan is aware of string literals, quoted identifiers, and comments, and
is anchored to the end of the statement, so it never touches a FORMAT nested
in a subquery or an identifier/function such as formatDateTime(...).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D7VzXR4q2UWHrYdU2139MS
There was a problem hiding this comment.
Code Review
This pull request introduces a utility function stripTrailingFormatClause to strip trailing FORMAT clauses and semicolons from SQL queries before sending them to the ClickHouse API, preventing 400 errors caused by nested FORMAT clauses in subqueries. The feedback suggests extending the comment-masking logic to support # as a single-line comment character (supported by ClickHouse for MySQL compatibility) and adding corresponding test coverage.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
ClickHouse accepts `#` and `#!` single-line comments (MySQL compatibility). maskLiterals only handled `--` and block comments, so a `#` comment ending in a FORMAT-like token could be wrongly stripped, and a real trailing FORMAT followed by a `#` comment was missed. Fold `#` into the line-comment branch and cover both directions with tests. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D7VzXR4q2UWHrYdU2139MS
Summary
Fixes a bug where SQL queries with trailing
FORMATclauses would fail when submitted to the query API. The API wraps user queries in a paginating subquery with its ownFORMAT JSONclause, but ClickHouse forbidsFORMATclauses inside subqueries, causing 400 errors.Changes
New module
src/lib/sql.ts: ImplementsstripTrailingFormatClause()function that safely removes trailingFORMAT <name>clauses and semicolons from SQL statementsformatDateTime()function calls, columns namedformat)Comprehensive test suite
test/lib/sql.test.ts: 157 lines of tests covering:Updated
src/commands/query.ts: AppliesstripTrailingFormatClause()to user input before posting to the API, with explanatory commentImplementation Details
The
stripTrailingFormatClause()function uses a two-pass approach:https://claude.ai/code/session_01D7VzXR4q2UWHrYdU2139MS
Need help on this PR? Tag
/codesmithwith what you need. Autofix is disabled.