Repository navigation
Reject a Distributed sharding key with IN over a subquery or a non-Set table - #124646
alexey-milovidov merged 3 commits into
Conversation
…t table The sharding key is compiled once in the StorageDistributed constructor. An IN over a subquery or over a non-Set table registers a FutureSetFromSubquery there, and nothing ever builds it for table metadata, so every consumer of the key sees an unready set: an INSERT fails in DistributedSink::createSelector with "Not-ready Set is passed as the second argument for function 'in'" (LOGICAL_ERROR), and the optimize_skip_unused_shards path folds the key to a wrong constant and prunes to the wrong shard (26.8 and 26.3 return 0 instead of 1 for a point query on such a table). Reject such a key with BAD_ARGUMENTS right after it is built. A tuple or Set-table IN is a ready set and keeps working. As for arrayJoin (be38893), only a definition stated now is checked (is_fresh_definition): stored tables still load and ALTER does not revalidate. The check runs after the build so a missing table in the key still reports UNKNOWN_TABLE (02111_global_context_temporary_tables). Found by BuzzHouse (amd_tsan) on ClickHouse#117004, where the INSERT into remote() with c0 GLOBAL NOT IN <Alias of a Remote table> segfaulted before ClickHouse#121639 and fails with this LOGICAL_ERROR after it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove the comment above the sharding key subquery-set check; the exception message states the same thing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (click to expand)Head
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20261008-130122 |
|
Workflow [PR], commit [1a95d8a] Summary: ✅
AI ReviewSummaryThis PR closes the original Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 1a95d8a.
Changed lines: Changed C/C++ lines covered: 42/42 (100.00%) · Uncovered code |
…e by a Set table A sharding key with `IN` over a subquery or a non-Set table is rejected only when the definition is stated now (`is_fresh_definition`), so such a table created by an older version still loads. Its set is still never built, so an `INSERT` that needs the key failed with the `Not-ready Set` logical error and `optimize_skip_unused_shards` folded the key to a wrong constant. Remember that the key holds such a set: the insert selector throws the same `BAD_ARGUMENTS` before executing the key, shard pruning does not use it (with `force_optimize_skip_unused_shards` the `BAD_ARGUMENTS` is thrown), and an `INSERT` with `insert_shard_id` or into a one-shard cluster, which never executes the key, keeps working. `IN <Set table>` stays allowed, but the `Set` table can change after rows are placed by it, so a key reading one is no longer treated as deterministic for shard pruning, the same as a key reading a `Join` table through `joinGet`. Pruning by it now needs `allow_nondeterministic_optimize_skip_unused_shards`. Measured on master: rows placed while the set was empty, then `INSERT INTO set_t VALUES (1)`, and `WHERE c0 = 1 SETTINGS optimize_skip_unused_shards = 1` returned 0 instead of 1. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #117004
Related: #121639
Related: #110463
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A sharding key of a
Distributedtable, aRemotetable or theremote/clustertable functions that containsINwith a subquery or with a non-Settable is now rejected when the table is defined. The set of such anINwas never built, so everyINSERTfailed with aNot-ready Setlogical error andoptimize_skip_unused_shardscould prune to the wrong shard and return wrong results. A table defined earlier with such a key still loads, but anINSERTthat needs the key fails withBAD_ARGUMENTSand shard pruning does not use it. Pruning by a key withINover aSettable, which can change after rows are placed, now needsallow_nondeterministic_optimize_skip_unused_shards, as forjoinGet.Description
CI:
BuzzHouse (amd_tsan)on #117004,INSERT INTO FUNCTION remote(..., c0 GLOBAL NOT IN <Alias of a Remote table>). Its segfault is fixed by #121639; with it, the statement fails withNot-ready Set(LOGICAL_ERROR,DistributedSink::createSelector).On 26.8.2.7 (also 26.3.42.3), with sharding key
c0 IN vip(vipa MergeTree table holding1) and the rows placed by that key,SELECT count() FROM d WHERE c0 = 1 SETTINGS optimize_skip_unused_shards = 1returns 0 instead of 1, andINSERT INTO dfails with the same logical error.Root cause: the key is compiled once in the
StorageDistributedconstructor, where such anINregisters aFutureSetFromSubquerythat nothing builds.The constructor now rejects such a key with
BAD_ARGUMENTS. As in #117791 (arrayJoin), only a definition stated now is checked (is_fresh_definition), so stored tables still load. For them theINSERTselector throws the same error before executing the key, shard pruning skips it (force_optimize_skip_unused_shardsthrows), and a storedAS remote(...)table throws on first access.IN (1, 2)andIN <Set table>are ready sets and still work, but aSettable can change after rows are placed, so such a key is now non-deterministic for shard pruning, likejoinGet(on master, adding a value to the set made the query above return 0 instead of 1).Alternative: a syntactic
checkExpressionDoesntContainSubqueries(as for MergeTree keys) would also reject theIN <Set table>key.Tests:
05339and05340, both fail on master.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/124646]
Version info
26.10.1.1990-master(included in26.10and later)