You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/06/05 18:50:25 UTC

[GitHub] [arrow-datafusion] msathis opened a new pull request #513: Implement constant folding for CAST

msathis opened a new pull request #513:
URL: https://github.com/apache/arrow-datafusion/pull/513


   # Which issue does this PR close?
   Closes #417.
   
    # Rationale for this change
   Optimize cast function during planning stage
   
   # What changes are included in this PR?
   Optimize cast function during planning stage
   
   # Are there any user-facing changes?
   N/A
   


-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-datafusion] jorgecarleitao merged pull request #513: Implement constant folding for CAST

Posted by GitBox <gi...@apache.org>.
jorgecarleitao merged pull request #513:
URL: https://github.com/apache/arrow-datafusion/pull/513


   


-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-datafusion] alamb commented on a change in pull request #513: Implement constant folding for CAST

Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #513:
URL: https://github.com/apache/arrow-datafusion/pull/513#discussion_r646110743



##########
File path: datafusion/src/optimizer/constant_folding.rs
##########
@@ -724,6 +744,44 @@ mod tests {
         assert_eq!(expected, actual);
     }
 
+    #[test]
+    fn cast_expr() {
+        let table_scan = test_table_scan().unwrap();
+        let proj = vec![Expr::Cast {
+            expr: Box::new(Expr::Literal(ScalarValue::Utf8(Some("0".to_string())))),
+            data_type: DataType::Int32,
+        }];
+        let plan = LogicalPlanBuilder::from(&table_scan)
+            .project(proj)
+            .unwrap()
+            .build()
+            .unwrap();
+
+        let expected = "Projection: Int32(0)\
+            \n  TableScan: test projection=None";
+        let actual = get_optimized_plan_formatted(&plan, &chrono::Utc::now());
+        assert_eq!(expected, actual);
+    }
+
+    #[test]
+    fn cast_expr_wrong_arg() {
+        let table_scan = test_table_scan().unwrap();
+        let proj = vec![Expr::Cast {
+            expr: Box::new(Expr::Literal(ScalarValue::Utf8(Some("".to_string())))),
+            data_type: DataType::Int32,
+        }];
+        let plan = LogicalPlanBuilder::from(&table_scan)
+            .project(proj)
+            .unwrap()
+            .build()
+            .unwrap();
+
+        let expected = "Projection: Int32(NULL)\

Review comment:
       👍 

##########
File path: datafusion/src/optimizer/constant_folding.rs
##########
@@ -724,6 +744,44 @@ mod tests {
         assert_eq!(expected, actual);
     }
 
+    #[test]
+    fn cast_expr() {
+        let table_scan = test_table_scan().unwrap();
+        let proj = vec![Expr::Cast {
+            expr: Box::new(Expr::Literal(ScalarValue::Utf8(Some("0".to_string())))),
+            data_type: DataType::Int32,
+        }];
+        let plan = LogicalPlanBuilder::from(&table_scan)
+            .project(proj)
+            .unwrap()
+            .build()
+            .unwrap();
+
+        let expected = "Projection: Int32(0)\

Review comment:
       this is beautiful ❤️  -- thank you @msathis 




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-datafusion] Dandandan commented on a change in pull request #513: Implement constant folding for CAST

Posted by GitBox <gi...@apache.org>.
Dandandan commented on a change in pull request #513:
URL: https://github.com/apache/arrow-datafusion/pull/513#discussion_r646036515



##########
File path: datafusion/src/optimizer/constant_folding.rs
##########
@@ -247,6 +248,25 @@ impl<'a> ExprRewriter for ConstantRewriter<'a> {
                     }
                 }
             }
+            Expr::Cast {
+                expr: inner,
+                data_type,
+            } => match inner.as_ref() {
+                Expr::Literal(val) => {
+                    let scalar_array = val.to_array();

Review comment:
       Great idea👍 I think converting to an array is the best we can do for now, without duplicating the code.
   




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org