Skip to content

Stream the AND restriction push-down for LIMIT queries - #19589

Merged
xiangfu0 merged 9 commits into
apache:masterfrom
gortiz:spike/lazy-restriction
Nov 25, 2026
Merged

xiangfu0 merged 9 commits into
apache:masterfrom
gortiz:spike/lazy-restriction

Conversation

@gortiz

@gortiz gortiz commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

PR flow

Streaming restricts scan to candidate ids chunk‑by‑chunk via matchDocIds.

flowchart TD
  N0["CandidateDocIdIterator #40;F4#41;"]:::stModified
  N1["RestrictedScanDocIdIterator #40;F4#41;"]:::stAdded
  N2["ScanBasedDocIdIterator#46;matchDocIds #40;F6#44; F5#44; F3#41;"]:::stModified
  N3["Matching document ids output #40;F4#41;"]:::stModified
  N0 -->|"supplies candidate doc ids"| N1
  N1 -->|"calls matchDocIds on chunk"| N2
  N1 -->|"returns matching doc ids"| N3
  classDef stAdded fill:#dafbe1,stroke:#1a7f37,color:#1f2328,stroke-width:2px
  classDef stModified fill:#fff8c5,stroke:#9a6700,color:#1f2328,stroke-width:2px
  classDef stRemoved fill:#ffebe9,stroke:#cf222e,color:#1f2328,stroke-width:2px
  classDef stUnchanged fill:#f6f8fa,stroke:#656d76,color:#1f2328,stroke-width:1px
Loading

AI-generated · Green: added · Yellow: modified · Red: removed · Gray: existing

Diff evidence
  • F3: pinot-core/src/main/java/org/apache/pinot/core/operator/dociditerators/MVScanDocIdIterator.java — before · after
  • F4: pinot-core/src/main/java/org/apache/pinot/core/operator/dociditerators/RestrictedScanDocIdIterator.java — after
  • F5: pinot-core/src/main/java/org/apache/pinot/core/operator/dociditerators/SVScanDocIdIterator.java — before · after
  • F6: pinot-core/src/main/java/org/apache/pinot/core/operator/dociditerators/ScanBasedDocIdIterator.java — before · after
  • Regenerate PR flow

Summary

This PR adds a STREAMING strategy to the AND restriction push-down (andRestrictionPushdownMode). With it, an AND evaluates its scans and its composite children (OR, AND, NOT) on its candidate documents one chunk at a time. Before, it materialized a bitmap at every step. An eligible unordered selection with a LIMIT can now stop after reading enough chunks.

The main user is row-level security (RLS). Since #20183, a query under RLS always pushes the RLS bitmap into the user filter, so that no user expression runs on a hidden row. That push-down was eager: a LIMIT 10 under RLS evaluated the user filter on every allowed row of the segment. With this PR, a single eligible user predicate can stream. The eligibility decision is made before segment pruning. The policy bitmap bounds the user filter even when the policy matches the whole segment.

Background

#19408 added the push-down. An AND gives the document ids that it has matched to its composite children, and each child returns a bitmap. This fixes #19339, but it removes early termination. For that reason the push-down is off by default (NEVER), and AUTO enables it only for queries that read the whole filter.

The scan children of an AND that has an index child were already eager on master: ScanBasedDocIdIterator#applyAnd returns a full bitmap. So a selection with a LIMIT paid for the full scan when the AND had an index child, also without this push-down. RLS always gives the AND an index child: the RLS bitmap.

Changes

AND(rlsBitmap, a = 1)

EAGER      Match a = 1 against all candidates and materialize the result.
STREAMING  Match a = 1 in candidate chunks until the consumer stops or reaches EOF.
  • BlockDocIdSet#restrictedIterator(ImmutableRoaringBitmap) is the lazy counterpart of applyAnd. AndDocIdSet, OrDocIdSet and NotDocIdSet implement it. NOT uses the new RestrictedNotDocIdIterator.
  • RestrictedScanDocIdIterator applies the scans of an AND to each chunk, in order. The chunk starts at 256 documents and doubles up to MAX_DOC_PER_CALL. An advance() that skips candidates resets it to 256. The iterator checks query termination after each chunk.
  • ScanBasedDocIdIterator#matchDocIds(int[], int) is the per-chunk kernel. SV and MV scans match in place. ExpressionScanDocIdIterator reuses the built-in projection and reader contexts across chunks. Custom projections keep their per-chunk lifecycle.
  • AndRestrictionPushdown (OFF, EAGER, STREAMING plus the number of documents to read) replaces the boolean flag in the filter operators. A streaming AND falls back to EAGER when LIMIT + OFFSET is not less than its number of candidates, because it reads them all anyway.
  • DocIdSetOperator reports the scan cost also on the call that returns no document. A lazy filter can scan the rest of the segment in that call.
  • SelectionOnlyOperator closes its DocIdSetOperator when it has its rows or a read fails. AND, OR, and NOT forward the close to the scans. NOT defers its first pull until after construction and closes the child if that pull fails. This builds on Enforce scan budgets between restricted scan chunks #20231.

Strategy selection

Mode Without RLS With RLS
NEVER (default) OFF Stream one eligible user predicate. Otherwise use EAGER.
ALWAYS EAGER EAGER
STREAMING (new) Stream unordered selections with a positive LIMIT and a finite LIMIT + OFFSET. Otherwise use EAGER. Stream one eligible user predicate. Otherwise use EAGER.
AUTO EAGER for aggregation and group-by queries with a positive LIMIT. Otherwise use OFF. Stream one eligible user predicate. Otherwise use EAGER.

