Copilot commented on code in PR #11422:
URL: https://github.com/apache/gravitino/pull/11422#discussion_r3392484514


##########
clients/client-python/gravitino/client/gravitino_metalake.py:
##########
@@ -1,3 +1,4 @@
+# pylint: disable=too-many-lines
 # Licensed to the Apache Software Foundation (ASF) under one

Review Comment:
   This file now has a duplicate `# pylint: disable=too-many-lines` (one before 
the ASF header and another later before imports). Keeping both is redundant; 
since the directive already exists later in the file, drop the newly added one 
at the top.



##########
clients/client-python/gravitino/api/stats/partition_range.py:
##########
@@ -0,0 +1,228 @@
+# 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 __future__ import annotations
+
+from dataclasses import dataclass
+from enum import Enum
+from typing import ClassVar
+
+from gravitino.api.rel.expressions.named_reference import (
+    PARTITION_NAME_FIELD,
+)
+from gravitino.api.rel.expressions.sorts.sort_direction import SortDirection
+from gravitino.api.rel.expressions.sorts.sort_orders import SortOrder, 
SortOrders
+from gravitino.utils.precondition import Precondition
+
+
+@dataclass()
+class PartitionRange:
+    DEFAULT_COMPARATOR: ClassVar[SortOrder] = SortOrders.of(
+        PARTITION_NAME_FIELD, SortDirection.ASCENDING
+    )
+    _lower_partition_name: str | None = None
+    _lower_bound_type: BoundType | None = None
+    _upper_partition_name: str | None = None
+    _upper_bound_type: BoundType | None = None
+    _comparator: SortOrder = DEFAULT_COMPARATOR
+
+    class BoundType(Enum):
+        """Enum representing the type of bounds for a partition range."""
+
+        OPEN = "OPEN"  # exclusive
+        CLOSED = "CLOSED"  # inclusive
+
+    @classmethod
+    def up_to(
+        cls,
+        upper_partition_name: str,
+        upper_bound_type: BoundType,
+        comparator: SortOrder = DEFAULT_COMPARATOR,
+    ) -> "PartitionRange":
+        """
+        Creates a PartitionRange which only has upper bound partition name 
with a specific comparator type.
+
+        Args:
+            upper_partition_name (str): the upper partition name.
+            upper_bound_type (BoundType): the type of the upper bound (open or 
closed).
+            comparator (SortOrder): the comparator to use for this range.
+
+        Returns:
+            PartitionRange: a PartitionRange with the upper partition name and 
the specified comparator type.
+        """
+        Precondition.check_argument(
+            upper_partition_name is not None,
+            "Upper partition name cannot be null",
+        )
+        Precondition.check_argument(
+            upper_bound_type is not None,
+            "Upper bound type cannot be null",
+        )
+        Precondition.check_argument(
+            upper_partition_name.strip() != "",
+            "Upper partition name cannot be empty",
+        )
+

Review Comment:
   `up_to()` allows callers to pass `comparator=None`, which would create a 
`PartitionRange` whose `comparator()` violates its `SortOrder` contract and can 
later fail when used for sorting/serialization. The Java API also rejects null 
comparators; add a precondition here as well.



