Skip to content

Commit dd0fd88

Browse files
xudong963alamb
andauthored
Make create_ordering pub and add doc for it (#14996)
* Make create_ordering public and add notes * fix test * fix * Update datafusion/core/src/datasource/mod.rs Co-authored-by: Andrew Lamb <[email protected]> --------- Co-authored-by: Andrew Lamb <[email protected]>
1 parent 6c95b56 commit dd0fd88

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

datafusion/core/src/datasource/listing/table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ mod tests {
13061306
vec![vec![
13071307
col("int_col").add(lit(1)).sort(true, true),
13081308
]],
1309-
Err("Expected single column references in output_ordering, got int_col + Int32(1)"),
1309+
Err("Expected single column reference in sort_order[0][0], got int_col + Int32(1)"),
13101310
),
13111311
// ok with one column
13121312
(

datafusion/core/src/datasource/mod.rs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,55 @@ use datafusion_common::{plan_err, Result};
5353
use datafusion_expr::{Expr, SortExpr};
5454
use 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

Comments
 (0)