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
40 changes: 17 additions & 23 deletions pyiceberg/expressions/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ def _evaluate_like_statement(result: ParseResults) -> BooleanExpression:
predicate = (between | comparison | in_check | null_check | nan_check | starts_check | boolean).set_results_name("predicate")


@predicate.add_parse_action
def _(result: ParseResults) -> BooleanExpression:
# A bare "true" or "false" in an operand position folds to AlwaysTrue or AlwaysFalse
expr = result[0]
if isinstance(expr, BooleanLiteral):
return AlwaysTrue() if expr.value else AlwaysFalse()
return expr


def handle_not(result: ParseResults) -> Not:
return Not(result[0][0])

Expand All @@ -280,29 +289,14 @@ def handle_or(result: ParseResults) -> Or:
return Or(*result[0])


def handle_always_expression(result: ParseResults) -> BooleanExpression:
# If the entire result is "true" or "false", return AlwaysTrue or AlwaysFalse
expr = result[0]
if isinstance(expr, BooleanLiteral):
if expr.value:
return AlwaysTrue()
else:
return AlwaysFalse()
return result[0]


boolean_expression = (
infix_notation(
predicate,
[
(Suppress(NOT), 1, opAssoc.RIGHT, handle_not),
(Suppress(AND), 2, opAssoc.LEFT, handle_and),
(Suppress(OR), 2, opAssoc.LEFT, handle_or),
],
)
.set_name("expr")
.add_parse_action(handle_always_expression)
)
boolean_expression = infix_notation(
predicate,
[
(Suppress(NOT), 1, opAssoc.RIGHT, handle_not),
(Suppress(AND), 2, opAssoc.LEFT, handle_and),
(Suppress(OR), 2, opAssoc.LEFT, handle_or),
],
).set_name("expr")


def parse(expr: str) -> BooleanExpression:
Expand Down
22 changes: 22 additions & 0 deletions tests/expressions/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
AlwaysFalse,
AlwaysTrue,
And,
BooleanExpression,
EqualTo,
GreaterThan,
GreaterThanOrEqual,
Expand Down Expand Up @@ -272,3 +273,24 @@ def test_valid_between_with_numerics() -> None:
) == parser.parse("foo between '2025-01-01T00:00:00.000000' and '2025-01-10T12:00:00.000000'")

assert parser.parse("foo between 1 and 3") == parser.parse("1 <= foo and foo <= 3")


@pytest.mark.parametrize(
"expression, expected",
[
("true and foo = 1", EqualTo(Reference("foo"), literal(1))),
("foo = 1 and true", EqualTo(Reference("foo"), literal(1))),
("foo = 1 or false", EqualTo(Reference("foo"), literal(1))),
("foo = 1 or true", AlwaysTrue()),
("foo = 1 and false", AlwaysFalse()),
("not true", AlwaysFalse()),
("not false", AlwaysTrue()),
],
)
def test_boolean_as_operand(expression: str, expected: BooleanExpression) -> None:
assert parser.parse(expression) == expected


def test_boolean_as_literal_is_unchanged() -> None:
assert parser.parse("foo = true") == EqualTo(Reference("foo"), literal(True))
assert parser.parse("foo in (true, false)") == In(Reference("foo"), {literal(True), literal(False)})
Loading