Skip to content

Commit

Permalink
test: update similar-to-escape to test explicitly disable escape (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jetjinser authored Jan 9, 2024
1 parent 0c5abb4 commit 580a60b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
14 changes: 12 additions & 2 deletions e2e_test/batch/functions/similar_to_escape.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,22 @@ select 'foobar' similar to '%#"o_b#"%' escape '\';
----
f

# fallback to default escape string
# disable escape
query B
select 'foobar' similar to '%#"o_b#"%' escape '';
select 'foo\bar' similar to 'foo\%bar' escape '';
----
t

query B
select 'foo\bar' similar to 'foo\%bar' escape '\';
----
f

query B
select 'foo%bar' similar to 'foo\%bar' escape '\';
----
t

query B
select 'foobar' similar to '%#"o_b#"%' escape '#';
----
Expand Down
28 changes: 26 additions & 2 deletions src/expr/impl/src/scalar/similar_to_escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use risingwave_expr::{function, ExprError, Result};

// escape `similar-to` pattern to POSIX regex pattern
// Adapted from:
// https://github.com/postgres/postgres/blob/REL_16_STABLE/src/backend/utils/adt/regexp.c#L768
// https://github.com/postgres/postgres/blob/db4f21e4a34b1d5a3f7123e28e77f575d1a971ea/src/backend/utils/adt/regexp.c#L768
fn similar_escape_internal(
pat: &str,
esc_text: Option<char>,
Expand Down Expand Up @@ -58,7 +58,7 @@ fn similar_escape_internal(

afterescape = false;
}
c if esc_text.is_some() && c == esc_text.unwrap() => {
c if esc_text.is_some_and(|t| t == c) => {
afterescape = true;
}
c if incharclass => {
Expand Down Expand Up @@ -221,4 +221,28 @@ mod tests {
let res = similar_to_escape_with_escape_text(pat, "💅💅", &mut writer);
assert!(res.is_err())
}

#[test]
fn test_escape_with_escape_disabled() {
let cases = vec![
("", "^(?:)$"),
("_bcd%", "^(?:.bcd.*)$"),
("bcd%", "^(?:bcd.*)$"),
(r#"_bcd\%"#, r#"^(?:.bcd\\.*)$"#),
("bcd[]ee", "^(?:bcd[]ee)$"),
(r#"bcd[]ee"""#, r#"^(?:bcd[]ee"")$"#),
(r#"bcd[]"ee""#, r#"^(?:bcd[]"ee")$"#),
("bcd[pp]ee", "^(?:bcd[pp]ee)$"),
("bcd[pp_%.]ee", "^(?:bcd[pp_%.]ee)$"),
("bcd[pp_%.]ee_%.", r#"^(?:bcd[pp_%.]ee..*\.)$"#),
("bcd[pp_%.](ee_%.)", r#"^(?:bcd[pp_%.](?:ee..*\.))$"#),
(r#"%\"o_b\"%"#, r#"^(?:.*\\"o.b\\".*)$"#),
];

for (pat, escaped) in cases {
let mut writer = String::new();
similar_to_escape_with_escape_text(pat, "", &mut writer).ok();
assert_eq!(writer, escaped);
}
}
}

0 comments on commit 580a60b

Please sign in to comment.