Skip to content

Apache DataFusion SQL Query Engine

feat: allow scalars to appear as the LHS arg in arithmetic operations on temporal values (#6196)

Kyle McCarthy62127df207e6
diff --git a/datafusion/physical-expr/src/expressions/datetime.rs b/datafusion/physical-expr/src/expressions/datetime.rs
index b8a7146a08..6e7b8a43fa 100644
--- a/datafusion/physical-expr/src/expressions/datetime.rs
+++ b/datafusion/physical-expr/src/expressions/datetime.rs
@@ -120,16 +120,21 @@ impl PhysicalExpr for DateTimeIntervalExpr {
(ColumnarValue::Array(array_lhs), ColumnarValue::Scalar(array_rhs)) => {
resolve_temporal_op_scalar(&array_lhs, sign, &array_rhs)
}
+ // This function evaluates operations between a scalar value and an array of temporal
+ // values. One example is calculating the duration between a scalar timestamp and an
+ // array of timestamps (i.e. `now() - some_column`).
+ (ColumnarValue::Scalar(scalar_lhs), ColumnarValue::Array(array_rhs)) => {
+ let array_lhs = scalar_lhs.to_array_of_size(array_rhs.len());
+ Ok(ColumnarValue::Array(resolve_temporal_op(
+ &array_lhs, sign, &array_rhs,
+ )?))
+ }
// This function evaluates temporal array operations, such as timestamp - timestamp, interval + interval,
// timestamp + interval, and interval + timestamp. It takes two arrays as input and an integer sign representing
// the operation (+1 for addition and -1 for subtraction).
(ColumnarValue::Array(array_lhs), ColumnarValue::Array(array_rhs)) => Ok(
ColumnarValue::Array(resolve_temporal_op(&array_lhs, sign, &array_rhs)?),
),
- (_, _) => {
- let msg = "If RHS of the operation is an array, then LHS also must be";
- Err(DataFusionError::Internal(msg.to_string()))
- }
}
}