-
Notifications
You must be signed in to change notification settings - Fork 485
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
Open
nssalian
wants to merge
1
commit into
apache:main
Choose a base branch
from
nssalian:cast-expression-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,14 @@ | |
| Reference, | ||
| StartsWith, | ||
| ) | ||
| from pyiceberg.expressions.literals import DecimalLiteral, LongLiteral, literal | ||
| from pyiceberg.expressions.literals import DecimalLiteral, LongLiteral, StringLiteral, literal | ||
| from pyiceberg.transforms import ( | ||
| DayTransform, | ||
| HourTransform, | ||
| MonthTransform, | ||
| UnboundTransform, | ||
| YearTransform, | ||
| ) | ||
|
|
||
|
|
||
| def test_always_true() -> None: | ||
|
|
@@ -272,3 +279,86 @@ 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
+285
to
+288
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 |
||
| assert expected == parser.parse("CAST(created_at AS date) = '2024-01-01'") | ||
|
|
||
|
|
||
| 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) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we've already copied this logic multiple times, should we consolidate this:
iceberg-python/pyiceberg/expressions/parser.py
Lines 161 to 192 in d008a04