@@ -53,16 +53,55 @@ use datafusion_common::{plan_err, Result};
5353use datafusion_expr:: { Expr , SortExpr } ;
5454use datafusion_physical_expr:: { expressions, LexOrdering , PhysicalSortExpr } ;
5555
56- fn create_ordering (
56+ /// Converts logical sort expressions to physical sort expressions
57+ ///
58+ /// This function transforms a collection of logical sort expressions into their physical
59+ /// representation that can be used during query execution.
60+ ///
61+ /// # Arguments
62+ ///
63+ /// * `schema` - The schema containing column definitions
64+ /// * `sort_order` - A collection of logical sort expressions grouped into lexicographic orderings
65+ ///
66+ /// # Returns
67+ ///
68+ /// A vector of lexicographic orderings for physical execution, or an error if the transformation fails
69+ ///
70+ /// # Examples
71+ ///
72+ /// ```
73+ /// // Create orderings from columns "id" and "name"
74+ /// # use arrow::datatypes::{Schema, Field, DataType};
75+ /// # use datafusion::datasource::create_ordering;
76+ /// # use datafusion_common::Column;
77+ /// # use datafusion_expr::{Expr, SortExpr};
78+ /// #
79+ /// // Create a schema with two fields
80+ /// let schema = Schema::new(vec![
81+ /// Field::new("id", DataType::Int32, false),
82+ /// Field::new("name", DataType::Utf8, false),
83+ /// ]);
84+ ///
85+ /// let sort_exprs = vec![
86+ /// vec![
87+ /// SortExpr { expr: Expr::Column(Column::new(Some("t"), "id")), asc: true, nulls_first: false }
88+ /// ],
89+ /// vec![
90+ /// SortExpr { expr: Expr::Column(Column::new(Some("t"), "name")), asc: false, nulls_first: true }
91+ /// ]
92+ /// ];
93+ /// let result = create_ordering(&schema, &sort_exprs).unwrap();
94+ /// ```
95+ pub fn create_ordering (
5796 schema : & Schema ,
5897 sort_order : & [ Vec < SortExpr > ] ,
5998) -> Result < Vec < LexOrdering > > {
6099 let mut all_sort_orders = vec ! [ ] ;
61100
62- for exprs in sort_order {
101+ for ( group_idx , exprs) in sort_order. iter ( ) . enumerate ( ) {
63102 // Construct PhysicalSortExpr objects from Expr objects:
64103 let mut sort_exprs = LexOrdering :: default ( ) ;
65- for sort in exprs {
104+ for ( expr_idx , sort) in exprs. iter ( ) . enumerate ( ) {
66105 match & sort. expr {
67106 Expr :: Column ( col) => match expressions:: col ( & col. name , schema) {
68107 Ok ( expr) => {
@@ -80,8 +119,11 @@ fn create_ordering(
80119 } ,
81120 expr => {
82121 return plan_err ! (
83- "Expected single column references in output_ordering, got {expr}"
84- )
122+ "Expected single column reference in sort_order[{}][{}], got {}" ,
123+ group_idx,
124+ expr_idx,
125+ expr
126+ ) ;
85127 }
86128 }
87129 }
0 commit comments