Skip to content

Commit

Permalink
fix: subquery column with same name fix (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
gandronchik authored May 26, 2022
1 parent 49ff818 commit 0c3d877
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
17 changes: 8 additions & 9 deletions datafusion/core/src/physical_plan/subquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use arrow::compute::concat;
use std::any::Any;
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
Expand Down Expand Up @@ -61,16 +62,14 @@ impl SubqueryExec {
input: Arc<dyn ExecutionPlan>,
cursor: Arc<OuterQueryCursor>,
) -> Result<Self> {
let input_schema = (*input.schema()).clone();
let input_schema = input.schema();

let merged_schema = Schema::try_merge(
vec![input_schema].into_iter().chain(
subqueries
.iter()
.map(|s| (*s.schema()).clone())
.collect::<Vec<_>>(),
),
)?;
let mut total_fields = input_schema.fields().clone();
for q in subqueries.iter() {
total_fields.append(&mut q.schema().fields().clone());
}

let merged_schema = Schema::new_with_metadata(total_fields, HashMap::new());

if merged_schema.fields().len()
!= input.schema().fields().len() + subqueries.len()
Expand Down
4 changes: 3 additions & 1 deletion datafusion/core/src/sql/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2061,7 +2061,9 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {

SQLExpr::Subquery(q) => {
let with_outer_query_context = self.with_context(|c| c.outer_query_context_schema.push(Arc::new(schema.clone())));
let plan = with_outer_query_context.query_to_plan(*q)?;
let alias_name = format!("subquery-{}", self.context.subqueries_plans().unwrap_or_default().unwrap_or_default().len());
let plan = with_outer_query_context.query_to_plan_with_alias(*q, Some(alias_name), &mut HashMap::new())?;

let fields = plan.schema().fields();
if fields.len() != 1 {
return Err(DataFusionError::Plan(format!("Correlated sub query requires only one column in result set but found: {:?}", fields)));
Expand Down

0 comments on commit 0c3d877

Please sign in to comment.