Repository navigation
Add tests for in-order optimizations and primary key analysis over a CAST into Bool - #129683
Conversation
`Bool` is a `UInt8` with a custom name, so `createFunctionBaseCast` gave a CAST into `Bool` the monotonicity of the cast into `UInt8`. For a `UInt8` source that is the identity (strictly monotonic), and for a `DateTime64` source it is "monotonic where defined", which relies on the `UInt8` conversion throwing outside of its range. The cast into `Bool` is `x != 0`: it maps 1 and 2 to the same value, and it never throws, so it is not monotonic across the epoch (-1 -> true, 0 -> false, 1 -> true). The false `is_strict` let read in order, aggregation / DISTINCT / LIMIT BY in order, sorting by input stream properties and fixed key columns from a `CAST(u, 'Bool') = c` filter trust an order by the following keys that does not hold, so `ORDER BY CAST(u AS Bool), k LIMIT 1` over a `(u, k)` key returned a row with a wrong `k` (and DISTINCT / LIMIT BY hit the "Equal values are not contiguous" assertion in debug builds). The false `is_always_monotonic_where_defined` let the primary key analysis push `ts <= c` through a `CAST(ts AS Bool)` sorting key and skip rows before 1970. Clear both flags for a `Bool` target. Every remaining claim of the `UInt8` metadata holds for `x != 0`, so the cast is still a non-strict sorting prefix and is still used for primary key pruning. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261106-054900 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261106-045100 |
|
cc @nihalzp @antaljanosbenjamin, could you review this and add the |
|
Workflow [PR], commit [535cc79] Summary: ✅
AI ReviewSummaryReviewed PR head 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 units6 translation units recompiled, 5 s compile time in total, 6 of them have a recent master baseline. |
Manerone
left a comment
There was a problem hiding this comment.
The fix is correct, but master already has it: #129731 (merged) added the same branch to createFunctionBaseCast, extended to SimpleAggregateFunction(f, Bool) (src/Functions/FunctionsConversion.cpp:3549 on master). That is why this PR now conflicts.
The test is still worth landing. Master's test only covers ORDER BY ... LIMIT and DISTINCT over CAST(u, 'Bool'). This one adds GROUP BY and LIMIT BY in order, sorting by input stream properties, the fixed key from WHERE, the ALIAS column, the EXPLAIN prefix check, and the DateTime64 pre-epoch pruning arm, which is the only coverage of is_always_monotonic_where_defined for Bool.
To land it as a test-only PR:
- Merge master and take master's side of
createFunctionBaseCast. TheDateTime64sentence of your comment ("unlike the cast intoUInt8it does not throw for aDateTime64outside of theUInt8range") would be a good addition to master's comment, which only explains the non-injectivity, not whyis_always_monotonic_where_definedis cleared. - Renumber the test:
05556is now taken by05556_cast_to_simple_aggregate_function_bool. - Change the category to
Not for changelogand drop the entry. #129731's entry already covers the wrong rows from in-order optimizations over aCASTintoBool, and with a test-only diff the bug-fix validation would run the new test on a master build that already has the fix and go red. - Retitle accordingly, e.g. "Add tests for in-order optimizations and primary key analysis over a
CASTintoBool".
Happy to approve after that.
…notonicity # Conflicts: # src/Functions/FunctionsConversion.cpp
Co-Authored-By: Claude Opus 5.5 <[email protected]>
BoolCAST into Bool
|
Thanks, done:
Checked on the new head (debug build): with the |
Related: #129731
Related: #129642
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
Test-only follow-up to #129731, which fixed the monotonicity of a
CASTintoBool(Boolis aUInt8with a custom name, so the cast reused the strictly monotonicUInt8metadata, although it isx != 0).The existing test covers
ORDER BY ... LIMITandDISTINCToverCAST(u, 'Bool'). This one addsGROUP BYandLIMIT BYin order, sorting by input stream properties, the fixed key fromWHERE CAST(u, 'Bool') = true, aBoolALIAScolumn, anEXPLAINcheck that only the cast is used as a sort prefix, and aDateTime64sorting keyCAST(ts AS Bool)with a row before 1970, which is the only test of the clearedis_always_monotonic_where_defined(the primary key analysis skipped that row). The comment increateFunctionBaseCastnow also says why that flag is cleared.Validation (debug build): with the
Boolbranch ofcreateFunctionBaseCastremoved, the server aborts withEqual values are not contiguous within the range assumed to be sortedand theDateTime64query loses the 1969 row; with this branch the test passes 50/50 with randomized settings.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/129683]
Version info
26.11.1.2734-master(included in26.11and later)