Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions datafusion/expr-common/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ pub enum Operator {
Plus,
/// Subtraction
Minus,
/// Multiplication operator, like `*`
/// Multiplication
Multiply,
/// Division operator, like `/`
/// Division
Divide,
/// Remainder operator, like `%`
/// Remainder
Modulo,
/// Logical AND, like `&&`
/// Logical AND
And,
/// Logical OR, like `||`
/// Logical OR
Or,
/// `IS DISTINCT FROM` (see [`distinct`])
///
Expand Down Expand Up @@ -80,20 +80,20 @@ pub enum Operator {
BitwiseShiftRight,
/// Bitwise left, like `<<`
BitwiseShiftLeft,
/// String concat
/// String concatenation, like `||`
StringConcat,
/// At arrow, like `@>`.
///
/// Currently only supported to be used with lists:
/// ```sql
/// select [1,3] <@ [1,2,3]
/// select [1,2,3] @> [1,3]
/// ```
AtArrow,
/// Arrow at, like `<@`.
///
/// Currently only supported to be used with lists:
/// ```sql
/// select [1,2,3] @> [1,3]
/// select [1,3] <@ [1,2,3]
/// ```
ArrowAt,
/// Arrow, like `->`.
Expand All @@ -120,7 +120,7 @@ pub enum Operator {
///
/// Not implemented in DataFusion yet.
IntegerDivide,
/// Hash Minis, like `#-`
/// Hash Minus, like `#-`
///
/// Not implemented in DataFusion yet.
HashMinus,
Expand Down Expand Up @@ -163,17 +163,17 @@ impl Operator {
Operator::ILikeMatch => Some(Operator::NotILikeMatch),
Operator::NotLikeMatch => Some(Operator::LikeMatch),
Operator::NotILikeMatch => Some(Operator::ILikeMatch),
Operator::RegexMatch => Some(Operator::RegexNotMatch),
Operator::RegexIMatch => Some(Operator::RegexNotIMatch),
Operator::RegexNotMatch => Some(Operator::RegexMatch),
Operator::RegexNotIMatch => Some(Operator::RegexIMatch),
Operator::Plus
| Operator::Minus
| Operator::Multiply
| Operator::Divide
| Operator::Modulo
| Operator::And
| Operator::Or
| Operator::RegexMatch
| Operator::RegexIMatch
| Operator::RegexNotMatch
| Operator::RegexNotIMatch
| Operator::BitwiseAnd
| Operator::BitwiseOr
| Operator::BitwiseXor
Expand Down Expand Up @@ -377,17 +377,16 @@ impl Operator {
| Operator::Question
| Operator::QuestionAnd
| Operator::QuestionPipe
| Operator::Colon => true,
| Operator::Colon
| Operator::StringConcat => true,

// E.g. `TRUE OR NULL` is `TRUE`
Operator::Or
// E.g. `FALSE AND NULL` is `FALSE`
| Operator::And
// IS DISTINCT FROM and IS NOT DISTINCT FROM always return a TRUE/FALSE value, never NULL
| Operator::IsDistinctFrom
| Operator::IsNotDistinctFrom
// DataFusion string concatenation operator treats NULL as an empty string
| Operator::StringConcat => false,
| Operator::IsNotDistinctFrom => false,
}
}

Expand Down
41 changes: 41 additions & 0 deletions datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2919,6 +2919,21 @@ mod tests {
}
}

#[test]
fn test_simplify_concat_by_null() {
let null = Expr::Literal(ScalarValue::Utf8(None), None);
// A || null --> null
{
let expr = binary_expr(col("c1"), Operator::StringConcat, null.clone());
assert_eq!(simplify(expr), null);
}
// null || A --> null
{
let expr = binary_expr(null.clone(), Operator::StringConcat, col("c1"));
assert_eq!(simplify(expr), null);
}
}

#[test]
fn test_simplify_composed_bitwise_and() {
// ((c2 > 5) & (c1 < 6)) & (c2 > 5) --> (c2 > 5) & (c1 < 6)
Expand Down Expand Up @@ -3538,6 +3553,32 @@ mod tests {
assert_no_change(regex_match(col("c1"), lit("foo|bar|baz|blarg|bozo|etc")));
}

#[test]
fn test_simplify_not_regex_match() {
let pattern = || lit("foo.*");

// NOT (c1 ~ pattern) --> c1 !~ pattern
assert_eq!(
simplify(regex_match(col("c1"), pattern()).not()),
regex_not_match(col("c1"), pattern()),
);
// NOT (c1 !~ pattern) --> c1 ~ pattern
assert_eq!(
simplify(regex_not_match(col("c1"), pattern()).not()),
regex_match(col("c1"), pattern()),
);
// NOT (c1 ~* pattern) --> c1 !~* pattern
assert_eq!(
simplify(regex_imatch(col("c1"), pattern()).not()),
regex_not_imatch(col("c1"), pattern()),
);
// NOT (c1 !~* pattern) --> c1 ~* pattern
assert_eq!(
simplify(regex_not_imatch(col("c1"), pattern()).not()),
regex_imatch(col("c1"), pattern()),
);
}

#[track_caller]
fn assert_no_change(expr: Expr) {
let optimized = simplify(expr.clone());
Expand Down
Loading