vbarua commented on code in PR #16984:
URL: https://github.com/apache/datafusion/pull/16984#discussion_r2244050989


##########
datafusion/substrait/src/logical_plan/consumer/expr/scalar_function.rs:
##########
@@ -283,6 +290,91 @@ impl BuiltinExprBuilder {
             case_insensitive,
         }))
     }
+
+    async fn build_binary_expr(
+        consumer: &impl SubstraitConsumer,
+        fn_name: &str,
+        f: &ScalarFunction,
+        input_schema: &DFSchema,
+    ) -> Result<Expr> {
+        if f.arguments.len() != 2 {
+            return substrait_err!("Expect two arguments for `{fn_name}` expr");
+        }
+        let Some(ArgType::Value(a_substrait)) = &f.arguments[0].arg_type else {
+            return substrait_err!("Invalid argument type for `{fn_name}` 
expr");
+        };
+        let a = consumer
+            .consume_expression(a_substrait, input_schema)
+            .await?;
+        let Some(ArgType::Value(b_substrait)) = &f.arguments[1].arg_type else {
+            return substrait_err!("Invalid argument type for `{fn_name}` 
expr");
+        };
+        let b = consumer
+            .consume_expression(b_substrait, input_schema)
+            .await?;
+        match fn_name {
+            "and_not" => Ok(Self::build_and_not_expr(a, b)),
+            "xor" => Ok(Self::build_xor_expr(a, b)),
+            _ => not_impl_err!("Unsupported builtin expression: {}", fn_name),
+        }
+    }
+
+    fn build_and_not_expr(a: Expr, b: Expr) -> Expr {
+        Expr::BinaryExpr(BinaryExpr {
+            left: Box::new(a),
+            op: Operator::And,
+            right: Box::new(Expr::Not(Box::new(b))),
+        })
+    }
+
+    fn build_xor_expr(a: Expr, b: Expr) -> Expr {
+        let or_expr = Expr::BinaryExpr(BinaryExpr {
+            left: Box::new(a.clone()),
+            op: Operator::Or,
+            right: Box::new(b.clone()),
+        });
+        let and_expr = Expr::BinaryExpr(BinaryExpr {
+            left: Box::new(a),
+            op: Operator::And,
+            right: Box::new(b),
+        });
+        Self::build_and_not_expr(or_expr, and_expr)
+    }
+
+    async fn build_between_expr(
+        consumer: &impl SubstraitConsumer,
+        fn_name: &str,
+        f: &ScalarFunction,
+        input_schema: &DFSchema,
+    ) -> Result<Expr> {
+        if f.arguments.len() != 3 {
+            return substrait_err!("Expect three arguments for `{fn_name}` 
expr");
+        }
+        let Some(ArgType::Value(expression_substrait)) = 
&f.arguments[0].arg_type else {
+            return substrait_err!("Invalid argument type for `{fn_name}` 
expr");
+        };
+        let expression = consumer
+            .consume_expression(expression_substrait, input_schema)
+            .await?;
+        let Some(ArgType::Value(low_substrait)) = &f.arguments[1].arg_type 
else {
+            return substrait_err!("Invalid argument type for `{fn_name}` 
expr");
+        };
+        let low = consumer
+            .consume_expression(low_substrait, input_schema)
+            .await?;
+        let Some(ArgType::Value(high_substrait)) = &f.arguments[2].arg_type 
else {

Review Comment:
   minor: we pattern match on `ArgType::Value` a number of times, both here and 
in the binary handling function. It might be cleaner to introduce a helper that 
takes the vector arguments and returns a vector of Substrait expressions, or 
maybe even the converter Datafusion expressions.



-- 
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: github-unsubscr...@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to