alamb commented on code in PR #10405:
URL: https://github.com/apache/datafusion/pull/10405#discussion_r1592317393


##########
datafusion/optimizer/src/optimize_projections/mod.rs:
##########
@@ -1184,13 +1206,32 @@ mod tests {
         assert_optimized_plan_equal(plan, expected)
     }
 
+    // Test Case expression
+    #[test]
+    fn test_case_merged() -> Result<()> {
+        let table_scan = test_table_scan()?;
+        let plan = LogicalPlanBuilder::from(table_scan)
+            .project(vec![col("a"), lit(0).alias("d")])?
+            .project(vec![
+                col("a"),
+                when(col("a").eq(lit(1)), lit(10))
+                    .otherwise(col("d"))?
+                    .alias("d"),
+            ])?
+            .build()?;
+
+        let expected = "Projection: test.a, CASE WHEN test.a = Int32(1) THEN 
Int32(10) ELSE Int32(0) END AS d\
+        \n  TableScan: test projection=[a]";
+        assert_optimized_plan_equal(plan, expected)
+    }
+
     // Test outer projection isn't discarded despite the same schema as inner
     // https://github.com/apache/datafusion/issues/8942
     #[test]
     fn test_derived_column() -> Result<()> {
         let table_scan = test_table_scan()?;
         let plan = LogicalPlanBuilder::from(table_scan)
-            .project(vec![col("a"), lit(0).alias("d")])?
+            .project(vec![col("a").add(lit(1)).alias("a"), lit(0).alias("d")])?

Review Comment:
   Without this change the projection is actually merged
   
   This is due to using the general purpose `transform_up` rather than special 
casing certain `Expr` types in the projection rewrite and now `CASE` is handled
   
   In order to prevent merging the projections, I needed to make the lower 
projection non trivial so changed it from `a` to `a + 1 as a`
   
   Note that the tests for correctness added for 
https://github.com/apache/arrow-datafusion/issues/8942 in
   https://github.com/apache/datafusion/pull/8960 such as 
`consecutive_projection_same_schema` are also still passing
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to