codewithsruthi opened a new pull request, #20406:
URL: https://github.com/apache/pinot/pull/20406
Fixes #20083
### Summary
`TextScoreTransformFunction` previously materialized a segment-wide
`Int2FloatOpenHashMap` for every matching document on the first block,
regardless of filter selectivity, `LIMIT k`, Phase-2 `ORDER BY ... LIMIT k`,
`ExpressionFilterOperator`, or MSE leaf filters.
This PR introduces adaptive block-level scoring via
`TextIndexReader.DocScorer` so selective queries only score the requested block
docIds instead of allocating and scoring the entire segment posting list.
### Changes
- **`pinot-segment-spi` (SPI addition — 100% source & binary backward
compatible):**
- Added `@FunctionalInterface TextIndexReader.DocScorer` (`void
scoreDocs(int[] docIds, int length, double[] scores)`) with single-threaded,
zero-closeable-resource lifecycle semantics (safe with `TransformFunction`
having no `close()`).
- Added `default DocScorer createDocScorer(String, @Nullable String,
@Nullable TextScoreStats)` on `TextIndexReader` and column-aware `default
createDocScorer(String, String, @Nullable String, @Nullable TextScoreStats)` on
`MultiColumnTextIndexReader` delegating to existing `getDocScores(...)`
methods. All existing `getDocScores(...)` methods remain intact for external
plugins.
- **`pinot-segment-local`:**
- Added package-private `LuceneDocScorer` shared by
`LuceneTextIndexReader` and `MultiColumnLuceneTextIndexReader`:
- **Identity mapping (`NoOpDocIdTranslator`):** Streams Lucene `Scorer`
/ `TwoPhaseIterator` across leaves with zero score-map heap, handles multi-leaf
`docBase`, `liveDocs`, duplicates, out-of-range docIds, descending Block 1
(`ReverseDocIdSetOperator`), and falls back to eager collection on out-of-order
Block 2+.
- **Non-identity mapping (`DefaultDocIdTranslator`):** Uses filtered
`LuceneDocScoreCollector` (`estimatedMatches <= maxDoc / 4`) or reverse buffer
scan + `Scorer.advance()` (`estimatedMatches > maxDoc / 4`) on selective Block
1, and eager map collection on small match sets (`<= 1024`), full blocks, or
Block 2+. Fixed 64-bit `(long) luceneDocId * Integer.BYTES` buffer offset math.
- Added optional `@Nullable IntPredicate` filtering +
`allowedDocIdsPredicate(...)` helper to `LuceneDocScoreCollector`.
- Overrode `createDocScorer(...)` on `RealtimeLuceneTextIndex` and
`MultiColumnRealtimeLuceneTextIndex` to run filtered collection inside a single
`SEARCHER_POOL` `acquire()`/`release()` for selective Block 1 (`length <
10_000`, copying non-contiguous `docIds` to avoid `THREAD_LOCAL_DOC_IDS`
aliasing on cancellation), falling back to cached eager `getDocScores(...)` on
full blocks or Block 2+.
- **`pinot-core`:**
- Updated `TextScoreTransformFunction` to lazily initialize and invoke
`TextIndexReader.DocScorer` per block.
### Benchmark (500,000-Doc Segment, `ThreadMXBean.getThreadAllocatedBytes`)
Corpus: 500k docs (`'usb'` matches 50%, `'"usb charger"'` matches 25% with
`TwoPhaseIterator` false positives on 12.5%, `'mediumterm'` matches 12.5%,
`'usb OR charger OR cable'` matches 75%). Exact score parity verified on every
scenario.
**Identity (`NoOpDocIdTranslator`):**
| Scenario | Eager ms | Lazy ms | Speedup | Eager Alloc | Lazy Alloc | Alloc
% |
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
| `'usb'` + narrow filter (10 rows) | 14.13 ms | 0.62 ms | 22.8x | 8.03 MB |
19.5 KB | 0.24% |
| `'usb'` + narrow filter (100 rows) | 13.25 ms | 0.45 ms | 29.4x | 8.03 MB
| 19.5 KB | 0.24% |
| `'usb'` + narrow filter (1,000 rows) | 12.99 ms | 0.58 ms | 22.5x | 8.03
MB | 19.5 KB | 0.24% |
| `'usb'` + unfiltered `LIMIT 10` | 13.47 ms | 0.40 ms | 33.6x | 8.03 MB |
19.5 KB | 0.24% |
| `'usb'` + descending `LIMIT 10` (`ReverseDocIdSetOperator`) | 13.02 ms |
0.28 ms | 46.0x | 8.03 MB | 19.5 KB | 0.24% |
| `'"usb charger"'` phrase (`TwoPhaseIterator`) + 100 rows | 14.47 ms | 0.64
ms | 22.5x | 4.03 MB | 33.7 KB | 0.81% |
| `'mediumterm'` (12.5%) + filter (100 rows) | 3.57 ms | 0.20 ms | 18.1x |
2.03 MB | 19.6 KB | 0.95% |
| `'usb'` + unfiltered full scan (500k) | 16.93 ms | 6.24 ms | 2.71x | 8.03
MB | 19.5 KB | 0.24% |
| `'usb OR charger OR cable'` + full scan (500k) | 28.62 ms | 21.48 ms |
1.33x | 8.17 MB | 50.3 KB | 0.60% |
**Non-Identity (`DefaultDocIdTranslator`, scrambled bijection `(luceneDoc *
37 + 13) % 500000`):**
| Scenario | Eager ms | Lazy ms | Speedup | Eager Alloc | Lazy Alloc | Alloc
% |
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
| `'usb'` + narrow filter (10 rows) | 15.50 ms | 3.11 ms | 4.99x | 8.03 MB |
19.7 KB | 0.24% |
| `'usb'` + narrow filter (100 rows) | 15.53 ms | 5.91 ms | 2.63x | 8.03 MB
| 20.4 KB | 0.25% |
| `'usb'` + narrow filter (1,000 rows) | 15.68 ms | 10.39 ms | 1.51x | 8.03
MB | 27.4 KB | 0.33% |
| `'usb'` + unfiltered `LIMIT 10` | 15.67 ms | 1.10 ms | 14.26x | 8.03 MB |
19.7 KB | 0.24% |
| `'usb'` + descending `LIMIT 10` (`ReverseDocIdSetOperator`) | 15.28 ms |
1.28 ms | 11.95x | 8.03 MB | 19.8 KB | 0.24% |
| `'"usb charger"'` phrase (`TwoPhaseIterator`) + 100 rows | 14.83 ms | 4.53
ms | 3.27x | 4.03 MB | 34.5 KB | 0.84% |
| `'mediumterm'` (12.5%) + filter (100 rows) | 3.91 ms | 2.63 ms | 1.49x |
2.03 MB | 32.9 KB | 1.59% |
| `'usb'` + unfiltered full scan (500k) | 20.14 ms | 21.28 ms | 0.95x | 8.03
MB | 8.03 MB | 100.09% |
| `'usb OR charger OR cable'` + full scan (500k) | 32.57 ms | 33.83 ms |
0.96x | 8.17 MB | 8.20 MB | 100.28% |
### Verification
- Unit & query tests (53/53 passed): `TextIndexReaderScoringDefaultsTest`,
`LuceneDocScoreCollectorTest`, `LuceneDocScorerTest`,
`LuceneMutableTextIndexTest`, `TextScoreQueriesTest`,
`TextScoreGlobalStatsQueriesTest`, `TextScoreConsumingSegmentQueriesTest`
- Integration test (8/8 passed): `HybridSearchTest`
- Pre-commit checks (`spotless:apply`, `license:check`, `checkstyle:check`,
compiler warnings on added lines): clean across `pinot-segment-spi`,
`pinot-segment-local`, `pinot-core`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]