Copilot commented on code in PR #10877: URL: https://github.com/apache/gravitino/pull/10877#discussion_r3160418188
########## clients/client-python/gravitino/api/stats/statistic_values.py: ########## @@ -0,0 +1,238 @@ +# 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: list[StatisticValue[T]]) -> "ListValue[T]": + """Creates a statistic value that holds a list of other statistic values. + + Args: + value_list: 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_list) + + @staticmethod + def object_value(value_list: dict[str, StatisticValue[Any]]) -> "ObjectValue[Any]": + """Creates a statistic value that holds a list of other statistic values. + + Args: + value_list: 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.ObjectValue(value_list) + + 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 hash(tuple(v.value() for v in self._value_list)) + Review Comment: `ListValue.__hash__` hashes `v.value()` for each element. This will raise `TypeError` for nested statistic values whose `.value()` is unhashable (e.g., a nested `ListValue` returns `list`, `ObjectValue` returns `dict`). Hashing should be based on the `StatisticValue` elements themselves (or on a stable serialized representation), not their raw `.value()`. ########## clients/client-python/tests/unittests/api/stats/test_statistic_values.py: ########## @@ -0,0 +1,181 @@ +# 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.assertEqual( + value.data_type().name(), + Types.ListType.nullable(value_list[0].data_type()).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) + + self.assertEqual(value.value(), value_dict) + self.assertEqual( + value.data_type().name(), + Types.StructType.of( + Types.StructType.Field.nullable_field(key, value.data_type()) + for key, value in value_dict.items() + ).name(), + ) + self.assertEqual( + hash(value), hash(tuple(v.value() for v in value_dict.values())) + ) + self.assertEqual(value, twin_value) + self.assertNotEqual(value, another_value) + self.assertEqual( + value.data_type().name(), + Types.StructType.of( + Types.StructType.Field.nullable_field(key, value.data_type()) + for key, value in value_dict.items() + ).name(), + ) + self.assertEqual( Review Comment: This assertion doesn’t actually validate the struct’s field types: `StructType.name()` is always `Name.STRUCT`, and the expected `StructType.of(...)` call is also constructed incorrectly (it passes a generator as a single field). Consider asserting on `value.data_type().fields()` or `simple_string()` and build the expected struct with `Types.StructType.of(*field_list)` using each entry’s `StatisticValue.data_type()`. ```suggestion 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().fields(), expected_data_type.fields()) self.assertEqual( hash(value), hash(tuple(v.value() for v in value_dict.values())) ) self.assertEqual(value, twin_value) self.assertNotEqual(value, another_value) self.assertEqual(value.data_type().fields(), expected_data_type.fields()) self.assertEqual( ``` ########## clients/client-python/gravitino/api/stats/statistic_values.py: ########## @@ -0,0 +1,238 @@ +# 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: list[StatisticValue[T]]) -> "ListValue[T]": + """Creates a statistic value that holds a list of other statistic values. + + Args: + value_list: 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_list) + + @staticmethod + def object_value(value_list: dict[str, StatisticValue[Any]]) -> "ObjectValue[Any]": + """Creates a statistic value that holds a list of other statistic values. + + Args: + value_list: 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.ObjectValue(value_list) + + 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 hash(tuple(v.value() for v in self._value_list)) + + 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() + ) + + def __hash__(self) -> int: + return hash(tuple(v.value() for v in self._value_map.values())) + Review Comment: `ObjectValue.__hash__` hashes only `value_map.values()` in insertion order. This can violate the hash contract because two equal dicts (same key/value pairs but different insertion order) compare equal yet can produce different hashes; it also fails for nested unhashable `.value()` results. Use a key-inclusive, order-independent representation (e.g., a frozenset/tuple of sorted (key, value) pairs, hashing the `StatisticValue` objects). ########## clients/client-python/tests/unittests/api/stats/test_statistic_values.py: ########## @@ -0,0 +1,181 @@ +# 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.assertEqual( + value.data_type().name(), + Types.ListType.nullable(value_list[0].data_type()).name(), + ) + self.assertEqual(hash(value), hash(tuple(v.value() for v in value_list))) + self.assertEqual(value, twin_value) + self.assertNotEqual(value, another_value) Review Comment: There are duplicated assertions in this test (the `data_type().name()`/`hash()`/`==`/`!=` checks are repeated). Removing the duplicates will make the test easier to maintain without reducing coverage. ########## clients/client-python/tests/unittests/api/stats/test_statistic_values.py: ########## @@ -0,0 +1,181 @@ +# 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.assertEqual( + value.data_type().name(), + Types.ListType.nullable(value_list[0].data_type()).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) + + self.assertEqual(value.value(), value_dict) + self.assertEqual( + value.data_type().name(), + Types.StructType.of( + Types.StructType.Field.nullable_field(key, value.data_type()) + for key, value in value_dict.items() + ).name(), + ) + self.assertEqual( + hash(value), hash(tuple(v.value() for v in value_dict.values())) + ) + self.assertEqual(value, twin_value) + self.assertNotEqual(value, another_value) + self.assertEqual( + value.data_type().name(), + Types.StructType.of( + Types.StructType.Field.nullable_field(key, value.data_type()) + for key, value in value_dict.items() + ).name(), + ) + self.assertEqual( + hash(value), hash(tuple(v.value() for v in value_dict.values())) + ) + self.assertEqual(value, twin_value) + self.assertNotEqual(value, another_value) Review Comment: There are duplicated assertions in this test (the `data_type().name()`/`hash()`/`==`/`!=` checks are repeated). Removing the duplicates will make the test easier to maintain without reducing coverage. ```suggestion ``` ########## clients/client-python/gravitino/api/stats/json_serdes/statistic_value_serdes.py: ########## @@ -0,0 +1,106 @@ +# 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 + +from dataclasses_json.core import Json + +from gravitino.api.rel.types.json_serdes.base import JsonSerializable +from gravitino.api.rel.types.type import Name +from gravitino.api.stats.statistic_value import StatisticValue +from gravitino.api.stats.statistic_values import StatisticValues +from gravitino.utils.precondition import Precondition + + +class StatisticValueSerdes(JsonSerializable[StatisticValue[Any]]): + """Customized JSON Serializer and Deserializer for StatisticValue.""" + Review Comment: The PR description mentions adding Java classes (e.g., `JsonUtils.java` / `StatisticValues.java`), but the actual changes in this PR are in the Python client. Please update the PR description to reflect the Python implementation so reviewers/users aren’t confused. ########## clients/client-python/gravitino/api/stats/statistic_values.py: ########## @@ -0,0 +1,238 @@ +# 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: list[StatisticValue[T]]) -> "ListValue[T]": + """Creates a statistic value that holds a list of other statistic values. + + Args: + value_list: 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_list) + + @staticmethod + def object_value(value_list: dict[str, StatisticValue[Any]]) -> "ObjectValue[Any]": + """Creates a statistic value that holds a list of other statistic values. + + Args: + value_list: 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.ObjectValue(value_list) Review Comment: `object_value` takes a dict/map, but its docstring/argument naming still refers to a “list”, and the Returns section says it returns `ListValue`. This is misleading for API consumers; please update the docstring and rename the parameter to something like `value_map` (and fix the Returns description to `ObjectValue`). ```suggestion def object_value(value_map: dict[str, StatisticValue[Any]]) -> "ObjectValue[Any]": """Creates a statistic value that holds a map of other statistic values. Args: value_map: the map of statistic values to be held by this statistic value Returns: An ObjectValue instance containing the provided map of statistic values """ return StatisticValues.ObjectValue(value_map) ``` ########## clients/client-python/gravitino/api/stats/statistic_values.py: ########## @@ -0,0 +1,238 @@ +# 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: list[StatisticValue[T]]) -> "ListValue[T]": + """Creates a statistic value that holds a list of other statistic values. + + Args: + value_list: 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_list) + + @staticmethod + def object_value(value_list: dict[str, StatisticValue[Any]]) -> "ObjectValue[Any]": + """Creates a statistic value that holds a list of other statistic values. + + Args: + value_list: 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.ObjectValue(value_list) + + 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 hash(tuple(v.value() for v in self._value_list)) + + 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: `Types.StructType.of` is a varargs factory (`of(*fields)`), but this code passes a generator expression as a single argument. That builds a StructType whose only “field” is a generator object, which will break when callers use `fields()` / `simple_string()` / serialization. Build a list of `Field` and splat it (or use `of(*field_list)`). ```suggestion *[ Types.StructType.Field.nullable_field(key, value.data_type()) for key, value in self._value_map.items() ] ``` -- 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]
