Copilot commented on code in PR #10877:
URL: https://github.com/apache/gravitino/pull/10877#discussion_r3167149465
##########
clients/client-python/gravitino/api/rel/types/json_serdes/base.py:
##########
@@ -25,13 +25,21 @@
from gravitino.api.rel.expressions.sorts.sort_order import SortOrder
from gravitino.api.rel.indexes.index import Index
from gravitino.api.rel.types.types import Type
+from gravitino.api.stats.statistic_value import StatisticValue
from gravitino.dto.rel.partitioning.partitioning import Partitioning
from gravitino.dto.rel.partitions.partition_dto import PartitionDTO
_GravitinoTypeT = TypeVar(
"_GravitinoTypeT",
bound=Union[
- Expression, Type, Partitioning, PartitionDTO, Distribution, Index,
SortOrder
+ Expression,
+ Type,
+ Partitioning,
+ PartitionDTO,
+ Distribution,
+ Index,
+ SortOrder,
+ StatisticValue,
],
Review Comment:
Adding `StatisticValue` to the `JsonSerializable` type bound introduces an
unnecessary dependency from `api/rel/types/json_serdes` into `api/stats` (and
forces importing stats whenever `JsonSerializable` is imported). Since
`JsonSerializable` is a generic serializer interface, consider removing the
restrictive `bound=Union[...]` (or loosening it to a generic unconstrained
`TypeVar`) so new serdes types don’t require editing this base module and to
keep package layering clean.
##########
clients/client-python/tests/unittests/api/stats/test_statistic_values.py:
##########
@@ -0,0 +1,204 @@
+# 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 random
+import unittest
+
+from gravitino.api.rel.types.types import Types
+from gravitino.api.stats.statistic_value import StatisticValue
+from gravitino.api.stats.statistic_values import StatisticValues
+from gravitino.exceptions.base import IllegalArgumentException
+
+
+class TestStatisticValues(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls) -> None:
+ super().setUpClass()
+ cls._rand_int = random.randint(0, 500)
+ cls._rand_int_another = random.randint(cls._rand_int + 1, 1000)
+ cls._rand_float = random.uniform(0, 500)
+ cls._rand_float_another = random.uniform(cls._rand_float + 1, 1000)
+ cls._rand_str = f"str-{cls._rand_int}"
+ cls._rand_str_another = f"str-{cls._rand_int_another}"
+
+ def test_long_value(self):
+ value = StatisticValues.LongValue(self._rand_int)
+ twin_value = StatisticValues.long_value(self._rand_int)
+ another_value = StatisticValues.LongValue(self._rand_int_another)
+
+ self.assertEqual(value.value(), self._rand_int)
+ self.assertEqual(value.data_type().name(), Types.LongType.get().name())
+ self.assertEqual(hash(value), hash(self._rand_int))
+ self.assertEqual(value, twin_value)
+ self.assertNotEqual(value, another_value)
+ self.assertNotEqual(value,
StatisticValues.DoubleValue(self._rand_float))
+
+ def test_double_value(self):
+ value = StatisticValues.DoubleValue(float(self._rand_float))
+ twin_value = StatisticValues.double_value(float(self._rand_float))
+ another_value =
StatisticValues.DoubleValue(float(self._rand_float_another))
+
+ self.assertEqual(value.value(), float(self._rand_float))
+ self.assertEqual(value.data_type().name(),
Types.DoubleType.get().name())
+ self.assertEqual(hash(value), hash(float(self._rand_float)))
+ self.assertEqual(value, twin_value)
+ self.assertNotEqual(value, another_value)
+ self.assertNotEqual(value, StatisticValues.LongValue(self._rand_int))
+
+ def test_string_value(self):
+ value = StatisticValues.StringValue(self._rand_str)
+ twin_value = StatisticValues.string_value(self._rand_str)
+ another_value = StatisticValues.StringValue(self._rand_str_another)
+
+ self.assertEqual(value.value(), self._rand_str)
+ self.assertEqual(value.data_type().name(),
Types.StringType.get().name())
+ self.assertEqual(hash(value), hash(self._rand_str))
+ self.assertEqual(value, twin_value)
+ self.assertNotEqual(value, another_value)
+ self.assertNotEqual(value, StatisticValues.LongValue(self._rand_int))
+
+ def test_list_value(self):
+ value_list: list[StatisticValue[int]] = [
+ StatisticValues.LongValue(random.randint(0, 100)) for i in
range(10)
+ ]
+ another_value_list: list[StatisticValue[int]] = [
+ StatisticValues.LongValue(random.randint(0, 100)) for i in
range(10)
+ ]
+ value = StatisticValues.ListValue(value_list)
+ twin_value: StatisticValues.ListValue[int] =
StatisticValues.list_value(
+ value_list
+ )
+ another_value = StatisticValues.ListValue(another_value_list)
+
+ self.assertEqual(value.value(), value_list)
+ self.assertEqual(
+ value.data_type().name(),
+ Types.ListType.nullable(Types.LongType.get()).name(),
+ )
+ self.assertEqual(hash(value), hash(tuple(v.value() for v in
value_list)))
+ self.assertEqual(value, twin_value)
+ self.assertNotEqual(value, another_value)
+ self.assertNotEqual(value, StatisticValues.LongValue(self._rand_int))
+
+ def test_object_value(self):
+ value_dict: dict[str, StatisticValue[int]] = {
+ f"key_{i}": StatisticValues.LongValue(random.randint(0, 100))
+ for i in range(10)
+ }
+ another_value_dict: dict[str, StatisticValue[int]] = {
+ f"key_{i}": StatisticValues.LongValue(random.randint(0, 100))
+ for i in range(10)
+ }
+ value = StatisticValues.ObjectValue(value_dict)
+ twin_value: StatisticValues.ObjectValue[int] =
StatisticValues.object_value(
+ value_dict
+ )
+ another_value = StatisticValues.ObjectValue(another_value_dict)
+
+ expected_data_type = Types.StructType.of(
+ *[
+ Types.StructType.Field.nullable_field(key,
statistic_value.data_type())
+ for key, statistic_value in value_dict.items()
+ ]
+ )
+ self.assertEqual(value.value(), value_dict)
+ self.assertEqual(
+ value.data_type().simple_string(),
expected_data_type.simple_string()
+ )
+ self.assertEqual(
+ hash(value), hash(tuple(v.value() for v in value_dict.values()))
+ )
Review Comment:
`ObjectValue.__hash__` is computed via `StatisticValues._make_hash`, which
includes keys (sorted) and hashes of nested values. This test’s expected hash
(`hash(tuple(v.value() for v in value_dict.values()))`) ignores keys and
insertion-order differences, so it won’t match the actual implementation.
Update the assertion to either compare `hash(value)` with `hash(twin_value)`
only, or compute the expected hash using the same key-inclusive,
order-independent representation.
```suggestion
self.assertEqual(hash(value), hash(twin_value))
```
##########
clients/client-python/gravitino/api/stats/statistic_values.py:
##########
@@ -0,0 +1,265 @@
+# 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.
+
+
+from typing import Any, TypeVar
+
+from gravitino.api.rel.types.type import Type
+from gravitino.api.rel.types.types import Types
+from gravitino.api.stats.statistic_value import StatisticValue
+from gravitino.utils.precondition import Precondition
+
+T = TypeVar("T")
+
+
+class StatisticValues:
+ """A class representing a collection of statistic values."""
+
+ @staticmethod
+ def boolean_value(value: bool) -> "BooleanValue":
+ """Creates a statistic value that holds a boolean value.
+
+ Args:
+ value: the boolean value to be held by this statistic value
+
+ Returns:
+ A BooleanValue instance containing the provided boolean value
+ """
+ return StatisticValues.BooleanValue(value)
+
+ @staticmethod
+ def long_value(value: int) -> "LongValue":
+ """Creates a statistic value that holds a long value.
+
+ Args:
+ value: the long value to be held by this statistic value
+
+ Returns:
+ A LongValue instance containing the provided long value
+ """
+ return StatisticValues.LongValue(value)
+
+ @staticmethod
+ def double_value(value: float) -> "DoubleValue":
+ """Creates a statistic value that holds a double value.
+
+ Args:
+ value: the double value to be held by this statistic value
+
+ Returns:
+ A DoubleValue instance containing the provided double value
+ """
+ return StatisticValues.DoubleValue(value)
+
+ @staticmethod
+ def string_value(value: str) -> "StringValue":
+ """Creates a statistic value that holds a string value.
+
+ Args:
+ value: the string value to be held by this statistic value
+
+ Returns:
+ A StringValue instance containing the provided string value
+ """
+ return StatisticValues.StringValue(value)
+
+ @staticmethod
+ def list_value(value: list[StatisticValue[T]]) -> "ListValue[T]":
+ """Creates a statistic value that holds a list of other statistic
values.
+
+ Args:
+ value: the list of statistic values to be held by this statistic
value
+
+ Returns:
+ A ListValue instance containing the provided list of statistic
values
+ """
+ return StatisticValues.ListValue(value)
+
+ @staticmethod
+ def object_value(value: dict[str, StatisticValue[Any]]) ->
"ObjectValue[Any]":
+ """Creates a statistic value that holds a map of string keys to other
statistic values.
+
+ Args:
+ value: the map of string keys to statistic values to be held by
this statistic value
+
+ Returns:
+ An ObjectValue instance containing the provided map of statistic
values
+ """
+ return StatisticValues.ObjectValue(value)
+
+ @staticmethod
+ def _make_hash(value: StatisticValue[Any]) -> int:
+ """Recursively compute hash for any StatisticValue.
+
+ Args:
+ value: the StatisticValue to hash
+
+ Returns:
+ Hash code for the StatisticValue
+ """
+ match value:
+ case StatisticValues.ListValue():
+ return hash(tuple(StatisticValues._make_hash(v) for v in
value.value()))
+ case StatisticValues.ObjectValue():
+ return hash(
+ tuple(
+ sorted(
+ (k, StatisticValues._make_hash(v))
+ for k, v in value.value().items()
+ )
+ )
+ )
+ case _:
+ return hash(value.value())
+
+ class BooleanValue(StatisticValue[bool]):
+ """A statistic value that holds a Boolean value."""
+
+ def __init__(self, value: bool) -> None:
+ self._value = value
+
+ def value(self) -> bool:
+ return self._value
+
+ def data_type(self) -> Type:
+ return Types.BooleanType.get()
+
+ def __hash__(self) -> int:
+ return hash(self._value)
+
+ def __eq__(self, other) -> bool:
+ if not isinstance(other, StatisticValues.BooleanValue):
+ return False
+ return self._value == other._value
+
+ class LongValue(StatisticValue[int]):
+ """A statistic value that holds a Long value."""
+
+ def __init__(self, value: int) -> None:
+ self._value = value
+
+ def value(self) -> int:
+ return self._value
+
+ def data_type(self) -> Type:
+ return Types.LongType.get()
+
+ def __hash__(self) -> int:
+ return hash(self._value)
+
+ def __eq__(self, other) -> bool:
+ if not isinstance(other, StatisticValues.LongValue):
+ return False
+ return self._value == other._value
+
+ class DoubleValue(StatisticValue[float]):
+ """A statistic value that holds a Double value."""
+
+ def __init__(self, value: float) -> None:
+ self._value = value
+
+ def value(self) -> float:
+ return self._value
+
+ def data_type(self) -> Type:
+ return Types.DoubleType.get()
+
+ def __hash__(self) -> int:
+ return hash(self._value)
+
+ def __eq__(self, other) -> bool:
+ if not isinstance(other, StatisticValues.DoubleValue):
+ return False
+ return self._value == other._value
+
+ class StringValue(StatisticValue[str]):
+ """A statistic value that holds a String value."""
+
+ def __init__(self, value: str) -> None:
+ self._value = value
+
+ def value(self) -> str:
+ return self._value
+
+ def data_type(self) -> Type:
+ return Types.StringType.get()
+
+ def __hash__(self) -> int:
+ return hash(self._value)
+
+ def __eq__(self, other) -> bool:
+ if not isinstance(other, StatisticValues.StringValue):
+ return False
+ return self._value == other._value
+
+ class ListValue(StatisticValue[list[StatisticValue[T]]]):
+ """A statistic value that holds a List of other statistic values."""
+
+ def __init__(self, value_list: list[StatisticValue[T]]) -> None:
+ Precondition.check_argument(
+ value_list is not None and len(value_list) > 0,
+ "Values cannot be null or empty",
+ )
+ data_type = value_list[0].data_type()
+ Precondition.check_argument(
+ all(value.data_type() == data_type for value in value_list),
+ "All values in the list must have the same data type",
+ )
+ self._value_list = value_list
+
+ def value(self) -> list[StatisticValue[T]]:
+ return self._value_list
+
+ def data_type(self) -> Type:
+ return Types.ListType.nullable(self._value_list[0].data_type())
+
+ def __hash__(self) -> int:
+ return StatisticValues._make_hash(self)
+
+ def __eq__(self, other) -> bool:
+ if not isinstance(other, StatisticValues.ListValue):
+ return False
+ return self._value_list == other._value_list
+
+ class ObjectValue(StatisticValue[dict[str, StatisticValue[T]]]):
+ """A statistic value that holds a Map of String keys to other
statistic values."""
+
+ def __init__(self, value_map: dict[str, StatisticValue[T]]) -> None:
+ Precondition.check_argument(
+ value_map is not None and len(value_map) > 0,
+ "Values cannot be null or empty",
+ )
+ self._value_map = value_map
+
+ def value(self) -> dict[str, StatisticValue[T]]:
+ return self._value_map
+
+ def data_type(self) -> Type:
+ return Types.StructType.of(
+ *[
+ Types.StructType.Field.nullable_field(key,
value.data_type())
+ for key, value in self._value_map.items()
Review Comment:
`ObjectValue.data_type()` builds a `StructType` using
`self._value_map.items()` iteration order. Since `Types.StructType.__eq__` is
order-sensitive (`fields` list equality), two `ObjectValue`s with identical
key/value pairs but different insertion order will report different
`data_type()`, and `ListValue` type-homogeneity checks can fail for lists of
such objects. Consider constructing struct fields in a deterministic order
(e.g., sort by key) so the schema is stable and independent of dict insertion
order.
```suggestion
Types.StructType.Field.nullable_field(
key, self._value_map[key].data_type()
)
for key in sorted(self._value_map)
```
##########
clients/client-python/tests/unittests/api/stats/test_statistic_value_serdes.py:
##########
@@ -0,0 +1,173 @@
+# 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 random
+import unittest
+
+from gravitino.api.stats.json_serdes.statistic_value_serdes import (
+ StatisticValueSerdes,
+)
+from gravitino.api.stats.statistic_values import StatisticValues
+
+
+class TestStatisticValueJsonSerdes(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls) -> None:
+ super().setUpClass()
+ cls._rand_int = random.randint(0, 500)
+ cls._rand_float = random.uniform(0, 500)
+ cls._rand_str = f"str-{cls._rand_int}"
+
+ def test_deserialize_naive_types(self):
+ self.assertEqual(
+ StatisticValueSerdes.deserialize(self._rand_int),
+ StatisticValues.long_value(self._rand_int),
+ )
+ self.assertEqual(
+ StatisticValueSerdes.deserialize(self._rand_float),
+ StatisticValues.double_value(self._rand_float),
+ )
+ self.assertEqual(
+ StatisticValueSerdes.deserialize(self._rand_str),
+ StatisticValues.string_value(self._rand_str),
+ )
+ self.assertEqual(
+ StatisticValueSerdes.deserialize(True),
StatisticValues.boolean_value(True)
+ )
+
+ def test_deserialize_object_type(self):
+ data = {
+ "boolean": True,
+ "long": self._rand_int,
+ "double": self._rand_float,
+ "string": self._rand_str,
+ "list": [self._rand_int, self._rand_int + 1, self._rand_int + 2],
+ "struct": {
+ "nested_boolean": False,
+ "nested_string": "nested",
+ "nested_list": [self._rand_str, self._rand_str + "_another"],
+ },
+ }
+
+ deserialized_value = StatisticValueSerdes.deserialize(data)
+
+ expected_value = StatisticValues.object_value(
+ {
+ "boolean": StatisticValues.boolean_value(True),
+ "long": StatisticValues.long_value(self._rand_int),
+ "double": StatisticValues.double_value(self._rand_float),
+ "string": StatisticValues.string_value(self._rand_str),
+ "list": StatisticValues.list_value(
+ [
+ StatisticValues.long_value(self._rand_int),
+ StatisticValues.long_value(self._rand_int + 1),
+ StatisticValues.long_value(self._rand_int + 2),
+ ]
+ ),
+ "struct": StatisticValues.object_value(
+ {
+ "nested_boolean": StatisticValues.boolean_value(False),
+ "nested_string":
StatisticValues.string_value("nested"),
+ "nested_list": StatisticValues.list_value(
+ [
+ StatisticValues.string_value(self._rand_str),
+ StatisticValues.string_value(
+ self._rand_str + "_another"
+ ),
+ ]
+ ),
+ }
+ ),
+ }
+ )
+
+ self.assertEqual(deserialized_value, expected_value)
+
+ def test_deserialize_unsupported_type(self):
+ with self.assertRaises(ValueError):
+ StatisticValueSerdes.deserialize(None)
+
Review Comment:
`StatisticValueSerdes.deserialize(None)` triggers
`Precondition.check_argument(data is not None, ...)` which raises
`IllegalArgumentException`, not `ValueError`. This test will fail as written;
update it to expect `IllegalArgumentException` (and optionally assert on the
message).
--
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]