Skip to content

Apache DataFusion SQL Query Engine

#17392 Avoid panic when 'with order' expression could not be converted to a logical expression (#17394)

Pepijn Van Eeckhoudt002b021952b5
diff --git a/datafusion/sql/src/statement.rs b/datafusion/sql/src/statement.rs
index 4d812169d0..011b4f4f1c 100644
--- a/datafusion/sql/src/statement.rs
+++ b/datafusion/sql/src/statement.rs
@@ -1445,25 +1445,23 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
.map(|order_by_expr| {
let ordered_expr = &order_by_expr.expr;
let ordered_expr = ordered_expr.to_owned();
- let ordered_expr = self
- .sql_expr_to_logical_expr(
- ordered_expr,
- schema,
- planner_context,
- )
- .unwrap();
+ let ordered_expr = self.sql_expr_to_logical_expr(
+ ordered_expr,
+ schema,
+ planner_context,
+ )?;
let asc = order_by_expr.options.asc.unwrap_or(true);
let nulls_first =
order_by_expr.options.nulls_first.unwrap_or_else(|| {
self.options.default_null_ordering.nulls_first(asc)
});
- SortExpr::new(ordered_expr, asc, nulls_first)
+ Ok(SortExpr::new(ordered_expr, asc, nulls_first))
})
- .collect::<Vec<SortExpr>>();
- result
+ .collect::<Result<Vec<SortExpr>>>()?;
+ Ok(result)
})
- .collect::<Vec<Vec<SortExpr>>>();
+ .collect::<Result<Vec<Vec<SortExpr>>>>()?;
return Ok(results);
}
diff --git a/datafusion/sqllogictest/test_files/order.slt b/datafusion/sqllogictest/test_files/order.slt
index c5674d6998..1050b59613 100644
--- a/datafusion/sqllogictest/test_files/order.slt
+++ b/datafusion/sqllogictest/test_files/order.slt
@@ -766,6 +766,13 @@ physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/
query error DataFusion error: Error during planning: Column a is not in schema
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';
+
+# Create external table with order column expression that can't be planned
+# This is currently expected to fail, but should not panic
+query error DataFusion error: Schema error: No field named a\.
+CREATE EXTERNAL TABLE dt STORED AS CSV WITH ORDER (a || b) LOCATION 'file://path/to/table';
+
+
# Sort with duplicate sort expressions
# Table is sorted multiple times on the same column name and should not fail
statement ok