From 9a168fecd429860b54275a7c426bd330ff4310a1 Mon Sep 17 00:00:00 2001 From: Guillaume Marty Date: Wed, 25 Nov 2026 18:24:09 +0100 Subject: [PATCH] Unescape \_ in LIKE row filters The row filter parser unescaped \% but not \_, so an escaped underscore kept its backslash. "name LIKE 'tmp\_%'" became StartsWith("tmp\_"), which matches no rows, and a delete with that filter removed nothing. --- pyiceberg/expressions/parser.py | 8 ++++++-- tests/expressions/test_parser.py | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pyiceberg/expressions/parser.py b/pyiceberg/expressions/parser.py index 6150e3ce42..f4daa47e3c 100644 --- a/pyiceberg/expressions/parser.py +++ b/pyiceberg/expressions/parser.py @@ -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") diff --git a/tests/expressions/test_parser.py b/tests/expressions/test_parser.py index 581ee90b72..f1340d4487 100644 --- a/tests/expressions/test_parser.py +++ b/tests/expressions/test_parser.py @@ -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'"]