Skip to content

Commit 8ba7fa7

Browse files
committed
add docs
1 parent b6e36db commit 8ba7fa7

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
@@ -2683,6 +2683,55 @@ impl TableScan {
26832683
})
26842684
}
26852685

2686+
/// Sets the preferred ordering for this table scan using the builder pattern.
2687+
///
2688+
/// The preferred ordering serves as a hint to table providers about the desired
2689+
/// sort order for the data. Table providers can use this information to optimize
2690+
/// data access patterns, choose appropriate indexes, or leverage existing sort
2691+
/// orders in the underlying storage.
2692+
///
2693+
/// # Parameters
2694+
///
2695+
/// * `preferred_ordering` - An optional vector of sort expressions representing
2696+
/// the desired ordering. `None` indicates no specific ordering preference.
2697+
///
2698+
/// # Returns
2699+
///
2700+
/// Returns `self` to enable method chaining in the builder pattern.
2701+
///
2702+
/// # Examples
2703+
///
2704+
/// ```rust
2705+
/// use datafusion_expr::{col, SortExpr};
2706+
/// # use datafusion_expr::logical_plan::TableScan;
2707+
/// # use std::sync::Arc;
2708+
/// # use datafusion_common::TableReference;
2709+
///
2710+
/// // Create a table scan with preferred ordering by column 'a' ascending
2711+
/// # let table_name = TableReference::bare("test");
2712+
/// # let source = Arc::new(datafusion_expr::test::table_source(vec![]));
2713+
/// # let projection = None;
2714+
/// # let projected_schema = Arc::new(datafusion_common::DFSchema::empty());
2715+
/// # let filters = vec![];
2716+
/// # let fetch = None;
2717+
/// let table_scan = TableScan {
2718+
/// table_name,
2719+
/// source,
2720+
/// projection,
2721+
/// projected_schema,
2722+
/// filters,
2723+
/// fetch,
2724+
/// preferred_ordering: None,
2725+
/// }.with_preferred_ordering(Some(vec![
2726+
/// SortExpr::new(col("a"), true, false) // ASC NULLS LAST
2727+
/// ]));
2728+
/// ```
2729+
///
2730+
/// # Notes
2731+
///
2732+
/// This is purely an optimization hint. The table provider may choose to ignore
2733+
/// the preferred ordering if it cannot be efficiently satisfied, and the query
2734+
/// execution engine should not rely on the data being returned in this order.
26862735
pub fn with_preferred_ordering(
26872736
mut self,
26882737
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)