##########
clients/client-python/tests/unittests/api/stats/test_partition_range.py:
##########
@@ -0,0 +1,114 @@
+# 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 unittest
+
+from gravitino.api.stats.partition_range import ALL_PARTITIONS, PartitionRange
+from gravitino.exceptions.base import IllegalArgumentException
+
+
+class TestPartitionRange(unittest.TestCase):
+    def test_partition_range(self) -> None:
+        range1: PartitionRange = PartitionRange.down_to(
+            "partition1", PartitionRange.BoundType.OPEN
+        )
+        self.assertIsNotNone(range1.lower_partition_name())
+        self.assertEqual("partition1", range1.lower_partition_name())
+        self.assertEqual(PartitionRange.BoundType.OPEN, 
range1.lower_bound_type())
+        self.assertIsNotNone(range1.lower_bound_type())
+        self.assertIsNone(range1.upper_partition_name())
+        self.assertIsNone(range1.upper_bound_type())
+        self.assertIsNotNone(range1.comparator())
+
+        range2: PartitionRange = PartitionRange.up_to(
+            "partition3", PartitionRange.BoundType.CLOSED
+        )
+        self.assertIsNotNone(range2.upper_partition_name())
+        self.assertEqual("partition3", range2.upper_partition_name())
+        self.assertEqual(PartitionRange.BoundType.CLOSED, 
range2.upper_bound_type())
+        self.assertIsNotNone(range2.upper_bound_type())
+        self.assertIsNone(range2.lower_partition_name())
+        self.assertIsNone(range2.lower_bound_type())
+        self.assertIsNotNone(range2.comparator())
+
+        range3: PartitionRange = PartitionRange.between(
+            "partition1",
+            PartitionRange.BoundType.OPEN,
+            "partition3",
+            PartitionRange.BoundType.CLOSED,
+        )
+        self.assertIsNotNone(range3.lower_partition_name())
+        self.assertEqual("partition1", range3.lower_partition_name())
+        self.assertIsNotNone(range3.lower_bound_type())
+        self.assertEqual(PartitionRange.BoundType.OPEN, 
range3.lower_bound_type())
+        self.assertIsNotNone(range3.upper_partition_name())
+        self.assertEqual("partition3", range3.upper_partition_name())
+        self.assertIsNotNone(range3.upper_bound_type())
+        self.assertEqual(PartitionRange.BoundType.CLOSED, 
range3.upper_bound_type())
+        self.assertIsNotNone(range3.comparator())
+
+        range4: PartitionRange = PartitionRange.between(
+            "partition1",
+            PartitionRange.BoundType.CLOSED,
+            "partition3",
+            PartitionRange.BoundType.OPEN,
+        )
+        self.assertIsNotNone(range4.lower_partition_name())
+        self.assertEqual("partition1", range4.lower_partition_name())
+        self.assertIsNotNone(range4.lower_bound_type())
+        self.assertEqual(PartitionRange.BoundType.CLOSED, 
range4.lower_bound_type())
+        self.assertIsNotNone(range4.upper_partition_name())
+        self.assertEqual("partition3", range4.upper_partition_name())
+        self.assertIsNotNone(range4.upper_bound_type())
+        self.assertEqual(PartitionRange.BoundType.OPEN, 
range4.upper_bound_type())
+        self.assertIsNotNone(range4.comparator())
+
+    def test_create_down_range_with_null_arguments(self) -> None:
+        with self.assertRaises(IllegalArgumentException):
+            PartitionRange.down_to(None, PartitionRange.BoundType.CLOSED)  # 
type: ignore
+
+        with self.assertRaises(IllegalArgumentException):
+            PartitionRange.down_to("partition1", None)  # type: ignore
+
+    def test_create_up_range_with_null_arguments(self) -> None:
+        with self.assertRaises(IllegalArgumentException):
+            PartitionRange.up_to(None, PartitionRange.BoundType.CLOSED)  # 
type: ignore
+
+        with self.assertRaises(IllegalArgumentException):
+            PartitionRange.up_to("partition1", None)  # type: ignore
+
+    def test_up_to_with_null_comparator(self) -> None:
+        down_closed_range = PartitionRange.down_to(
+            "partition1", PartitionRange.BoundType.CLOSED
+        )
+
+        self.assertIsNotNone(down_closed_range.comparator())
+
+        up_open_range = PartitionRange.up_to(
+            "partition2", PartitionRange.BoundType.OPEN
+        )
+
+        self.assertIsNotNone(up_open_range.comparator())
+

Review Comment:
   `test_up_to_with_null_comparator` doesn't actually pass a null comparator, 
so it won't catch the (currently allowed) `comparator=None` bug. Update the 
test to assert that passing `None` for the comparator raises 
`IllegalArgumentException` (and ideally cover `up_to`, `down_to`, and 
`between`).



##########
clients/client-python/gravitino/api/rel/expressions/named_reference.py:
##########
@@ -84,3 +84,30 @@ def __hash__(self) -> int:
     def __str__(self) -> str:
         """Returns the string representation of the field reference."""
         return ".".join(self._field_names)
+
+
+class MetadataField(NamedReference):
+    """A NamedReference that references a metadata field."""
+
+    _field_names: list[str]
+
+    def __init__(self, field_names: list[str]) -> None:
+        super().__init__()
+        self._field_names = field_names
+

Review Comment:
   `MetadataField` stores the caller-provided `field_names` list directly. 
Because `__hash__`/`__str__` depend on `_field_names`, mutating the original 
list after construction can change the object's hash/string representation, 
which is unsafe (e.g., if used as a dict key). Copy the list in the constructor 
to make the instance stable.



