Skip to content

fix(clickhouse): emit MergeTree ORDER BY instead of PRIMARY KEY constraints - #594

Merged
huyplb merged 1 commit into
mainfrom
cursor/clickhouse-mergetree-primary-key
Nov 6, 2026
Merged

huyplb merged 1 commit into
mainfrom
cursor/clickhouse-mergetree-primary-key

Conversation

@huyplb

@huyplb huyplb commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • ClickHouse rejects table-level PRIMARY KEY (Expected CHECK, ASSUME). CREATE now uses ENGINE = MergeTree ORDER BY …, and an engine PRIMARY KEY only when it differs from ORDER BY.
  • Composite keys become ORDER BY (a, b). Catalog sorting_key / primary_key win on introspect. In-place key/engine changes are -- review: recreate notes, not invalid ALTER ADD PRIMARY KEY.
  • Rebased cleanly onto current main (0.2.389). 199 focused tests passed. Docker Desktop would not start here, so live ClickHouse was not re-run.

Test plan

  • clickhouse.sql-dialect.test.ts — MergeTree ORDER BY, composite wrap, PRIMARY KEY only when it differs
  • sql-generator / compare / schema-to-tables / tableBlueprintSql
  • Compare two ClickHouse connections: create order_coupons with ORDER BY (order_id, coupon_id) (no CONSTRAINT … PRIMARY KEY)
  • Drop/recreate still uses review notes, not ALTER ADD PRIMARY KEY

Made with Cursor


Note

Medium Risk
Touches migration DDL, schema compare, and introspection for ClickHouse; wrong ORDER BY or qualification could produce unusable or mis-targeted SQL, though Postgres/MySQL paths are guarded by dialect flags and tests.

Reviewed by Cursor Bugbot for commit c18b533. Bugbot is set up for automated code reviews on this repo. Configure here.

…raints

ClickHouse rejects table-level PRIMARY KEY (only CHECK/ASSUME are valid there).
Map logical and composite keys to ENGINE = MergeTree ORDER BY, and review in-place
key changes instead of ALTER ADD PRIMARY KEY.

Co-authored-by: Cursor <cursoragent@cursor.com>
@cursor

cursor Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: 3dd3b408-9a26-4dde-8a99-362dd1d1779a)

@huyplb
huyplb merged commit c9941e4 into main Nov 6, 2026
12 checks passed
@huyplb
huyplb deleted the cursor/clickhouse-mergetree-primary-key branch November 6, 2026 19:34
huyplb pushed a commit that referenced this pull request Nov 7, 2026
Two paths put raw catalog names into the ClickHouse `ENGINE … ORDER BY`
clause, bypassing `ident`. Both are injectable the same way as the quoted
identifier fixed in the previous commit.

PRIMARY KEY

    pkCols = table.primaryKey?.columns ?? []
    ` PRIMARY KEY ${clickHouseOrderBySql(pkCols)}`

The dialect read the names off `table` itself, so they never met a quoter:

    ) ENGINE = MergeTree PRIMARY KEY id) ORDER BY (id) -- ORDER BY (id, ts);

`createTableSuffix` now receives `quotedPrimaryKey` from the caller, which is
the only place that holds `ident`, and is documented to use the arrays rather
than reading names off `table`. A primary key is a column list, so it is always
identifier-quoted, with no expression passthrough.

ORDER BY

The caller decided what was an expression by looking at the text:

    orderSrc.map((c) => (/[()]/.test(c) ? c : this.ident(c)))

That exists because a sorting key may legitimately be `tuple()` or
`cityHash64(id)`, which quoting would break. But a column named
`id) ENGINE = MergeTree ORDER BY (id` also contains a paren, so it went through
raw and rewrote the clause.

Trust now comes from provenance, not from the text. `TableSchema.orderByFromCatalog`
marks entries that are ClickHouse's own rendering of `system.tables.sorting_key`
— already valid, correctly quoted SQL, and the only place an expression can come
from. The provider sets it there and deliberately leaves it unset for the
`system.columns.is_in_sorting_key` fallback, which pushes bare column names.
Marked entries pass through verbatim; everything else is quoted.

Call sites

Both callers were checked. The SQL editor's table blueprint
(`tableBlueprintSql.ts`) had the same paren rule and also handed the dialect raw
`primaryKey.columns`; its names are typed by the person building the table, never
a catalog expression, so they are now quoted with no passthrough. Its quoting
primitive (`quoteIdentifierIfNeeded`) was already sound — a strict plain-name
regex, otherwise always escaping — so no change was needed there.

Compatibility

Existing valid output is byte-identical, verified against the cases #594 added:

    ) ENGINE = MergeTree ORDER BY id;
    ) ENGINE = MergeTree ORDER BY (order_id, coupon_id);
    ) ENGINE = ReplacingMergeTree PRIMARY KEY id ORDER BY (id, code);
    ) ENGINE = MergeTree ORDER BY tuple();

and catalog expressions still survive: `cityHash64(id)`, `tuple()`,
`` `order id` ``. Bare, properly quoted and schema-qualified identifiers are
unaffected.

The new tests use payloads carrying a parenthesis, because the rule being
replaced passed any entry containing one through raw — without it they could not
tell a fixed generator from a broken one. 8 of them fail on the unfixed
generator and all pass with it; the compatibility and catalog-expression cases
pass either way by design.

Known trade-off: a catalog `primary_key` that is itself an expression is now
emitted quoted, so such a table needs manual review rather than silently
producing a key over a column that does not exist. PRIMARY KEY taking no
expressions is deliberate.

  npx vitest run         6914 passed, 1 expected fail, 145 skipped (was 6898)
  npx eslint .           exit 0
  npm run lint:security  exit 0
  npm run typecheck      exit 0

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ehcTb7vYMCawV1ajxwfxB
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.

1 participant