Skip to content

Commit bd905dc

Browse files
committed
fix: display of nested phys. binary expr should use parenthesis
Found while working on apache#4695.
1 parent cf8e81d commit bd905dc

1 file changed

Lines changed: 91 additions & 1 deletion

File tree

  • datafusion/physical-expr/src/expressions

‎datafusion/physical-expr/src/expressions/binary.rs‎

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,34 @@ impl BinaryExpr {
118118

119119
impl std::fmt::Display for BinaryExpr {
120120
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
121-
write!(f, "{} {} {}", self.left, self.op, self.right)
121+
// Put parentheses around child binary expressions so that we can see the difference
122+
// between `(a OR b) AND c` and `a OR (b AND c)`. We only insert parentheses when needed,
123+
// based on operator precedence. For example, `(a AND b) OR c` and `a AND b OR c` are
124+
// equivalent and the parentheses are not necessary.
125+
126+
fn write_child(
127+
f: &mut std::fmt::Formatter,
128+
expr: &dyn PhysicalExpr,
129+
precedence: u8,
130+
) -> std::fmt::Result {
131+
if let Some(child) = expr.as_any().downcast_ref::<BinaryExpr>() {
132+
let p = child.op.precedence();
133+
if p == 0 || p < precedence {
134+
write!(f, "({child})")?;
135+
} else {
136+
write!(f, "{child}")?;
137+
}
138+
} else {
139+
write!(f, "{expr}")?;
140+
}
141+
142+
Ok(())
143+
}
144+
145+
let precedence = self.op.precedence();
146+
write_child(f, self.left.as_ref(), precedence)?;
147+
write!(f, " {} ", self.op)?;
148+
write_child(f, self.right.as_ref(), precedence)
122149
}
123150
}
124151

@@ -4249,4 +4276,67 @@ mod tests {
42494276

42504277
Ok(())
42514278
}
4279+
4280+
#[test]
4281+
fn test_display_and_or_combo() {
4282+
let expr = BinaryExpr::new(
4283+
Arc::new(BinaryExpr::new(
4284+
lit(ScalarValue::from(1)),
4285+
Operator::And,
4286+
lit(ScalarValue::from(2)),
4287+
)),
4288+
Operator::And,
4289+
Arc::new(BinaryExpr::new(
4290+
lit(ScalarValue::from(3)),
4291+
Operator::And,
4292+
lit(ScalarValue::from(4)),
4293+
)),
4294+
);
4295+
assert_eq!(expr.to_string(), "1 AND 2 AND 3 AND 4");
4296+
4297+
let expr = BinaryExpr::new(
4298+
Arc::new(BinaryExpr::new(
4299+
lit(ScalarValue::from(1)),
4300+
Operator::Or,
4301+
lit(ScalarValue::from(2)),
4302+
)),
4303+
Operator::Or,
4304+
Arc::new(BinaryExpr::new(
4305+
lit(ScalarValue::from(3)),
4306+
Operator::Or,
4307+
lit(ScalarValue::from(4)),
4308+
)),
4309+
);
4310+
assert_eq!(expr.to_string(), "1 OR 2 OR 3 OR 4");
4311+
4312+
let expr = BinaryExpr::new(
4313+
Arc::new(BinaryExpr::new(
4314+
lit(ScalarValue::from(1)),
4315+
Operator::And,
4316+
lit(ScalarValue::from(2)),
4317+
)),
4318+
Operator::Or,
4319+
Arc::new(BinaryExpr::new(
4320+
lit(ScalarValue::from(3)),
4321+
Operator::And,
4322+
lit(ScalarValue::from(4)),
4323+
)),
4324+
);
4325+
assert_eq!(expr.to_string(), "1 AND 2 OR 3 AND 4");
4326+
4327+
let expr = BinaryExpr::new(
4328+
Arc::new(BinaryExpr::new(
4329+
lit(ScalarValue::from(1)),
4330+
Operator::Or,
4331+
lit(ScalarValue::from(2)),
4332+
)),
4333+
Operator::And,
4334+
Arc::new(BinaryExpr::new(
4335+
lit(ScalarValue::from(3)),
4336+
Operator::Or,
4337+
lit(ScalarValue::from(4)),
4338+
)),
4339+
);
4340+
assert_eq!(expr.to_string(), "(1 OR 2) AND (3 OR 4)");
4341+
}
42524342
}

0 commit comments

Comments
 (0)