rdblue commented on a change in pull request #3450: URL: https://github.com/apache/iceberg/pull/3450#discussion_r750452819
########## File path: python/src/iceberg/transforms.py ########## @@ -0,0 +1,446 @@ +# 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 datetime import datetime +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. The default implementation is for VoidTransform. + """ + + def __init__( + self, + transform_string: str, + repr_string: str, + to_human_str: Callable[[Any], str] = transform_util.to_string, + ): + 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): + _MAX_32_BITS_INT = 2147483647 + _FUNCTIONS_MAP = { # [0] is hash function and [1] is can_transform check function + DateType: ( + lambda v: mmh3.hash(struct.pack("q", v)), + lambda t: t in [IntegerType, DateType], + ), + IntegerType: ( + lambda v: mmh3.hash(struct.pack("q", v)), + lambda t: t in [IntegerType, DateType], + ), + TimeType: ( + lambda v: mmh3.hash(struct.pack("q", v)), + lambda t: t in {LongType, TimeType, TimestampType, TimestamptzType}, + ), + TimestampType: ( + lambda v: mmh3.hash(struct.pack("q", v)), + lambda t: t in {LongType, TimeType, TimestampType, TimestamptzType}, + ), + TimestamptzType: ( + lambda v: mmh3.hash(struct.pack("q", v)), + lambda t: t in {LongType, TimeType, TimestampType, TimestamptzType}, + ), + LongType: ( + lambda v: mmh3.hash(struct.pack("q", v)), + lambda t: t in [LongType, TimeType, TimestampType, TimestamptzType], + ), + StringType: ( + lambda v: mmh3.hash(v), + lambda t: t == StringType, + ), + BinaryType: ( + lambda v: mmh3.hash(v), + lambda t: t == BinaryType, + ), + UUIDType: ( + lambda v: mmh3.hash( + struct.pack( + ">QQ", + (v.int >> 64) & 0xFFFFFFFFFFFFFFFF, + v.int & 0xFFFFFFFFFFFFFFFF, + ) + ), + lambda t: t == UUIDType, + ), + # bucketing by Float/Double is not allowed by the spec, but they have hash implementation + FloatType: ( + lambda v: mmh3.hash(struct.pack("d", v)), + lambda t: t == FloatType, + ), + DoubleType: ( + lambda v: mmh3.hash(struct.pack("d", v)), + lambda t: t == DoubleType, + ), + } + + def __init__(self, source_type: Type, num_buckets: int): + if ( + source_type not in Bucket._FUNCTIONS_MAP + and not isinstance(source_type, FixedType) + and not isinstance(source_type, DecimalType) + ): + raise ValueError(f"Cannot bucket by type: {source_type}") + + 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): + self._hash_func = lambda v: mmh3.hash(v) + self._can_transform = lambda t: isinstance(t, FixedType) Review comment: It's hard to read this class having some of these inline and others in the map. Can you move all of these implementations to the same place? -- 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]
