Skip to content

Commit 0c3d877

Browse files
authored
fix: subquery column with same name fix (#33)
1 parent 49ff818 commit 0c3d877

2 files changed

Lines changed: 11 additions & 10 deletions

File tree

datafusion/core/src/physical_plan/subquery.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
2323
use arrow::compute::concat;
2424
use std::any::Any;
25+
use std::collections::HashMap;
2526
use std::pin::Pin;
2627
use std::sync::Arc;
2728
use std::task::{Context, Poll};
@@ -61,16 +62,14 @@ impl SubqueryExec {
6162
input: Arc<dyn ExecutionPlan>,
6263
cursor: Arc<OuterQueryCursor>,
6364
) -> Result<Self> {
64-
let input_schema = (*input.schema()).clone();
65+
let input_schema = input.schema();
6566

66-
let merged_schema = Schema::try_merge(
67-
vec![input_schema].into_iter().chain(
68-
subqueries
69-
.iter()
70-
.map(|s| (*s.schema()).clone())
71-
.collect::<Vec<_>>(),
72-
),
73-
)?;
67+
let mut total_fields = input_schema.fields().clone();
68+
for q in subqueries.iter() {
69+
total_fields.append(&mut q.schema().fields().clone());
70+
}
71+
72+
let merged_schema = Schema::new_with_metadata(total_fields, HashMap::new());
7473

7574
if merged_schema.fields().len()
7675
!= input.schema().fields().len() + subqueries.len()

datafusion/core/src/sql/planner.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2061,7 +2061,9 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
20612061

20622062
SQLExpr::Subquery(q) => {
20632063
let with_outer_query_context = self.with_context(|c| c.outer_query_context_schema.push(Arc::new(schema.clone())));
2064-
let plan = with_outer_query_context.query_to_plan(*q)?;
2064+
let alias_name = format!("subquery-{}", self.context.subqueries_plans().unwrap_or_default().unwrap_or_default().len());
2065+
let plan = with_outer_query_context.query_to_plan_with_alias(*q, Some(alias_name), &mut HashMap::new())?;
2066+
20652067
let fields = plan.schema().fields();
20662068
if fields.len() != 1 {
20672069
return Err(DataFusionError::Plan(format!("Correlated sub query requires only one column in result set but found: {:?}", fields)));

0 commit comments

Comments
 (0)