You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by al...@apache.org on 2024/02/17 11:11:55 UTC

(arrow-datafusion) branch main updated: fix: issue #9130 substitute redundant columns when doing cross join (#9154)

This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new bb00b63f97 fix: issue #9130 substitute redundant columns when doing cross join (#9154)
bb00b63f97 is described below

commit bb00b63f97e85928ecdd61bd832f001ea316b965
Author: Lordworms <48...@users.noreply.github.com>
AuthorDate: Sat Feb 17 05:11:49 2024 -0600

    fix: issue #9130 substitute redundant columns when doing cross join (#9154)
    
    * fix: issue #9130 substitute redundant columns when doing cross join
    
    * add test
    
    * fix:bugs
    
    * optimize code
    
    * optimize code
    
    * deleting extra debug info
    
    * adding test and optimized code
    
    * fix test
    
    * fix test
---
 datafusion-cli/src/exec.rs                         |  1 -
 datafusion/expr/src/logical_plan/builder.rs        | 47 +++++++++++++++-
 datafusion/expr/src/logical_plan/plan.rs           |  6 +-
 datafusion/sql/src/select.rs                       |  1 -
 .../test_files/same_column_name_cross_join.slt     | 64 ++++++++++++++++++++++
 5 files changed, 115 insertions(+), 4 deletions(-)

diff --git a/datafusion-cli/src/exec.rs b/datafusion-cli/src/exec.rs
index 6ca8dfe927..d1e4a5b363 100644
--- a/datafusion-cli/src/exec.rs
+++ b/datafusion-cli/src/exec.rs
@@ -228,7 +228,6 @@ async fn exec_and_print(
                 | LogicalPlan::DescribeTable(_)
                 | LogicalPlan::Analyze(_)
         );
-
         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 68499f09d2..39df96d61f 100644
--- a/datafusion/expr/src/logical_plan/builder.rs
+++ b/datafusion/expr/src/logical_plan/builder.rs
@@ -1124,7 +1124,27 @@ impl LogicalPlanBuilder {
         )?))
     }
 }
-
+pub fn change_redundant_column(fields: Vec<DFField>) -> Vec<DFField> {
+    let mut name_map = HashMap::new();
+    fields
+        .into_iter()
+        .map(|field| {
+            let counter = name_map.entry(field.name().to_string()).or_insert(0);
+            *counter += 1;
+            if *counter > 1 {
+                let new_name = format!("{}:{}", field.name(), *counter - 1);
+                DFField::new(
+                    field.qualifier().cloned(),
+                    &new_name,
+                    field.data_type().clone(),
+                    field.is_nullable(),
+                )
+            } else {
+                field
+            }
+        })
+        .collect()
+}
 /// Creates a schema for a join operation.
 /// The fields from the left side are first
 pub fn build_join_schema(
@@ -1237,6 +1257,7 @@ pub(crate) fn validate_unique_names<'a>(
     expressions: impl IntoIterator<Item = &'a Expr>,
 ) -> Result<()> {
     let mut unique_names = HashMap::new();
+
     expressions.into_iter().enumerate().try_for_each(|(position, expr)| {
         let name = expr.display_name()?;
         match unique_names.get(&name) {
@@ -1375,6 +1396,7 @@ pub fn project(
                 .push(columnize_expr(normalize_col(e, &plan)?, input_schema)),
         }
     }
+
     validate_unique_names("Projections", projected_expr.iter())?;
 
     Projection::try_new(projected_expr, Arc::new(plan)).map(LogicalPlan::Projection)
@@ -2076,4 +2098,27 @@ 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 t2_field_3 = 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, t2_field_3];
+        let remove_redundant = change_redundant_column(field_vec);
+
+        assert_eq!(
+            remove_redundant,
+            vec![
+                DFField::new_unqualified("a", DataType::Int32, false),
+                DFField::new_unqualified("a:1", DataType::Int32, false),
+                DFField::new_unqualified("b", DataType::Int32, false),
+                DFField::new_unqualified("b:1", DataType::Int32, false),
+                DFField::new_unqualified("a:2", DataType::Int32, false),
+            ]
+        );
+        Ok(())
+    }
 }
diff --git a/datafusion/expr/src/logical_plan/plan.rs b/datafusion/expr/src/logical_plan/plan.rs
index b7d75dc0ae..ba768cf3c6 100644
--- a/datafusion/expr/src/logical_plan/plan.rs
+++ b/datafusion/expr/src/logical_plan/plan.rs
@@ -24,6 +24,7 @@ use std::sync::Arc;
 
 use super::dml::CopyTo;
 use super::DdlStatement;
+use crate::builder::change_redundant_column;
 use crate::dml::CopyOptions;
 use crate::expr::{
     Alias, Exists, InSubquery, Placeholder, Sort as SortExpr, WindowFunction,
@@ -1891,7 +1892,9 @@ impl SubqueryAlias {
         alias: impl Into<OwnedTableReference>,
     ) -> Result<Self> {
         let alias = alias.into();
-        let schema: Schema = plan.schema().as_ref().clone().into();
+        let fields = change_redundant_column(plan.schema().fields().clone());
+        let meta_data = plan.schema().as_ref().metadata().clone();
+        let schema: Schema = DFSchema::new_with_metadata(fields, meta_data)?.into();
         // Since schema is the same, other than qualifier, we can use existing
         // functional dependencies:
         let func_dependencies = plan.schema().functional_dependencies().clone();
@@ -2181,6 +2184,7 @@ impl TableScan {
                 df_schema.with_functional_dependencies(func_dependencies)
             })?;
         let projected_schema = Arc::new(projected_schema);
+
         Ok(Self {
             table_name,
             source: table_source,
diff --git a/datafusion/sql/src/select.rs b/datafusion/sql/src/select.rs
index f47720d467..b5b6ada48f 100644
--- a/datafusion/sql/src/select.rs
+++ b/datafusion/sql/src/select.rs
@@ -77,7 +77,6 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
         // handle named windows before processing the projection expression
         check_conflicting_windows(&select.named_window)?;
         match_window_definitions(&mut select.projection, &select.named_window)?;
-
         // process the SELECT expressions, with wildcards expanded.
         let select_exprs = self.prepare_select_exprs(
             &base_plan,
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 0000000000..de004267c9
--- /dev/null
+++ b/datafusion/sqllogictest/test_files/same_column_name_cross_join.slt
@@ -0,0 +1,64 @@
+# 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;
+-------
+----
+1 2 3 4 5 6
+
+
+
+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 3 4 3 4 5 6 1 2 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 3 4 3 4 5 6 1 2 5 6 3 4 5 6