goldmedal commented on code in PR #13260:
URL: https://github.com/apache/datafusion/pull/13260#discussion_r1830993512
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -3633,32 +3697,123 @@ mod tests {
#[test]
fn test_like_and_ilke() {
- // LIKE '%'
- let expr = like(col("c1"), "%");
+ let null = lit(ScalarValue::Utf8(None));
+
+ // expr [NOT] [I]LIKE NULL
+ let expr = like(col("c1"), null.clone());
+ assert_eq!(simplify(expr), lit_bool_null());
+
+ let expr = not_like(col("c1"), null.clone());
+ assert_eq!(simplify(expr), lit_bool_null());
+
+ let expr = ilike(col("c1"), null.clone());
+ assert_eq!(simplify(expr), lit_bool_null());
+
+ let expr = not_ilike(col("c1"), null.clone());
+ assert_eq!(simplify(expr), lit_bool_null());
+
+ // expr [NOT] [I]LIKE '%'
+ let expr = like(col("c1"), lit("%"));
+ assert_eq!(simplify(expr), if_not_null(col("c1"), true));
+
+ let expr = not_like(col("c1"), lit("%"));
+ assert_eq!(simplify(expr), if_not_null(col("c1"), false));
+
+ let expr = ilike(col("c1"), lit("%"));
+ assert_eq!(simplify(expr), if_not_null(col("c1"), true));
+
+ let expr = not_ilike(col("c1"), lit("%"));
+ assert_eq!(simplify(expr), if_not_null(col("c1"), false));
+
+ // expr [NOT] [I]LIKE '%%'
Review Comment:
> I thought `expr LIKE '%%'` means the same as `expr = '%'` (match the
actual literal character `%` )
>
> Specifically that `%%` is how to escape the wildcard to not be a wildcard
I think it depends on how we define it. I assume we prefer to follow the
Postgres behavior. 🤔
In Postgres, `%%` doesn't mean matching the actual literal '%'. In Postgres,
the escape character is `\`.
```
test=# select '1' like '%%';
?column?
----------
t
(1 row)
test=# select '%' like '%%';
?column?
----------
t
(1 row)
test=# select '%' like '\%';
?column?
----------
t
(1 row)
test=# select '1' like '\%';
?column?
----------
f
(1 row)
```
I think it is just a redundant wildcard. So, we can simplify it to be only
one `%`.
By the way, in DuckDB, there is a similar behavior for double '%%'.
```
D select '1' like '%%';
┌───────────────┐
│ ('1' ~~ '%%') │
│ boolean │
├───────────────┤
│ true │
└───────────────┘
D select '%' like '%%';
┌───────────────┐
│ ('%' ~~ '%%') │
│ boolean │
├───────────────┤
│ true │
└───────────────┘
````
However, the escape character should be set by `ESCAPE` syntax
```
D select '%' like '\%';
┌───────────────┐
│ ('%' ~~ '\%') │
│ boolean │
├───────────────┤
│ false │
└───────────────┘
D select '%' like '\%' escape '\';
┌──────────────────────────────────┐
│ main.like_escape('%', '\%', '\') │
│ boolean │
├──────────────────────────────────┤
│ true │
└──────────────────────────────────┘
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]