Repository navigation
Fix inconsistent casting of Bool type in IN operator - #93115
Conversation
The Bool type in ClickHouse is implemented as UInt8 with a custom name. When converting values for the IN operator, the equals() check between UInt8 and Bool types returns true (same underlying typeid), causing values like 10 or 255 to be stored as-is instead of being converted to 0/1 (false/true). This fix adds a special case in both convertFieldToType() and convertFieldToTypeImpl() to detect when the target type is Bool but the source type is not Bool (e.g., plain UInt8), and proceed with proper Bool conversion instead of short-circuiting. Before fix: SELECT CAST(1, 'Bool') IN (10); -- returned 0 (incorrect) SELECT CAST(1, 'Bool') IN (255); -- returned 0 (incorrect) SELECT CAST(1, 'Bool') IN (256); -- returned 1 (correct) SELECT CAST(1, 'Bool') IN (10000);-- returned 1 (correct) After fix: All non-zero values correctly convert to Bool true (1) Zero correctly converts to Bool false (0)
nihalzp
left a comment
There was a problem hiding this comment.
Thanks for working on the issue! Please add a test.
Test covers: - Basic tests from the issue: values 10, 255, 256, 10000 with Bool true - Zero value correctly matches Bool false - Bool false with various numeric literals - Multiple values in IN clause - Negative numbers (convert to true) - Materialized values to test non-constant path
|
Added in a test @nihalzp! Would you mind adding the can be tested flag? |
|
Workflow [PR], commit [58aaa65] Summary: ✅ AI ReviewSummaryThis PR introduces Findings❌ Blockers
ClickHouse Rules
Final Verdict
|
nihalzp
left a comment
There was a problem hiding this comment.
The changes fixed the bugs from the issue! I noticed there are some other cases of Bool conversion that should work. For example,
SELECT CAST(1, 'Nullable(Bool)') IN (toNullable(10));
0
SELECT CAST(1, 'Nullable(Bool)') IN (toNullable(255));
0
SELECT CAST(1, 'Nullable(Bool)') IN (toNullable(256));
1
SELECT CAST(1, 'Nullable(Bool)') IN (toNullable(1000));
1I think it might be best if we fix them with this PR.
Additionally, the PR has additionally fixed the following kind of queries:
SELECT toLowCardinality(CAST(1, 'Bool')) IN (10);
SELECT toLowCardinality(CAST(1, 'Bool')) IN (255);Let's add these to tests as well.
…test suite in 03778
|
@ashrithb Thanks for working on the fix! I found some issue with semantics and pushed some changes. Once the CI is good, we should be good to go! |
|
Thank you @nihalzp! sorry about that, the changes must have been a bit stale. |
LLVM Coverage Report
Changed lines: 95.52% (64/67) · Uncovered code |
|
CI is good! |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
The
INoperator now uses exact value semantics forBooltype: only0and1values in the set matchBoolvalues. Previously, numeric values greater than255in theINset were incorrectly clamped to true when compared againstBool, soSELECT CAST(1, 'Bool') IN (256)returned 1. Now it correctly returns0. Closes #92980.Version info
26.4.1.415