alamb commented on code in PR #16984: URL: https://github.com/apache/datafusion/pull/16984#discussion_r2243696144
########## 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( Review Comment: Perhaps you can use https://docs.rs/datafusion/latest/datafusion/logical_expr/enum.Expr.html#method.between here as well to simplify things a bit ########## 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))), + }) + } Review Comment: You can use the built in `and` method if you prefer: https://docs.rs/datafusion/latest/datafusion/logical_expr/enum.Expr.html#method.and So `a.and(b.not())` -- 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