@@ -53,16 +53,55 @@ use datafusion_common::{plan_err, Result};
53
53
use datafusion_expr:: { Expr , SortExpr } ;
54
54
use datafusion_physical_expr:: { expressions, LexOrdering , PhysicalSortExpr } ;
55
55
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 (
57
96
schema : & Schema ,
58
97
sort_order : & [ Vec < SortExpr > ] ,
59
98
) -> Result < Vec < LexOrdering > > {
60
99
let mut all_sort_orders = vec ! [ ] ;
61
100
62
- for exprs in sort_order {
101
+ for ( group_idx , exprs) in sort_order. iter ( ) . enumerate ( ) {
63
102
// Construct PhysicalSortExpr objects from Expr objects:
64
103
let mut sort_exprs = LexOrdering :: default ( ) ;
65
- for sort in exprs {
104
+ for ( expr_idx , sort) in exprs. iter ( ) . enumerate ( ) {
66
105
match & sort. expr {
67
106
Expr :: Column ( col) => match expressions:: col ( & col. name , schema) {
68
107
Ok ( expr) => {
@@ -80,8 +119,11 @@ fn create_ordering(
80
119
} ,
81
120
expr => {
82
121
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
+ ) ;
85
127
}
86
128
}
87
129
}
0 commit comments