tsungchih commented on code in PR #10877: URL: https://github.com/apache/gravitino/pull/10877#discussion_r3169170264
########## 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: addressed ########## 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: addressed -- 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]
