clickhouse-js icon indicating copy to clipboard operation
clickhouse-js copied to clipboard

fix: back-quote insert() column identifiers with special characters

Open polyglotAI-bot opened this issue 4 months ago • 2 comments

Description

Fixes https://github.com/ClickHouse/clickhouse-js/issues/945.

getInsertQuery built the INSERT INTO t (<cols>) FORMAT ... statement by joining the columns array (and the columns.except list) verbatim. A column name containing a space or other special character therefore produced invalid SQL — e.g. columns: ['test id', 'name'] generated INSERT INTO t (test id, name) FORMAT ..., which the server rejects with Code: 62 ... Syntax error.

The fix back-quotes each column identifier at the generation site, escaping backslashes and backticks exactly the way the ClickHouse server's own backQuote does (\ → \\, ` → \`). Identifiers that are already quoted (with backticks or double quotes) are passed through unchanged, so callers that pre-quoted their column names as a workaround keep working — the change is backward compatible.

Verified against ClickHouse 26.5:

  • (\test id`, `name`)` is accepted; the unquoted form is the reported syntax error.
  • A backslash inside a back-quoted identifier is an escape character, so it must itself be escaped (naive `a\b` targets the wrong column) — hence matching backQuote.
  • Whole-string quoting a dotted/Nested column (`n.arr1`) targets the same physical column as the unquoted form, so no dot-splitting is needed.

The changed code lives in the shared common module that is bundled into both @clickhouse/client and @clickhouse/client-web, so the fix (and changelog entries) apply to both.

Changes

  • packages/client-common/src/client.ts: add an escapeColumn helper and apply it to both the array and columns.except branches of getInsertQuery; update the InsertParams.columns JSDoc to document the auto-quoting + passthrough behavior.
  • packages/client-node/CHANGELOG.md and packages/client-web/CHANGELOG.md: add a # 1.23.2 → ## Bug Fixes entry.

Test

  • Unit (packages/client-common/__tests__/unit/insert_query_columns.test.ts, new): asserts the exact generated SQL through the real client.insert() path (mocked connection) for plain, spaced, embedded-backtick, embedded-backslash, combined backslash+backtick, lone-backtick, and dotted/Nested identifiers, plus the columns.except cases. Includes contrast cases proving already back-quoted / double-quoted identifiers are passed through unchanged.
  • Integration (insert_specific_columns.test.ts): inserts into a space-named column via columns and excludes a space-named column via columns.except, then reads the data back.
  • Both were confirmed to fail without the fix (server syntax error / wrong SQL) and pass with it; the existing insert tests and the full node unit suite remain green.

Pre-PR validation gate

  • [x] Deterministic repro confirmed (fails on main, passes on this branch)
  • [x] Root cause documented above
  • [x] Fix targets the root cause (back-quoting at the query-generation site)
  • [x] Test fails without fix, passes with fix
  • [x] No existing tests broken (full node unit suite + insert integration tests green)
  • [x] Convention compliance verified per AGENTS.md (changelog in both affected packages, prettier/eslint/typecheck clean)
  • [x] Backward compatible: already-quoted identifiers pass through unchanged

Note

Related source bug in another client: https://github.com/ClickHouse/clickhouse-cpp/issues/30 (identifier quoting in generated INSERT column lists is an SDK-level concern each client implements independently). No backport is requested here; maintainers own any backport decision.

polyglotAI-bot avatar Jul 16 '26 13:07 polyglotAI-bot

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests.

:loudspeaker: Thoughts on this report? Let us know!

codecov[bot] avatar Jul 16 '26 13:07 codecov[bot]

CI note — the failing code-quality (Prettier) check is pre-existing, unrelated to this PR, and non-blocking.

The single failing check is the whole-tree prettier --check . step in code-quality, and it flags exactly one file:

[warn] CODE_OF_CONDUCT.md
[warn] Code style issues found in the above file. Run Prettier with --write to fix.

That file is not part of this PR's diff — it is byte-identical to main. All five files this PR actually changes (packages/client-common/src/client.ts, packages/client-node/CHANGELOG.md, packages/client-web/CHANGELOG.md, and the two new test files) are Prettier-clean.

Root cause: CODE_OF_CONDUCT.md was added to main in #942 in a state Prettier reformats. The node workflow (.github/workflows/tests-node.yml, which hosts the whole-tree code-quality job) only triggers on changes under packages/**, tests/**, package.json, etc. — root-level markdown is excluded from both its push and pull_request path filters. So the whole-tree formatting gate never ran on #942, and it doesn't run on main pushes for that file either. This PR is the first packages/** PR after #942, so it is simply the first to run prettier --check . against the unformatted file.

Since code-quality is not a required status check, this PR is already mergeable (mergeStateStatus: UNSTABLE, not BLOCKED), and the insert-columns backtick-quoting fix is complete — all functional checks (unit, integration, cluster, cloud) are green.

I've intentionally kept the CODE_OF_CONDUCT.md reformat out of this bug PR to keep it to a single concern. It's best fixed at source on main (npx prettier --write CODE_OF_CONDUCT.md), which would also unblock the whole-tree gate for every future packages/** PR; I've flagged that as a separate follow-up.

polyglotAI-bot avatar Jul 16 '26 14:07 polyglotAI-bot