Late Materialization (Query-Then-Fetch) for pure Sort queries - #21836
Conversation
…ge skeleton Squash of: - Initial planner changes for Late Materialization a.k.a. QueryThenFetch for Sort+Limit - Refactored tests and LateMaterializationRewriter - Added Wiring for LateMaterializationScheduler Signed-off-by: expani <anijainc@amazon.com>
Squash of: - Take rowIdField from DocumentInput - Add global id fetch logic from the data node for QTF for query phase - Add basic constructs for context management for QTF - add wiring and assertions in the code - Add fixes for tests in qtf Signed-off-by: Arpit Bandejiya <abandeji@amazon.com>
Squash of: - More rebase changes and fix compilation issues in Rust bench and tests even in mainline - Spotless and JavaDocs post rebase - QTF partial changes for Scheduler integration - Scheduler integration - E2E QueryThenFetch integrated and verified working with a QA module integ test - Fixed leaks during Coordinator Reduce Signed-off-by: expani <anijainc@amazon.com>
Signed-off-by: Arpit Bandejiya <abandeji@amazon.com>
Signed-off-by: expani <anijainc@amazon.com>
PR Code Analyzer ❗AI-powered 'Code-Diff-Analyzer' found issues on commit 5c0ccdb. 'Diff too large, requires skip by maintainers after manual review' Pull Requests Author(s): Please update your Pull Request according to the report above. Repository Maintainer(s): You can Thanks. |
…d handled multi shards in datanode readerctx Signed-off-by: expani <anijainc@amazon.com>
Signed-off-by: expani <anijainc@amazon.com>
Signed-off-by: expani <anijainc@amazon.com>
Signed-off-by: Arpit Bandejiya <abandeji@amazon.com>
Signed-off-by: expani <anijainc@amazon.com>
Signed-off-by: expani <anijainc@amazon.com>
Signed-off-by: expani <anijainc@amazon.com>
Signed-off-by: expani <anijainc@amazon.com>
mch2
left a comment
There was a problem hiding this comment.
this is a huge feature thanks @expani and @alchemist51 for taking this on
Signed-off-by: expani <anijainc@amazon.com>
…debugging Signed-off-by: expani <anijainc@amazon.com>
Signed-off-by: expani <anijainc@amazon.com>
PR Reviewer Guide 🔍(Review updated until commit d28655a)Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Latest suggestions up to d28655a Explore these optional code suggestions:
Previous suggestionsSuggestions up to commit d28655a
Suggestions up to commit b6c1cc1
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #21836 +/- ##
============================================
+ Coverage 73.37% 73.49% +0.12%
- Complexity 75448 75555 +107
============================================
Files 6034 6033 -1
Lines 342504 342572 +68
Branches 49259 49276 +17
============================================
+ Hits 251310 251776 +466
+ Misses 71175 70789 -386
+ Partials 20019 20007 -12 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Signed-off-by: expani <anijainc@amazon.com>
|
Persistent review updated to latest commit b6c1cc1 |
Signed-off-by: expani <anijainc@amazon.com>
|
Persistent review updated to latest commit d28655a |
|
❌ Gradle check result for d28655a: FAILURE Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change? |
|
Persistent review updated to latest commit d28655a |
…arch-project#21836) * QTF planner foundation: late-materialization rewriter + scheduler stage skeleton Squash of: - Initial planner changes for Late Materialization a.k.a. QueryThenFetch for Sort+Limit - Refactored tests and LateMaterializationRewriter - Added Wiring for LateMaterializationScheduler Signed-off-by: expani <anijainc@amazon.com> * QTF data-node: row-id fetch, global-id query phase, context management Squash of: - Take rowIdField from DocumentInput - Add global id fetch logic from the data node for QTF for query phase - Add basic constructs for context management for QTF - add wiring and assertions in the code - Add fixes for tests in qtf Signed-off-by: Arpit Bandejiya <abandeji@amazon.com> * QTF coordinator integration: scheduler wiring, leak fixes, E2E IT Squash of: - More rebase changes and fix compilation issues in Rust bench and tests even in mainline - Spotless and JavaDocs post rebase - QTF partial changes for Scheduler integration - Scheduler integration - E2E QueryThenFetch integrated and verified working with a QA module integ test - Fixed leaks during Coordinator Reduce Signed-off-by: expani <anijainc@amazon.com> * Make it working Signed-off-by: Arpit Bandejiya <abandeji@amazon.com> * Spotless and LoggerUsage checks Signed-off-by: expani <anijainc@amazon.com> * Fixed a bug in Rewritter for properly mapping above sort operators and handled multi shards in datanode readerctx Signed-off-by: expani <anijainc@amazon.com> * Made fetchByRowIds async Signed-off-by: expani <anijainc@amazon.com> * Refactored to remove duplicate code Signed-off-by: expani <anijainc@amazon.com> * Add imports correctly Signed-off-by: Arpit Bandejiya <abandeji@amazon.com> * Test failures and error handling Signed-off-by: expani <anijainc@amazon.com> * Reverted the disabling of infer schema caused by PR-21826 Signed-off-by: expani <anijainc@amazon.com> * Fix QtfSubstraitDumpIT after merge Signed-off-by: expani <anijainc@amazon.com> * Fixed a bug with double closing the sink on empty shard results Signed-off-by: expani <anijainc@amazon.com> * Fixed thread safety to use explicit lock AND removed loggers used in debugging Signed-off-by: expani <anijainc@amazon.com> * Flipping loggers to ERROR for debugging in CI as not reproable locally Signed-off-by: expani <anijainc@amazon.com> * Fixed a NASTY bug Signed-off-by: expani <anijainc@amazon.com> --------- Signed-off-by: expani <anijainc@amazon.com> Signed-off-by: Arpit Bandejiya <abandeji@amazon.com> Co-authored-by: Arpit Bandejiya <abandeji@amazon.com>
Late Materialization (Query-Then-Fetch) for top-N queries
Implements QTF for
Sort + Limitqueries: shards return only the sort columns plus a row-id helper, the coordinator picks the top-K rows globally, then fetches the remaining columns from the originating shards by row id and stitches them back into sort order.Why
For a query like "select a few columns from a large index, ordered by one column, limit K", the existing path materializes every projected column on every matching row cross every shard before the global sort. With QTF, shards only ship the sort columns and row ids, the coordinator picks K winners, and only those K rows drive a second per-shard fetch for the remaining columns. This drops shard-to-coordinator network I/O and avoids per-row materialization on rows that were never going to win.
DAG shape
Four stages (three if the cluster has a single shard, where the rewriter short-circuits):
Stage 0: shard scan (returns sort columns + row id, per-shard ordinal stamped at the boundary)
Stage 1: coord reduce (Sort + Limit picks K winners)
Stage 2: late materialization (drain → group rows by shard → scatter fetch → stitch back into order)
Stage 3: coord reduce (post-LM derived projection / filter / aggregate)
Coordinator
Planner
Scheduler / Core
interface.
DataFusion backend (coordinator-relevant pieces)
Data node
Core
DataFusion backend (Java)
DataFusion backend (Rust)
#21653
Row-id emission infrastructure:
Core-side opt-in:
Cross-cutting (Framework SPI)
node for in the second-pass fetch).
Tests
UPPER(url)) over the stitched output, with per-row valueassertions.
Known limitations / follow-ups
Streaming the post-LM stage (per-shard sub-batch emission, dropping the position-sortedness invariant) is deferred until a real workload demands it.