Repository navigation
Remove false isInjective claims from MACNumToString, unhex/unbin and UUIDStringToNum - #117210
Conversation
…in` and `UUIDStringToNum`
All three declared themselves injective while mapping distinct arguments onto one result:
- `MACNumToString` formats only the low 6 bytes of its `UInt64` argument, so 2^64 inputs
land on 2^48 outputs.
- `unhex`/`unbin` decode case-insensitively and pad an incomplete leading group, so
`unhex('0a')`, `unhex('0A')` and `unhex('a')` all give the same byte.
- `UUIDStringToNum` parses case-insensitively.
The default-enabled `optimize_injective_functions_in_group_by` and
`optimize_injective_functions_inside_uniq` then group and count by the raw argument instead
of the function result, so `GROUP BY` returned one row per colliding input and `uniqExact`
counted them separately. The same claim also feeds partition-wise independent aggregation
and `optimize_distributed_group_by_sharding_key`.
Closes: #117183
Closes: #117184
Closes: #117185
|
Workflow [PR], commit [46555f6] Summary: ✅
AI ReviewSummaryThis PR removes false Findings
Tests
Final Verdict
LLVM Coverage ReportMeasured on commit 46555f6.
Changed lines: Changed C/C++ lines covered: 8/12 (66.67%) · Uncovered code |
| SELECT uniqExact(UUIDStringToNum(s)) FROM (SELECT arrayJoin(['61f0c404-5cb3-11e7-907b-a6006ad3dba0', '61F0C404-5CB3-11E7-907B-A6006AD3DBA0']) AS s); | ||
| SELECT uniqExact(UUIDStringToNum(s)) FROM (SELECT arrayJoin(['61f0c404-5cb3-11e7-907b-a6006ad3dba0', '61F0C404-5CB3-11E7-907B-A6006AD3DBA0']) AS s) SETTINGS optimize_injective_functions_inside_uniq = 0; | ||
|
|
||
| -- The same false claim also fed partition-wise independent aggregation. |
There was a problem hiding this comment.
The changed isInjective contract is also consumed by tryConvertAnyOuterJoinToInnerJoin in src/Processors/QueryPlan/Optimizations/convertOuterJoinToInnerJoin.cpp:105-123, which peels wrappers from the join key before deciding it is safe to rewrite ANY OUTER into ALL INNER. A false positive there is more serious than the GROUP BY / uniq* cases: joining on MACNumToString(x) with colliding values such as 1 and 1 + 281474976710656 can duplicate rows after the rewrite instead of merely mis-grouping them.
This regression file proves GROUP BY, uniq*, and partition-wise aggregation, but it still leaves that join path untested. Please add a focused LEFT / RIGHT ANY ... JOIN case so the PR demonstrates that these functions no longer let that optimizer fire.
There was a problem hiding this comment.
🕵 Added. The regression now also covers tryConvertAnyOuterJoinToInnerJoin: 05059_injective_decoding_functions.sql joins a one-row side against an aggregated side on MACNumToString(agg.x) under a filter that removes not-matched rows, in both the ANY LEFT and the ANY RIGHT orientation, and asserts one result row.
I verified the shapes are real regressions rather than merely correct queries: with the MACNumToString isInjective claim restored in a local build, both shapes return two rows (the pass peels the function off the join key, sees the aggregation key covered, and rewrites ANY OUTER into ALL INNER, so the two colliding aggregation groups both match); with the claim removed the pass declines and both return one row.
The test file also moved from 05054 to 05059, because master has taken the earlier numbers.
There was a problem hiding this comment.
The added join cases in tests/queries/0_stateless/05059_injective_decoding_functions.sql:53-74 still do not guarantee that tryConvertAnyOuterJoinToInnerJoin is exercised. tests/clickhouse-test randomizes query_plan_convert_outer_join_to_inner_join, and this file never pins it, so the queries can still pass with the optimizer disabled and miss the regression entirely.
Please set query_plan_convert_outer_join_to_inner_join = 1 for this section, like the existing join-conversion tests that pin it because CI may inject false.
|
The same false-claim pattern (a fallback value shared by many inputs breaking injectivity) is still live in a few more |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units9 translation units recompiled, 16 s compile time in total, 9 of them have a recent master baseline. |
…y claim `tryConvertAnyOuterJoinToInnerJoin` peels an injective function off the join key of the aggregated side to conclude that every row of the other side has at most one match, and then rewrites `ANY OUTER` into `ALL INNER`. A false `isInjective` answer collapses two aggregation groups onto one join key there, so the rewritten join duplicates rows instead of keeping the single row `ANY` returns. Verified against a binary with the `MACNumToString` claim restored: both the `ANY LEFT` and the `ANY RIGHT` shape return two rows before the fix and one row after it. The test also moves from `05054` to `05059`, because `master` has taken the earlier numbers.
|
🕵 Merged
The test file also moved from |
|
🕵 Thanks — agreed that they are the same pattern, but I would rather keep this pull request to the three functions its issues cover, so that each false claim is removed with its own regression and can be reverted independently if one of them turns out to be load-bearing somewhere. |
| -- `MACNumToString` ignores the two high bytes of its argument, `unhex`/`unbin` decode | ||
| -- case-insensitively and pad an incomplete leading group, and `UUIDStringToNum` parses | ||
| -- case-insensitively. None of them is injective, so the passes that eliminate an injective | ||
| -- function under `GROUP BY` or inside `uniq` must not fire for them. Every pair below prints the |
There was a problem hiding this comment.
The changed isInjective contract is also consumed by the default-on optimize_injective_functions_in_limit_by pass (src/Analyzer/Passes/OptimizeLimitByInjectiveFunctionsPass.cpp:26-41). A false positive there returns too many rows, not just a different plan: LIMIT 1 BY unhex(s) over ['0a', '0A'] should keep one row, but with the old unhex claim the pass rewrites it to LIMIT 1 BY s and keeps both spellings.
This file proves GROUP BY, uniq*, partition-wise aggregation, and the ANY OUTER join rewrite, but it still leaves that default-on wrong-result consumer untested. Please add a focused LIMIT BY regression (for unhex, unbin, MACNumToString, or UUIDStringToNum) with the optimized and optimize_injective_functions_in_limit_by = 0 results compared.
alexey-milovidov
left a comment
There was a problem hiding this comment.
Ok, though we compromise performance in favor of correctness on out-of-range values.
Closes: #117183
Closes: #117184
Closes: #117185
Related: #117137
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix wrong results for
GROUP BYanduniq*overMACNumToString,unhex,unbinandUUIDStringToNum. All four declared themselves injective, so the default-enabledoptimize_injective_functions_in_group_byandoptimize_injective_functions_inside_uniqgrouped and counted by the raw argument: arguments that map onto the same result came back as
separate groups and were counted separately by
uniqExact.Description
All three functions map distinct arguments onto one result:
MACNumToStringformats only the low 6 bytes of itsUInt64argument — the source evendocuments it, "Higher two bytes in UInt64 are just ignored" — so 2^64 inputs land on 2^48
outputs and
MACNumToString(1) = MACNumToString(1 + 0x1000000000000).unhex/unbindecoding is deliberately tolerant: case-insensitive, and an incomplete leadinggroup is padded.
unhex('0a'),unhex('0A')andunhex('a')all decode to the same byte, andunbin('1010') = unbin('01010').UUIDStringToNumparses case-insensitively, so the upper- and lower-case spellings of one UUIDgive the same
FixedString(16).isInjectiveis consumed as a proof by a growing number of default-on rewrites, so a false claimsilently changes results. Answering
falsecan only make those rewrites decline, never fire.Test
05059_injective_decoding_functionscovers both passes for every function, plus thepartition-wise independent aggregation consumer of the same claim and the
ANY OUTER JOINtoALL INNER JOINrewrite intryConvertAnyOuterJoinToInnerJoin, which peels an injective functionoff the join key of the aggregated side. With the claim in place that rewrite duplicates rows: both
the
ANY LEFTand theANY RIGHTshape in the test return two rows before the fix and one after.Workflow [PR]
Sync PR [sync-upstream/pr/117210]