rdblue commented on a change in pull request #3450:
URL: https://github.com/apache/iceberg/pull/3450#discussion_r783355906



##########
File path: python/src/iceberg/transforms.py
##########
@@ -0,0 +1,471 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import re
+import struct
+from typing import Any, Callable, Optional
+
+import mmh3  # type: ignore
+
+from iceberg.types import (
+    BinaryType,
+    DateType,
+    DecimalType,
+    DoubleType,
+    FixedType,
+    FloatType,
+    IntegerType,
+    LongType,
+    StringType,
+    TimestampType,
+    TimestamptzType,
+    TimeType,
+    Type,
+    UUIDType,
+)
+from iceberg.utils import transform_util
+
+
+class Transform:
+    """Transform base class for concrete transforms.
+
+    A base class to transform values and project predicates on partition 
values.
+    This class is not used directly. Instead, use one of module method to 
create the child classes.
+
+    Args:
+        transform_string (str): name of the transform type
+        repr_string (str): string representation of a transform instance
+        to_human_str (callable, optional): A function that returns the 
human-readable string
+          given a value. By default, the built-in `str` method is used.
+    """
+
+    def __init__(
+        self,
+        transform_string: str,
+        repr_string: str,
+        to_human_str: Callable[[Any], str] = str,
+    ):
+        self._transform_string = transform_string
+        self._repr_string = repr_string
+        self._to_human_string = to_human_str
+
+    def __repr__(self):
+        return self._repr_string
+
+    def __str__(self):
+        return self._transform_string
+
+    def apply(self, value):
+        raise NotImplementedError()
+
+    def can_transform(self, target: Type) -> bool:
+        return False
+
+    def result_type(self, source: Type) -> Type:
+        return source
+
+    def preserves_order(self) -> bool:
+        return False
+
+    def satisfies_order_of(self, other) -> bool:
+        return self == other
+
+    def to_human_string(self, value) -> str:
+        if value is None:
+            return "null"
+        return self._to_human_string(value)
+
+    def dedup_name(self) -> str:
+        return self._transform_string
+
+
+class Bucket(Transform):
+    """Transforms a value into a bucket partition value
+
+    Transforms are parameterized by a number of buckets. Bucket partition 
transforms use a 32-bit
+    hash of the source value to produce a positive value by mod the bucket 
number.
+
+    Args:
+      source_type (Type): An Iceberg Type of IntegerType, LongType, 
DecimalType, DateType, TimeType,
+      TimestampType, TimestamptzType, StringType, BinaryType, UUIDType, 
FloatType, or DoubleType.
+      num_buckets (int): The number of buckets.
+
+    Raises:
+      ValueError: If a type is provided that is incompatible with a Bucket 
transform
+    """
+
+    _MAX_32_BITS_INT = 2147483647
+    _INT_TRANSFORMABLE_TYPES = {
+        IntegerType,
+        DateType,
+        LongType,
+        TimeType,
+        TimestampType,
+        TimestamptzType,
+    }
+    _SAME_TRANSFORMABLE_TYPES = {
+        StringType,
+        BinaryType,
+        UUIDType,
+        FloatType,
+        DoubleType,
+    }
+
+    def __init__(self, source_type: Type, num_buckets: int):
+        super().__init__(
+            f"bucket[{num_buckets}]",
+            f"transforms.bucket(source_type={repr(source_type)}, 
num_buckets={num_buckets})",
+        )
+        self._type = source_type
+        self._num_buckets = num_buckets
+
+        if isinstance(self._type, FixedType) or isinstance(self._type, 
DecimalType):

Review comment:
       This still seems overly complicated to me.
   
   It looks like this is attempting to pass lambdas around to do the same thing 
as could be achieved by smaller implementations classes. What is the value of 
using lambdas instead of having an `BucketIntegerTransform` class? I think it 
would be more readable to have specific classes and select the one to use based 
on the type.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to