From 642848c5005bcba80377bed40c880b17660ce879 Mon Sep 17 00:00:00 2001 From: Ethan Knox Date: Mon, 31 Mar 2025 11:46:59 -0400 Subject: [PATCH] now supports quoted identifiers for colums --- pyiceberg/expressions/parser.py | 13 ++++++++++++- tests/expressions/test_parser.py | 6 ++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pyiceberg/expressions/parser.py b/pyiceberg/expressions/parser.py index bad2df9550..b9b6f9aba7 100644 --- a/pyiceberg/expressions/parser.py +++ b/pyiceberg/expressions/parser.py @@ -22,8 +22,10 @@ DelimitedList, Group, MatchFirst, + ParseException, ParserElement, ParseResults, + QuotedString, Suppress, Word, alphanums, @@ -79,7 +81,16 @@ LIKE = CaselessKeyword("like") unquoted_identifier = Word(alphas + "_", alphanums + "_$") -quoted_identifier = Suppress('"') + unquoted_identifier + Suppress('"') +quoted_identifier = QuotedString('"', escChar="\\", unquoteResults=True) + + +@quoted_identifier.set_parse_action +def validate_quoted_identifier(result: ParseResults) -> str: + if "." in result[0]: + raise ParseException("Expected '\"', found '.'") + return result[0] + + identifier = MatchFirst([unquoted_identifier, quoted_identifier]).set_results_name("identifier") column = DelimitedList(identifier, delim=".", combine=False).set_results_name("column") diff --git a/tests/expressions/test_parser.py b/tests/expressions/test_parser.py index 807aabeb6f..064fdb8f68 100644 --- a/tests/expressions/test_parser.py +++ b/tests/expressions/test_parser.py @@ -230,9 +230,11 @@ def test_quoted_column_with_dots() -> None: with pytest.raises(ParseException) as exc_info: parser.parse("\"foo.bar\".baz = 'data'") - assert "Expected '\"', found '.'" in str(exc_info.value) - with pytest.raises(ParseException) as exc_info: parser.parse("'foo.bar'.baz = 'data'") assert "Expected <= | <> | < | >= | > | == | = | !=, found '.'" in str(exc_info.value) + + +def test_quoted_column_with_spaces() -> None: + assert EqualTo("Foo Bar", "data") == parser.parse("\"Foo Bar\" = 'data'")