Skip to content

Fix inconsistent casting of Bool type in IN operator - #93115

Merged
nihalzp merged 11 commits into
ClickHouse:masterfrom
ashrithb:fix/bool-in-casting-92980
Mar 30, 2026
Merged

nihalzp merged 11 commits into
ClickHouse:masterfrom
ashrithb:fix/bool-in-casting-92980

Conversation

@ashrithb

@ashrithb ashrithb commented Dec 29, 2025 •

Copy link
Copy Markdown
Contributor
SELECT CAST(1, 'Bool') IN (10);
0
SELECT CAST(1, 'Bool') IN (255);
0
SELECT CAST(1, 'Bool') IN (256);
1 (should be 0)
SELECT CAST(1, 'Bool') IN (10000);
1 (should be 0)

Changelog category (leave one):

  • Backward Incompatible Change

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

The IN operator now uses exact value semantics for Bool type: only 0 and 1 values in the set match Bool values. Previously, numeric values greater than 255 in the IN set were incorrectly clamped to true when compared against Bool, so SELECT CAST(1, 'Bool') IN (256) returned 1. Now it correctly returns 0. Closes #92980.

Version info

  • Merged into: 26.4.1.415

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 nihalzp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on the issue! Please add a test.

@nihalzp nihalzp self-assigned this Dec 29, 2025
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
@ashrithb
ashrithb marked this pull request as ready for review December 29, 2025 21:44
@ashrithb

ashrithb commented Dec 29, 2025 •

Copy link
Copy Markdown
Contributor Author

Added in a test @nihalzp! Would you mind adding the can be tested flag?

@nihalzp nihalzp added the can be tested Allows running workflows for external contributors label Dec 30, 2025
@clickhouse-gh

clickhouse-gh Bot commented Dec 30, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [58aaa65]

Summary: ✅


AI Review

Summary

This PR introduces strict_bool handling in convertFieldToType and adds broad stateless coverage for Bool IN semantics. However, there is still a correctness hole in the top-level fast path: convertFieldToType can return early on from_type_hint->equals(to_type) before applying strict Bool representability checks, which can admit non-representable RHS values in strict set conversion paths.

Findings

❌ Blockers

  • [src/Interpreters/convertFieldToType.cpp:792] The early return if (from_type_hint && from_type_hint->equals(to_type)) return from_value; bypasses strict_bool logic. For strict set conversion (convertFieldToTypeStrict), from_type=UInt8, to_type=Bool can satisfy equals, returning 10 unchanged and skipping Bool representability filtering.
    • Suggested fix: guard this fast path the same way as the Bool special-case below (do not short-circuit when target is Bool and source type is not custom Bool).
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ⚠️ Strict Bool check can still be bypassed for some typed RHS paths due to early return.
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ⚠️ Incomplete strict path may yield inconsistent Bool IN behavior across RHS types.
Compilation time ✅
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Fix the top-level convertFieldToType equality fast path so strict_bool cannot be bypassed for UInt8 -> Bool and similar custom-name-equal cases.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Dec 30, 2025

@nihalzp nihalzp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));
1

I 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.

@nihalzp

nihalzp commented Mar 29, 2026

Copy link
Copy Markdown
Member

@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!

@ashrithb

Copy link
Copy Markdown
Contributor Author

Thank you @nihalzp! sorry about that, the changes must have been a bit stale.

@clickhouse-gh

clickhouse-gh Bot commented Mar 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.70% 76.70% +0.00%

Changed lines: 95.52% (64/67) · Uncovered code

Full report · Diff report

@alexey-milovidov

Copy link
Copy Markdown
Member

CI is good!

@nihalzp
nihalzp added this pull request to the merge queue Mar 30, 2026
@nihalzp nihalzp added pr-backward-incompatible Pull request with backwards incompatible changes and removed pr-bugfix Pull request with bugfix, not backported by default labels Mar 30, 2026
@nihalzp nihalzp changed the title Fix inconsistent casting of Bool type in IN operator (#92980) Fix inconsistent casting of Bool type in IN operator Mar 30, 2026
@nihalzp
nihalzp removed this pull request from the merge queue due to a manual request Mar 30, 2026
@nihalzp
nihalzp added this pull request to the merge queue Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-backward-incompatible Pull request with backwards incompatible changes pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistency in casting of Bool type

4 participants