Skip to content

Make UnaryPredicate predicate JSON serializable #2522

Description

@Fokko

Feature Request / Improvement

Make sure that the UnaryPredicate predicate can be serialized to JSON:

classUnaryPredicate(UnboundPredicate[Any], ABC):
defbind(self, schema: Schema, case_sensitive: bool=True) ->BoundUnaryPredicate[Any]:
bound_term=self.term.bind(schema, case_sensitive)
returnself.as_bound(bound_term)
def__repr__(self) ->str:
"""Return the string representation of the UnaryPredicate class."""
returnf"{str(self.__class__.__name__)}(term={repr(self.term)})"
@property
@abstractmethod
defas_bound(self) ->Type[BoundUnaryPredicate[Any]]: ...

This predicate has four implementations: IsNull, NotNull, IsNaN, and NotNan, and translates to:

{
"type": "is-null"// Or not-null, is-nan, not-nan"term": str, // The column name
}

We use Pydantic for JSON serialization, which can be enabled by deriving from the IcebergBaseModel:

classPartitionSpec(IcebergBaseModel):

Example tests can be found here:

deftest_serialize_partition_spec() ->None:
partitioned=PartitionSpec(
PartitionField(source_id=1, field_id=1000, transform=TruncateTransform(width=19), name="str_truncate"),
PartitionField(source_id=2, field_id=1001, transform=BucketTransform(num_buckets=25), name="int_bucket"),
spec_id=3,
)
assert (
partitioned.model_dump_json()
=="""{"spec-id":3,"fields":[{"source-id":1,"field-id":1000,"transform":"truncate[19]","name":"str_truncate"},{"source-id":2,"field-id":1001,"transform":"bucket[25]","name":"int_bucket"}]}"""
)
deftest_deserialize_unpartition_spec() ->None:
json_partition_spec="""{"spec-id":0,"fields":[]}"""
spec=PartitionSpec.model_validate_json(json_partition_spec)
assertspec==PartitionSpec(spec_id=0)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions