fmonjalet commented on code in PR #16031:
URL: https://github.com/apache/datafusion/pull/16031#discussion_r2087256490


##########
datafusion/substrait/src/logical_plan/consumer/expr/scalar_function.rs:
##########
@@ -124,6 +109,31 @@ pub fn name_to_op(name: &str) -> Option<Operator> {
     }
 }
 
+/// Build a balanced tree of binary operations from a binary operator and a 
list of arguments.
+///
+/// For example, `OR` `(a, b, c, d, e)` will be converted to: `OR(OR(a, OR(b, 
c)), OR(d, e))`.
+///
+/// `args` must not be empty.
+fn arg_list_to_binary_op_tree(
+    op: Operator,
+    args: &[Expr],
+) -> Expr {
+    if args.len() == 1 {
+        return args[0].clone();
+    } else if args.len() == 0 {
+        panic!("must not be called with empty args");
+    }
+    // Cut argument list in 2 balanced parts
+    let mid_idx = args.len() / 2;
+    let left = arg_list_to_binary_op_tree(op, &args[0..mid_idx]);
+    let right = arg_list_to_binary_op_tree(op, &args[mid_idx..]);
+    Expr::BinaryExpr(BinaryExpr {
+        left: Box::new(left),
+        op,
+        right: Box::new(right),
+    })
+}

Review Comment:
   @gabotechs unfortunately [`.split_off()` calls 
clone](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.split_off), but 
I got a simple enough version working with an explicit `Drain`. Thanks for 
making me look again!



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