Repository navigation
Compare a Bool JOIN key with an integer key by value - #129894
alexey-milovidov merged 2 commits into
Conversation
IDataType::equals treats Bool and UInt8 as the same type, so the least
supertype of a Nullable(Bool) key and a UInt8 key is whichever comes
first. With the Bool key on the left the keys were compared in
Nullable(Bool): the UInt8 key was CAST to Bool, which maps every nonzero
value to true, so 2 and 255 matched true. The runtime filter type
({build, probe} order) and the subtype fallback had the same order
dependence, and a USING key reused its output cast into Bool as the
comparison key because that check used equals.
JoinCommon::compareBoolKeysAsUInt8 replaces a Bool of the common key type
by UInt8 where the other key has an integer in that place. It is applied
to the comparison type in predicateOperandsToCommonType (supertype and
subtype fallback) and to the runtime filter type, and the USING output
cast is reused only when its type name matches.
The type of the merged USING column is deliberately unchanged: it still
follows getLeastSupertype. Retyping it to UInt8 makes one column carry
both Bool and UInt8 in a query, because the analyzer compares column
types with equals.
The type walk pairs the common type with each key the way the common
type was built: a named and an unnamed Tuple by position, through a
custom name such as Nested on the key, and JSON typed paths by path.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (3 rounds, final HEAD 9ac2e99): 0 open findings
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261107-054600 |
|
cc @vdimir @antaljanosbenjamin, could you review this and add the |
|
Workflow [PR], commit [b548d8f] Summary: ✅
AI ReviewSummaryThe PR makes Final Verdict✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes9 object files changed (+32.71 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units175 translation units recompiled, 1702 s compile time in total, 175 of them have a recent master baseline. |
getLeastSupertype skips Nothing, so the common type of the keys Tuple(Array(Array(UInt8)), Nullable(Bool)) and Tuple(Array(Nothing), UInt8) is Tuple(Array(Array(UInt8)), Nullable(Bool)): it has structure where the second key has Nothing. The type walk in compareBoolKeysAsUInt8 refused the pair at Array(UInt8) vs Nothing, so the Bool element was never replaced and the UInt8 key was still CAST to Bool (2 matched true) when the Bool table came first, in the JOIN comparison and in the runtime filter type. With left_is_common_type, a Nothing on the key side now leaves that part of the common type as is, and the walk continues with its siblings. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…IN keys and JSONExtract like Bool Review feedback on ClickHouse#129731: - The monotonicity override is checked after `removeNullable`, so for `SimpleAggregateFunction(any, Nullable(Bool))` (custom name on the Nullable wrapper) it saw plain `Bool`, which, like `Bool` and `Nullable(Bool)` themselves, fell into the `UInt8` branch that reports the cast as strict. Match plain `Bool` as well, so `optimize_read_in_order` / `optimize_distinct_in_order` over such a CAST no longer return wrong rows (the DISTINCT case aborted a debug build). - ClickHouse#129894 (merged into master) compares a `Bool` JOIN key with an integer key in `UInt8`, but checks for `Bool` by name. With the CAST into `SimpleAggregateFunction(any, Bool)` now mapping 2 to 1, a `Nullable(SimpleAggregateFunction(any, Bool))` key joined with an `UInt8` key 2 matched `true` in one table order only. `compareBoolKeysAsUInt8` now treats the SAF type as `Bool`. - `JSONExtract(..., 'SimpleAggregateFunction(any, Bool)')` kept 2 instead of 1, and so did `LowCardinality(Bool)`, whose branch checked the `LowCardinality` type instead of its dictionary type. The test gains `Bool` and `SimpleAggregateFunction(any, Nullable(Bool))` in the in-order section, and queries that show subquery `IN`, JOIN keys and `JSONExtract` give the same results for the SAF type and `Bool`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`Bool` equals `UInt8` (`DataTypeNumber::equals` compares `typeid`), so the common-type loop of `tryAddJoinRuntimeFilter` took the equal-types branch and kept the build key type, `Bool`, for the runtime filter. The JOIN compares such keys by value (no cast for equal key types), but the `Set` behind `ExactSetRuntimeFilter` converts a probe into a `Bool` key with a real cast (`Set::castToKeyType`, the `IN` semantics), so the integer 2 became `true`. For `LEFT ANTI JOIN` the filter is `NOT IN`, and every probe row whose key is neither 0 nor 1 was dropped before the JOIN saw it: `t_int (k UInt8) LEFT ANTI JOIN t_bool (k Bool)` returned 0 rows instead of 2. With default settings the filter is built unless the probe side is estimated at no more than `join_runtime_filter_min_probe_rows` (1000) rows. Pass the equal-types branch through `JoinCommon::compareBoolKeysAsUInt8` too, as the unequal-types branch already does since ClickHouse#129894. The filter key type is then `UInt8` (also inside `Nullable` and `LowCardinality`), and the probe bytes are compared as the JOIN compares them. All other equal pairs keep their type. Closes ClickHouse#129970 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #129731
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
JOINof aBoolkey with an integer key matchingtruewith integers other than 1 (such as 2) depending on the order of the tables. The keys are now compared by value, as=does.Description
On 26.10.3.7 (and 26.8, 26.3),
SELECT count() FROM (SELECT CAST(true, 'Nullable(Bool)') AS v) a JOIN (SELECT toUInt8(2) AS v) b ON a.v = b.vreturns 1, whileCAST(true, 'Nullable(Bool)') = toUInt8(2)is 0 and the same join with the tables swapped returns 0. Found while reviewing #129731.Root cause:
IDataType::equalstreatsBoolandUInt8as the same type, sogetLeastSupertypereturns whichever key type comes first. With theBoolkey on the left the keys are compared inNullable(Bool), and the CAST of theUInt8key intoBoolmaps every nonzero value totrue. The runtime filter type ({build, probe}order) and the subtype fallback have the same order dependence, and aUSINGkey reused its output cast intoBoolas the comparison key.Change:
JoinCommon::compareBoolKeysAsUInt8replaces aBoolof the common key type byUInt8where the other key has an integer in that place (throughNullable,LowCardinality,Array, named or unnamedTuple,NestedandJSONtyped paths, also when the other key has an empty array in a sibling element). It is applied to the comparison type inpredicateOperandsToCommonType(supertype and subtype fallback) and to the runtime filter type, and theUSINGoutput cast is reused only when its type name matches. Output column types are unchanged, including the mergedUSINGcolumn, which still followsgetLeastSupertype(Boolwhen theBooltable is on the left).getLeastSupertypeitself is unchanged, becausearray,if,coalesceandUNIONalso take their result type from it. Two similarBool/UInt8defects outsideJOIN(number IN (SELECT true),=betweenArray(Nullable(Bool))andArray(UInt8)) are in different code and not changed here.Validation: the new test fails on master and passes with the fix (local and server, 50/50 randomized runs each way). 1705 join/using/asof/bool/runtime-filter/nested/json tests show no new failures against master.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/129894]
Version info
26.11.1.1606-master(included in26.11and later)