@@ -2683,6 +2683,55 @@ impl TableScan {
2683
2683
} )
2684
2684
}
2685
2685
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.
2686
2735
pub fn with_preferred_ordering (
2687
2736
mut self ,
2688
2737
preferred_ordering : Option < Vec < SortExpr > > ,
0 commit comments