Skip to content

Commit

Permalink
Support LIKE with ESCAPE \ (#13312)
Browse files Browse the repository at this point in the history
* Support LIKE with ESCAPE `\`

Other escape characters are currently not supported.

* fmt
  • Loading branch information
findepi authored Nov 24, 2024
1 parent b122bab commit b42cb8b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
7 changes: 5 additions & 2 deletions datafusion/physical-expr/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,11 @@ pub fn create_physical_expr(
escape_char,
case_insensitive,
}) => {
if escape_char.is_some() {
return exec_err!("LIKE does not support escape_char");
// `\` is the implicit escape, see https://github.com/apache/datafusion/issues/13291
if escape_char.unwrap_or('\\') != '\\' {
return exec_err!(
"LIKE does not support escape_char other than the backslash (\\)"
);
}
let physical_expr =
create_physical_expr(expr, input_dfschema, execution_props)?;
Expand Down
28 changes: 24 additions & 4 deletions datafusion/sqllogictest/test_files/string/string_literal.slt
Original file line number Diff line number Diff line change
Expand Up @@ -861,23 +861,43 @@ SELECT
----
false false true false true false

# \ as an explicit escape character is currently not supported
query error DataFusion error: Execution error: LIKE does not support escape_char
query BBBB
SELECT
'a' LIKE '\%' ESCAPE '\',
'\a' LIKE '\%' ESCAPE '\',
'%' LIKE '\%' ESCAPE '\',
'\%' LIKE '\%' ESCAPE '\'
----
false false true false

# \ as an explicit escape character is currently not supported
query error DataFusion error: Execution error: LIKE does not support escape_char
query BBBBBB
SELECT
'a' LIKE '\_' ESCAPE '\',
'\a' LIKE '\_' ESCAPE '\',
'_' LIKE '\_' ESCAPE '\',
'\_' LIKE '\_' ESCAPE '\',
'abc' LIKE 'a_c' ESCAPE '\',
'abc' LIKE 'a\_c' ESCAPE '\'
----
false false true false true false

# Only \ is currently supported as an explicit escape character
query error DataFusion error: Execution error: LIKE does not support escape_char other than the backslash \(\\\)
SELECT
'a' LIKE '$%' ESCAPE '$',
'\a' LIKE '$%' ESCAPE '$',
'%' LIKE '$%' ESCAPE '$',
'\%' LIKE '$%' ESCAPE '$'

# Only \ is currently supported as an explicit escape character
query error DataFusion error: Execution error: LIKE does not support escape_char other than the backslash \(\\\)
SELECT
'a' LIKE '$_' ESCAPE '$',
'\a' LIKE '$_' ESCAPE '$',
'_' LIKE '$_' ESCAPE '$',
'\_' LIKE '$_' ESCAPE '$',
'abc' LIKE 'a_c' ESCAPE '$',
'abc' LIKE 'a$_c' ESCAPE '$'

# a LIKE pattern containing escape can never match an empty string
query BBBBB
Expand Down

0 comments on commit b42cb8b

Please sign in to comment.