RLS streaming also requires an unordered selection with a positive LIMIT and a finite LIMIT + OFFSET. The streaming response path uses eager evaluation whenever the query would otherwise select streaming push-down.

With the default configuration, only eligible queries under RLS change behavior.

Compatibility

  • No wire or format change.
  • STREAMING is a new value of the andRestrictionPushdownMode query option and of the server configuration pinot.server.query.executor.and.restriction.pushdown.mode. A server that does not know it rejects a query that sets it. Set it explicitly only after every server is upgraded. AUTO and the RLS path resolve the strategy on the server, so they need no new value on the wire.

Benchmarks

Query level

BenchmarkRowLevelSecurityLimitQueries runs single-stage queries end to end on the server. It combines two segment entries of 1M rows each. The RLS filter uses an inverted index and allows 10% of the rows. SCAN and EXPRESSION compare eager ALWAYS with the streaming-eligible default NEVER. OR, OR_FEW_MATCHES, and COUNT use eager evaluation in both modes.

The chunk-size and iterator tables below report historical measurements from the earlier implementation, run on a Ryzen 7 5700X with JDK 25, jemalloc, and one fork.

Chunk size

BenchmarkRestrictedScanChunkSize runs RestrictedScanDocIdIterator with one SVScanDocIdIterator on 2M documents, with different first chunk sizes. The chunk doubles after each call, up to 10000 (MAX_DOC_PER_CALL), so 10000 never grows. Time in µs/op.

candidates documents read 64 256 (default) 1024 10000
1% 10 2.3 2.7 7.9 55.4
1% 1000 24.1 22.8 19.1 46.1
1% all 115.1 115.5 115.2 93.7
50% 10 2.4 3.3 7.2 49.2
50% 1000 21.7 20.9 37.0 41.5
50% all 6892.7 6884.4 6872.6 6884.8

256 is within 1 µs of the best value for a LIMIT 10, and close to the best for 1000 documents. Because the chunk grows, the first chunk size changes almost nothing for a full read. A fixed chunk of 10000 is 20x slower for a LIMIT 10.

Iterator level

BenchmarkAndRestrictionPushdown: 2M documents, a real SVScanDocIdIterator on a fixed-bit forward index, the real AndDocIdSet in each strategy. The setup checks that the three strategies return the same documents. LIMIT reads 10 documents. sel is the fraction of documents in the candidate bitmap. 3 warmup and 5 measurement iterations of 3 s. Time in µs/op. The ratios are against OFF.

shape consume sel OFF EAGER STREAMING EAGER alloc STREAMING alloc
SINGLE_SCAN DRAIN 0.01 118.4 117.1 (0.99x) 115.0 (0.97x) 1.00x 0.72x
SINGLE_SCAN DRAIN 0.5 6257.5 6241.0 (1.00x) 6817.8 (1.09x) 1.00x 0.03x
SINGLE_SCAN LIMIT 0.01 102.0 102.7 (1.01x) 3.4 (0.03x) 1.00x 0.70x
SINGLE_SCAN LIMIT 0.5 5621.5 5613.0 (1.00x) 3.3 (0.001x) 1.00x 0.03x
SCAN_IN_OR DRAIN 0.01 3564.4 115.4 (0.03x) 140.0 (0.04x) 0.10x 0.08x
SCAN_IN_OR DRAIN 0.5 4149.4 2762.8 (0.67x) 4652.5 (1.12x) 1.14x 0.29x
SCAN_IN_OR LIMIT 0.01 3600.2 104.2 (0.03x) 57.7 (0.02x) 0.10x 0.08x
SCAN_IN_OR LIMIT 0.5 3508.1 2145.2 (0.61x) 52.7 (0.02x) 1.14x 0.29x
SCAN_ONLY_OR DRAIN 0.01 768.3 222.3 (0.29x) 240.2 (0.31x) 21.92x 15.11x
SCAN_ONLY_OR DRAIN 0.5 25281.6 8126.4 (0.32x) 14754.5 (0.58x) 266.47x 12.24x
SCAN_ONLY_OR LIMIT 0.01 1.3 211.7 (168x) 6.7 (5.3x) 25.00x 16.64x
SCAN_ONLY_OR LIMIT 0.5 0.7 7435.2 (10790x) 6.5 (9.4x) 382.65x 16.62x
INDEXED_OR DRAIN 0.01 72.3 72.4 (1.00x) 75.7 (1.05x) 1.00x 1.00x
INDEXED_OR DRAIN 0.5 913.8 917.2 (1.00x) 917.6 (1.00x) 1.00x 1.00x
INDEXED_OR LIMIT 0.01 52.1 52.6 (1.01x) 52.1 (1.00x) 1.00x 1.00x
INDEXED_OR LIMIT 0.5 48.2 48.5 (1.01x) 48.8 (1.01x) 1.00x 1.00x

The shapes:

What the iterator benchmarks measured

  • In the SINGLE_SCAN iterator workload with sel 0.5 and a short read, the measured cost dropped from 5.6 ms to 3.3 µs.
  • STREAMING removes the cliff of EAGER when the query stops early: 7435 µs becomes 6.5 µs on SCAN_ONLY_OR.
  • EAGER stays faster when the query reads the whole filter (SCAN_IN_OR and SCAN_ONLY_OR with DRAIN, sel 0.5). That is why AUTO keeps EAGER for aggregations.
  • SCAN_ONLY_OR with a LIMIT is 5x to 9x slower than OFF, but in absolute terms the difference is about 6 µs per segment. AUTO does not pick a push-down for it without RLS. The current RLS planner uses EAGER for this compound filter. The iterator benchmark calls STREAMING directly.

