diff --git a/datafusion-cli/src/exec.rs b/datafusion-cli/src/exec.rs index 6e057d584cba9..a38ca681b61c8 100644 --- a/datafusion-cli/src/exec.rs +++ b/datafusion-cli/src/exec.rs @@ -224,7 +224,6 @@ async fn exec_and_print( let statements = DFParser::parse_sql_with_dialect(&sql, dialect.as_ref())?; for statement in statements { - // println!("cur statement is {:?}", statement); let plan = create_plan(ctx, statement).await?; // For plans like `Explain` ignore `MaxRows` option and always display all rows @@ -234,7 +233,6 @@ async fn exec_and_print( | LogicalPlan::DescribeTable(_) | LogicalPlan::Analyze(_) ); - // println!("the final logical plan is {:?}", plan); let df = ctx.execute_logical_plan(plan).await?; let physical_plan = df.create_physical_plan().await?; diff --git a/datafusion/expr/src/logical_plan/builder.rs b/datafusion/expr/src/logical_plan/builder.rs index 1d3452f79d965..3e11a5ded32c3 100644 --- a/datafusion/expr/src/logical_plan/builder.rs +++ b/datafusion/expr/src/logical_plan/builder.rs @@ -890,7 +890,6 @@ impl LogicalPlanBuilder { pub fn cross_join(self, right: LogicalPlan) -> Result { let join_schema = build_join_schema(self.plan.schema(), right.schema(), &JoinType::Inner)?; - println!("left is {:?} \n right is {:?}", self.plan, right); Ok(Self::from(LogicalPlan::CrossJoin(CrossJoin { left: Arc::new(self.plan), right: Arc::new(right), @@ -1212,7 +1211,6 @@ pub fn build_join_schema( join_type, left_fields.len(), ); - // println!("func_dependencies is {:?}", func_dependencies); let mut metadata = left.metadata().clone(); metadata.extend(right.metadata().clone()); // let schema = DFSchema::new_with_metadata(change_redundant_column(fields), metadata)?; @@ -1406,10 +1404,7 @@ pub fn project( .push(columnize_expr(normalize_col(e, &plan)?, input_schema)), } } - // println!( - // "before validation the projection name is {:?} \n and the expression is {:?}", - // plan, projected_expr - // ); + validate_unique_names("Projections", projected_expr.iter())?; Projection::try_new(projected_expr, Arc::new(plan)).map(LogicalPlan::Projection) @@ -1604,6 +1599,7 @@ mod tests { use crate::{col, expr, expr_fn::exists, in_subquery, lit, scalar_subquery, sum}; use arrow::datatypes::{DataType, Field}; + use arrow::ipc::Int; use datafusion_common::{OwnedTableReference, SchemaError, TableReference}; #[test] @@ -2111,4 +2107,25 @@ mod tests { Ok(()) } + #[test] + fn test_change_redundant_column() -> Result<()> { + let t1_field_1 = DFField::new_unqualified("a", DataType::Int32, false); + let t2_field_1 = DFField::new_unqualified("a", DataType::Int32, false); + let t1_field_2 = DFField::new_unqualified("b", DataType::Int32, false); + let t2_field_2 = DFField::new_unqualified("b", DataType::Int32, false); + + let field_vec = vec![t1_field_1, t2_field_1, t1_field_2, t2_field_2]; + let remove_redundant = change_redundant_column(field_vec); + + assert_eq!( + remove_redundant, + vec![ + DFField::new_unqualified("a", DataType::Int32, false), + DFField::new_unqualified("a:0", DataType::Int32, false), + DFField::new_unqualified("b", DataType::Int32, false), + DFField::new_unqualified("b:0", DataType::Int32, false) + ] + ); + Ok(()) + } } diff --git a/datafusion/expr/src/logical_plan/plan.rs b/datafusion/expr/src/logical_plan/plan.rs index d80bef1df7f35..8e6b015f74d1a 100644 --- a/datafusion/expr/src/logical_plan/plan.rs +++ b/datafusion/expr/src/logical_plan/plan.rs @@ -2186,10 +2186,7 @@ impl TableScan { df_schema.with_functional_dependencies(func_dependencies) })?; let projected_schema = Arc::new(projected_schema); - // println!( - // "projected_schema is {:?} \n and projection is {:?}", - // projected_schema, projection, - // ); + Ok(Self { table_name, source: table_source, diff --git a/datafusion/sql/src/select.rs b/datafusion/sql/src/select.rs index a38272be21a72..d0bcbb1daa4ab 100644 --- a/datafusion/sql/src/select.rs +++ b/datafusion/sql/src/select.rs @@ -66,8 +66,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { if !select.sort_by.is_empty() { return not_impl_err!("SORT BY"); } - // println!("select from is {:?}", select.from); - // println!("current planner_context is {:?}", planner_context); + // process `from` clause let plan = self.plan_from_tables(select.from, planner_context)?; let empty_from = matches!(plan, LogicalPlan::EmptyRelation(_)); @@ -85,10 +84,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { empty_from, planner_context, )?; - // println!( - // "base plan is {:?} \n select expression is {:?} \n planner_context is {:?}", - // base_plan, select_exprs, planner_context - // ); + // having and group by clause may reference aliases defined in select projection let projected_plan = self.project(base_plan.clone(), select_exprs.clone())?; let mut combined_schema = (**projected_plan.schema()).clone(); diff --git a/datafusion/sqllogictest/test_files/same_column_name_cross_join.slt b/datafusion/sqllogictest/test_files/same_column_name_cross_join.slt new file mode 100644 index 0000000000000..b47b8b7eef9f3 --- /dev/null +++ b/datafusion/sqllogictest/test_files/same_column_name_cross_join.slt @@ -0,0 +1,61 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +# prepare the tables + +statement ok +create table t1 (a int, b int); + +statement ok +create table t2 (a int, b int); + +statement ok +create table t3 (a int, b int); + +statement ok +insert into t1 values (1, 2); + +statement ok +insert into t2 values (3, 4); + +statement ok +insert into t3 values (5, 6); + +query IIIIII +select * from (t1 cross join t2) as t cross join t3; +------- +---- +5 6 1 2 3 4 + +query IIIIIIII +select * from (t1 cross join t2) as t cross join (t2 cross join t3) +------- +---- +1 2 3 4 3 4 5 6 + +query IIIIIIIIIIII +select * from (t1 cross join t2) as t cross join (t2 cross join t3) cross join (t1 cross join t3) as tt +-------- +---- +1 2 5 6 1 2 3 4 3 4 5 6 + +query IIIIIIIIIIIIIIII +select * from (t1 cross join t2) as t cross join (t2 cross join t3) cross join (t1 cross join t3) as tt cross join (t2 cross join t3) as ttt; +-------- +---- +1 2 5 6 1 2 3 4 3 4 5 6 3 4 5 6