alamb commented on code in PR #4115:
URL: https://github.com/apache/arrow-datafusion/pull/4115#discussion_r1015485753
##########
datafusion/proto/src/to_proto.rs:
##########
@@ -455,11 +455,36 @@ impl TryFrom<&Expr> for protobuf::LogicalExprNode {
}
}
Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
- let binary_expr = Box::new(protobuf::BinaryExprNode {
- l: Some(Box::new(left.as_ref().try_into()?)),
- r: Some(Box::new(right.as_ref().try_into()?)),
+ // Try to linerize a nested binary expression tree of the same
operator
Review Comment:
Is it correct that this code will work for `(((A AND B) AND C) AND D)` but
not `A AND (B AND (C AND D)))`?
##########
datafusion/proto/src/bytes/mod.rs:
##########
@@ -332,7 +361,8 @@ mod test {
println!("testing: {n}");
let expr_base = col("a").lt(lit(5i32));
- let expr = (0..n).fold(expr_base.clone(), |expr, _|
expr.and(expr_base.clone()));
+ // Generate a tree of AND and OR expressions (no subsequent
ANDs or ORs).
Review Comment:
I would love to see a test that made the other type of nesting
`(expr_base.clone().and(expr))`
##########
datafusion/proto/src/from_proto.rs:
##########
@@ -690,11 +690,34 @@ pub fn parse_expr(
.ok_or_else(|| Error::required("expr_type"))?;
match expr_type {
- ExprType::BinaryExpr(binary_expr) =>
Ok(Expr::BinaryExpr(BinaryExpr::new(
- Box::new(parse_required_expr(&binary_expr.l, registry, "l")?),
- from_proto_binary_op(&binary_expr.op)?,
- Box::new(parse_required_expr(&binary_expr.r, registry, "r")?),
- ))),
+ ExprType::BinaryExpr(binary_expr) => {
+ let op = from_proto_binary_op(&binary_expr.op)?;
+ let operands = binary_expr
+ .operands
+ .iter()
+ .map(|expr| parse_expr(expr, registry))
+ .collect::<Result<Vec<_>, _>>()?;
+
+ if operands.len() < 2 {
+ return Err(proto_error(
+ "A binary expression must always have at least 2 operands",
+ ));
+ }
+
+ // Reduce the linearized operands (ordered by left innermost to
right
+ // outermost) into a single expression tree.
+ Ok(operands
+ .into_iter()
+ .reduce(|left, right| {
+ Expr::BinaryExpr(BinaryExpr::new(Box::new(left), op,
Box::new(right)))
+ })
+ .unwrap_or_else(|| {
+ // As long as we have the bounds check above, this should
never happen.
+ panic!(
Review Comment:
I think the comment is clear that this "should not happen" and thus I
believe the `panic` is fine
You could also write this same logic slightly more concisely as
`expect("Binary expression could not be reduced to a single expression.")`
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]