Repository navigation
fix(clickhouse): emit MergeTree ORDER BY instead of PRIMARY KEY constraints - #594
Merged
Merged
Conversation
…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>
Contributor
Bugbot couldn't run - usage limit reachedBugbot 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
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
5 of 6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PRIMARY KEY(Expected CHECK, ASSUME). CREATE now usesENGINE = MergeTree ORDER BY …, and an enginePRIMARY KEYonly when it differs from ORDER BY.ORDER BY (a, b). Catalogsorting_key/primary_keywin on introspect. In-place key/engine changes are-- review:recreate notes, not invalid ALTER ADD PRIMARY KEY.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 differsorder_couponswithORDER BY (order_id, coupon_id)(noCONSTRAINT … 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.