-
Notifications
You must be signed in to change notification settings - Fork 412
feat: make LiteralPredicate serializable via internal IcebergBaseModel #2561
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
Changes from all commits
6d2e99b
07014e5
f985dc2
2092a0b
bad9859
b2ff877
f1bf81a
1a3e702
1b23faa
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 |
|---|---|---|
|
|
@@ -120,7 +120,7 @@ def _try_import(module_name: str, extras_name: Optional[str] = None) -> types.Mo | |
| raise NotInstalledError(msg) from None | ||
|
|
||
|
|
||
| def _transform_literal(func: Callable[[L], L], lit: Literal[L]) -> Literal[L]: | ||
| def _transform_literal(func: Callable[[Any], Any], lit: Literal[L]) -> Literal[L]: | ||
|
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. nit: is this change relevant? i dont see
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. In order to silence this mypy errors: |
||
| """Small helper to upwrap the value from the literal, and wrap it again.""" | ||
| return literal(func(lit.value)) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,19 +50,22 @@ | |
| IsNull, | ||
| LessThan, | ||
| LessThanOrEqual, | ||
| LiteralPredicate, | ||
| Not, | ||
| NotEqualTo, | ||
| NotIn, | ||
| NotNaN, | ||
| NotNull, | ||
| NotStartsWith, | ||
| Or, | ||
| Reference, | ||
| StartsWith, | ||
| UnboundPredicate, | ||
| ) | ||
| from pyiceberg.expressions.literals import Literal, literal | ||
| from pyiceberg.expressions.visitors import _from_byte_buffer | ||
| from pyiceberg.schema import Accessor, Schema | ||
| from pyiceberg.typedef import Record | ||
| from pyiceberg.typedef import L, Record | ||
| from pyiceberg.types import ( | ||
| DecimalType, | ||
| DoubleType, | ||
|
|
@@ -915,6 +918,7 @@ def test_bound_less_than_or_equal(term: BoundReference[Any]) -> None: | |
|
|
||
| def test_equal_to() -> None: | ||
| equal_to = EqualTo(Reference("a"), literal("a")) | ||
| assert equal_to.model_dump_json() == '{"term":"a","type":"eq","value":"a"}' | ||
| assert str(equal_to) == "EqualTo(term=Reference(name='a'), literal=literal('a'))" | ||
| assert repr(equal_to) == "EqualTo(term=Reference(name='a'), literal=literal('a'))" | ||
| assert equal_to == eval(repr(equal_to)) | ||
|
|
@@ -923,6 +927,7 @@ def test_equal_to() -> None: | |
|
|
||
| def test_not_equal_to() -> None: | ||
| not_equal_to = NotEqualTo(Reference("a"), literal("a")) | ||
| assert not_equal_to.model_dump_json() == '{"term":"a","type":"not-eq","value":"a"}' | ||
| assert str(not_equal_to) == "NotEqualTo(term=Reference(name='a'), literal=literal('a'))" | ||
| assert repr(not_equal_to) == "NotEqualTo(term=Reference(name='a'), literal=literal('a'))" | ||
| assert not_equal_to == eval(repr(not_equal_to)) | ||
|
|
@@ -931,6 +936,7 @@ def test_not_equal_to() -> None: | |
|
|
||
| def test_greater_than_or_equal_to() -> None: | ||
| greater_than_or_equal_to = GreaterThanOrEqual(Reference("a"), literal("a")) | ||
| assert greater_than_or_equal_to.model_dump_json() == '{"term":"a","type":"gt-eq","value":"a"}' | ||
| assert str(greater_than_or_equal_to) == "GreaterThanOrEqual(term=Reference(name='a'), literal=literal('a'))" | ||
| assert repr(greater_than_or_equal_to) == "GreaterThanOrEqual(term=Reference(name='a'), literal=literal('a'))" | ||
| assert greater_than_or_equal_to == eval(repr(greater_than_or_equal_to)) | ||
|
|
@@ -939,6 +945,7 @@ def test_greater_than_or_equal_to() -> None: | |
|
|
||
| def test_greater_than() -> None: | ||
| greater_than = GreaterThan(Reference("a"), literal("a")) | ||
| assert greater_than.model_dump_json() == '{"term":"a","type":"gt","value":"a"}' | ||
| assert str(greater_than) == "GreaterThan(term=Reference(name='a'), literal=literal('a'))" | ||
| assert repr(greater_than) == "GreaterThan(term=Reference(name='a'), literal=literal('a'))" | ||
| assert greater_than == eval(repr(greater_than)) | ||
|
|
@@ -947,6 +954,7 @@ def test_greater_than() -> None: | |
|
|
||
| def test_less_than() -> None: | ||
| less_than = LessThan(Reference("a"), literal("a")) | ||
| assert less_than.model_dump_json() == '{"term":"a","type":"lt","value":"a"}' | ||
| assert str(less_than) == "LessThan(term=Reference(name='a'), literal=literal('a'))" | ||
| assert repr(less_than) == "LessThan(term=Reference(name='a'), literal=literal('a'))" | ||
| assert less_than == eval(repr(less_than)) | ||
|
|
@@ -955,12 +963,23 @@ def test_less_than() -> None: | |
|
|
||
| def test_less_than_or_equal() -> None: | ||
| less_than_or_equal = LessThanOrEqual(Reference("a"), literal("a")) | ||
| assert less_than_or_equal.model_dump_json() == '{"term":"a","type":"lt-eq","value":"a"}' | ||
| assert str(less_than_or_equal) == "LessThanOrEqual(term=Reference(name='a'), literal=literal('a'))" | ||
| assert repr(less_than_or_equal) == "LessThanOrEqual(term=Reference(name='a'), literal=literal('a'))" | ||
| assert less_than_or_equal == eval(repr(less_than_or_equal)) | ||
| assert less_than_or_equal == pickle.loads(pickle.dumps(less_than_or_equal)) | ||
|
|
||
|
|
||
| def test_starts_with() -> None: | ||
| starts_with = StartsWith(Reference("a"), literal("a")) | ||
| assert starts_with.model_dump_json() == '{"term":"a","type":"starts-with","value":"a"}' | ||
|
|
||
|
|
||
| def test_not_starts_with() -> None: | ||
| not_starts_with = NotStartsWith(Reference("a"), literal("a")) | ||
| assert not_starts_with.model_dump_json() == '{"term":"a","type":"not-starts-with","value":"a"}' | ||
|
|
||
|
|
||
| def test_bound_reference_eval(table_schema_simple: Schema) -> None: | ||
| """Test creating a BoundReference and evaluating it on a StructProtocol""" | ||
| struct = Record("foovalue", 123, True) | ||
|
|
@@ -1199,7 +1218,15 @@ def test_bind_ambiguous_name() -> None: | |
| # |_| |_|\_, |_| \_, | | ||
| # |__/ |__/ | ||
|
|
||
| assert_type(EqualTo("a", "b"), EqualTo[str]) | ||
|
|
||
| def _assert_literal_predicate_type(expr: LiteralPredicate[L]) -> None: | ||
| assert_type(expr, LiteralPredicate[L]) | ||
|
|
||
|
|
||
| _assert_literal_predicate_type(EqualTo("a", "b")) | ||
| _assert_literal_predicate_type(In("a", ("a", "b", "c"))) | ||
| _assert_literal_predicate_type(In("a", (1, 2, 3))) | ||
| _assert_literal_predicate_type(NotIn("a", ("a", "b", "c"))) | ||
| assert_type(In("a", ("a", "b", "c")), In[str]) | ||
| assert_type(In("a", (1, 2, 3)), In[int]) | ||
| assert_type(NotIn("a", ("a", "b", "c")), NotIn[str]) | ||
|
Comment on lines
1230
to
1232
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. nit: should we use
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. Sure thing |
||
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.
👍
this matches the
LiteralExpressiondefinitionhttps://github.com/apache/iceberg/blob/b987e60bbd581d6e9e583107d5a85022261ff0d8/open-api/rest-catalog-open-api.yaml#L2264
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.
nit: could you include this in the PR description so its easily referenced in the future?
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.
done