Skip to content

Commit 2983a7f

Browse files
authored
fix: incorrect nullability of Like expressions (apache#6829)
* fix: incorrect nullability of Like expr * Improve the documentation for ScalarType
1 parent bee2265 commit 2983a7f

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

datafusion/common/src/scalar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3802,7 +3802,7 @@ impl fmt::Debug for ScalarValue {
38023802
}
38033803
}
38043804

3805-
/// Trait used to map a NativeTime to a ScalarType.
3805+
/// Trait used to map a NativeType to a ScalarValue
38063806
pub trait ScalarType<T: ArrowNativeType> {
38073807
/// returns a scalar from an optional T
38083808
fn scalar(r: Option<T>) -> ScalarValue;

datafusion/expr/src/expr_schema.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,11 @@ impl ExprSchemable for Expr {
251251
ref right,
252252
..
253253
}) => Ok(left.nullable(input_schema)? || right.nullable(input_schema)?),
254-
Expr::Like(Like { expr, .. }) => expr.nullable(input_schema),
255-
Expr::ILike(Like { expr, .. }) => expr.nullable(input_schema),
256-
Expr::SimilarTo(Like { expr, .. }) => expr.nullable(input_schema),
254+
Expr::Like(Like { expr, pattern, .. })
255+
| Expr::ILike(Like { expr, pattern, .. })
256+
| Expr::SimilarTo(Like { expr, pattern, .. }) => {
257+
Ok(expr.nullable(input_schema)? || pattern.nullable(input_schema)?)
258+
}
257259
Expr::Wildcard => Err(DataFusionError::Internal(
258260
"Wildcard expressions are not valid in a logical query plan".to_owned(),
259261
)),
@@ -437,6 +439,22 @@ mod tests {
437439
assert!(expr.nullable(&get_schema(false)).unwrap());
438440
}
439441

442+
#[test]
443+
fn test_like_nullability() {
444+
let get_schema = |nullable| {
445+
MockExprSchema::new()
446+
.with_data_type(DataType::Utf8)
447+
.with_nullable(nullable)
448+
};
449+
450+
let expr = col("foo").like(lit("bar"));
451+
assert!(!expr.nullable(&get_schema(false)).unwrap());
452+
assert!(expr.nullable(&get_schema(true)).unwrap());
453+
454+
let expr = col("foo").like(lit(ScalarValue::Utf8(None)));
455+
assert!(expr.nullable(&get_schema(false)).unwrap());
456+
}
457+
440458
#[test]
441459
fn expr_schema_data_type() {
442460
let expr = col("foo");

0 commit comments

Comments
 (0)