Skip to content

Commit 2589fa8

Browse files
authored
doc: Add documentation for pushing limit into plan (#20271)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Besides pushing `LimitExec` down the query plan, there is another optimization that allows plan nodes to *absorb* a limit, so it can potentially stop early. I’ve noticed that this form of limit absorption has not been implemented by many operators. This suggests the optimization is non-obvious, so I’d like to improve the documentation for it. A recent PR that implements this optimization is: - #20228 ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 86cb815 commit 2589fa8

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

datafusion/physical-optimizer/src/limit_pushdown.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,47 @@
1717

1818
//! [`LimitPushdown`] pushes `LIMIT` down through `ExecutionPlan`s to reduce
1919
//! data transfer as much as possible.
20+
//!
21+
//! # Plan Limit Absorption
22+
//! In addition to pushing down [`LimitExec`] in the plan, some operators can
23+
//! "absorb" a limit and stop early during execution.
24+
//!
25+
//! ## Background: vectorized volcano execution model
26+
//! DataFusion uses a batched volcano model. For most operators, output is
27+
//! produced in batches of `datafusion.execution.batch_size` (default 8192), so
28+
//! the batch sizes typically look like:
29+
//! ```text
30+
//! 8192, 8192, ..., 8192, 100 (the final batch may be partial)
31+
//! ```
32+
//!
33+
//! ## Example
34+
//! For a join with an expensive, selective predicate:
35+
//! ```text
36+
//! LimitExec(fetch=10)
37+
//! -- NestedLoopJoinExec(on=expr_expensive_and_selective)
38+
//! --- DataSourceExec()
39+
//! --- DataSourceExec()
40+
//! ```
41+
//!
42+
//! Under this model, `NestedLoopJoinExec` would keep working until it can emit
43+
//! a full batch (8192 rows), even though the query only needs 10. If the limit
44+
//! cannot be pushed below the join, we can still embed it inside the join so it
45+
//! stops once the limit is satisfied. The transformed plan looks like:
46+
//!
47+
//! ```text
48+
//! NestedLoopJoinExec(on=expr_expensive_and_selective, fetch=10)
49+
//! --- DataSourceExec()
50+
//! --- DataSourceExec()
51+
//! ```
52+
//!
53+
//! ## Implementation
54+
//! The current optimizer rule optionally pushes `fetch` requirements into
55+
//! operators via [`ExecutionPlan::with_fetch`].
56+
//!
57+
//! To support early termination in operators, [`LimitedBatchCoalescer`](https://docs.rs/datafusion/latest/datafusion/physical_plan/coalesce/struct.LimitedBatchCoalescer.html)
58+
//! can help manage the output buffer.
59+
//!
60+
//! Reference implementation in Hash Join: <https://github.com/apache/datafusion/pull/20228>
2061
2162
use std::fmt::Debug;
2263
use std::sync::Arc;

datafusion/physical-plan/src/execution_plan.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,10 @@ pub trait ExecutionPlan: Debug + DisplayAs + Send + Sync {
579579

580580
/// Returns a fetching variant of this `ExecutionPlan` node, if it supports
581581
/// fetch limits. Returns `None` otherwise.
582+
///
583+
/// See physical optimizer rule [`limit_pushdown`] for details.
584+
///
585+
/// [`limit_pushdown`]: https://docs.rs/datafusion/latest/datafusion/physical_optimizer/limit_pushdown/index.html
582586
fn with_fetch(&self, _limit: Option<usize>) -> Option<Arc<dyn ExecutionPlan>> {
583587
None
584588
}

0 commit comments

Comments
 (0)