Repository navigation
Forbid table references in IN operator in key expressions - #107424
alexey-milovidov merged 3 commits into
Conversation
A key expression such as `ORDER BY (x IN some_table)` (or in PRIMARY KEY, PARTITION BY, or a skip INDEX) builds a FutureSet for the IN operator that is only filled in a SELECT pipeline. During INSERT the key is evaluated by the MergeTree writer, where the set is never built, so FunctionIn aborts the server with `Logical error: Not-ready Set is passed as the second argument for function 'in'`. The subquery form `x IN (SELECT ...)` is already rejected at DDL time by checkExpressionDoesntContainSubqueries (added to forbid subqueries in key expressions). The table-identifier form `x IN table` produces an ASTIdentifier / ASTTableIdentifier on the right-hand side instead of an ASTSubquery, so it slips past that check and then crashes on the first INSERT. It has the same defect and no practical use case, so reject it at DDL time as well. Literal sets (`x IN (1, 2, 3)`, `x IN [1, 2, 3]`) are constants and remain allowed in key expressions. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-0:20260613-220500 |
|
Workflow [PR], commit [4bee9a0] Summary: ✅
AI ReviewSummaryThis PR rejects Findings
Tests
Final VerdictStatus: Minimum required actions: make the new table-RHS validation preserve alias-expanded literal sets, and either address or explicitly document the upgrade/startup behavior for existing metadata that now fails validation. |
|
CI finished on
|
…able-in-key-expression
Address review feedback (@alexey-milovidov): a bare identifier on the right-hand side of `IN`, such as `c0 IN c1`, is always interpreted as a table reference, never as a column. `MarkTableIdentifiersVisitor` rewrites it to a table identifier, so `a IN b` resolves to `a IN (SELECT * FROM b)`. There is therefore no legitimate "column on the right of IN" key expression to preserve: on previous versions such a key failed with `UNKNOWN_TABLE` when the name did not resolve to a table, or with the "Not-ready Set" `LOGICAL_ERROR` on the first INSERT when it did. Rejecting it at DDL time is not an over-rejection of a valid case. Add the column form (`c0 IN c1`) and the qualified-table form (`c0 IN default.tref`) to `03916_merge_tree_order_by_subquery`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Picked this up ( Disposition of the two open threads:
CI was green on |
| if (args && args->children.size() == 2) | ||
| { | ||
| const auto & rhs = args->children[1]; | ||
| if (rhs->as<ASTIdentifier>() || rhs->as<ASTTableIdentifier>()) |
There was a problem hiding this comment.
This rejects more than table references because the check runs before the alias pass that normally disambiguates IN RHS identifiers. TreeRewriter::normalize first collects aliases, MarkTableIdentifiersVisitor deliberately skips an IN RHS when aliases.contains(identifier->name()), and then QueryNormalizer expands the alias. So a key such as:
ORDER BY (c0 IN ((1, 2, 3) AS s), c0 IN s)uses only literal sets and should not build a table FutureSet, but this pre-analysis ASTIdentifier check rejects the second expression and will also reject existing metadata on ATTACH/startup. Please make this validation alias-aware, or perform the table-RHS rejection after table identifiers have been marked, and add a regression for an alias to a literal set.
There was a problem hiding this comment.
I tested the exact example on master (before this PR): the alias-to-literal-set form is not a working key.
ORDER BY (c0 IN ((1, 2, 3) AS s), c0 IN s) -- CREATE succeeds, but the first INSERT aborts with
Code: 10. NOT_FOUND_COLUMN_IN_BLOCK: Not found column in(c0, s) in block. There are only columns: c0, in(c0, (1, 2, 3)).
The alias s is not expanded inside the stored key: SHOW CREATE keeps it as ORDER BY (c0 IN ((1, 2, 3) AS s), c0 IN (s)), so at INSERT time the key wants a column in(c0, s) that the block does not have. Unlike a SELECT projection, the key expression is not normalized through the alias pass, so c0 IN s does not become a literal set. Reversed order fails even earlier with UNKNOWN_TABLE (s taken as a table).
So c0 IN s is the same bug class as c0 IN <table> / c0 IN <column>: a non-functional key that fails on first use. The structural reject is not over-rejecting a valid case; it converts the first-INSERT abort into a clean BAD_ARGUMENTS at CREATE.
The alias-DEFINE occurrence c0 IN ((1, 2, 3) AS s) is unaffected: its RHS is a Literal Tuple (alias s), not a bare identifier, so the check does not fire and it still works. The ATTACH/startup point is the compatibility concern already resolved on this PR (these non-functional keys are rejected unconditionally on purpose).
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 10/10 (100.00%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 1 line(s) · Uncovered code |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a server crash (
Logical error: Not-ready Set is passed as the second argument for function 'in') when a key expression (ORDER BY,PRIMARY KEY,PARTITION BY, or a skipINDEX) contained anINoperator with a table on the right-hand side, e.g.ORDER BY (x IN some_table). Such key expressions are now rejected at table creation time.Description
Found by the AST fuzzer (
AST fuzzer (amd_debug, targeted), STID0250-424f) on PR #106650.Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106650&sha=fbc0e4d4abc1d27e19154633fa7c8ec6accdcd5a&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29
This is not a regression of #106650 (the sort PR). It is a pre-existing trunk bug in DDL validation that the fuzzer happened to surface there.
Reproducer (deterministic, plain master, no fuzzer)
Root cause
ORDER BY (x IN (SELECT ...))is already rejected at DDL time bycheckExpressionDoesntContainSubqueries(the change that forbade subqueries in key expressions, closing #96657). That check only looks forASTSubquerynodes. The table-reference formx IN some_tableparses to anASTIdentifier/ASTTableIdentifieron the right-hand side, not anASTSubquery, so it slips past the check. The table is created, and on the first INSERT the MergeTree writer evaluates the sorting key, where theFunctionInset was never built (set-building plans are only added for SELECT pipelines), soFunctionIn::executeImplaborts the server.Fix
Extend
checkExpressionDoesntContainSubqueriesto also reject anIN/GLOBAL IN/NOT IN(and thenull/globalvariants) operator whose second argument is an identifier (table reference) in a key expression. This mirrors the existing subquery rejection and coversORDER BY,PRIMARY KEY,PARTITION BY, skipINDEX, and theALTERpaths through the same chokepoint. Literal sets (x IN (1, 2, 3),x IN [1, 2, 3]) are constants and remain allowed. Test cases for the new behavior are added to03916_merge_tree_order_by_subquery.Note on backward compatibility: a table with
x IN tablein a key can be created by current versions, persists on disk, and re-attaches on restart (every INSERT to it crashes the server). The new rejection runs throughgetKeyFromAST/getIndexFromAST, the same path the subquery rejection uses, so it fires at CREATE. Since such a table is already non-functional, failing fast at CREATE seems acceptable, but I am happy to gate the new check onLoadingStrictnessLevel::CREATEif maintainers prefer to keep ATTACH of pre-existing broken tables working.Version info
26.6.1.1072