Skip to content

Commit 9930c31

Browse files
committed
add docs
1 parent dbaa678 commit 9930c31

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

datafusion/expr/src/logical_plan/plan.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,6 +2656,55 @@ impl TableScan {
26562656
})
26572657
}
26582658

2659+
/// Sets the preferred ordering for this table scan using the builder pattern.
2660+
///
2661+
/// The preferred ordering serves as a hint to table providers about the desired
2662+
/// sort order for the data. Table providers can use this information to optimize
2663+
/// data access patterns, choose appropriate indexes, or leverage existing sort
2664+
/// orders in the underlying storage.
2665+
///
2666+
/// # Parameters
2667+
///
2668+
/// * `preferred_ordering` - An optional vector of sort expressions representing
2669+
/// the desired ordering. `None` indicates no specific ordering preference.
2670+
///
2671+
/// # Returns
2672+
///
2673+
/// Returns `self` to enable method chaining in the builder pattern.
2674+
///
2675+
/// # Examples
2676+
///
2677+
/// ```rust
2678+
/// use datafusion_expr::{col, SortExpr};
2679+
/// # use datafusion_expr::logical_plan::TableScan;
2680+
/// # use std::sync::Arc;
2681+
/// # use datafusion_common::TableReference;
2682+
///
2683+
/// // Create a table scan with preferred ordering by column 'a' ascending
2684+
/// # let table_name = TableReference::bare("test");
2685+
/// # let source = Arc::new(datafusion_expr::test::table_source(vec![]));
2686+
/// # let projection = None;
2687+
/// # let projected_schema = Arc::new(datafusion_common::DFSchema::empty());
2688+
/// # let filters = vec![];
2689+
/// # let fetch = None;
2690+
/// let table_scan = TableScan {
2691+
/// table_name,
2692+
/// source,
2693+
/// projection,
2694+
/// projected_schema,
2695+
/// filters,
2696+
/// fetch,
2697+
/// preferred_ordering: None,
2698+
/// }.with_preferred_ordering(Some(vec![
2699+
/// SortExpr::new(col("a"), true, false) // ASC NULLS LAST
2700+
/// ]));
2701+
/// ```
2702+
///
2703+
/// # Notes
2704+
///
2705+
/// This is purely an optimization hint. The table provider may choose to ignore
2706+
/// the preferred ordering if it cannot be efficiently satisfied, and the query
2707+
/// execution engine should not rely on the data being returned in this order.
26592708
pub fn with_preferred_ordering(
26602709
mut self,
26612710
preferred_ordering: Option<Vec<SortExpr>>,

datafusion/optimizer/src/push_down_sort.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,47 @@ use datafusion_expr::{Expr, SortExpr};
3434
/// This rule looks for `Sort -> TableScan` patterns and moves the sort
3535
/// expressions into the `TableScan.preferred_ordering` field, allowing
3636
/// table providers to potentially optimize the scan based on sort requirements.
37+
///
38+
/// # Behavior
39+
///
40+
/// The optimizer preserves the original `Sort` node as a fallback while passing
41+
/// the ordering preference to the `TableScan` as an optimization hint. This ensures
42+
/// correctness even if the table provider cannot satisfy the requested ordering.
43+
///
44+
/// # Supported Sort Expressions
45+
///
46+
/// Currently, only simple column references are supported for pushdown because
47+
/// table providers typically cannot optimize complex expressions in sort operations.
48+
/// Complex expressions like `col("a") + col("b")` or function calls are not pushed down.
49+
///
50+
/// # Examples
51+
///
52+
/// ```text
53+
/// Before optimization:
54+
/// Sort: test.a ASC NULLS LAST
55+
/// TableScan: test
56+
///
57+
/// After optimization:
58+
/// Sort: test.a ASC NULLS LAST -- Preserved as fallback
59+
/// TableScan: test -- Now includes preferred_ordering hint
60+
/// ```
3761
#[derive(Default, Debug)]
3862
pub struct PushDownSort {}
3963

4064
impl PushDownSort {
41-
#[allow(missing_docs)]
65+
/// Creates a new instance of the `PushDownSort` optimizer rule.
66+
///
67+
/// # Returns
68+
///
69+
/// A new `PushDownSort` optimizer rule that can be added to the optimization pipeline.
70+
///
71+
/// # Examples
72+
///
73+
/// ```rust
74+
/// use datafusion_optimizer::push_down_sort::PushDownSort;
75+
///
76+
/// let rule = PushDownSort::new();
77+
/// ```
4278
pub fn new() -> Self {
4379
Self {}
4480
}

0 commit comments

Comments
 (0)