-
Notifications
You must be signed in to change notification settings - Fork 501
Add CAST expression support to parser #3207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,17 @@ | |
| Reference, | ||
| StartsWith, | ||
| ) | ||
| from pyiceberg.expressions.literals import DecimalLiteral, LongLiteral, literal | ||
| from pyiceberg.expressions.literals import DateLiteral, DecimalLiteral, LongLiteral, StringLiteral, literal | ||
| from pyiceberg.schema import Schema | ||
| from pyiceberg.transforms import ( | ||
| BoundTransform, | ||
| DayTransform, | ||
| HourTransform, | ||
| MonthTransform, | ||
| UnboundTransform, | ||
| YearTransform, | ||
| ) | ||
| from pyiceberg.types import DateType, NestedField | ||
|
|
||
|
|
||
| def test_always_true() -> None: | ||
|
|
@@ -272,3 +282,94 @@ 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") | ||
|
|
||
|
|
||
| def test_cast_date_comparison() -> None: | ||
| expected = EqualTo( | ||
| UnboundTransform(Reference("created_at"), DayTransform()), | ||
| StringLiteral("2024-01-01"), | ||
| ) | ||
|
Comment on lines
+288
to
+291
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For testing, I think it would be good to have an integration test as well. The
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a unit test for this. |
||
| assert expected == parser.parse("CAST(created_at AS date) = '2024-01-01'") | ||
|
|
||
|
|
||
| def test_cast_date_binds_string_literal_to_date() -> None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have more tests like this with uneven types. I posted an example in another comment. |
||
| """Binding a CAST(... AS date) predicate converts the StringLiteral to a DateLiteral.""" | ||
| schema = Schema(NestedField(1, "created_at", DateType(), required=False)) | ||
| bound = parser.parse("CAST(created_at AS date) = '2024-01-01'").bind(schema) | ||
| assert isinstance(bound.term, BoundTransform) | ||
| assert isinstance(bound.literal, DateLiteral) | ||
|
|
||
|
|
||
| def test_cast_year_comparison() -> None: | ||
| expected = GreaterThan( | ||
| UnboundTransform(Reference("ts"), YearTransform()), | ||
| LongLiteral(2020), | ||
| ) | ||
| assert expected == parser.parse("CAST(ts AS year) > 2020") | ||
|
|
||
|
|
||
| def test_cast_month_comparison() -> None: | ||
| expected = EqualTo( | ||
| UnboundTransform(Reference("ts"), MonthTransform()), | ||
| LongLiteral(6), | ||
| ) | ||
| assert expected == parser.parse("CAST(ts AS month) = 6") | ||
|
|
||
|
|
||
| def test_cast_hour_comparison() -> None: | ||
| expected = GreaterThanOrEqual( | ||
| UnboundTransform(Reference("ts"), HourTransform()), | ||
| LongLiteral(12), | ||
| ) | ||
| assert expected == parser.parse("CAST(ts AS hour) >= 12") | ||
|
|
||
|
|
||
| def test_cast_case_insensitive() -> None: | ||
| """CAST keyword and type name should be case-insensitive.""" | ||
| expected = EqualTo( | ||
| UnboundTransform(Reference("created_at"), DayTransform()), | ||
| StringLiteral("2024-01-01"), | ||
| ) | ||
| assert expected == parser.parse("cast(created_at as DATE) = '2024-01-01'") | ||
|
|
||
|
|
||
| def test_cast_nested_field() -> None: | ||
| """CAST should work with dotted column references.""" | ||
| expected = EqualTo( | ||
| UnboundTransform(Reference("event.timestamp"), DayTransform()), | ||
| StringLiteral("2024-01-01"), | ||
| ) | ||
| assert expected == parser.parse("CAST(event.timestamp AS date) = '2024-01-01'") | ||
|
|
||
|
|
||
| def test_cast_unsupported_type() -> None: | ||
| with pytest.raises(ValueError, match="Unsupported CAST target type"): | ||
| parser.parse("CAST(col AS foobar) = 5") | ||
|
|
||
|
|
||
| def test_cast_not_equal() -> None: | ||
| expected = NotEqualTo( | ||
| UnboundTransform(Reference("ts"), YearTransform()), | ||
| LongLiteral(2020), | ||
| ) | ||
| assert expected == parser.parse("CAST(ts AS year) != 2020") | ||
|
|
||
|
|
||
| def test_cast_less_than_or_equal() -> None: | ||
| expected = LessThanOrEqual( | ||
| UnboundTransform(Reference("ts"), MonthTransform()), | ||
| LongLiteral(6), | ||
| ) | ||
| assert expected == parser.parse("CAST(ts AS month) <= 6") | ||
|
|
||
|
|
||
| def test_cast_with_and() -> None: | ||
| """CAST predicates compose with AND/OR via infix_notation.""" | ||
| result = parser.parse("CAST(ts AS date) = '2024-01-01' and status = 'active'") | ||
| assert isinstance(result, And) | ||
|
|
||
|
|
||
| def test_cast_with_not() -> None: | ||
| """NOT should negate a CAST predicate.""" | ||
| result = parser.parse("not CAST(ts AS date) = '2024-01-01'") | ||
| assert isinstance(result, Not) | ||
Uh oh!
There was an error while loading. Please reload this page.