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


##########
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:
   Nice! do you think we can do it without cloning the expressions? something 
like:
   ```rust
   fn arg_list_to_binary_op_tree(op: Operator, mut args: Vec<Expr>) -> Expr {
       if args.is_empty() {
           panic!("must not be called with empty args");
       }
       if args.len() == 1 {
           return args.into_iter().next().unwrap();
       }
       // Cut argument list in 2 balanced parts
       let mid_idx = args.len() / 2;
       let right = arg_list_to_binary_op_tree(op, args.split_off(mid_idx));
       let left = arg_list_to_binary_op_tree(op, args);
       Expr::BinaryExpr(BinaryExpr {
           left: Box::new(left),
           op,
           right: Box::new(right),
       })
   }
   ```



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