Apply min/max segment pruning to filtered selection ORDER BY col LIMIT n#18692
Open
gortiz wants to merge 1 commit into
Open
Apply min/max segment pruning to filtered selection ORDER BY col LIMIT n#18692gortiz wants to merge 1 commit into
gortiz wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #18692 +/- ##
============================================
+ Coverage 64.46% 64.51% +0.04%
Complexity 1291 1291
============================================
Files 3371 3372 +1
Lines 208551 208721 +170
Branches 32569 32621 +52
============================================
+ Hits 134450 134653 +203
+ Misses 63292 63266 -26
+ Partials 10809 10802 -7
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
73b9172 to
e4b78b4
Compare
SelectionQuerySegmentPruner only applied its order-by min/max + LIMIT segment pruning to unfiltered selection queries, so adding any filter to a `SELECT ... ORDER BY <col> LIMIT n` query defeated the pruning and engaged every matching segment (apache#18685). Extend the pruner to run with a filter by counting each segment toward the LIMIT only the rows that provably match the filter: its total docs when the segment fully satisfies the filter (the dual of ColumnValueSegmentPruner's no-match test, computed from min/max metadata for >, >=, <, <=, =, <>), else 0. Using this lower bound on matching rows keeps the order-by boundary safe, so a segment is never pruned when it might still hold a top-n matching row; an AND with a non-provable conjunct simply degrades to no pruning. The optimization is skipped when null handling is active for a column, because nulls are stored as a default value that pollutes the min/max metadata, and a NaN min/max is never treated as a full match. Add unit tests (range/eq/neq predicates, ASC/DESC, straddling-segment and AND counterexamples, NaN guard, null-handling gate) and a JMH benchmark (BenchmarkSelectionOrderByFilterPruning) that toggles null handling to compare the optimized and pre-fix paths within a single build. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
e4b78b4 to
e0ae4d2
Compare
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.
Problem
For
SELECT ... ORDER BY <col> [DESC] LIMIT non a table range-partitioned on<col>,SelectionQuerySegmentPrunerprunes down to the handful of segments the LIMIT needs (an unfiltered "latest n" query touches a couple of segments). Adding any filter — even a trivially-true one on an unrelated column — defeated that pruning, and the query engaged every matching segment.The cause:
SelectionQuerySegmentPruner.isApplicableToonly allowed the order-by/LIMIT pruning when there was no filter, because the pruner sizes its keep-set by accumulating the unfilteredgetTotalDocs()untilLIMIT + OFFSET— which, with a filter, is an upper bound on matching rows and would prune segments that still hold top-n matches (wrong results).See #18685 for the full reproduction and analysis.
Change
Extend
SelectionQuerySegmentPrunerto run with a filter by accumulating a lower bound on each segment's matching rows instead ofgetTotalDocs():ColumnValueSegmentPruner's no-match test, computed from the predicate column's min/max for>,>=,<,<=,=,<>(conjunction = all children full; OR = any child full; anything unprovable =false).AND (...)with a non-provable conjunct simply degrades to "no pruning" — never wrong.NaNmin/max is never treated as a full match.The execution-time
MinMaxValueBasedSelectionOrderByCombineOperatorskip already worked with a filter; this restores the plan-time pruning that avoids building/opening the pruned segments at all.Tests
SelectionQuerySegmentPrunerTestadds coverage for: each comparison operator, ASC/DESC, the straddling-segment counterexample (the case the lower bound exists to protect), theAND-with-non-provable-conjunct degradation, the FLOAT/DOUBLE NaN guard, and the null-handling gate (incl. column-based null handling: non-nullable column still optimizes, nullable does not).Benchmark
Adds
BenchmarkSelectionOrderByFilterPruning(pinot-perf). It togglesenableNullHandlingto compare the optimized path against the pre-fix path within one build (null handling gates the optimization off). On a 100-segment table (20k rows/segment),LIMIT 10:With the fix the filtered query reaches parity with the unfiltered one; without it the same query is measurably slower. The gap widens with segment count and per-segment cost.
Closes #18685
🤖 Generated with Claude Code