Repository navigation
Do not rewrite a comparison chain into IN across String and FixedString - #117293
alexey-milovidov wants to merge 41 commits into
Conversation
…dString`
`equals` compares the string family zero-padded - `'a' = toFixedString('a', 2)` is true - while
set membership is bytewise, so `'a' IN (toFixedString('a', 2))` is false. Rewriting a chain of
comparisons into `IN`/`NOT IN` therefore made a query contradict itself: with a `String` column
and `FixedString` constants,
`s != toFixedString('a', 2) AND s != toFixedString('b', 2) AND s != toFixedString('c', 2)`
evaluated to true while its own first conjunct evaluated to false, and the `OR`/`IN` direction
lost rows the same way.
`ArrayExistsToHasPass` already declines such a pair for the identical reason; its helper moves to
`Analyzer/Utils` and both passes now share it. The existing lossless-conversion check does not
cover this: converting `FixedString(2)` 'a\0' to `String` preserves the bytes, it is the
comparison semantics that differ.
Closes: #116852
|
Workflow [PR], commit [8a7b8b2] Summary: ✅
AI ReviewSummaryThe PR preserves comparison semantics by declining unsafe rewrites to Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units91 translation units recompiled, 489 s compile time in total, 91 of them have a recent master baseline. |
…redundancy pruning
`addComparisonFilter` converted the constant to the column type and used the result as the
pruning key. For a `String` column and a `FixedString` constant that key is bytewise narrower
than the padded `equals` semantics: `s = toFixedString('a', 2)` converts to `s = 'a\0'`, which
made the pass drop `s != 'a'` as implied, so
`s = toFixedString('a', 2) AND s != 'a'` returned true for `s = 'a'`.
Such filters are now kept opaque, so they take part in neither pruning nor contradiction
detection. Also tag the test `no-old-analyzer`: the rewrite lives in the analyzer and
`EXPLAIN QUERY TREE` is analyzer-only.
…ed-string-padding
|
🕵 @groeneai, investigate the failure: It is a job-level container memory-budget guard with no individual test failures, and this pull request only changes an analyzer pass, so it cannot be the cause. I could not find an open issue or pull request tracking it. The |
|
Confirmed: every test passed on all three jobs ( The victim class in each job's own kernel record picks the owner. Every kill is
Per shard, not in aggregate. Trees carrying the The entire live residual is I could not find an open issue either, and I have not filed one: two merged fixes plus an open successor track this more precisely than a new issue would. Say so and I will file it. |
Master took `05079` for two other tests, so move `05079_chain_to_in_fixed_string_padding` to `05166`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Merged Both reds on 1. SELECT 1, * FROM (SELECT 2 AS x) AS a LEFT JOIN (SELECT 1, 3 AS y) AS b ON y = x
-- 1 2 0 0
SELECT * FROM (SELECT 1, * FROM (SELECT 2 AS x) AS a LEFT JOIN (SELECT 1, 3 AS y) AS b ON y = x)
-- 0 2 0 0I reproduced it on an unrelated build of the same 2. @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117293&sha=1209779bd81c5de44849dc0908019d0bda7b670a&name_0=PR&name_1=Stress%20test%20%28arm_debug%29 and provide a fix in a separate pull request. If the fix is already in progress, link it here. |
|
Acknowledged. There is no fix in progress for this one: the signature (STID 2508-4d0e) has exactly one occurrence in 90 days of CIDB and I found no open issue or pull request for it, so I am investigating and will follow up here with a separate pull request. |
|
Fix: #119264
The fix is one line: create the task deactivated, which is the convention the storage constructor already states for its other tasks. The refused A new integration test forces the interleaving with a failpoint plus |
…ed-string-padding
…the test `equals` compares floating-point values numerically, while the set built for `IN` is keyed on the raw bits: `-0.0 = 0.0` is true but `-0.0 IN (0.0)` is false, and `nan = nan` is false but `nan IN (nan)` is true. Rewriting `f = 0.0 OR f = 1.0 OR f = 2.0` into `f IN (0.0, 1.0, 2.0)` therefore lost the `-0.0` rows, and the `NOT IN` direction had the symmetric bug. Decline both chain rewrites when the expression's type contains a float (`Float32`, `Float64`, `BFloat16`, also inside `Nullable`, `LowCardinality`, `Array`, `Tuple` and `Map`), the same way `HasToInPass` already does. The existing test is renumbered from `05166` to `05227` because `master` took `05166` for other tests meanwhile. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Verified the test gap raised on #121770 (two The file covers the Measured on master
Block, to go in before the final -- Two `equals` on the same expression need no chain: the `FixedString` constant converts to
-- `'a\0'`, which contradicts `s = 'a'`, so the conjunction was folded to constant false while
-- both of its terms are true. `optimize_redundant_comparisons` is on by default here.
SELECT 'equals pair';
SELECT (s = 'a' AND s = toFixedString('a', 2)) AS pair,
(s = 'a') AS lhs,
(s = toFixedString('a', 2)) AS rhs
FROM t_chain_fixed_string;
SELECT count() FROM t_chain_fixed_string WHERE s = 'a' AND s = toFixedString('a', 2);
SELECT count() FROM t_chain_fixed_string WHERE toLowCardinality(s) = 'a' AND toLowCardinality(s) = toFixedString('a', 2);
-- A pair of same-type constants is still folded.
SELECT count() FROM t_chain_fixed_string WHERE s = 'a' AND s = 'b';and for With the block appended, the file's output on your code is exactly the current Not pushing to your branch and not opening a competing PR: take the block or drop it as you prefer. |
…ed-string-padding
`ORDER BY f` ties `-0.0` with `0.0`, so their relative order varied between runs and the test failed in `Stateless tests (amd_debug, parallel)` and the flaky check. Order by `toString(f)` instead, which yields the reference order. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117293&sha=b5a565ca735c0ba7a006740105dfdf4a663e311e&name_0=PR Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… or `LowCardinality` floats `has` compares only `Float32` and `Float64` numerically. An `Array(BFloat16)` goes through the generic column comparison and an `Array(LowCardinality(Float64))` through dictionary lookup; both match a stored NaN, while the `equals` in the lambda does not. Keep the plain-float exemption only for `Float32` / `Float64` elements that are not dictionary-encoded. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: |
|
Already fixed by my #126345 (merged 2026-10-19 10:34 UTC), so I am not opening a new PR. This CI run was created 2026-10-18 07:17 UTC, so the merge commit it tested predates the fix. Its stdout is exactly what that PR handles: Since the merge the test ran 861 times on master; its 3 failures there are on commits from before the merge, and every PR failure since comes from a workflow created before it. The CI for the new head |
…members into set lookups `equals` and `notEquals` on tuples propagate the NULL of a member, while set membership and `has` treat it as an ordinary value. Decline the equality chain to `IN`, the inequality chain to `NOT IN`, and the `arrayExists` to `has` rewrites when either side is a tuple with a `Nullable` member. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ullable` members The previous commit stops rewriting an equality chain over a tuple with a `Nullable` member into a set lookup, because tuple `equals` propagates the member's NULL while membership treats it as a value. The test pinned the old rewrite of `tu = (1, 1) OR ...` for `tu Tuple(Nullable(Int32), Int32)`; it now expects the chain to stay as it is. The result lines are unchanged. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117293&sha=217cb5aad77fdae4c27ae5939451ede3ebde8cc1&name_0=PR&name_1=Fast%20test #117293 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…NULL `tupleHasNullableMember` checked only for a `Nullable` wrapper, so a tuple member of type `Variant` (or `Dynamic`), which carries NULL without one, passed the guard. `equals` on `Tuple(Variant(UInt64))` propagates the member's NULL while set membership and `has` treat it as a value, so the equality chain to `IN`, the inequality chain to `NOT IN`, and the `arrayExists` to `has` rewrites still changed results. Rename the helper to `tupleMemberCanContainNull` and test each member with `canContainNull`. Test `05453_chain_to_in_tuple_variant_member` covers the reviewer's reproducers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ng_distributed` reachable The test added on master asserts that `x = ... OR x = ... OR x = ...` over `Tuple(DateTime, Variant(UInt64))` is rewritten into `IN`. This PR makes that rewrite decline for tuples with a member that can contain NULL, because tuple `equals` propagates the member's NULL while set membership does not. Move the `Variant` into an `Array` member, which compares its elements as values, so the rewrite still fires and the test still checks that the named member type and the raw `DateTime` timestamp survive serialization to a secondary server. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117293&sha=1e6af916dcf7f00101051cd32905631e56664c9c&name_0=PR&name_1=Fast%20test PR: #117293 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged master and fixed the Fast test failure in |
|
🕵 Merged master ( |
|
🕵 The only failure on |
|
🕵 Merged |
…ed-string-padding Conflict in `05059_nested_variant_constant_folding_distributed.sql`: master (#113470) made the `OR`-to-`IN` rewrite decline any `Variant` in the expression type and moved the `Variant` into the constants only, while this PR made the rewrite decline a tuple constant with a `Variant` member and moved the `Variant` into an `Array`. Together no `Tuple` chain with a `Variant` reaches the rewrite (`Array(UInt64)` vs `Array(Variant(UInt64))` does not type-check either), so the case is split in two: a `Tuple(DateTime, UInt64)` chain pins the raw `DateTime` timestamp in the `IN` constant, and a `UInt64` chain with `Variant` constants pins the named member cast. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master (#113470) now declines the equality-chain-to-`IN` rewrite for any expression with a `Variant` in its type, including a single-alternative `Variant(UInt64)`, so the "still rewritten" check no longer holds. Keep it as a positive control on a plain `UInt64` column, which shows the float guard does not over-decline. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117293&sha=092c493e7704049920edf85219ee565d1752ade2&name_0=PR&name_1=Fast%20test PR: #117293 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Fixed the |
…ed-string-padding Conflict in `LogicalExpressionOptimizerPass.cpp`: keep both the `stringFamilyPairIsNotEqualityEquivalent` / `tupleMemberCanContainNull` guards of this branch and the `loses_type_in_field` guard for `Variant` and `Dynamic` constants added on master. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
🕵 Merged |
Closes: #116852
Related: #115311
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix silently wrong results when a chain of comparisons against
FixedStringconstants over aStringcolumn is rewritten into
IN/NOT IN.x != c1 AND x != c2 AND x != c3evaluated to true whilex != c1alone evaluated to false in the same query, and theOR/INdirection lost rows the sameway. Also do not rewrite such a chain over a floating-point expression, where the rewrite dropped
-0.0rows (-0.0 = 0.0is true while-0.0 IN (0.0)is false) and mishandledNaN.Description
equalscompares the string family zero-padded, so'a' = toFixedString('a', 2)is true. Setmembership is bytewise, so
'a' IN (toFixedString('a', 2))is false. The chain rewrites inLogicalExpressionOptimizerPass(optimize_min_equality_disjunction_chain_length/optimize_min_inequality_conjunction_chain_length) swapped one for the other, and the query becameinternally inconsistent: a conjunction was true while one of its terms was false, a disjunction false
while one of its terms was true. In
WHEREposition theNOT INspelling kept a row that everyindividual conjunct excludes.
The existing lossless-conversion guard in both loops does not cover this. Converting a
FixedString(2)'a\0'toStringpreserves the bytes exactly; what differs is the comparisonsemantics, not the value.
ArrayExistsToHasPassalready declines such a pair for precisely this reason(
stringFamilyPairIsNotEqualityEquivalent, added for #115311). That helper moves toAnalyzer/Utilsand both passes share it, so the two rewrites cannot drift apart.The same rewrites were also unsound for floating-point expressions:
equalscompares floatsnumerically while the set is keyed on the raw bits, so
-0.0 = 0.0is true but-0.0 IN (0.0)isfalse, and
nan = nanis false butnan IN (nan)is true.f = 0.0 OR f = 1.0 OR f = 2.0lost the-0.0rows once rewritten, and theNOT INdirection kept them. Both chain rewrites now declinewhen the expression's type contains a float (
Float32,Float64,BFloat16, also insideNullable,LowCardinality,Array,Tuple,MapandVariant), asHasToInPassalready does.Test
05227_chain_to_in_fixed_string_paddingcovers both directions, theWHEREposition, and that achain of same-type constants is still rewritten to
IN. Test05228_chain_to_in_float_negative_zero_nancovers
-0.0andNaNfor both directions and the float carriers above, and that an integer columncompared with float literals is still rewritten.
Test
05243_chain_to_in_float_variantcovers aVariantwith a floating-point alternative.The range/
notEqualspruning in the same pass (addComparisonFilter,optimize_redundant_comparisons) compared constants of composite float carriers (Tuple(Float64, ...),Array(Float64),Map,Variant) asFieldcontainers, which order a nested NaN after all values:tup = (1, 1) AND tup < (1, nan)was pruned totup = (1, 1). Such filters are now kept out of the analysis; test05397_chain_comparison_composite_float_nancovers it.ArrayExistsToHasPasslikewise no longer rewritesarrayExists(x -> x = c, arr)tohaswhenhasdoes not compare the element numerically: a composite carrier of floats,BFloat16, orArray(LowCardinality(Float64)), wherehasmatches a stored NaN whileequalsdoes not. Tests05407_array_exists_to_has_composite_float_nanand05408_array_exists_to_has_bfloat16_low_cardinality_nancover it.Workflow [PR]
Sync PR [sync-upstream/pr/117293]