Skip to content

Commit 7dc625c

Browse files
committed
.
1 parent 971cf99 commit 7dc625c

2 files changed

Lines changed: 57 additions & 17 deletions

File tree

datafusion/expr-common/src/operator.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ pub enum Operator {
3636
Plus,
3737
/// Subtraction
3838
Minus,
39-
/// Multiplication operator, like `*`
39+
/// Multiplication
4040
Multiply,
41-
/// Division operator, like `/`
41+
/// Division
4242
Divide,
43-
/// Remainder operator, like `%`
43+
/// Remainder
4444
Modulo,
45-
/// Logical AND, like `&&`
45+
/// Logical AND
4646
And,
47-
/// Logical OR, like `||`
47+
/// Logical OR
4848
Or,
4949
/// `IS DISTINCT FROM` (see [`distinct`])
5050
///
@@ -80,20 +80,20 @@ pub enum Operator {
8080
BitwiseShiftRight,
8181
/// Bitwise left, like `<<`
8282
BitwiseShiftLeft,
83-
/// String concat
83+
/// String concatenation, like `||`
8484
StringConcat,
8585
/// At arrow, like `@>`.
8686
///
8787
/// Currently only supported to be used with lists:
8888
/// ```sql
89-
/// select [1,3] <@ [1,2,3]
89+
/// select [1,2,3] @> [1,3]
9090
/// ```
9191
AtArrow,
9292
/// Arrow at, like `<@`.
9393
///
9494
/// Currently only supported to be used with lists:
9595
/// ```sql
96-
/// select [1,2,3] @> [1,3]
96+
/// select [1,3] <@ [1,2,3]
9797
/// ```
9898
ArrowAt,
9999
/// Arrow, like `->`.
@@ -120,7 +120,7 @@ pub enum Operator {
120120
///
121121
/// Not implemented in DataFusion yet.
122122
IntegerDivide,
123-
/// Hash Minis, like `#-`
123+
/// Hash Minus, like `#-`
124124
///
125125
/// Not implemented in DataFusion yet.
126126
HashMinus,
@@ -163,17 +163,17 @@ impl Operator {
163163
Operator::ILikeMatch => Some(Operator::NotILikeMatch),
164164
Operator::NotLikeMatch => Some(Operator::LikeMatch),
165165
Operator::NotILikeMatch => Some(Operator::ILikeMatch),
166+
Operator::RegexMatch => Some(Operator::RegexNotMatch),
167+
Operator::RegexIMatch => Some(Operator::RegexNotIMatch),
168+
Operator::RegexNotMatch => Some(Operator::RegexMatch),
169+
Operator::RegexNotIMatch => Some(Operator::RegexIMatch),
166170
Operator::Plus
167171
| Operator::Minus
168172
| Operator::Multiply
169173
| Operator::Divide
170174
| Operator::Modulo
171175
| Operator::And
172176
| Operator::Or
173-
| Operator::RegexMatch
174-
| Operator::RegexIMatch
175-
| Operator::RegexNotMatch
176-
| Operator::RegexNotIMatch
177177
| Operator::BitwiseAnd
178178
| Operator::BitwiseOr
179179
| Operator::BitwiseXor
@@ -377,17 +377,16 @@ impl Operator {
377377
| Operator::Question
378378
| Operator::QuestionAnd
379379
| Operator::QuestionPipe
380-
| Operator::Colon => true,
380+
| Operator::Colon
381+
| Operator::StringConcat => true,
381382

382383
// E.g. `TRUE OR NULL` is `TRUE`
383384
Operator::Or
384385
// E.g. `FALSE AND NULL` is `FALSE`
385386
| Operator::And
386387
// IS DISTINCT FROM and IS NOT DISTINCT FROM always return a TRUE/FALSE value, never NULL
387388
| Operator::IsDistinctFrom
388-
| Operator::IsNotDistinctFrom
389-
// DataFusion string concatenation operator treats NULL as an empty string
390-
| Operator::StringConcat => false,
389+
| Operator::IsNotDistinctFrom => false,
391390
}
392391
}
393392

datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,6 +2919,21 @@ mod tests {
29192919
}
29202920
}
29212921

2922+
#[test]
2923+
fn test_simplify_concat_by_null() {
2924+
let null = Expr::Literal(ScalarValue::Utf8(None), None);
2925+
// A || null --> null
2926+
{
2927+
let expr = binary_expr(col("c1"), Operator::StringConcat, null.clone());
2928+
assert_eq!(simplify(expr), null);
2929+
}
2930+
// null || A --> null
2931+
{
2932+
let expr = binary_expr(null.clone(), Operator::StringConcat, col("c1"));
2933+
assert_eq!(simplify(expr), null);
2934+
}
2935+
}
2936+
29222937
#[test]
29232938
fn test_simplify_composed_bitwise_and() {
29242939
// ((c2 > 5) & (c1 < 6)) & (c2 > 5) --> (c2 > 5) & (c1 < 6)
@@ -3538,6 +3553,32 @@ mod tests {
35383553
assert_no_change(regex_match(col("c1"), lit("foo|bar|baz|blarg|bozo|etc")));
35393554
}
35403555

3556+
#[test]
3557+
fn test_simplify_not_regex_match() {
3558+
let pattern = || lit("foo.*");
3559+
3560+
// NOT (c1 ~ pattern) --> c1 !~ pattern
3561+
assert_eq!(
3562+
simplify(regex_match(col("c1"), pattern()).not()),
3563+
regex_not_match(col("c1"), pattern()),
3564+
);
3565+
// NOT (c1 !~ pattern) --> c1 ~ pattern
3566+
assert_eq!(
3567+
simplify(regex_not_match(col("c1"), pattern()).not()),
3568+
regex_match(col("c1"), pattern()),
3569+
);
3570+
// NOT (c1 ~* pattern) --> c1 !~* pattern
3571+
assert_eq!(
3572+
simplify(regex_imatch(col("c1"), pattern()).not()),
3573+
regex_not_imatch(col("c1"), pattern()),
3574+
);
3575+
// NOT (c1 !~* pattern) --> c1 ~* pattern
3576+
assert_eq!(
3577+
simplify(regex_not_imatch(col("c1"), pattern()).not()),
3578+
regex_imatch(col("c1"), pattern()),
3579+
);
3580+
}
3581+
35413582
#[track_caller]
35423583
fn assert_no_change(expr: Expr) {
35433584
let optimized = simplify(expr.clone());

0 commit comments

Comments
 (0)