Skip to content

Commit 81052a0

Browse files
committed
#17392 Avoid panic when 'with order' expression could not be converted to a logical expression
1 parent 338a96e commit 81052a0

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

datafusion/sql/src/statement.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,20 +1450,19 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
14501450
ordered_expr,
14511451
schema,
14521452
planner_context,
1453-
)
1454-
.unwrap();
1453+
)?;
14551454
let asc = order_by_expr.options.asc.unwrap_or(true);
14561455
let nulls_first =
14571456
order_by_expr.options.nulls_first.unwrap_or_else(|| {
14581457
self.options.default_null_ordering.nulls_first(asc)
14591458
});
14601459

1461-
SortExpr::new(ordered_expr, asc, nulls_first)
1460+
Ok(SortExpr::new(ordered_expr, asc, nulls_first))
14621461
})
1463-
.collect::<Vec<SortExpr>>();
1464-
result
1462+
.collect::<Result<Vec<SortExpr>>>()?;
1463+
Ok(result)
14651464
})
1466-
.collect::<Vec<Vec<SortExpr>>>();
1465+
.collect::<Result<Vec<Vec<SortExpr>>>>()?;
14671466

14681467
return Ok(results);
14691468
}

datafusion/sqllogictest/test_files/order.slt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,13 @@ physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/
766766
query error DataFusion error: Error during planning: Column a is not in schema
767767
CREATE EXTERNAL TABLE dt (a_id integer, a_str string, a_bool boolean) STORED AS CSV WITH ORDER (a ASC) LOCATION 'file://path/to/table';
768768

769+
770+
# Create external table with order column expression that can't be planned
771+
# This is currently expected to fail, but should not panic
772+
query error DataFusion error: Schema error: No field named a\.
773+
CREATE EXTERNAL TABLE dt STORED AS CSV WITH ORDER (a || b) LOCATION 'file://path/to/table';
774+
775+
769776
# Sort with duplicate sort expressions
770777
# Table is sorted multiple times on the same column name and should not fail
771778
statement ok

0 commit comments

Comments
 (0)