Skip to content

Commit

Permalink
fix UNION ALL bug: thread 'main' panicked at 'index out of bounds: th…
Browse files Browse the repository at this point in the history
…e len is 1 but the index is 1', ./src/datatypes/schema.rs:165:10
  • Loading branch information
xudong963 committed Oct 8, 2021
1 parent 396cb79 commit 66dc838
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
12 changes: 9 additions & 3 deletions datafusion/src/optimizer/projection_push_down.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ fn optimize_plan(
.filter(|f| new_required_columns.contains(&f.qualified_column()))
.map(|f| f.field())
.collect::<HashSet<&Field>>();

let new_inputs = inputs
.iter()
.map(|input_plan| {
Expand All @@ -416,10 +415,17 @@ fn optimize_plan(
)
})
.collect::<Result<Vec<_>>>()?;

let new_schema = DFSchema::new(
schema
.fields()
.iter()
.filter(|f| union_required_fields.contains(f.field()))
.cloned()
.collect(),
)?;
Ok(LogicalPlan::Union {
inputs: new_inputs,
schema: schema.clone(),
schema: Arc::new(new_schema),
alias: alias.clone(),
})
}
Expand Down
11 changes: 11 additions & 0 deletions datafusion/tests/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5076,3 +5076,14 @@ async fn union_distinct() -> Result<()> {
assert_eq!(expected, actual);
Ok(())
}

#[tokio::test]
async fn union_all_with_aggregate() -> Result<()> {
let mut ctx = ExecutionContext::new();
let sql =
"SELECT SUM(d) FROM (SELECT 1 as c, 2 as d UNION ALL SELECT 1 as c, 3 AS d) as a";
let actual = execute(&mut ctx, sql).await;
let expected = vec![vec!["5"]];
assert_eq!(expected, actual);
Ok(())
}

0 comments on commit 66dc838

Please sign in to comment.