Tests

  • RestrictedScanDocIdIteratorTest: the same documents as the eager applyAnd, chunk growth and reset, advance() moves the candidate stream, later scans see only the survivors, a chunk larger than the scan batch, and the close at EOF and when a scan fails to close.
  • AndDocIdSetPushdownTest: the three strategies return the same documents, and the fallback to EAGER.
  • AndRestrictionPushdownModeTest: the AUTO rules and the RLS overrides.
  • DocIdSetOperatorScanCostTest: scan cost on the last call, early close, and cleanup when the first pull of a negated compound filter fails.
  • AndRestrictionPushdownQueriesTest, AndRestrictionPushdownNullQueriesTest and RowLevelSecurityScanQueriesTest: query level, with LIMIT (the scan cost is bounded) and with ORDER BY (eager), H3 under RLS, and an RLS filter with fewer matches than the LIMIT.
  • Also run: the pinot-core suite, QueryRunnerTest (MSE leaf under RLS) and RowLevelSecurityIntegrationTest.

Follow-ups

@codecov-commenter

codecov-commenter commented Sep 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 79.43262% with 87 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.47%. Comparing base (92468bf) to head (db84f95).
⚠️ Report is 40 commits behind head on master.

Files with missing lines Patch % Lines
...or/dociditerators/ExpressionScanDocIdIterator.java 54.00% 21 Missing and 2 partials ⚠️
...query/request/context/utils/QueryContextUtils.java 62.06% 4 Missing and 7 partials ⚠️
...che/pinot/core/operator/docidsets/AndDocIdSet.java 75.67% 6 Missing and 3 partials ⚠️
...e/operator/dociditerators/MVScanDocIdIterator.java 0.00% 6 Missing ⚠️
...or/dociditerators/RestrictedScanDocIdIterator.java 90.00% 2 Missing and 4 partials ⚠️
...ava/org/apache/pinot/core/plan/FilterPlanNode.java 50.00% 2 Missing and 3 partials ⚠️
...ache/pinot/core/operator/docidsets/OrDocIdSet.java 69.23% 2 Missing and 2 partials ⚠️
...va/org/apache/pinot/core/common/BlockDocIdSet.java 80.00% 2 Missing and 1 partial ⚠️
...core/operator/dociditerators/NotDocIdIterator.java 88.88% 2 Missing and 1 partial ⚠️
...che/pinot/core/operator/docidsets/NotDocIdSet.java 50.00% 2 Missing and 1 partial ⚠️
... and 9 more
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #19589      +/-   ##
============================================
+ Coverage     71.16%   71.47%   +0.30%     
  Complexity     1639     1639              
============================================
  Files          3594     3615      +21     
  Lines        242126   244484    +2358     
  Branches      39215    39763     +548     
============================================
+ Hits         172308   174739    +2431     
+ Misses        56922    56558     -364     
- Partials      12896    13187     +291     
Flag Coverage Δ
integration 100.00% <ø> (ø)
integration1 100.00% <ø> (ø)
integration2 0.00% <ø> (ø)
java-25 71.47% <79.43%> (+0.30%) ⬆️
lane-a 100.00% <ø> (ø)
lane-b 0.00% <ø> (ø)
temurin 71.47% <79.43%> (+0.30%) ⬆️
unittests 71.47% <79.43%> (+0.30%) ⬆️
unittests1 61.22% <79.43%> (+0.76%) ⬆️
unittests2 42.97% <7.56%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

gortiz added a commit that referenced this pull request Oct 9, 2026
<!-- pinot-pr-flow:start -->

### PR flow

Pushes AND restriction down to composite children and scans when enabled\.

```mermaid
flowchart TD
  N0["InstancePlanMakerImplV2#46;applyQueryOptions #40;F18#41;"]:::stAdded
  N1["QueryContext#46;isAndRestrictionPushdownEnabled #40;F20#41;"]:::stAdded
  N2["AndFilterOperator constructor #40;F13#41;"]:::stModified
  N3["AndDocIdSet constructor #40;F4#41;"]:::stModified
  N4["AndDocIdSet#46;iterator#40;#41; #40;F4#41;"]:::stModified
  N5["AndDocIdSet#46;applyAnd#40;#41; #40;F4#41;"]:::stAdded
  N6["OrDocIdSet#46;applyAnd#40;#41; #40;F10#41;"]:::stModified
  N7["NotDocIdSet#46;applyAnd#40;#41; #40;F9#41;"]:::stModified
  N8["ScanBasedDocIdIterator#46;applyAnd#40;#41; #40;F3#41;"]:::stModified
  N0 -->|"sets flag from config#47;query option to QueryContext field"| N1
  N1 -->|"AndFilterOperator reads flag via getter"| N2
  N2 -->|"AndFilterOperator passes flag to AndDocIdSet constructor"| N3
  N3 -->|"AndDocIdSet#46;iterator checks #95;restrictionPushdownEnabled to #40;"| N4
  classDef stAdded fill:#dafbe1,stroke:#1a7f37,color:#1f2328,stroke-width:2px
  classDef stModified fill:#fff8c5,stroke:#9a6700,color:#1f2328,stroke-width:2px
  classDef stRemoved fill:#ffebe9,stroke:#cf222e,color:#1f2328,stroke-width:2px
  classDef stUnchanged fill:#f6f8fa,stroke:#656d76,color:#1f2328,stroke-width:1px
```

