Skip to content

Commit

Permalink
fix: Order by mentioning missing column multiple times (#13158)
Browse files Browse the repository at this point in the history
* fix: Order by mentioning missing column multiple times

Previously we crashed on queries where the order by mentioned a missing
columns multiple times.

Closes #13157

* Use HashSet to avoid quadratic algorithm

* Use IndexSet to maintain old order and be deterministic
  • Loading branch information
eejbyfeldt authored Oct 30, 2024
1 parent 7d34ccc commit 68bf7ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
18 changes: 10 additions & 8 deletions datafusion/expr/src/logical_plan/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ use datafusion_common::{
UnnestOptions,
};
use datafusion_expr_common::type_coercion::binary::type_union_resolution;
use indexmap::IndexSet;

/// Default table name for unnamed table
pub const UNNAMED_TABLE: &str = "?table?";
Expand Down Expand Up @@ -567,7 +568,7 @@ impl LogicalPlanBuilder {
/// See <https://github.com/apache/datafusion/issues/5065> for more details
fn add_missing_columns(
curr_plan: LogicalPlan,
missing_cols: &[Column],
missing_cols: &IndexSet<Column>,
is_distinct: bool,
) -> Result<LogicalPlan> {
match curr_plan {
Expand Down Expand Up @@ -612,7 +613,7 @@ impl LogicalPlanBuilder {

fn ambiguous_distinct_check(
missing_exprs: &[Expr],
missing_cols: &[Column],
missing_cols: &IndexSet<Column>,
projection_exprs: &[Expr],
) -> Result<()> {
if missing_exprs.is_empty() {
Expand Down Expand Up @@ -677,15 +678,16 @@ impl LogicalPlanBuilder {
let schema = self.plan.schema();

// Collect sort columns that are missing in the input plan's schema
let mut missing_cols: Vec<Column> = vec![];
let mut missing_cols: IndexSet<Column> = IndexSet::new();
sorts.iter().try_for_each::<_, Result<()>>(|sort| {
let columns = sort.expr.column_refs();

columns.into_iter().for_each(|c| {
if !schema.has_column(c) {
missing_cols.push(c.clone());
}
});
missing_cols.extend(
columns
.into_iter()
.filter(|c| !schema.has_column(c))
.cloned(),
);

Ok(())
})?;
Expand Down
8 changes: 8 additions & 0 deletions datafusion/sqllogictest/test_files/order.slt
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ select column1 from foo order by log(column2);
3
5

# Test issue: https://github.com/apache/datafusion/issues/13157
query I
select column1 from foo order by column2 % 2, column2;
----
1
3
5

# Cleanup
statement ok
drop table foo;
Expand Down

0 comments on commit 68bf7ad

Please sign in to comment.