Repository navigation
Cherry pick #121065 to 26.8: Keep detecting an always-false equals chain when comparison pruning is off - #121727
Conversation
…s off `optimize_redundant_comparisons` (new in 26.7, #99736) gates the whole of `addComparisonFilter` behind an early return at the top of the function: with the setting off, every operand is parked in `opaque_filters` unanalysed, so the function never returns ALWAYS_FALSE, `found_conflict` is never set, and the AND is never collapsed to a constant false. Detecting that two `equals` on the same expression carry different values is older than that setting. `v26.6.1.1193-stable` calls `tryOptimizeAndEqualsNotEqualsChain` with no setting gate at all, and declares no `optimize_redundant_comparisons` (grep -c = 0); #99736 deleted that function and folded its contradiction check into the new, gated analysis. Since the setting is recorded in the 26.7 block of SettingsChangesHistory.cpp, every `compatibility` below 26.7 now removes a fold that 25.12 genuinely performed, and a query whose WHERE is always false executes its full plan. On the reported reproducer that is a LEFT JOIN onto an `argMax ... GROUP BY` subquery over 2.2M rows: 2,200,000 rows read where 0 are needed, 51 ms -> 3641 ms on the customer's data. So detect that one contradiction regardless of the setting, and leave pruning, boundary folding and strengthening (all genuinely introduced with it) gated. Neutralizing the history entry instead would be wrong: v26.7.1.1315-stable both ships the setting and records it in its own newest block, so the placement is correct, and that route also leaves an explicit `optimize_redundant_comparisons = 0` regressed. Why comparing against a single stored representative is equivalent to a full pairwise scan, which is what keeps the disabled path linear in the operands of the AND: the only question asked is whether two `equals` on this expression carry different values, every `equals` is compared against the representative, and `found_conflict` is only ever set, never cleared. The representative is recorded as a position rather than a pointer or iterator because `opaque_filters.push_back` reallocates; in disabled mode that vector only ever grows, since no erase path reaches it. The new block sits below every existing keep-opaque guard (nullable result, non-lossless conversion, NaN constant), so the disabled path is a strict subset of the enabled one and inherits any guard added above it, and it reaches the collapse through the single existing `found_conflict` site, so that site's non-convertible-operand veto still keeps a TYPE_MISMATCH the unoptimized query would raise from being suppressed. For EQUALS vs EQUALS `compareComparisonFilters` returns only PRUNE_RIGHT or ALWAYS_FALSE, never a STRENGTHEN_*, so nothing is mutated on this path. Three narrowings are deliberate. A duplicate `a = 1 AND a = 1` is still not deduplicated with pruning off, although 26.6 dropped it: that is pure evaluation cost with no result change. A non-convertible constant (`Int32 a = 3 AND a = 3.5`) still does not fold, because reusing the enabled path's lossless-conversion requirement is what keeps 26.6's wrong result for `Float64 a = 1 AND a = 1.0` away. And a contradiction `optimize_and_compare_chain` derives now folds the AND with pruning off, which is what its appended plain conjunct is for; 26.6 never derived one. Twelve reference lines change across the three 04032* files, every one an `optimize_redundant_comparisons = 0` case holding two `equals` on the same expression with different, losslessly convertible values. Closes: #121007 Closes: #121012 Co-Authored-By: Claude Opus 5 <[email protected]>
Drop the block comment restating the setting split, cut the new field doc and the addComparisonFilter, method and header doc clauses to one line each.
…n-without-pruning Keep detecting an always-false equals chain when comparison pruning is off
This arm is blocked on #121750, and it clears itself once that landsThe conflict is one hunk in #111535 is now being backported to 26.8 as #121750 (mergeable, CI running), so
Merge order matters, and in one direction only. With #121750 first, the retried cherry-pick is clean. With |
|
The |
59cfdbd to
8e2c508
Compare
Original pull-request #121065
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Before you resolve anything
Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job