Skip to content

Do not rewrite an OR chain into IN over Variant constants with several alternatives - #133127

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
lazerg:fix/issue-132702-or-in-variant-constants
Nov 23, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
lazerg:fix/issue-132702-or-in-variant-constants

Conversation

@lazerg

@lazerg lazerg commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #132702

Description

tryReplaceOrEqualsChainWithIn already skipped the rewrite when the expression is a Variant or Dynamic, but not when only the constants are. It builds the IN tuple from each constant's Field, which does not keep the Variant alternative or the Dynamic type, so the set puts the value into whatever type accepts it first. 5::UInt64::Variant(Date, UInt64) became 1970-01-06 as a Date, while equals compares the UInt64. On a DateTime column the matching row was dropped, and on a Date column a row came back instead of ILLEGAL_TYPE_OF_ARGUMENT. toDate(5)::Dynamic had the same problem the other way round.

Now the chain is kept when a constant's type contains Dynamic or a Variant with more than one alternative. A single-alternative Variant can only go to one place, so it is still rewritten (05059_nested_variant_constant_folding_distributed checks that). The notIn direction is not affected: a notEquals with such a constant is Nullable and is never merged.

The new test fails on master.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed wrong results of an OR chain of equalities with Variant or Dynamic constants (for example x = 5::UInt64::Variant(Date, UInt64) OR ...), which was rewritten into an IN that matched different rows.


Workflow [PR]
Sync PR [sync-upstream/pr/133127]

Version info

  • Merged into: 26.12.1.240-master (included in 26.12 and later)

@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Nov 20, 2026
@tuanpach tuanpach added the can be tested Allows running workflows for external contributors label Nov 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1800fa4]

Summary: ✅


AI Review

Summary

The PR preserves equality semantics by keeping OR chains whose constants contain Dynamic or a multi-alternative Variant, while retaining safe single-alternative rewrites. No new actionable findings at head 1800fa475f735addd9b5e2cad6ba097e17176890. The added regression matches its reference on the PR binary; focused nested-constant and single-alternative temporal checks also pass. The separate, pre-existing Array(Dynamic) constant issue in the notIn rewrite is already covered by PR #117637, linked from the original issue.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 20, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 1800fa475 with master 463f45f5f (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline.

Job report

@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Nov 21, 2026
@alexey-milovidov alexey-milovidov self-assigned this Nov 23, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 23, 2026
Merged via the queue into ClickHouse:master with commit 03c499c Nov 23, 2026
203 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 23, 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 comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default 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.

OR-to-IN rewrite rebuilds Variant constants from Fields, dropping or adding rows

4 participants