##########
clients/client-python/gravitino/api/stats/partition_range.py:
##########
@@ -0,0 +1,228 @@
+# 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 __future__ import annotations
+
+from dataclasses import dataclass
+from enum import Enum
+from typing import ClassVar
+
+from gravitino.api.rel.expressions.named_reference import (
+    PARTITION_NAME_FIELD,
+)
+from gravitino.api.rel.expressions.sorts.sort_direction import SortDirection
+from gravitino.api.rel.expressions.sorts.sort_orders import SortOrder, 
SortOrders
+from gravitino.utils.precondition import Precondition
+
+
+@dataclass()
+class PartitionRange:
+    DEFAULT_COMPARATOR: ClassVar[SortOrder] = SortOrders.of(
+        PARTITION_NAME_FIELD, SortDirection.ASCENDING
+    )
+    _lower_partition_name: str | None = None
+    _lower_bound_type: BoundType | None = None
+    _upper_partition_name: str | None = None
+    _upper_bound_type: BoundType | None = None
+    _comparator: SortOrder = DEFAULT_COMPARATOR
+
+    class BoundType(Enum):
+        """Enum representing the type of bounds for a partition range."""
+
+        OPEN = "OPEN"  # exclusive
+        CLOSED = "CLOSED"  # inclusive
+
+    @classmethod
+    def up_to(
+        cls,
+        upper_partition_name: str,
+        upper_bound_type: BoundType,
+        comparator: SortOrder = DEFAULT_COMPARATOR,
+    ) -> "PartitionRange":
+        """
+        Creates a PartitionRange which only has upper bound partition name 
with a specific comparator type.
+
+        Args:
+            upper_partition_name (str): the upper partition name.
+            upper_bound_type (BoundType): the type of the upper bound (open or 
closed).
+            comparator (SortOrder): the comparator to use for this range.
+
+        Returns:
+            PartitionRange: a PartitionRange with the upper partition name and 
the specified comparator type.
+        """
+        Precondition.check_argument(
+            upper_partition_name is not None,
+            "Upper partition name cannot be null",
+        )
+        Precondition.check_argument(
+            upper_bound_type is not None,
+            "Upper bound type cannot be null",
+        )
+        Precondition.check_argument(
+            upper_partition_name.strip() != "",
+            "Upper partition name cannot be empty",
+        )
+
+        return PartitionRange(
+            _upper_partition_name=upper_partition_name,
+            _upper_bound_type=upper_bound_type,
+            _comparator=comparator,
+        )
+
+    @classmethod
+    def down_to(
+        cls,
+        lower_partition_name: str,
+        lower_bound_type: BoundType,
+        comparator: SortOrder = DEFAULT_COMPARATOR,
+    ) -> "PartitionRange":
+        """
+        Creates a PartitionRange which only has lower bound partition name 
with a specific comparator type.
+
+        Args:
+            lower_partition_name (str): the lower partition name.
+            lower_bound_type (BoundType): the type of the lower bound (open or 
closed).
+            comparator (SortOrder): the comparator to use for this range.
+
+        Returns:
+            PartitionRange: a PartitionRange with the lower partition name and 
the specified comparator type.
+        """
+        Precondition.check_argument(
+            lower_partition_name is not None,
+            "Lower partition name cannot be null",
+        )
+        Precondition.check_argument(
+            lower_bound_type is not None,
+            "Lower bound type cannot be null",
+        )
+        Precondition.check_argument(
+            lower_partition_name.strip() != "",
+            "Lower partition name cannot be empty",
+        )
+

Review Comment:
   `down_to()` allows `comparator=None`, which would produce an invalid 
`PartitionRange` (and `comparator()` would return None despite its type 
signature). Add a precondition check to reject null comparators, consistent 
with the server-side/Java behavior.