AI-generated · Green: added · Yellow: modified · Red: removed · Gray: existing

<details>
<summary>Diff evidence</summary>

- F3: pinot\-core/src/main/java/org/apache/pinot/core/operator/dociditerators/MVScanDocIdIterator\.java — [before](https://github.com/apache/pinot/blob/15b838a5df8d1cc34ab6302289defed31c7f44f9/pinot-core/src/main/java/org/apache/pinot/core/operator/dociditerators/MVScanDocIdIterator.java) · [after](https://github.com/apache/pinot/blob/0c87bb5ae77c1c08707ed05eede55567acf5447e/pinot-core/src/main/java/org/apache/pinot/core/operator/dociditerators/MVScanDocIdIterator.java)
- F4: pinot\-core/src/main/java/org/apache/pinot/core/operator/docidsets/AndDocIdSet\.java — [before](https://github.com/apache/pinot/blob/15b838a5df8d1cc34ab6302289defed31c7f44f9/pinot-core/src/main/java/org/apache/pinot/core/operator/docidsets/AndDocIdSet.java) · [after](https://github.com/apache/pinot/blob/0c87bb5ae77c1c08707ed05eede55567acf5447e/pinot-core/src/main/java/org/apache/pinot/core/operator/docidsets/AndDocIdSet.java)
- F9: pinot\-core/src/main/java/org/apache/pinot/core/operator/docidsets/NotDocIdSet\.java — [before](https://github.com/apache/pinot/blob/15b838a5df8d1cc34ab6302289defed31c7f44f9/pinot-core/src/main/java/org/apache/pinot/core/operator/docidsets/NotDocIdSet.java) · [after](https://github.com/apache/pinot/blob/0c87bb5ae77c1c08707ed05eede55567acf5447e/pinot-core/src/main/java/org/apache/pinot/core/operator/docidsets/NotDocIdSet.java)
- F10: pinot\-core/src/main/java/org/apache/pinot/core/operator/docidsets/OrDocIdSet\.java — [before](https://github.com/apache/pinot/blob/15b838a5df8d1cc34ab6302289defed31c7f44f9/pinot-core/src/main/java/org/apache/pinot/core/operator/docidsets/OrDocIdSet.java) · [after](https://github.com/apache/pinot/blob/0c87bb5ae77c1c08707ed05eede55567acf5447e/pinot-core/src/main/java/org/apache/pinot/core/operator/docidsets/OrDocIdSet.java)
- F13: pinot\-core/src/main/java/org/apache/pinot/core/operator/filter/AndFilterOperator\.java — [before](https://github.com/apache/pinot/blob/15b838a5df8d1cc34ab6302289defed31c7f44f9/pinot-core/src/main/java/org/apache/pinot/core/operator/filter/AndFilterOperator.java) · [after](https://github.com/apache/pinot/blob/0c87bb5ae77c1c08707ed05eede55567acf5447e/pinot-core/src/main/java/org/apache/pinot/core/operator/filter/AndFilterOperator.java)
- F18: pinot\-core/src/main/java/org/apache/pinot/core/plan/maker/InstancePlanMakerImplV2\.java — [before](https://github.com/apache/pinot/blob/15b838a5df8d1cc34ab6302289defed31c7f44f9/pinot-core/src/main/java/org/apache/pinot/core/plan/maker/InstancePlanMakerImplV2.java) · [after](https://github.com/apache/pinot/blob/0c87bb5ae77c1c08707ed05eede55567acf5447e/pinot-core/src/main/java/org/apache/pinot/core/plan/maker/InstancePlanMakerImplV2.java)
- F20: pinot\-core/src/main/java/org/apache/pinot/core/query/request/context/QueryContext\.java — [before](https://github.com/apache/pinot/blob/15b838a5df8d1cc34ab6302289defed31c7f44f9/pinot-core/src/main/java/org/apache/pinot/core/query/request/context/QueryContext.java) · [after](https://github.com/apache/pinot/blob/0c87bb5ae77c1c08707ed05eede55567acf5447e/pinot-core/src/main/java/org/apache/pinot/core/query/request/context/QueryContext.java)

</details>

- [ ] Regenerate PR flow

<!-- pinot-pr-flow:meta eyJiYXNlX3NoYSI6IjE1YjgzOGE1ZGY4ZDFjYzM0YWI2MzAyMjg5ZGVmZWQzMWM3ZjQ0ZjkiLCJjb250ZXh0X2hhc2giOiJkYjdhODRhM2NkMTU3MmYxYmY5ZGRjNjE2ZmExYjJiYmU5Y2NhMzAxOGI4MjgyMzMxZDU5ZDU4YzAyMmEzODg5IiwiZ2VuZXJhdGVkX2F0IjoxNzkwNjczMTQ3LCJoZWFkX3NoYSI6IjBjODdiYjVhZTc3YzFjMDg3MDdlZDA1ZWVkZTU1NTY3YWNmNTQ0N2UiLCJtb2RlbCI6Im52aWRpYS9uZW1vdHJvbi0zLXN1cGVyLTEyMGItYTEyYjpmcmVlIiwib2JzZXJ2ZWRfYmFzZV9zaGEiOiJlZjc1ZWQ5NzE0OGY1ZWIyOTRlNTQ0YjkxODVhMzQ4MzdjZDViMzU4IiwicG9saWN5IjoicGlub3QtcHItZmxvdy12MSJ9 -->
<!-- pinot-pr-flow:signature 3194e019799374a8f9b31d0b2e0cc39757184be42c3cbdcbe0c57351222b504c -->
<!-- pinot-pr-flow:end -->

Fixes #19339

## Problem

When a filter has the shape `P AND (A OR B)`, the `(A OR B)` subtree is evaluated independently of `P`.

`AndDocIdSet#iterator()` is the only place a restriction is ever pushed anywhere: it merges the index-based children into a bitmap and hands it to each `ScanBasedDocIdIterator` via `applyAnd`. `OrDocIdSet` has no equivalent, so a predicate nested inside an OR branch never learns about `P`.

That has two consequences:

- A predicate with no usable index inside an OR branch — `IN_SUBQUERY` evaluated through `ExpressionScanDocIdIterator`, or a plain scan on a segment missing its range index — is evaluated on every document its branch matches, however selective `P` is.
- **Adding an index to a column inside an OR branch can make a query much slower.** With no index-based sibling the branch stays lazy and is driven by the outer merged bitmap; add one and the branch takes the eager path in `AndDocIdSet#iterator()`, materializing over the whole segment before the outer AND intersects. In one deployment the same query returning the same two rows went from ~500 to ~17,000,000 `numEntriesScannedInFilter`, allocating 4.6 GB, after two columns in the branch gained a range index and an inverted index.

The same gap has been reported twice before and both reports are still open: #10396 and #9402.

## Fix

Generalize `applyAnd` from `ScanBasedDocIdIterator` up to `BlockDocIdSet`, so an AND can hand the document ids it has matched to its composite children:

- `AndDocIdSet#applyAnd` adds the candidate set to its children as one more index-based child and runs `iterator()`. Everything `iterator()` already does — the sorted-range intersection, sorting bitmaps by cardinality, `AndScanReordering`, running every scan against the merged document ids — therefore applies to the restricted evaluation too, with a single implementation of AND.
- `OrDocIdSet#applyAnd` unions the branches, each restricted to the candidates no earlier branch has matched, using `(A OR B) AND S == (A AND S) OR (B AND (S MINUS (A AND S)))`. A scan-based branch costs time linear in the candidate set it is handed, so narrowing avoids one full pass per branch.
- `NotDocIdSet#applyAnd` returns the candidates the child does not match, so the child is only evaluated on the candidates.

`AndDocIdSet#iterator()` defers `iterator()` on children that report `isApplyAndDeferrable()` and pushes its merged document ids into them instead. With no index-based child to seed the push-down it falls back to the lazy `AndDocIdIterator`, exactly as before.

Deferral is limited to subtrees that still have a scan to restrict, via `BlockDocIdSet#isScanBased()`, which the composites compute over their children. An index-only subtree produces the same bitmap either way, so handing it a candidate set would only add an intersection per branch. Reading this off the DocIdSet rather than the operator is also more accurate: an operator that scans internally and returns a bitmap — a non-exact range index, an H3 index — has already paid for the scan by the time its DocIdSet exists, and correctly reports `false`.

## This ships disabled

The push-down **materializes** the filter result instead of streaming it, so a query that stops before reading every matching document ends up doing the full filter work. It is gated by `AndRestrictionPushdownMode`, defaulting to `NEVER`:

| Mode | Behavior |
|---|---|
| `NEVER` (default) | Never push down. Provably identical to today. |
| `ALWAYS` | Always push down. |
| `AUTO` | Push down only for queries that read every matching document — aggregation and group-by. |

`AUTO` excludes a selection query without ORDER BY (stops at its LIMIT), a DISTINCT query (`DistinctOperator` stops the same way, and DISTINCT is modelled as an aggregation function so it needs excluding explicitly), `LIMIT 0`, and — conservatively — a selection query *with* ORDER BY, because whether it can stop early is decided per segment in `SelectionPlanNode`, which picks `SelectionPartiallyOrderedBy*` from the segment's sorted column.

- Server config: `pinot.server.query.executor.and.restriction.pushdown.mode`
- Query option: `SET andRestrictionPushdownMode = 'always'`

The mode is resolved once per query in `InstancePlanMakerImplV2#applyQueryOptions` and carried on `QueryContext`, so the filter operators only ever see the resolved flag.

## Benchmark

`BenchmarkAndRestrictionPushdown` (new, in `pinot-perf`) measures wall-clock time, and allocation under `-prof gc`, because `numEntriesScannedInFilter` can fall while both of those rise. 2M documents, real `SVScanDocIdIterator` over a fixed-bit forward index. Ratios of push-down on to push-down off — lower is better:

| shape | consume | sel | time | alloc |
|---|---|---|---|---|
| SCAN_IN_OR | DRAIN | 0.01 | **0.03x** | 0.10x |
| SCAN_IN_OR | DRAIN | 0.5 | 0.67x | 1.14x |
| SCAN_IN_OR | LIMIT | 0.01 | **0.02x** | 0.10x |
| SCAN_IN_OR | LIMIT | 0.5 | 0.64x | 1.14x |
| SCAN_ONLY_OR | DRAIN | 0.01 | 0.28x | 21.95x |
| SCAN_ONLY_OR | DRAIN | 0.5 | 0.34x | 231.14x |
| SCAN_ONLY_OR | LIMIT | 0.01 | **144.77x** | 25.04x |
| SCAN_ONLY_OR | LIMIT | 0.5 | **9431.50x** | 383.65x |
| INDEXED_OR | all four | | 0.99–1.02x | 1.00x |

`SCAN_IN_OR` is a scan next to an index-based predicate inside an OR branch, the reported shape. `SCAN_ONLY_OR` is scans directly under the OR with no index-based sibling. `INDEXED_OR` has no scan anywhere.

Three things to take from it:

- **`INDEXED_OR` is neutral**, which is what restricting deferral to scan-bearing subtrees is for.
- **The reported shape gains most where the enclosing AND is selective** — 30 to 50 times — which is the production case.
- **`SCAN_ONLY_OR` with a LIMIT is the shape `AUTO` exists to exclude, and it is far worse than an entries-scanned proxy suggests.** That OR is already lazy on master, so materializing it replaces a scan of a few dozen documents with a scan of the whole segment. This is the reason the default is `NEVER`, and it means `ALWAYS` is genuinely dangerous on a selection workload — it is not a "slightly more work" setting.

## Follow-up

Delivering the restriction as a stream rather than a bitmap removes that cliff: worst case 3.27x instead of 9431x, allocation 1.33x instead of 384x. Spike with numbers in #19589. It does not remove the need for a mode — eager is still the better strategy for a full drain — but it changes what the mode chooses, and makes the excluded branch fast rather than merely safe.

Pushing the restriction further down, into the range index itself via `RangeBitmap`'s context parameter (#7597, and @richardstartin's suggestion on the issue), is a separate layer again and is not addressed here.

## Test plan

- `AndDocIdSetPushdownTest` — the reported shape (`AND(P, OR(AND(idx, scan), other))`): the scan visits 100 documents instead of 5000, with identical results. Plus the NOT case, the no-index-child fallback, sorted-range boundaries, the `numDocs` bound on NOT and MATCH-ALL, release of short-circuited OR branches, deferral limited to scan-bearing subtrees, and 200 randomized filter trees over bitmap/scan/sorted/match-all/empty leaves asserting identical document ids with the push-down on and off.
- `AndRestrictionPushdownModeTest` — mode resolution: every `AUTO` inclusion and exclusion, `ALWAYS`/`NEVER`, case-insensitivity, invalid values rejected, server default, and the query option overriding it.
- `AndRestrictionPushdownQueriesTest` — end to end against a real segment in every mode: `ALWAYS` scans strictly fewer entries than `NEVER` with identical rows and `numDocsScanned`; `AUTO` matches `NEVER` for selections and `ALWAYS` for aggregations; the default is pinned to off. Covers filtered aggregations (`CombinedFilterOperator`) and null handling, where `getFalses()` wraps the push-down in a NOT over `OR(trues, nulls)`.
- Full `org.apache.pinot.core.**` and `org.apache.pinot.queries.**` suites pass. No existing expectation changed, because the default leaves every existing query on its current plan.

## Release notes

Two new user-facing configuration surfaces, both defaulting to the current behavior: the server config `pinot.server.query.executor.and.restriction.pushdown.mode` and the query option `andRestrictionPushdownMode`. Enabling either changes `numEntriesScannedInFilter` for affected queries. Old servers in a mixed-version cluster ignore the unknown query option, which matches the `NEVER` default.
@xiangfu0 xiangfu0 added performance Related to performance optimization query Related to query processing index Related to indexing (general) labels Oct 20, 2026

@xiangfu0 xiangfu0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One inline finding on the streaming chunk capacity contract at 4f4a535. The current fixed-256 benchmark is safe; this needs addressing before the streaming kernel is wired into query execution. Source review only; no new local tests or benchmarks were run.

Adds the two pieces the lazy restriction needs, without wiring them in:

- ScanBasedDocIdIterator#matchDocIds(int[], int): a re-entrant per-chunk
  kernel. Unlike applyAnd it does not consume the iterator, so it can be
  called once per chunk. SVScanDocIdIterator delegates to the in-place
  compaction its ValueMatcher already does; MVScanDocIdIterator loops
  doesValueMatch; everything else gets a default built on applyAnd.

- RestrictedScanDocIdIterator: drives a scan from an upstream candidate
  stream a chunk at a time, so the restriction reaches the scan without
  materializing the result.

Measured on the stub scan: draining the stream costs exactly what the
eager applyAnd costs, while stopping after 10 documents costs one chunk
(256 entries) instead of 100000. advance() moves the candidate stream
rather than scanning forward from the target, which is the behaviour that
makes lazily-driven scans pathological today.

Not wired into AndDocIdSet. Spike only.

Refs apache#19339
Adds a STREAMING arm to BenchmarkAndRestrictionPushdown, modelling what a
streaming push-down produces by assembling the tree from the spike's
RestrictedScanDocIdIterator, since the kernel is not wired into
AndDocIdSet yet. Setup asserts that all three strategies return identical
documents, because a hand-assembled model that is subtly wrong would show
up as an impressive and meaningless number.

Time and allocation as a ratio of master's behaviour (2M docs, chunk 256):

  shape         consume sel     eager        streaming   eager    streaming
                                (time)       (time)      (alloc)  (alloc)
  SCAN_IN_OR    DRAIN   0.01      0.03x         0.03x     0.10x     0.06x
  SCAN_IN_OR    DRAIN   0.5       0.66x         1.21x     1.14x     0.27x
  SCAN_IN_OR    LIMIT   0.01      0.02x         0.01x     0.10x     0.06x
  SCAN_IN_OR    LIMIT   0.5       0.58x         0.04x     1.14x     0.26x
  SCAN_ONLY_OR  DRAIN   0.01      0.32x         0.40x    21.97x     1.41x
  SCAN_ONLY_OR  DRAIN   0.5       0.34x         0.53x   232.27x     1.17x
  SCAN_ONLY_OR  LIMIT   0.01    138.85x         2.23x    25.04x     1.33x
  SCAN_ONLY_OR  LIMIT   0.5    7925.64x         3.27x   383.31x     1.33x

Streaming removes the cliff: the worst case goes from 7926x slower to
3.3x, and allocation from 383x to 1.3x. It is also far better wherever the
consumer stops early, because it keeps the early termination the eager
path destroys.

Eager is still the better strategy for a full drain, though -- 0.66x versus
1.21x on SCAN_IN_OR at high candidate density, where chunking costs more
than it saves. So streaming does not replace the mode, it changes what the
mode chooses: eager when the query will read every matching document,
streaming when it may stop early.

Caveats: the model precomputes the bitmap intersections a real
implementation would also compute; chunk size is fixed at 256; and the scan
is an SVScanDocIdIterator, so this says nothing yet about an expression
scan, which builds a ProjectionOperator per chunk.

Refs apache#19339
SVScanDocIdIterator#matchDocIds passed the whole chunk to its ValueMatcher, whose value buffers have the batch size
of the iterator. RestrictedScanDocIdIterator accepts any chunk size, so a chunk larger than that batch size overran
the buffers. Match a larger chunk one batch at a time through a lazily allocated batch buffer, compacting the matching
document ids to the front of the chunk.

RestrictedScanDocIdIteratorTest#testChunkLargerThanTheScanBatchSize drives a 1041-document chunk through a
256-document SVScanDocIdIterator.
Add a STREAMING value to andRestrictionPushdownMode. Under STREAMING, an
AND evaluates its scans and its composite children (OR, AND, NOT) on the
candidate bitmap one chunk at a time, instead of materializing every step.
A selection with a LIMIT then reads only the chunks it needs, also under
row-level security, and the user filter still runs only on allowed rows.

- BlockDocIdSet#restrictedIterator(ImmutableRoaringBitmap) is the lazy
  counterpart of applyAnd. AndDocIdSet, OrDocIdSet and NotDocIdSet
  implement it; NOT uses the new RestrictedNotDocIdIterator.
- RestrictedScanDocIdIterator applies several scans per chunk. The chunk
  grows from 256 to MAX_DOC_PER_CALL, resets when advance() skips
  candidates, checks query termination per chunk and closes every scan at
  EOF.
- ScanBasedDocIdIterator#matchDocIds is abstract.
  ExpressionScanDocIdIterator implements it with one projection per chunk.
- AndRestrictionPushdown (OFF, EAGER, STREAMING plus the documents to read)
  replaces the boolean flag in the filter operators. An AND falls back to
  EAGER when LIMIT + OFFSET is not less than its candidate count. A null
  value means OFF, as the boolean it replaces defaulted to false.
- AUTO picks STREAMING for selection queries without ORDER BY, EAGER for
  aggregation and group-by, and OFF for everything else, DISTINCT
  included. Under RLS, NEVER resolves like AUTO and OFF becomes EAGER.
- DocIdSetOperator also reports the scan cost on the call that returns no
  document, because a lazy filter can scan the rest of the segment there.
- BenchmarkAndRestrictionPushdown measures the real AndDocIdSet in the
  three strategies.

A server that does not know STREAMING rejects the query option, so use it
only after every server is upgraded. Closing the scans when the consumer
stops early is left for a follow-up.
A streaming AND restriction keeps its scan readers open until the
candidate stream ends. A selection that fills its LIMIT never gets
there, so the readers stayed open until the segment plan was dropped.

- DocIdSetOperator implements AutoCloseable. close() closes the filter
  iterator when it has not reached EOF, and later nextBlock() calls
  return null.
- SelectionOnlyOperator closes its DocIdSetOperator once it has its
  rows. SelectionPlanNode passes it through the new
  ProjectPlanNode#createDocIdSetOperator() and
  ProjectPlanNode#run(DocIdSetOperator).
- AndDocIdIterator and OrDocIdIterator propagate close() to their
  children, so the close reaches a nested RestrictedScanDocIdIterator.
- BenchmarkAndRestrictionPushdown adds the SINGLE_SCAN shape (a bitmap
  AND one scan, the row-level security case) and runs INDEXED_OR.

The approach follows apache#20231.
@gortiz gortiz changed the title [SPIKE] Streaming restriction push-down (do not merge) Stream the AND restriction push-down for LIMIT queries Nov 13, 2026
- BenchmarkRowLevelSecurityLimitQueries runs single-stage queries under
  row-level security end to end on 4 segments of 1M rows: a scan, an
  expression scan, an OR, an OR with fewer matches than the LIMIT, and
  a COUNT as a control. It compares the eager push-down (ALWAYS) with
  the default (NEVER), which streams a selection under RLS, and prints
  numEntriesScannedInFilter for each mode.
- BenchmarkRestrictedScanChunkSize runs RestrictedScanDocIdIterator
  with first chunk sizes from 64 to MAX_DOC_PER_CALL.
- RestrictedScanDocIdIterator gets a constructor that takes the first
  chunk size. The other constructor keeps MIN_CHUNK_SIZE.
@gortiz
gortiz marked this pull request as ready for review November 13, 2026 14:52

@xiangfu0 xiangfu0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Risk: HIGH. This changes how filters are evaluated, and it changes the default for RLS selection queries, which carries the "user filter only sees allowed rows" invariant. It also changes reader-context lifecycle (early close) and adds a new andRestrictionPushdownMode value. Reviewed SHA: 41bfecd3e4ce6c3e5edf3ee96eb256335b805bdd. I read the full diff against merge-base 8d90c47a. Decision: COMMENT. I found no verified blocker, but a human owner needs to review this.

Validation

  • CI at this head: green except Integration Test Set 2. It failed during Maven dependency resolution (pulsar-client-all:5.0.1 and opentelemetry-api:1.68.0 are not in central), so that set never ran. It looks like infra, but it is still a gap.
  • Local, JDK 25: the PR's tests pass: AndDocIdSetPushdownTest (12), RestrictedScanDocIdIteratorTest (10), DocIdSetOperatorScanCostTest (3), AndRestrictionPushdownModeTest (13), AndRestrictionPushdownQueriesTest (7), AndRestrictionPushdownNullQueriesTest (6), RowLevelSecurityScanQueriesTest (146).
  • Extra differential test (local only, not pushed): 9,000 random AND/OR/NOT trees under an RLS-bitmap root, with EAGER and with STREAMING(k), read to the end or stopped early with close(). The documents always matched OFF. No scan ever looked at a document outside the RLS bitmap, and no scan ran after its close.
  • Earlier finding is fixed: the chunk-bigger-than-scan-batch finding from the earlier review is fixed at this head. SVScanDocIdIterator#matchDocIds now splits into batches, and testChunkLargerThanTheScanBatchSize passes.

Trade-off for the owner to accept. Under RLS with the default config, LIMIT queries that stop early are about 2x faster. Queries whose filter matches fewer rows than the LIMIT are about 12–25% slower than master. That matches the PR's own OR_FEW_MATCHES number. An adaptive fallback could limit that cost, for example materializing the rest eagerly after a few chunks that return little.

Base vs head latency (µs per query, median of 2 JVM forks × 9 rounds)

Setup: 2 × 1M-row segment, RLS tenant='T0' (10%), one mode per JVM. Base = 8d90c47a (eager under RLS). Head default = NEVER, which streams under RLS. Fork-to-fork noise is up to about ±15%.

query base head eager (ALWAYS) head default (streaming)
payload > 500 LIMIT 10 2600 2569 1329
(payload > 900 OR name='n7') LIMIT 10 2414 2410 1280
(id=3 OR name='needle') LIMIT 100 (20 rows) 1552 1632 1943
payload=7 AND name='n7' LIMIT 100 (4 rows) 1325 1335 1571
(payload=7 OR name='n7' OR id=3) LIMIT 1000 (796 rows) 2035 1858 2282
NOT(payload<999) AND name<>'n7' AND id>0 LIMIT 1000 3263 3156 2074
COUNT(*) … payload > 500 (eager in both) 2370 2280 2178

Row counts were identical across all modes. Head eager is within noise of base, so the existing eager path did not regress.

Human approval required; no agent approval submitted.

AI-generated review via IntentLab · Model: Claude Opus 5.5 (claude-opus-5-5).

xiangfu0 added a commit to xiangfu0/pinot that referenced this pull request Nov 19, 2026
Adapt the streaming kernel from Gonzalo Ortiz’s apache#19589 spike. Decide eligibility before segment pruning and stream only a supported predicate over policy-approved candidates, including when the policy matches all rows. Cap batches to the matcher capacity, reuse expression projection contexts, and close readers when selection finishes.

@xiangfu0 xiangfu0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed cef90d8989. The inline finding covers reader cleanup when the first pull fails.

[should-address] Update the strategy table and benchmark notes. Without RLS, AUTO leaves unordered selections off. Explicit STREAMING falls back to eager evaluation outside eligible finite unordered selections and on the streaming response path. With RLS, the compound OR and OR_FEW_MATCHES benchmark cases use eager evaluation in both modes. Please update the table and label the earlier benchmark numbers with the revision that produced them, or remove those numbers. Planner · Benchmark cases

@xiangfu0 xiangfu0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed the follow-up in db84f957b0 and verified the first-pull cleanup. The new regressions cover closing before the first read and closing both branches after an initial read failure while preserving that failure. All 31 focused JDK 25 tests and the four local quality checks passed. The PR description now reflects the current streaming eligibility and benchmark workloads.

@xiangfu0
xiangfu0 merged commit 38f0081 into apache:master Nov 25, 2026
18 of 19 checks passed
xiangfu0 added a commit to pinot-contrib/pinot-docs that referenced this pull request Nov 25, 2026
Updates the query options guide for the STREAMING mode, eager fallback,
row-level security behavior, and mixed-version restriction.

Validation: git diff --check and the docs validator passed (10 existing
anchor warnings).

Co-authored-by: Xiang Fu <xiangfu@Xiang-mac-mtv-2.local>
@xiangfu0

Copy link
Copy Markdown
Contributor

The query options guide now covers STREAMING eligibility, fallback behavior, and rolling-upgrade compatibility: pinot-contrib/pinot-docs#1219.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

index Related to indexing (general) performance Related to performance optimization query Related to query processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Predicates outside an OR do not restrict its branches, and adding an index there can cause a large regression

3 participants