Skip to content

Commit a6351ca

Browse files
committed
fix: gracefully skip SortExec when sort columns are not in the output schema
When a query like `SELECT name FROM t ORDER BY id` is federated, the sort column (`id`) is not in the plan's output schema (`[name]`). `create_physical_sort_expr` fails because it cannot resolve `id` against the output schema. Previously this returned an error, breaking such queries. Now we gracefully skip adding the local SortExec and rely on the remote database to handle the sorting via the generated SQL.
1 parent bda2122 commit a6351ca

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

  • datafusion-federation/src/sql

datafusion-federation/src/sql/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ impl FederationPlanner for SQLFederationPlanner {
195195
.collect::<Result<Vec<_>>>()
196196
{
197197
Ok(physical_sort_exprs) if physical_sort_exprs.is_empty() => {
198-
return Err(DataFusionError::Plan("Top-level Sort was detected, but no physical sort expressions could be created".to_string()));
198+
// No sort expressions could be created — skip the local
199+
// SortExec and rely on the remote database for ordering.
199200
}
200201
Ok(physical_sort_exprs) => {
201202
if let Some(lex_ordering) = LexOrdering::new(physical_sort_exprs) {
@@ -204,11 +205,11 @@ impl FederationPlanner for SQLFederationPlanner {
204205
));
205206
}
206207
}
207-
Err(e) => {
208-
return Err(DataFusionError::Context(
209-
"Failed to create `PhysicalSortExpr`".to_string(),
210-
Box::new(e),
211-
))
208+
Err(_) => {
209+
// Sort columns are not in the output schema (e.g.
210+
// `SELECT name FROM t ORDER BY id`). The remote database
211+
// will handle sorting via the generated SQL; skip the
212+
// local SortExec.
212213
}
213214
};
214215
}

0 commit comments

Comments
 (0)