Skip to content

Commit

Permalink
fix: incorrect nullability of Like expressions (apache#6829)
Browse files Browse the repository at this point in the history
* fix: incorrect nullability of Like expr

* Improve the documentation for ScalarType
  • Loading branch information
jonahgao authored and yukkit committed Jul 5, 2023
1 parent 2410c6e commit b3a8ab9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion datafusion/common/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3802,7 +3802,7 @@ impl fmt::Debug for ScalarValue {
}
}

/// Trait used to map a NativeTime to a ScalarType.
/// Trait used to map a NativeType to a ScalarValue
pub trait ScalarType<T: ArrowNativeType> {
/// returns a scalar from an optional T
fn scalar(r: Option<T>) -> ScalarValue;
Expand Down
24 changes: 21 additions & 3 deletions datafusion/expr/src/expr_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,11 @@ impl ExprSchemable for Expr {
ref right,
..
}) => Ok(left.nullable(input_schema)? || right.nullable(input_schema)?),
Expr::Like(Like { expr, .. }) => expr.nullable(input_schema),
Expr::ILike(Like { expr, .. }) => expr.nullable(input_schema),
Expr::SimilarTo(Like { expr, .. }) => expr.nullable(input_schema),
Expr::Like(Like { expr, pattern, .. })
| Expr::ILike(Like { expr, pattern, .. })
| Expr::SimilarTo(Like { expr, pattern, .. }) => {
Ok(expr.nullable(input_schema)? || pattern.nullable(input_schema)?)
}
Expr::Wildcard => Err(DataFusionError::Internal(
"Wildcard expressions are not valid in a logical query plan".to_owned(),
)),
Expand Down Expand Up @@ -449,6 +451,22 @@ mod tests {
assert!(expr.nullable(&get_schema(false)).unwrap());
}

#[test]
fn test_like_nullability() {
let get_schema = |nullable| {
MockExprSchema::new()
.with_data_type(DataType::Utf8)
.with_nullable(nullable)
};

let expr = col("foo").like(lit("bar"));
assert!(!expr.nullable(&get_schema(false)).unwrap());
assert!(expr.nullable(&get_schema(true)).unwrap());

let expr = col("foo").like(lit(ScalarValue::Utf8(None)));
assert!(expr.nullable(&get_schema(false)).unwrap());
}

#[test]
fn expr_schema_data_type() {
let expr = col("foo");
Expand Down

0 comments on commit b3a8ab9

Please sign in to comment.