##########
clients/client-python/gravitino/api/stats/partition_range.py:
##########
@@ -0,0 +1,228 @@
+# 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 __future__ import annotations
+
+from dataclasses import dataclass
+from enum import Enum
+from typing import ClassVar
+
+from gravitino.api.rel.expressions.named_reference import (
+    PARTITION_NAME_FIELD,
+)
+from gravitino.api.rel.expressions.sorts.sort_direction import SortDirection
+from gravitino.api.rel.expressions.sorts.sort_orders import SortOrder, 
SortOrders
+from gravitino.utils.precondition import Precondition
+
+
+@dataclass()
+class PartitionRange:
+    DEFAULT_COMPARATOR: ClassVar[SortOrder] = SortOrders.of(
+        PARTITION_NAME_FIELD, SortDirection.ASCENDING
+    )
+    _lower_partition_name: str | None = None
+    _lower_bound_type: BoundType | None = None
+    _upper_partition_name: str | None = None
+    _upper_bound_type: BoundType | None = None
+    _comparator: SortOrder = DEFAULT_COMPARATOR
+
+    class BoundType(Enum):
+        """Enum representing the type of bounds for a partition range."""
+
+        OPEN = "OPEN"  # exclusive
+        CLOSED = "CLOSED"  # inclusive
+
+    @classmethod
+    def up_to(
+        cls,
+        upper_partition_name: str,
+        upper_bound_type: BoundType,
+        comparator: SortOrder = DEFAULT_COMPARATOR,
+    ) -> "PartitionRange":
+        """
+        Creates a PartitionRange which only has upper bound partition name 
with a specific comparator type.
+
+        Args:
+            upper_partition_name (str): the upper partition name.
+            upper_bound_type (BoundType): the type of the upper bound (open or 
closed).
+            comparator (SortOrder): the comparator to use for this range.
+
+        Returns:
+            PartitionRange: a PartitionRange with the upper partition name and 
the specified comparator type.
+        """
+        Precondition.check_argument(
+            upper_partition_name is not None,
+            "Upper partition name cannot be null",
+        )
+        Precondition.check_argument(
+            upper_bound_type is not None,
+            "Upper bound type cannot be null",
+        )
+        Precondition.check_argument(
+            upper_partition_name.strip() != "",
+            "Upper partition name cannot be empty",
+        )
+
+        return PartitionRange(
+            _upper_partition_name=upper_partition_name,
+            _upper_bound_type=upper_bound_type,
+            _comparator=comparator,
+        )
+
+    @classmethod
+    def down_to(
+        cls,
+        lower_partition_name: str,
+        lower_bound_type: BoundType,
+        comparator: SortOrder = DEFAULT_COMPARATOR,
+    ) -> "PartitionRange":
+        """
+        Creates a PartitionRange which only has lower bound partition name 
with a specific comparator type.
+
+        Args:
+            lower_partition_name (str): the lower partition name.
+            lower_bound_type (BoundType): the type of the lower bound (open or 
closed).
+            comparator (SortOrder): the comparator to use for this range.
+
+        Returns:
+            PartitionRange: a PartitionRange with the lower partition name and 
the specified comparator type.
+        """
+        Precondition.check_argument(
+            lower_partition_name is not None,
+            "Lower partition name cannot be null",
+        )
+        Precondition.check_argument(
+            lower_bound_type is not None,
+            "Lower bound type cannot be null",
+        )
+        Precondition.check_argument(
+            lower_partition_name.strip() != "",
+            "Lower partition name cannot be empty",
+        )
+
+        return PartitionRange(
+            _lower_partition_name=lower_partition_name,
+            _lower_bound_type=lower_bound_type,
+            _comparator=comparator,
+        )
+
+    @classmethod
+    def between(
+        cls,
+        lower_partition_name: str,
+        lower_bound_type: BoundType,
+        upper_partition_name: str,
+        upper_bound_type: BoundType,
+        comparator: SortOrder = DEFAULT_COMPARATOR,
+    ) -> "PartitionRange":
+        """
+        Creates a PartitionRange which has both lower and upper partition 
names with a specific comparator type.
+
+        Args:
+            lower_partition_name (str): the lower partition name.
+            lower_bound_type (BoundType): the type of the lower bound (open or 
closed).
+            upper_partition_name (str):  the upper partition name.
+            upper_bound_type (BoundType): the type of the upper bound (open or 
closed).
+            comparator (SortOrder): the comparator to use for this range.
+
+        Returns:
+            PartitionRange: a PartitionRange with both lower and upper 
partition names
+            and the specified comparator type.
+        """
+        Precondition.check_argument(
+            lower_partition_name is not None,
+            "Lower partition name cannot be null",
+        )
+        Precondition.check_argument(
+            lower_bound_type is not None,
+            "Lower bound type cannot be null",
+        )
+        Precondition.check_argument(
+            lower_partition_name.strip() != "",
+            "Lower partition name cannot be empty",
+        )
+
+        Precondition.check_argument(
+            upper_partition_name is not None,
+            "Upper partition name cannot be null",
+        )
+        Precondition.check_argument(
+            upper_bound_type is not None,
+            "Upper bound type cannot be null",
+        )
+        Precondition.check_argument(
+            upper_partition_name.strip() != "",
+            "Upper partition name cannot be empty",
+        )
+

Review Comment:
   `between()` allows `comparator=None`, which can create a `PartitionRange` 
that later fails when the comparator is used (and contradicts the `SortOrder` 
return type). Add a precondition to reject null comparators, matching the Java 
API's validation.



-- 
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]

Reply via email to