Repository navigation
Conversation
With convert_query_to_cnf = 1, NOT (x < c) was rewritten into x >= c (and similarly for the other ordering comparisons). For floating-point values this is not an equivalent transformation: NaN fails every ordered comparison, so NOT (x < c) is true for x = NaN while x >= c is false, and rows containing NaN silently disappeared from the result. In the Analyzer CNF the ordering comparisons are now folded only when every argument type is known to be totally ordered (integers, Decimal, Enum, Date/DateTime/Time, String, UUID, IPv4/IPv6, also inside Nullable and LowCardinality); for floating-point, Dynamic, composite and unknown types the atom keeps its explicit negation. Comparisons mixing a string operand with a non-string one are not folded either: when the string constant cannot be converted, <, <=, >, >= all return false, which breaks the complementarity the same way NaN does (e.g. NOT (u < '257') for UInt8, or an Enum compared with a non-member literal). The old TreeCNFConverter works on bare ASTs without type information, so it keeps the negation for all ordering comparisons. A constraint whose negated ordering atom is not folded, e.g. ASSUME NOT (x < c) over a Float column, stays negative, and the comparison graph stores unnegated relations only, so it is skipped when building the graph instead of failing the pre-existing assertion. The constraint-driven index hint derivation (optimize_append_index) now also gives up on a negated ordering atom: the hint relation is derived from the function name alone, so consuming such an atom would produce an indexHint of the opposite range and prune valid granules. With the old analyzer the CNF resolution step consequently no longer recognises x > c and x <= c as complementary, so optimize_using_constraints performs fewer eliminations there even for integer columns; the oldanalyzer reference of 01622_constraints_simple_optimization is regenerated accordingly (query results are unchanged, only the rewritten WHERE in EXPLAIN SYNTAX keeps more conjuncts). The equals/notEquals, in/notIn, like/notLike and empty/notEmpty pairs are still always folded: they remain complementary for NaN.
|
Workflow [PR], commit [f651a3b] Summary: ❌
AI ReviewSummaryThis PR fixes the main analyzer-side CNF bug around folding negated ordering comparisons over NaN and updates the constraint and Findings❌ Blockers
Final VerdictNot ready to merge until the remaining LLVM Coverage ReportMeasured on commit f651a3b.
Changed lines: Changed C/C++ lines covered: 104/114 (91.23%) · Uncovered code |
…oat-nan # Conflicts: # tests/queries/0_stateless/01622_constraints_simple_optimization.oldanalyzer.reference
Master removed all old-analyzer stateless test coverage: the oldanalyzer reference files are gone and tests no longer carry SETTINGS enable_analyzer = 0. Follow that policy here: 04669_cnf_not_ordering_comparison_nan now pins enable_analyzer = 1 and keeps only the Analyzer variants of its queries, and the merge already dropped the regenerated oldanalyzer reference of 01622_constraints_simple_optimization together with the mechanism. The TreeCNFConverter part of the fix is unchanged: the old analyzer code path still exists and keeps its conservative no-fold behavior, it is just no longer exercised by stateless tests, matching the rest of the test suite.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes19 object files changed (+60.20 KiB total), 0 added.
716 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 units22 translation units recompiled, 148 s compile time in total, 22 of them have a recent master baseline. |
The syntactic "atom OR NOT atom" elimination in checkIfGroupAlwaysTrueAtoms is not sound in three-valued logic: for a NULL value both sides are NULL, so the group is not always true and the filter must stay. The Analyzer version now treats the pair as mutually exclusive only when the atom cannot produce NULL; the old analyzer works on bare ASTs without type information and keeps the existing behavior, which is now documented in a comment. The defect itself predates this pull request: on current master the folding of the negated atom and its later unfolding inside the constraint optimizer restore the literal pair, so the group is dropped the same way. It is fixed here because the review flagged it and the fix belongs to the same family of type-aware CNF equivalences.
Closes: #112036
With
convert_query_to_cnf = 1, the CNF converter rewroteNOT (x < c)intox >= c(and similarly for<=,>,>=). For floating-point values this is not an equivalent transformation: NaN fails every ordered comparison, soNOT (x < c)is true forx = NaNwhilex >= cis false. Rows containing NaN silently disappeared from the result, in both the new and the old analyzer.The fix makes the folding type-aware in the Analyzer CNF (
src/Analyzer/Passes/CNF.cpp): ordering comparisons are folded only when every argument type is known to be totally ordered (integers,Decimal,Enum,Date/DateTime/Timefamilies,String/FixedString,UUID,IPv4/IPv6, also insideNullable/LowCardinality). For floating-point,Dynamic, composite and unknown types the atom keeps its explicit negation, so with the Analyzer the existing constraint optimizations (e.g. theA AND NOT Aelimination in01622_constraints_simple_optimization) are preserved for totally ordered types. The oldTreeCNFConverterworks on bare ASTs without type information and now keeps the negation for all ordering comparisons. As a consequence, withenable_analyzer = 0the CNF resolution step no longer recognisesx > candx <= cas complementary, sooptimize_using_constraintsperforms fewer eliminations than before even for integer columns; the updated01622_constraints_simple_optimization.oldanalyzer.referencereflects that (the row counts asserted by the test are unchanged, only the rewrittenWHEREinEXPLAIN SYNTAXkeeps more conjuncts). One of the affectedEXPLAIN SYNTAXstatements also stops hitting a separate pre-existing defect where a filter reduced to a contradiction is dropped entirely instead of becomingWHERE 0; that defect remains reachable through the Analyzer and will be reported separately.A constraint whose negated ordering atom cannot be folded (e.g.
ASSUME NOT (x < c)over aFloat64column) is skipped when building the comparison graph instead of hitting the assertion there. Since negated ordering atoms can now survive CNF normalization, the constraint-driven index hint derivation (optimize_append_index) learned to give up on them: it derives the hint relation from the function name alone, so consuming a negated atom would produce anindexHintof the opposite range and prune valid granules.The
equals/notEquals,in/notIn,like/notLike,empty/notEmptypairs are still always folded, since they remain complementary for NaN.Two related degenerate cases are covered by the same guard. A comparison of a non-string operand with a string constant that cannot be converted (e.g.
u < '257'forUInt8, or anEnumcompared with a non-member literal) also returns false for<,<=,>,>=alike, so such mixed comparisons are not folded either. And the tautology elimination underoptimize_using_constraintsno longer treatsx >= c OR x < cover aFloat64column as always true (both sides are false for NaN).The fix is scoped to the CNF rewrite. The primary key index analysis (
KeyCondition) applies the same kind of negation folding independently ofconvert_query_to_cnf; that is a separate pre-existing issue and will be reported and fixed separately. The constraint-driven index hint derivation also has a pre-existing gap unrelated to negation: it silently skips atoms that are not two-argument comparisons instead of giving up on the group, which can produce an over-strongindexHint; that is left untouched here and will be reported separately as well.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix dropping of rows under
convert_query_to_cnf = 1when a negated ordering comparison was rewritten into the opposite comparison although the two are not complementary. This affected floating-point columns, where NaN fails bothx < candx >= c, and comparisons with an unconvertible string constant (e.g.NOT (u < '257')forUInt8), which fail both sides the same way. As a side effect, with the old analyzer (enable_analyzer = 0) the CNF converter no longer normalises ordering comparisons at all, sooptimize_using_constraintsmay eliminate fewer conditions than before, including for non-floating-point columns.Workflow [PR]
Sync PR [sync-upstream/pr/112989]