Repository navigation
Fix unsafe transitivity in optimize_and_compare_chain - #110621
Merged
Merged
Conversation
Restrict inferred comparison chains to native-number and `String` domains so pair-specific conversions cannot produce invalid predicates.
Contributor
|
Workflow [PR], commit [4bc3860] Summary: ✅
AI ReviewSummaryThis PR narrows PR Metadata
Final Verdict
|
Teach `FunctionComparison` to expose a closed comparison-order domain and make `LogicalExpressionOptimizerPass` carry it through inferred chains. Comparisons with pair-dependent conversions now fail closed, while native numbers, strings, dates, and equal-scale time points retain the optimization. Related: #110621
…mpare-chain-domains # Conflicts: # src/Analyzer/Passes/LogicalExpressionOptimizerPass.cpp
1 task done
Contributor
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 181/191 (94.76%) · Uncovered code |
evillique
pushed a commit
to evillique/ClickHouse
that referenced
this pull request
Sep 24, 2026
…-shifted bound Issue ClickHouse#121954 reports a valid query rejected at default settings with "Code: 43. No operation lessOrEquals between String and DateTime64(3)". The filter equates a String column with a date-like literal and also bounds a Date column by that same literal and by an interval-shifted one, so the AND chain derived a comparison between the String column and the DateTime64 upper bound, which no ordering supports. The defect is already fixed. Measured on the official builds through one byte-identical clickhouse local harness: Code 43 on 26.3.33.24 and 26.6.5.73, and 33 on 26.7.6.57, 26.8.2.7 and master 26.10.1.1. The change that closed it is d4c0509 in ClickHouse#99736 (merged 2026-07-20, first shipped in 26.7), which stopped the chain walk recursing through a constant intermediate node; the '2026-09-02' literal shared by the two conjuncts is exactly such a node. Master additionally carries ClickHouse#110621's ordering domains, which is why no single-line mutation on master can red this test and why the pre-fix red is taken from released code instead. Arm 1 is the reporter's query verbatim and the only arm that reds pre-fix. Arm 2 repeats it with optimize_and_compare_chain = 0, the reporter's own control. Arm 3 pins bound preservation: its rows sit one day past the interval-shifted bound and must be excluded, which catches a regression that drops the upper-bound conjunct. Both controls also pass on 26.6.5.73. The explicit SET optimize_and_compare_chain = 1 is load-bearing: clickhouse-test's SettingsRandomizer draws that setting off with probability 0.05 per run, and a run that drew it off would exercise nothing while still printing the expected counts. Of the branches still supported, only the 26.3 LTS carries the defect: Code 43 on 26.3.33.24 as well. Release coverage there would be a backport of ClickHouse#99736 rather than this test, so it is not carried here. A further arm pins the pass being live on the reporter's own chain rather than the fix. Results alone stay equal if the pass stops deriving anything, and an edge whose comparison domain is declined is dropped from the chain graph, so no walk happens and the counts do not move. The arm adds the same-domain edge c2 >= c3 to the reporter's chain, which gives it one legitimate transitive conjunct while the String column stays in its own domain, and compares the enabled and disabled trees by exact node count. It is a vacuity guard, not a second reproducer. Related: ClickHouse#121954 Related: ClickHouse#99736
feiyunwill
pushed a commit
to feiyunwill/ClickHouse
that referenced
this pull request
Oct 5, 2026
… DateTime64 Since ClickHouse#110621, `optimize_and_compare_chain` joins a comparison into a transitive chain only when both sides share one `ComparisonOrderDomain`, and `TimePoint` is keyed by scale (`DateTime` 0, `DateTime64(s)` s). A `DateTime` constant compared with a `DateTime64(6)` column therefore has no domain and the edge is skipped. In the query of ClickHouse#123706 (`t1.c3 < e1 AND t1.c3 >= t2.c3 AND t1.c3 <= t2.c3 + INTERVAL 48 HOUR`, `e1` a `DateTime`) `t2.c3 < e1` is no longer derived and t2 is read in full: 26.7 prunes it to 4/29 parts, 26.8 and master read 29/29. The comparison itself widens such a constant exactly to the column's scale. Do the same once, in the step that already retypes String constants (`tryNormalizeConstStringEdge`, now `tryNormalizeConstEdge`): a `DateTime` or lower-scale `DateTime64` constant compared with a `DateTime64` expression is converted with `tryConvertFieldToTypeExact`, which returns Null on overflow or loss. The edge then joins the expression's own `TimePoint(s)` domain. Every derived comparison is between equal `DateTime64` scales, so it cannot rescale or throw, and the inferred-domain check holds. A higher-scale or overflowing constant stays out of the chain as before. `Date` constants are not included, their conversion to a time point depends on the time zone. Closes ClickHouse#123706 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Restrict inferred comparison chains to native-number and
Stringdomains so pair-specific conversions cannot produce invalid predicates.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix wrong query results from the optimize_and_compare_chain optimization (enabled by default): a condition derived by transitivity from a < b AND b < c could use different comparison semantics than the original conditions when the chain mixes types with pair-specific conversion rules (e.g. Enum vs String, Decimal vs Float64, FixedString of different widths). Transitive conditions are now derived only within groups of types sharing a single comparison order.
Version info
26.8.1.346(included in26.8and later)