Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Lordworms committed Feb 7, 2024
1 parent b6f0f21 commit 5ec3909
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 18 deletions.
2 changes: 0 additions & 2 deletions datafusion-cli/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?;

Expand Down
29 changes: 23 additions & 6 deletions datafusion/expr/src/logical_plan/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,6 @@ impl LogicalPlanBuilder {
pub fn cross_join(self, right: LogicalPlan) -> Result<Self> {
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),
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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(())
}
}
5 changes: 1 addition & 4 deletions datafusion/expr/src/logical_plan/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions datafusion/sql/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_));
Expand All @@ -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();
Expand Down
61 changes: 61 additions & 0 deletions datafusion/sqllogictest/test_files/same_column_name_cross_join.slt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5ec3909

Please sign in to comment.