Skip to content
Merged
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
32 changes: 31 additions & 1 deletion python/src/iceberg/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def __init__(self, source_type: IcebergType, transform: str):
self._type = source_type
self._transform = transform

def apply(self, value):
def apply(self, value: Optional[S]):
raise AttributeError(f"Cannot apply unsupported transform: {self}")

def can_transform(self, target: IcebergType) -> bool:
Expand All @@ -244,6 +244,32 @@ def result_type(self, source: IcebergType) -> IcebergType:
return StringType()


class VoidTransform(Transform):
"""A transform that always returns None"""

_instance = None

def __new__(cls):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add a test for this:

def test_check_if_identical():
    assert transforms.always_null() is transforms.always_null()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Updated the test.

if cls._instance is None:
cls._instance = super(VoidTransform, cls).__new__(cls)
return cls._instance

def __init__(self):
super().__init__("void", "transforms.always_null()")

def apply(self, value: Optional[S]) -> None:
return None

def can_transform(self, target: IcebergType) -> bool:
return True

def result_type(self, source: IcebergType) -> IcebergType:
return source

def to_human_string(self, value: Optional[S]) -> str:
return "null"


def bucket(source_type: IcebergType, num_buckets: int) -> BaseBucketTransform:
if type(source_type) in {IntegerType, LongType, DateType, TimeType, TimestampType, TimestamptzType}:
return BucketNumberTransform(source_type, num_buckets)
Expand All @@ -259,3 +285,7 @@ def bucket(source_type: IcebergType, num_buckets: int) -> BaseBucketTransform:
return BucketUUIDTransform(num_buckets)
else:
raise ValueError(f"Cannot bucket by type: {source_type}")


def always_null() -> Transform:
return VoidTransform()
14 changes: 14 additions & 0 deletions python/tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,17 @@ def test_unknown_transform():
assert unknown_transform.can_transform(FixedType(8))
assert not unknown_transform.can_transform(FixedType(5))
assert isinstance(unknown_transform.result_type(BooleanType()), StringType)


def test_void_transform():
void_transform = transforms.always_null()
assert void_transform is transforms.always_null()
assert void_transform == eval(repr(void_transform))
assert void_transform.apply("test") is None
assert void_transform.can_transform(BooleanType())
assert isinstance(void_transform.result_type(BooleanType()), BooleanType)
assert not void_transform.preserves_order
assert void_transform.satisfies_order_of(transforms.always_null())
assert not void_transform.satisfies_order_of(transforms.bucket(DateType(), 100))
assert void_transform.to_human_string("test") == "null"
assert void_transform.dedup_name == "void"