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 2022/05/13 18:28:56 UTC

[arrow-datafusion] branch master updated: Fix projection pushdown produces incorrect results when column names are reused (#2463)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new a82589100 Fix projection pushdown produces incorrect results when column names are reused (#2463)
a82589100 is described below

commit a82589100d7b07cb629476a2df7eac002f48b8a0
Author: Jon Mease <jo...@gmail.com>
AuthorDate: Fri May 13 14:28:51 2022 -0400

    Fix projection pushdown produces incorrect results when column names are reused (#2463)
    
    * Add failing select_with_alias_overwrite test
    
    * candidate fix
    
    * reinstate optimization, but check that expressions are plain columns
---
 .../core/src/optimizer/projection_push_down.rs     |  7 +++-
 datafusion/core/tests/dataframe.rs                 | 42 ++++++++++++++++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/datafusion/core/src/optimizer/projection_push_down.rs b/datafusion/core/src/optimizer/projection_push_down.rs
index 9cbec1e90..44cfa9a67 100644
--- a/datafusion/core/src/optimizer/projection_push_down.rs
+++ b/datafusion/core/src/optimizer/projection_push_down.rs
@@ -32,6 +32,7 @@ use crate::optimizer::utils;
 use crate::sql::utils::find_sort_exprs;
 use arrow::datatypes::{Field, Schema};
 use arrow::error::Result as ArrowResult;
+use datafusion_expr::Expr;
 use std::{
     collections::{BTreeSet, HashSet},
     sync::Arc,
@@ -179,8 +180,12 @@ fn optimize_plan(
                 .map(|f| f.qualified_column())
                 .collect::<HashSet<Column>>();
 
+            let all_column_exprs = new_expr.iter().all(|e| matches!(e, Expr::Column(_)));
+
             if new_fields.is_empty()
-                || (has_projection && &new_required_columns_optimized == required_columns)
+                || (has_projection
+                    && all_column_exprs
+                    && &new_required_columns_optimized == required_columns)
             {
                 // no need for an expression at all
                 Ok(new_input)
diff --git a/datafusion/core/tests/dataframe.rs b/datafusion/core/tests/dataframe.rs
index 9dd78a029..234178e74 100644
--- a/datafusion/core/tests/dataframe.rs
+++ b/datafusion/core/tests/dataframe.rs
@@ -165,3 +165,45 @@ async fn filter_with_alias_overwrite() -> Result<()> {
 
     Ok(())
 }
+
+#[tokio::test]
+async fn select_with_alias_overwrite() -> Result<()> {
+    let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
+
+    let batch = RecordBatch::try_new(
+        Arc::new(schema.clone()),
+        vec![Arc::new(Int32Array::from_slice(&[1, 10, 10, 100]))],
+    )
+    .unwrap();
+
+    let ctx = SessionContext::new();
+    let provider = MemTable::try_new(Arc::new(schema), vec![vec![batch]]).unwrap();
+    ctx.register_table("t", Arc::new(provider)).unwrap();
+
+    let df = ctx
+        .table("t")
+        .unwrap()
+        .select(vec![col("a").alias("a")])
+        .unwrap()
+        .select(vec![(col("a").eq(lit(10))).alias("a")])
+        .unwrap()
+        .select(vec![col("a")])
+        .unwrap();
+
+    let results = df.collect().await.unwrap();
+
+    #[rustfmt::skip]
+        let expected = vec![
+        "+-------+",
+        "| a     |",
+        "+-------+",
+        "| false |",
+        "| true  |",
+        "| true  |",
+        "| false |",
+        "+-------+",
+    ];
+    assert_batches_eq!(expected, &results);
+
+    Ok(())
+}