Repository navigation
Conversation
…ther side's rows `ActionsDAG::splitActionsForJOINFilterPushDown` classifies a `WHERE` conjunct as pushable to both join inputs when every one of its inputs is an equi-join-key column, copies it to the opposite side with that key column substituted for the equivalent one, and installs the copy as a `FilterStep` below the opposite side's read. The copy therefore runs on values the side the conjunct names never held. The only property `getConjunctionNodes` tests about the conjunct's functions is `isDeterministicInScopeOfQuery()`, and a partial function can be perfectly deterministic. So `t2.c0 LIKE t2.c0`, which names only `t2`, became `like(__table3.c0, __table3.c0)` on the `t3` leg, and a `t3` row holding a single backslash (an intentionally invalid LIKE pattern that is not a join partner of anything in `t2`) turned a query with a well-defined answer into `CANNOT_PARSE_ESCAPE_SEQUENCE`. Reported in ClickHouse#119883. Admit a both-streams classification only for a conjunct built from functions that are total on their argument types. The invariant is not new: the sibling pass `propagatePredicateAcrossEquiJoin.cpp` already refuses the same transfer for the same reason in `atomSafelySubstitutable`, gating its comparison arm on `comparisonCanThrow`, and its in-tree comment states the argument outright. This extends that invariant from a single atom to a conjunct, on the other carrier of the same transfer, and reuses the same free function for comparisons. The property that matters is partiality, not `LIKE`: `toUInt64(u2.c0) = 1` over a `String` equi-key fails the same way for a target-only `'x'`, and is fixed by the same guard because the walk rejects the whole conjunct on its nested `toUInt64`. Two gates, no exemption. The type gate rejects a conjunct with a `Variant`, `Dynamic` or JSON type anywhere in a node's type, including nested, because the default implementation then builds the real function per stored alternative at execution time: `NOT k` over a `Dynamic` equi-key throws `ILLEGAL_TYPE_OF_ARGUMENT` for a `String` alternative only the opposite side holds. `hasDynamicStructure()` is not that predicate, since a plain `Variant(String, UInt64)` reports `false`. The name gate is a closed whitelist, each member total given the type gate: the logical functions read the boolean value and null map only, `isNull`/`isNotNull` return `UInt8` for every argument type without reading the value, the six comparisons are admitted only when `comparisonCanThrow` is false, and `__applyFilter` probes a set built from the very common type its key argument is cast to, so its per-row lookup is an identity cast. `in` is not admitted even though the sibling pass admits it: an `IN` set's key types come from the user's own expression list and are independent of the left argument's type, so there the cast is real. Rejecting from the both-streams classification loses nothing on the conjunct's own side, which is classified independently into that side's list and still erased from the post-join filter. Co-Authored-By: Claude Opus 5 <[email protected]>
The preceding commit rejected every set-probing conjunct from the both-streams classification, `in` included. That was too coarse and it had a measured price: TPC-H Q18's `o_orderkey IN (SELECT l_orderkey ... HAVING sum(l_quantity) > 300)` lost the copy of its filter on the `lineitem` leg, changing the pinned plans in `05024_tpch_join_order_plans_aggregation_pushdown` and `03836_tpch_join_order_plans`. A set lookup casts the probe column into the set's key type once per row (`Set::execute`), so it is total exactly when that type is already the probe column's own: `castColumn` returns its argument untouched for equal types, and an identity cast neither parses nor narrows a value. That is the same argument by which the preceding commit admits the JOIN runtime filter, which reaches the same lookup with its probe cast to the very common type its set was built from. The difference is only in how the equality is established: the runtime filter gets it by construction, while an `IN` set's key types come from the user's own expression list, so here it is checked on the node. The probe type this check reads is the type the lookup receives, because a substitution's replacement column always carries the type the name it replaces has in the JOIN output (`filterPushDown.cpp:526-528`, `:714-715`, `:765-780`). That is what the preceding commit's `__applyFilter` arm rests on too. The check reads the set's declared key types, which never builds the set and therefore cannot execute an `IN` subquery during optimization. A set whose header the planner has not installed reports no types at all; that is treated as unknown and rejects, so the guard fails closed. A multi-key (tuple) set and a `Variant`/`Dynamic`/JSON key type also reject. The declared key types have `LowCardinality` removed recursively while the type the lookup casts into keeps a nested one, so the probe type is normalized the same way; what can then still differ between the type compared here and the cast target is a `LowCardinality` wrapper, which re-encodes a value against a dictionary without reading it. `Nullable` is not stripped from either side, so a `Nullable` probe never matches a bare key type. Only the name `in` is admitted, since the per-row cast is an identity only where the types are already equal; `notIn`, `nullIn` and `globalIn` reach the same lookup but stay rejected because nothing here exercises them, the same rule that keeps `tuple` off the whitelist. Co-Authored-By: Claude Opus 5 <[email protected]>
…lity guard Eleven cases, one per decision the guard makes. The three that assert a plan shape count the legs a conjunct's filter appears under, because `EXPLAIN actions = 1` prints `Filter column: <expr>` and never the substituted qualified name: two legs means the conjunct was copied to the opposite side, one means only its own side kept it. Cases 1, 2, 2b, 5, 6b and 9 throw before this change (`Code: 25` for a trailing backslash LIKE pattern, `Code: 6` for a string that is not a number, `Code: 43` for a `Dynamic` alternative the function cannot accept, `Code: 27` for a string that is not a tuple) and return the joining row after it. Cases 4 and 8 are controls that pass before and after: a total predicate and an equal-type `IN` set are still copied to both legs, so an always-reject guard fails them. Case 3 is the negative plan assertion for the reported query, case 6 is the one for a set whose key type differs from the probe column's, and case 7 the one for a comparison of two decimals of different scales, which rescales one side per row. `transform_null_in` appears in case 6b only, where it renames the function to `nullIn`: that is the shape in which a set lookup's per-row cast is reached and throws, and it is refused by name rather than by the type check case 6 exercises. Co-Authored-By: Claude Opus 5 <[email protected]>
`propagatePredicateAcrossEquiJoin` is the second carrier of the substitution this
branch already guards in the both-streams filter push-down: it copies a conjunct
that sits below the join onto the other side with the equi-key substituted, and
`atomSafelySubstitutable` gated only the comparison shape, not `in`. So the same
partial set lookup was still transferred, and the copy ran on target rows the
source never held.
CREATE TABLE pk_tup_src (c0 String) ENGINE = MergeTree ORDER BY c0;
CREATE TABLE pk_tup_dst (c0 String) ENGINE = MergeTree ORDER BY c0;
INSERT INTO pk_tup_src VALUES ('(1)');
INSERT INTO pk_tup_dst VALUES ('(1)'), ('abc');
SELECT s.c0 FROM (SELECT pk_tup_src.c0 AS c0 FROM pk_tup_src
INNER JOIN pk_tup_dst ON pk_tup_src.c0 = pk_tup_dst.c0) AS s
WHERE s.c0 IN (SELECT tuple(toUInt64(1)));
returned `(1)` with `query_plan_propagate_predicate_across_join = 0` and threw
`Code: 27 ... Cannot parse input: expected '(' before: 'abc'` with `= 1`, naming
`__table3.c0`, the target's column. Reachability is ordinary: the pass needs a
`FilterStep` below the join on the source side, which the push-down produces for
the conjunct's own side, and a target reading a MergeTree primary key that holds
the substituted column.
The earlier probe of this pass could not have shown it: it pinned
`transform_null_in = 1`, which renames the function to `nullIn`, a name
`atomSafelySubstitutable` refuses outright. The admitted shape is a plain `in`
whose set key type is a `Tuple`, delivered by a subquery. A constant set of that
type does not reach the throwing arm at all.
To avoid two copies of the predicate, `probeTypeMatchesSetKey` becomes the
documented `ActionsDAG::setLookupCanThrow` static with inverted polarity, so both
call sites read like the `comparisonCanThrow` line they sit next to. No type gate
is owed at the new call site: `setLookupCanThrow` already rejects a set key type
that adapts per row, and an admitted pair must have the probe type equal to that
key type, so a `Variant`/`Dynamic`/JSON probe cannot be admitted either.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 blocker over two rounds, 0 findings on
Severity: ❌ blocker / One question for the owner of this code, which I deliberately did not decide myself. The same partiality Session id: cron:clickhouse-review-slot-8:20260914-184000 |
|
Workflow [PR], commit [3bc23f6] Summary: ❌
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20260914-173037 |
|
|
||
| /// getConjunctionNodes() classifies a conjunct as pushable to a side when all of its inputs are | ||
| /// allowed inputs of that side. A conjunct with no inputs (a pure constant such as a literal `1` | ||
| /// or a folded `NULL`) satisfies this trivially, so it is classified as pushable to EVERY side, |
There was a problem hiding this comment.
This closes the opposite-side substitution, but the same partial predicate is still pushed to its own side through left_stream_push_down_conjunctions / right_stream_push_down_conjunctions. Once that happens, removeUnusedConjunctions erases the original post-join filter and the pre-join FilterStep still evaluates the predicate on rows the join would have discarded.
For example, this shape can still throw on the unmatched src row holding '\\', even though the joined result is just 'a':
SELECT src.c0
FROM src INNER JOIN dst ON src.c0 = dst.c0
WHERE src.c0 LIKE src.c0;with src = {'a', '\\'} and dst = {'a'}.
I think the same conjunctIsTotal gate needs to run for the single-side push-down lists as well, or those non-total conjuncts need to stay above the join.
There was a problem hiding this comment.
Verified before answering, and the mechanism is real: the own-side conjunct is pushed below the join and the post-join copy is erased, so it runs on rows with no join partner. What I measured is that this is not something this PR introduces or leaves half done.
Your exact shape (src = {'a', '\'}, dst = {'a'}, MergeTree tables), on binaries differing only by this PR's diff:
| binary | result |
|---|---|
this branch, b4ea37d |
Code: 25 ... like(__table1.c0, __table1.c0) (CANNOT_PARSE_ESCAPE_SEQUENCE) |
pristine merge base 0d04f2b |
same error, same text |
| official 26.8.2.7 | same error, same text |
The qualifier is __table1, i.e. src, the predicate's own table. The copy this PR removes is the one rebound to the other table, __table3 in the issue's shape, which now returns the joining row. EXPLAIN on your shape shows the scope of the change: two Filter steps below the join on the merge base (one under src, one under dst), one after this change (only the one under src).
conjunctIsTotal is also not a gate that leg can use. Put the predicate on a non-key column (ON s2.k = d2.k with WHERE s2.c0 LIKE s2.c0) and the conjunct never enters the both-streams list at all, yet it throws identically on all three binaries, while query_plan_filter_push_down = 0 removes the throw on all three. So the own-side case is not a missing gate in this classification, it is the general push-down policy: a non-total predicate is evaluated before the join for every join shape. Narrowing that would stop push-down of every partial predicate (LIKE, casts, arithmetic, JSON extraction) under every join, which is wider than a bug fix, so I left it alone and asked the owner of this code about it in the adjudication comment on this PR instead of deciding it myself.
Correct mechanism, out of scope here, no change in this PR.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units1211 translation units recompiled, 8639 s compile time in total, 1211 of them have a recent master baseline. |
CI finish ledger - b4ea37dEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260915-010059 |
|
Agreed on the size, and most of it is mine to cut: of the 358 added lines, 224 are the test and 83 are code. I will fold the three helpers into one predicate with an explicit function whitelist, cut the comments to what the code does not already say, and reduce the test to the two forms from the issue plus the one One thing worth weighing first: the same gate also fixes six shapes that return wrong results on master today, on equal-typed equi-keys (measured at #120290 (comment)). If you would still rather not carry this, say so and I will close the PR instead of reworking it. Your question on the issue is answered there: #119883 (comment) |
…-pushdown Resolves the one conflict in `ActionsDAG::splitActionsForJOINFilterPushDown`: master's cross-type representation gate (from ClickHouse#117973) and this branch's totality gate filter the same both-streams conjunct list, so both blocks are kept, this branch's first. Co-Authored-By: Claude Opus 5 <[email protected]>
Same behaviour, expressed as one predicate. `functionIsTotal` and `conjunctIsTotal` are folded into `ActionsDAG::conjunctIsTotal` with the admitted names as two explicit sets, and `setLookupCanThrow` leaves the public interface: `propagatePredicateAcrossEquiJoin` now asks the same single question the both-streams gate asks, so the two carriers of the substitution cannot drift apart. The test keeps one case per scenario: the reported query, a `Dynamic` argument, a set lookup whose key type is not the probe column's own, plus the two controls that a total conjunct is still copied to both legs and that a matching-type set lookup is still substituted. Measured against master 3f29c3a: the three defect cases throw there (Code 25, Code 43, Code 27) and pass here, and both controls are unchanged. Six related push-down tests produce identical output on both binaries. Requested in review by rschu1ze: the previous shape was 358 added lines for a gate whose logic is six admitted function names. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Simplified and pushed as
That is 257 added lines against 358, not the 30 plus 40 I estimated. The residue is the six admitted function names, the two type-pair oracles, and one case per branch behind them; I did not find a smaller shape that keeps |
|
So the gate is conservative in a way that costs behaviour that is live on master today, not only unreachable cases. My earlier sweep of six related push-down tests did not include these two, which is how it got past me. Admitting both shapes needs two more rules on top of the 113 lines that are there: a That is a scope decision rather than a measurement, and both directions run against your review, so I would rather have your call than guess: extend the gate, narrow it to the top-level conjunct, or close this and leave #119883 as a known limitation of the push-down. I can do any of the three. Updating the two references is not on that list: their negative siblings assert the same |
|
Hi @rschu1ze, gentle ping on the scope call I asked for on 2026-09-17: extend the gate so that a |
Closes: #119883
Related: #117973
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed an exception from a
JOINquery whose filter over an equi-join key uses a function that is not defined for every value, such asLIKEortoUInt64. The filter was copied to the opposite side of the join with the key column substituted, so it ran on values only that side holds, and a value the function cannot process (forLIKE, a row holding a single backslash) turned a query with a well-defined answer into an error.Description
ActionsDAG::splitActionsForJOINFilterPushDownclassifies aWHEREconjunct as pushable to both joininputs when all of its inputs are equi-join-key columns, then copies it below the opposite side's read with
that key column substituted, so the copy runs on values the conjunct's own table never held. Deterministic
is all
getConjunctionNodestests, and a partial function is deterministic, sot2.c0 LIKE t2.c0becamelike(__table3.c0, __table3.c0)and a non-joiningt3row holding one backslash raisedCANNOT_PARSE_ESCAPE_SEQUENCE. Reproduced on 26.3, 26.6, 26.7 and 26.8. The property is partiality, notLIKE:toUInt64(t2.c0) = 1over aStringequi-key fails the same way.ActionsDAG::conjunctIsTotalanswers one question about the conjunct: does every function in it have avalue for every value of its argument types? Six names read a discriminator, a null map or a boolean
rather than the value; comparisons and set lookups are admitted on their type pair (
comparisonCanThrow,and the set's declared key type being the probe column's own); everything else is refused, as is any
Variant,Dynamicor JSON argument, whose real function is resolved per stored alternative at executiontime.
propagatePredicateAcrossEquiJoincarries the same substitution and had the same hole for a partialIN, so it now asks that same one question.Rejecting a conjunct loses nothing on its own side, which is classified independently; what loses the
opposite-side copy is a shape like
k LIKE 'a%'ork % 2 = 0. Each test case pins one branch of thegate: six of them throw or change the filter leg count on master, two are controls that a total conjunct
is still copied to both legs.
Workflow [PR]
Sync PR [sync-upstream/pr/120069]