Skip to content

Commit cef119d

Browse files
authored
fix: misc phys. expression display bugs (#5387)
* fix: `TRY_CAST` phys. expr display * refactor: move precedence from `BinaryExpr` to `Operator` This is useful in other contexts as well, e.g. for formatting phys. expressions. * fix: display of nested phys. binary expr should use parenthesis Found while working on #4695.
1 parent ac33ebc commit cef119d

4 files changed

Lines changed: 129 additions & 39 deletions

File tree

‎datafusion/expr/src/expr.rs‎

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -238,35 +238,6 @@ impl BinaryExpr {
238238
pub fn new(left: Box<Expr>, op: Operator, right: Box<Expr>) -> Self {
239239
Self { left, op, right }
240240
}
241-
242-
/// Get the operator precedence
243-
/// use <https://www.postgresql.org/docs/7.0/operators.htm#AEN2026> as a reference
244-
pub fn precedence(&self) -> u8 {
245-
match self.op {
246-
Operator::Or => 5,
247-
Operator::And => 10,
248-
Operator::NotEq
249-
| Operator::Eq
250-
| Operator::Lt
251-
| Operator::LtEq
252-
| Operator::Gt
253-
| Operator::GtEq => 20,
254-
Operator::Plus | Operator::Minus => 30,
255-
Operator::Multiply | Operator::Divide | Operator::Modulo => 40,
256-
Operator::IsDistinctFrom
257-
| Operator::IsNotDistinctFrom
258-
| Operator::RegexMatch
259-
| Operator::RegexNotMatch
260-
| Operator::RegexIMatch
261-
| Operator::RegexNotIMatch
262-
| Operator::BitwiseAnd
263-
| Operator::BitwiseOr
264-
| Operator::BitwiseShiftLeft
265-
| Operator::BitwiseShiftRight
266-
| Operator::BitwiseXor
267-
| Operator::StringConcat => 0,
268-
}
269-
}
270241
}
271242

272243
impl Display for BinaryExpr {
@@ -283,7 +254,7 @@ impl Display for BinaryExpr {
283254
) -> fmt::Result {
284255
match expr {
285256
Expr::BinaryExpr(child) => {
286-
let p = child.precedence();
257+
let p = child.op.precedence();
287258
if p == 0 || p < precedence {
288259
write!(f, "({child})")?;
289260
} else {
@@ -295,7 +266,7 @@ impl Display for BinaryExpr {
295266
Ok(())
296267
}
297268

298-
let precedence = self.precedence();
269+
let precedence = self.op.precedence();
299270
write_child(f, self.left.as_ref(), precedence)?;
300271
write!(f, " {} ", self.op)?;
301272
write_child(f, self.right.as_ref(), precedence)

‎datafusion/expr/src/operator.rs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,35 @@ impl Operator {
142142
| Operator::StringConcat => None,
143143
}
144144
}
145+
146+
/// Get the operator precedence
147+
/// use <https://www.postgresql.org/docs/7.0/operators.htm#AEN2026> as a reference
148+
pub fn precedence(&self) -> u8 {
149+
match self {
150+
Operator::Or => 5,
151+
Operator::And => 10,
152+
Operator::NotEq
153+
| Operator::Eq
154+
| Operator::Lt
155+
| Operator::LtEq
156+
| Operator::Gt
157+
| Operator::GtEq => 20,
158+
Operator::Plus | Operator::Minus => 30,
159+
Operator::Multiply | Operator::Divide | Operator::Modulo => 40,
160+
Operator::IsDistinctFrom
161+
| Operator::IsNotDistinctFrom
162+
| Operator::RegexMatch
163+
| Operator::RegexNotMatch
164+
| Operator::RegexIMatch
165+
| Operator::RegexNotIMatch
166+
| Operator::BitwiseAnd
167+
| Operator::BitwiseOr
168+
| Operator::BitwiseShiftLeft
169+
| Operator::BitwiseShiftRight
170+
| Operator::BitwiseXor
171+
| Operator::StringConcat => 0,
172+
}
173+
}
145174
}
146175

147176
impl fmt::Display for Operator {

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

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl TryCastExpr {
5858

5959
impl fmt::Display for TryCastExpr {
6060
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61-
write!(f, "CAST({} AS {:?})", self.expr, self.cast_type)
61+
write!(f, "TRY_CAST({} AS {:?})", self.expr, self.cast_type)
6262
}
6363
}
6464

@@ -132,7 +132,7 @@ pub fn try_cast(
132132
Ok(Arc::new(TryCastExpr::new(expr, cast_type)))
133133
} else {
134134
Err(DataFusionError::NotImplemented(format!(
135-
"Unsupported CAST from {expr_type:?} to {cast_type:?}"
135+
"Unsupported TRY_CAST from {expr_type:?} to {cast_type:?}"
136136
)))
137137
}
138138
}
@@ -155,7 +155,7 @@ mod tests {
155155

156156
// runs an end-to-end test of physical type cast
157157
// 1. construct a record batch with a column "a" of type A
158-
// 2. construct a physical expression of CAST(a AS B)
158+
// 2. construct a physical expression of TRY_CAST(a AS B)
159159
// 3. evaluate the expression
160160
// 4. verify that the resulting expression is of type B
161161
// 5. verify that the resulting values are downcastable and correct
@@ -171,7 +171,7 @@ mod tests {
171171

172172
// verify that its display is correct
173173
assert_eq!(
174-
format!("CAST(a@0 AS {:?})", $TYPE),
174+
format!("TRY_CAST(a@0 AS {:?})", $TYPE),
175175
format!("{}", expression)
176176
);
177177

@@ -202,7 +202,7 @@ mod tests {
202202

203203
// runs an end-to-end test of physical type cast
204204
// 1. construct a record batch with a column "a" of type A
205-
// 2. construct a physical expression of CAST(a AS B)
205+
// 2. construct a physical expression of TRY_CAST(a AS B)
206206
// 3. evaluate the expression
207207
// 4. verify that the resulting expression is of type B
208208
// 5. verify that the resulting values are downcastable and correct
@@ -218,7 +218,7 @@ mod tests {
218218

219219
// verify that its display is correct
220220
assert_eq!(
221-
format!("CAST(a@0 AS {:?})", $TYPE),
221+
format!("TRY_CAST(a@0 AS {:?})", $TYPE),
222222
format!("{}", expression)
223223
);
224224

@@ -542,7 +542,7 @@ mod tests {
542542
let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);
543543

544544
let result = try_cast(col("a", &schema).unwrap(), &schema, DataType::LargeBinary);
545-
result.expect_err("expected Invalid CAST");
545+
result.expect_err("expected Invalid TRY_CAST");
546546
}
547547

548548
// create decimal array with the specified precision and scale

0 commit comments

Comments
 (0)