Fokko commented on code in PR #7831:
URL: https://github.com/apache/iceberg/pull/7831#discussion_r1279077604
##########
python/pyiceberg/io/pyarrow.py:
##########
@@ -1039,35 +1042,49 @@ def map_value_partner(self, partner_map:
Optional[pa.Array]) -> Optional[pa.Arra
return partner_map.items if isinstance(partner_map, pa.MapArray) else
None
+_PRIMITIVE_TO_PHYSICAL = {
+ BooleanType(): "BOOLEAN",
+ IntegerType(): "INT32",
+ LongType(): "INT64",
+ FloatType(): "FLOAT",
+ DoubleType(): "DOUBLE",
+ DateType(): "INT32",
+ TimeType(): "INT64",
+ TimestampType(): "INT64",
+ TimestamptzType(): "INT64",
+ StringType(): "BYTE_ARRAY",
+ UUIDType(): "FIXED_LEN_BYTE_ARRAY",
+ BinaryType(): "BYTE_ARRAY",
+}
+_PHISICAL_TYPES = ["BOOLEAN", "INT32", "INT64", "INT96", "FLOAT", "DOUBLE",
"BYTE_ARRAY", "FIXED_LEN_BYTE_ARRAY"]
+
+
class StatsAggregator:
- def __init__(self, type_string: str, trunc_length: Optional[int] = None)
-> None:
+ def __init__(self, iceberg_type: PrimitiveType, physical_type_string: str,
trunc_length: Optional[int] = None) -> None:
self.current_min: Any = None
self.current_max: Any = None
self.trunc_length = trunc_length
- self.primitive_type: Optional[PrimitiveType] = None
-
- if type_string == "BOOLEAN":
- self.primitive_type = BooleanType()
- elif type_string == "INT32":
- self.primitive_type = IntegerType()
- elif type_string == "INT64":
- self.primitive_type = LongType()
- elif type_string == "INT96":
+
+ assert physical_type_string in _PHISICAL_TYPES, f"Unknown physical
type {physical_type_string}"
Review Comment:
Can you raise a `ValueError` instead? We try to avoid using `assert` outside
of `tests/`
##########
python/pyiceberg/io/pyarrow.py:
##########
@@ -1039,35 +1042,49 @@ def map_value_partner(self, partner_map:
Optional[pa.Array]) -> Optional[pa.Arra
return partner_map.items if isinstance(partner_map, pa.MapArray) else
None
+_PRIMITIVE_TO_PHYSICAL = {
+ BooleanType(): "BOOLEAN",
+ IntegerType(): "INT32",
+ LongType(): "INT64",
+ FloatType(): "FLOAT",
+ DoubleType(): "DOUBLE",
+ DateType(): "INT32",
+ TimeType(): "INT64",
+ TimestampType(): "INT64",
+ TimestamptzType(): "INT64",
+ StringType(): "BYTE_ARRAY",
+ UUIDType(): "FIXED_LEN_BYTE_ARRAY",
+ BinaryType(): "BYTE_ARRAY",
+}
+_PHISICAL_TYPES = ["BOOLEAN", "INT32", "INT64", "INT96", "FLOAT", "DOUBLE",
"BYTE_ARRAY", "FIXED_LEN_BYTE_ARRAY"]
+
+
class StatsAggregator:
- def __init__(self, type_string: str, trunc_length: Optional[int] = None)
-> None:
+ def __init__(self, iceberg_type: PrimitiveType, physical_type_string: str,
trunc_length: Optional[int] = None) -> None:
self.current_min: Any = None
self.current_max: Any = None
self.trunc_length = trunc_length
- self.primitive_type: Optional[PrimitiveType] = None
-
- if type_string == "BOOLEAN":
- self.primitive_type = BooleanType()
- elif type_string == "INT32":
- self.primitive_type = IntegerType()
- elif type_string == "INT64":
- self.primitive_type = LongType()
- elif type_string == "INT96":
+
+ assert physical_type_string in _PHISICAL_TYPES, f"Unknown physical
type {physical_type_string}"
+ if physical_type_string == "INT96":
raise NotImplementedError("Statistics not implemented for INT96
physical type")
- elif type_string == "FLOAT":
- self.primitive_type = FloatType()
- elif type_string == "DOUBLE":
- self.primitive_type = DoubleType()
- elif type_string == "BYTE_ARRAY":
- self.primitive_type = BinaryType()
- elif type_string == "FIXED_LEN_BYTE_ARRAY":
- self.primitive_type = BinaryType()
- else:
- raise AssertionError(f"Unknown physical type {type_string}")
+ assert (
Review Comment:
Same here, can you change this into a `ValueError`?
##########
python/pyiceberg/io/pyarrow.py:
##########
@@ -1025,3 +1040,291 @@ def map_key_partner(self, partner_map:
Optional[pa.Array]) -> Optional[pa.Array]
def map_value_partner(self, partner_map: Optional[pa.Array]) ->
Optional[pa.Array]:
return partner_map.items if isinstance(partner_map, pa.MapArray) else
None
+
+
+_PRIMITIVE_TO_PHYSICAL = {
+ BooleanType(): "BOOLEAN",
+ IntegerType(): "INT32",
+ LongType(): "INT64",
+ FloatType(): "FLOAT",
+ DoubleType(): "DOUBLE",
+ DateType(): "INT32",
+ TimeType(): "INT64",
+ TimestampType(): "INT64",
+ TimestamptzType(): "INT64",
+ StringType(): "BYTE_ARRAY",
+ UUIDType(): "FIXED_LEN_BYTE_ARRAY",
+ BinaryType(): "BYTE_ARRAY",
+}
+_PHISICAL_TYPES = ["BOOLEAN", "INT32", "INT64", "INT96", "FLOAT", "DOUBLE",
"BYTE_ARRAY", "FIXED_LEN_BYTE_ARRAY"]
+
+
+class StatsAggregator:
+ def __init__(self, iceberg_type: PrimitiveType, physical_type_string: str,
trunc_length: Optional[int] = None) -> None:
+ self.current_min: Any = None
+ self.current_max: Any = None
+ self.trunc_length = trunc_length
+
+ assert physical_type_string in _PHISICAL_TYPES, f"Unknown physical
type {physical_type_string}"
+ if physical_type_string == "INT96":
+ raise NotImplementedError("Statistics not implemented for INT96
physical type")
+ assert (
+ _PRIMITIVE_TO_PHYSICAL[iceberg_type] == physical_type_string
+ ), f"Unexpected physical type {physical_type_string} for
{iceberg_type}, expected {_PRIMITIVE_TO_PHYSICAL[iceberg_type]}"
+
+ self.primitive_type = iceberg_type
+
+ def serialize(self, value: Any) -> bytes:
+ if type(value) == date:
Review Comment:
I think this should be handled in `conversions.py`. We're inconsistent since
we handle the UUID conversion, but not the date types. I've created a separate
issue for this, so we can get this in:
https://github.com/apache/iceberg/issues/8191
##########
python/pyiceberg/io/pyarrow.py:
##########
@@ -1039,35 +1042,49 @@ def map_value_partner(self, partner_map:
Optional[pa.Array]) -> Optional[pa.Arra
return partner_map.items if isinstance(partner_map, pa.MapArray) else
None
+_PRIMITIVE_TO_PHYSICAL = {
+ BooleanType(): "BOOLEAN",
+ IntegerType(): "INT32",
+ LongType(): "INT64",
+ FloatType(): "FLOAT",
+ DoubleType(): "DOUBLE",
+ DateType(): "INT32",
+ TimeType(): "INT64",
+ TimestampType(): "INT64",
+ TimestamptzType(): "INT64",
+ StringType(): "BYTE_ARRAY",
+ UUIDType(): "FIXED_LEN_BYTE_ARRAY",
+ BinaryType(): "BYTE_ARRAY",
+}
+_PHISICAL_TYPES = ["BOOLEAN", "INT32", "INT64", "INT96", "FLOAT", "DOUBLE",
"BYTE_ARRAY", "FIXED_LEN_BYTE_ARRAY"]
+
+
class StatsAggregator:
- def __init__(self, type_string: str, trunc_length: Optional[int] = None)
-> None:
+ def __init__(self, iceberg_type: PrimitiveType, physical_type_string: str,
trunc_length: Optional[int] = None) -> None:
self.current_min: Any = None
self.current_max: Any = None
self.trunc_length = trunc_length
- self.primitive_type: Optional[PrimitiveType] = None
-
- if type_string == "BOOLEAN":
- self.primitive_type = BooleanType()
- elif type_string == "INT32":
- self.primitive_type = IntegerType()
- elif type_string == "INT64":
- self.primitive_type = LongType()
- elif type_string == "INT96":
+
+ assert physical_type_string in _PHISICAL_TYPES, f"Unknown physical
type {physical_type_string}"
+ if physical_type_string == "INT96":
raise NotImplementedError("Statistics not implemented for INT96
physical type")
- elif type_string == "FLOAT":
- self.primitive_type = FloatType()
- elif type_string == "DOUBLE":
- self.primitive_type = DoubleType()
- elif type_string == "BYTE_ARRAY":
- self.primitive_type = BinaryType()
- elif type_string == "FIXED_LEN_BYTE_ARRAY":
- self.primitive_type = BinaryType()
- else:
- raise AssertionError(f"Unknown physical type {type_string}")
+ assert (
+ _PRIMITIVE_TO_PHYSICAL[iceberg_type] == physical_type_string
+ ), f"Unexpected physical type {physical_type_string} for
{iceberg_type}, expected {_PRIMITIVE_TO_PHYSICAL[iceberg_type]}"
+
+ self.primitive_type = iceberg_type
def serialize(self, value: Any) -> bytes:
- if type(value) == str:
- value = value.encode()
+ if type(value) == date:
+ value = date_to_days(value)
+ elif type(value) == time:
+ value = time_object_to_micros(value)
+ elif type(value) == datetime:
+ value = datetime_to_micros(value)
+
+ if self.primitive_type == UUIDType():
+ value = uuid.UUID(bytes=value)
+
assert self.primitive_type is not None # appease mypy
Review Comment:
This one still required? The `primitive_type` is now a `PrimitiveType`,
instead of an `Optional[PrimitiveType]`
##########
python/pyiceberg/io/pyarrow.py:
##########
@@ -1013,3 +1025,271 @@ def map_key_partner(self, partner_map:
Optional[pa.Array]) -> Optional[pa.Array]
def map_value_partner(self, partner_map: Optional[pa.Array]) ->
Optional[pa.Array]:
return partner_map.items if isinstance(partner_map, pa.MapArray) else
None
+
+
+class StatsAggregator:
+ def __init__(self, type_string: str, trunc_length: Optional[int] = None)
-> None:
+ self.current_min: Any = None
+ self.current_max: Any = None
+ self.trunc_length = trunc_length
+ self.primitive_type: Optional[PrimitiveType] = None
+
+ if type_string == "BOOLEAN":
+ self.primitive_type = BooleanType()
+ elif type_string == "INT32":
+ self.primitive_type = IntegerType()
+ elif type_string == "INT64":
+ self.primitive_type = LongType()
+ elif type_string == "INT96":
+ raise NotImplementedError("Statistics not implemented for INT96
physical type")
+ elif type_string == "FLOAT":
+ self.primitive_type = FloatType()
+ elif type_string == "DOUBLE":
+ self.primitive_type = DoubleType()
+ elif type_string == "BYTE_ARRAY":
+ self.primitive_type = BinaryType()
+ elif type_string == "FIXED_LEN_BYTE_ARRAY":
+ self.primitive_type = BinaryType()
+ else:
+ raise AssertionError(f"Unknown physical type {type_string}")
+
+ def serialize(self, value: Any) -> bytes:
+ if type(value) == str:
+ value = value.encode()
+ assert self.primitive_type is not None # appease mypy
+ return to_bytes(self.primitive_type, value)
+
+ def add_min(self, val: Any) -> None:
+ if self.current_min is None:
+ self.current_min = val
+ else:
+ self.current_min = min(val, self.current_min)
+
+ def add_max(self, val: Any) -> None:
+ if self.current_max is None:
+ self.current_max = val
+ else:
+ self.current_max = max(self.current_max, val)
+
+ def get_min(self) -> bytes:
+ return self.serialize(self.current_min)[: self.trunc_length]
+
+ def get_max(self) -> bytes:
+ return self.serialize(self.current_max)[: self.trunc_length]
Review Comment:
> And should primitive types be truncated at all? I don't think it makes a
lot of sense.
This is important for str and bytes, since those can grow very big. For the
others, it isn't super important.
##########
python/pyiceberg/io/pyarrow.py:
##########
@@ -1039,35 +1042,49 @@ def map_value_partner(self, partner_map:
Optional[pa.Array]) -> Optional[pa.Arra
return partner_map.items if isinstance(partner_map, pa.MapArray) else
None
+_PRIMITIVE_TO_PHYSICAL = {
+ BooleanType(): "BOOLEAN",
+ IntegerType(): "INT32",
+ LongType(): "INT64",
+ FloatType(): "FLOAT",
+ DoubleType(): "DOUBLE",
+ DateType(): "INT32",
+ TimeType(): "INT64",
+ TimestampType(): "INT64",
+ TimestamptzType(): "INT64",
+ StringType(): "BYTE_ARRAY",
+ UUIDType(): "FIXED_LEN_BYTE_ARRAY",
+ BinaryType(): "BYTE_ARRAY",
+}
+_PHISICAL_TYPES = ["BOOLEAN", "INT32", "INT64", "INT96", "FLOAT", "DOUBLE",
"BYTE_ARRAY", "FIXED_LEN_BYTE_ARRAY"]
+
+
class StatsAggregator:
- def __init__(self, type_string: str, trunc_length: Optional[int] = None)
-> None:
+ def __init__(self, iceberg_type: PrimitiveType, physical_type_string: str,
trunc_length: Optional[int] = None) -> None:
self.current_min: Any = None
self.current_max: Any = None
self.trunc_length = trunc_length
- self.primitive_type: Optional[PrimitiveType] = None
-
- if type_string == "BOOLEAN":
- self.primitive_type = BooleanType()
- elif type_string == "INT32":
- self.primitive_type = IntegerType()
- elif type_string == "INT64":
- self.primitive_type = LongType()
- elif type_string == "INT96":
+
+ assert physical_type_string in _PHISICAL_TYPES, f"Unknown physical
type {physical_type_string}"
+ if physical_type_string == "INT96":
Review Comment:
As a side-note. Technically this should be impossible because the Iceberg
schema is used for writing. There is no INT96 in the Iceberg spec, so that
should not happen, but no harm in leaving this check-in there.
--
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]