Skip to content
Open
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
8 changes: 6 additions & 2 deletions pyiceberg/expressions/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,13 @@ def _evaluate_like_statement(result: ParseResults) -> BooleanExpression:
if match and match.groupdict()["invalid_wildcard"]:
raise ValueError("LIKE expressions only supports wildcard, '%', at the end of a string")
elif match and match.groupdict()["valid_wildcard"]:
return StartsWith(result.column, StringLiteral(literal_like.value[:-1].replace("\\%", "%")))
return StartsWith(result.column, StringLiteral(_unescape_like(literal_like.value[:-1])))
else:
return EqualTo(result.column, StringLiteral(literal_like.value.replace("\\%", "%")))
return EqualTo(result.column, StringLiteral(_unescape_like(literal_like.value)))


def _unescape_like(pattern: str) -> str:
return pattern.replace("\\%", "%").replace("\\_", "_")


predicate = (between | comparison | in_check | null_check | nan_check | starts_check | boolean).set_results_name("predicate")
Expand Down
6 changes: 6 additions & 0 deletions tests/expressions/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ def test_starts_with() -> None:
assert StartsWith("foo", "some data%") == parser.parse("foo LIKE 'some data\\%%'")


def test_like_escaped_underscore() -> None:
assert EqualTo("foo", "some_data") == parser.parse("foo LIKE 'some\\_data'")
assert StartsWith("foo", "some_") == parser.parse("foo LIKE 'some\\_%'")
assert NotStartsWith("foo", "some_") == parser.parse("foo NOT LIKE 'some\\_%'")


def test_invalid_likes() -> None:
invalid_statements = ["foo LIKE '%data%'", "foo LIKE 'da%ta'", "foo LIKE '%data'"]

Expand Down
Loading