Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#5199] feat(client-python): Add non-single field partitioning DTOs (#8004)

George T. C. Laie5df28b63e68
diff --git a/clients/client-python/gravitino/dto/rel/partitioning/__init__.py b/clients/client-python/gravitino/dto/rel/partitioning/__init__.py
new file mode 100644
index 0000000000..13a83393a9
--- /dev/null
+++ b/clients/client-python/gravitino/dto/rel/partitioning/__init__.py
@@ -0,0 +1,16 @@
+# 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.
diff --git a/clients/client-python/gravitino/dto/rel/partitioning/bucket_partitioning_dto.py b/clients/client-python/gravitino/dto/rel/partitioning/bucket_partitioning_dto.py
new file mode 100644
index 0000000000..ca845f460f
--- /dev/null
+++ b/clients/client-python/gravitino/dto/rel/partitioning/bucket_partitioning_dto.py
@@ -0,0 +1,66 @@
+# 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 List
+
+from gravitino.api.expressions.expression import Expression
+from gravitino.api.expressions.transforms.transforms import Transforms
+from gravitino.dto.rel.column_dto import ColumnDTO
+from gravitino.dto.rel.partition_utils import PartitionUtils
+from gravitino.dto.rel.partitioning.partitioning import Partitioning
+
+
+class BucketPartitioningDTO(Partitioning):
+ """Data transfer object representing bucket partitioning."""
+
+ def __init__(self, num_buckets: int, /, *field_names: List[str]):
+ self._num_buckets = num_buckets
+ self._field_names = [*field_names]
+
+ def num_buckets(self) -> int:
+ """Returns the number of buckets.
+
+ Returns:
+ int: The number of buckets.
+ """
+ return self._num_buckets
+
+ def field_names(self) -> List[List[str]]:
+ """Returns the field names.
+
+ Returns:
+ List[List[str]]: The field names.
+ """
+ return self._field_names
+
+ def strategy(self) -> Partitioning.Strategy:
+ return self.Strategy.BUCKET
+
+ def validate(self, columns: List[ColumnDTO]) -> None:
+ for field_name in self._field_names:
+ PartitionUtils.validate_field_existence(columns, field_name)
+
+ def name(self) -> str:
+ return self.strategy().name.lower()
+
+ def arguments(self) -> List[Expression]:
+ """Gets the arguments of the partitioning strategy.
+
+ Returns:
+ List[Expression]: The arguments of the partitioning strategy.
+ """
+ return Transforms.bucket(self._num_buckets, *self._field_names).arguments()
diff --git a/clients/client-python/gravitino/dto/rel/partitioning/function_partitioning_dto.py b/clients/client-python/gravitino/dto/rel/partitioning/function_partitioning_dto.py
new file mode 100644
index 0000000000..0289925f3e
--- /dev/null
+++ b/clients/client-python/gravitino/dto/rel/partitioning/function_partitioning_dto.py
@@ -0,0 +1,66 @@
+# 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 List
+
+from gravitino.api.expressions.expression import Expression
+from gravitino.dto.rel.column_dto import ColumnDTO
+from gravitino.dto.rel.expressions.function_arg import FunctionArg
+from gravitino.dto.rel.partition_utils import PartitionUtils
+from gravitino.dto.rel.partitioning.partitioning import Partitioning
+
+
+class FunctionPartitioningDTO(Partitioning):
+ """Data transfer object for function partitioning."""
+
+ def __init__(self, function_name: str, /, *function_arg: FunctionArg):
+ self._function_name = function_name
+ self._args = [*function_arg]
+
+ def function_name(self) -> str:
+ """Returns the name of the function.
+
+ Returns:
+ str: The name of the function.
+ """
+ return self._function_name
+
+ def args(self) -> List[FunctionArg]:
+ """Returns the arguments of the function.
+
+ Returns:
+ List[FunctionArg]: The arguments of the function.
+ """
+ return self._args
+
+ def strategy(self) -> Partitioning.Strategy:
+ return self.Strategy.FUNCTION
+
+ def validate(self, columns: List[ColumnDTO]) -> None:
+ for ref in self.references():
+ PartitionUtils.validate_field_existence(columns, ref.field_name())
+
+ def name(self) -> str:
+ return self._function_name
+
+ def arguments(self) -> List[Expression]:
+ """Returns the arguments of the function.
+
+ Returns:
+ List[Expression]: The arguments of the function.
+ """
+ return self._args
diff --git a/clients/client-python/gravitino/dto/rel/partitioning/list_partitioning_dto.py b/clients/client-python/gravitino/dto/rel/partitioning/list_partitioning_dto.py
new file mode 100644
index 0000000000..8dcee8fd7b
--- /dev/null
+++ b/clients/client-python/gravitino/dto/rel/partitioning/list_partitioning_dto.py
@@ -0,0 +1,69 @@
+# 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 List, Optional
+
+from gravitino.api.expressions.expression import Expression
+from gravitino.api.expressions.named_reference import NamedReference
+from gravitino.dto.rel.column_dto import ColumnDTO
+from gravitino.dto.rel.partition_utils import PartitionUtils
+from gravitino.dto.rel.partitioning.partitioning import Partitioning
+from gravitino.dto.rel.partitions.list_partition_dto import ListPartitionDTO
+
+
+class ListPartitioningDTO(Partitioning):
+ """Data transfer object representing a list partitioning."""
+
+ def __init__(
+ self,
+ field_names: List[List[str]],
+ assignments: Optional[List[ListPartitionDTO]] = None,
+ ):
+ self._field_names = field_names
+ self._assignments = assignments if assignments is not None else []
+
+ def field_names(self) -> List[List[str]]:
+ """Returns he names of the fields to partition.
+
+ Returns:
+ List[List[str]]: The names of the fields to partition.
+ """
+ return self._field_names
+
+ def assignments(self) -> List[ListPartitionDTO]:
+ return self._assignments
+
+ def strategy(self) -> Partitioning.Strategy:
+ return self.Strategy.LIST
+
+ def validate(self, columns: List[ColumnDTO]) -> None:
+ for field_name in self._field_names:
+ PartitionUtils.validate_field_existence(columns, field_name)
+
+ def name(self) -> str:
+ return self.strategy().name.lower()
+
+ def arguments(self) -> List[Expression]:
+ """Returns the arguments of the partitioning strategy.
+
+ Returns:
+ List[Expression]: The arguments of the partitioning strategy.
+ """
+ return [
+ NamedReference.field(field_name=field_name)
+ for field_name in self._field_names
+ ]
diff --git a/clients/client-python/gravitino/dto/rel/partitioning/partitioning.py b/clients/client-python/gravitino/dto/rel/partitioning/partitioning.py
new file mode 100644
index 0000000000..600090c469
--- /dev/null
+++ b/clients/client-python/gravitino/dto/rel/partitioning/partitioning.py
@@ -0,0 +1,157 @@
+# 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 abc import abstractmethod
+from enum import Enum, unique
+from typing import Final, List
+
+from gravitino.api.expressions.expression import Expression
+from gravitino.api.expressions.named_reference import NamedReference
+from gravitino.api.expressions.transforms.transform import Transform
+from gravitino.dto.rel.column_dto import ColumnDTO
+from gravitino.dto.rel.partition_utils import PartitionUtils
+from gravitino.exceptions.base import IllegalArgumentException
+from gravitino.utils.precondition import Precondition
+
+
+class Partitioning(Transform):
+ EMPTY_PARTITIONING: Final[List["Partitioning"]] = []
+ """An empty array of partitioning."""
+
+ @unique
+ class Strategy(str, Enum):
+ """Represents a partitioning strategy."""
+
+ IDENTITY = "identity"
+ """The identity partitioning strategy."""
+
+ YEAR = "year"
+ """The year partitioning strategy."""
+
+ MONTH = "month"
+ """The month partitioning strategy."""
+
+ DAY = "day"
+ """The day partitioning strategy."""
+
+ HOUR = "hour"
+ """The hour partitioning strategy."""
+
+ BUCKET = "bucket"
+ """The bucket partitioning strategy."""
+
+ TRUNCATE = "truncate"
+ """The truncate partitioning strategy."""
+
+ LIST = "list"
+ """The list partitioning strategy."""
+
+ RANGE = "range"
+ """The range partitioning strategy."""
+
+ FUNCTION = "function"
+ """The function partitioning strategy."""
+
+ @abstractmethod
+ def strategy(self) -> Strategy:
+ """Returns the name of the partitioning strategy.
+
+ Returns:
+ Strategy: The name of the partitioning strategy.
+ """
+ pass # pragma: no cover
+
+ @abstractmethod
+ def validate(self, columns: List[ColumnDTO]) -> None:
+ """Validates the partitioning columns.
+
+ Args:
+ columns (List[ColumnDTO]): The columns to be validated.
+
+ Raises:
+ IllegalArgumentException: If the columns are invalid, this exception is thrown.
+ """
+ pass # pragma: no cover
+
+ @staticmethod
+ def get_by_name(name: str) -> Strategy:
+ """Gets the partitioning strategy by name.
+
+ Args:
+ name (str): The name of the partitioning strategy.
+
+ Returns:
+ Strategy: The partitioning strategy.
+
+ Raises:
+ IllegalArgumentException: If the name is invalid, this exception is thrown.
+ """
+
+ for strategy in Partitioning.Strategy:
+ if strategy.name.lower() == name.lower():
+ return strategy
+ raise IllegalArgumentException(
+ f"Invalid partitioning strategy: {name}. "
+ f"Valid values are: {', '.join([str(s) for s in Partitioning.Strategy])}"
+ )
+
+
+class SingleFieldPartitioning(Partitioning):
+ """A single field partitioning strategy."""
+
+ def __init__(self, field_name: List[str]):
+ Precondition.check_argument(
+ field_name is not None and len(field_name) > 0,
+ "field_name cannot be null or empty",
+ )
+ self._field_name = field_name
+
+ def field_name(self) -> List[str]:
+ """Returns the field name of the partitioning.
+
+ Returns:
+ List[str]: The field name of the partitioning.
+ """
+ return self._field_name
+
+ def validate(self, columns: List[ColumnDTO]) -> None:
+ """Validates the partitioning columns.
+
+ Args:
+ columns (List[ColumnDTO]): The columns to be validated.
+
+ Raises:
+ IllegalArgumentException: If the columns are invalid, this exception is thrown.
+ """
+
+ PartitionUtils.validate_field_existence(columns, self._field_name)
+
+ def name(self) -> str:
+ """Returns the name of the partitioning strategy.
+
+ Returns:
+ str: The name of the partitioning strategy.
+ """
+ return self.strategy().name.lower()
+
+ def arguments(self) -> List[Expression]:
+ """Returns the arguments of the partitioning strategy.
+
+ Returns:
+ List[Expression]: The arguments of the partitioning strategy.
+ """
+ return [NamedReference.field(self._field_name)]
diff --git a/clients/client-python/gravitino/dto/rel/partitioning/range_partitioning_dto.py b/clients/client-python/gravitino/dto/rel/partitioning/range_partitioning_dto.py
new file mode 100644
index 0000000000..a301e282e6
--- /dev/null
+++ b/clients/client-python/gravitino/dto/rel/partitioning/range_partitioning_dto.py
@@ -0,0 +1,65 @@
+# 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 List, Optional
+
+from gravitino.api.expressions.expression import Expression
+from gravitino.api.expressions.named_reference import NamedReference
+from gravitino.dto.rel.column_dto import ColumnDTO
+from gravitino.dto.rel.partition_utils import PartitionUtils
+from gravitino.dto.rel.partitioning.partitioning import Partitioning
+from gravitino.dto.rel.partitions.range_partition_dto import RangePartitionDTO
+
+
+class RangePartitioningDTO(Partitioning):
+ """Data transfer object representing a range partitioning."""
+
+ def __init__(
+ self,
+ field_name: List[str],
+ assignments: Optional[List[RangePartitionDTO]] = None,
+ ):
+ self._field_name = field_name
+ self._assignments = assignments if assignments is not None else []
+
+ def field_name(self) -> List[str]:
+ """Returns the name of the field to partition.
+
+ Returns:
+ List[str]: The name of the field to partition.
+ """
+ return self._field_name
+
+ def assignments(self) -> List[RangePartitionDTO]:
+ return self._assignments
+
+ def strategy(self) -> Partitioning.Strategy:
+ return self.Strategy.RANGE
+
+ def validate(self, columns: List[ColumnDTO]) -> None:
+ PartitionUtils.validate_field_existence(columns, self._field_name)
+
+ def name(self) -> str:
+ return self.strategy().name.lower()
+
+ def arguments(self) -> List[Expression]:
+ """Returns the arguments of the partitioning strategy.
+
+ Returns:
+ List[Expression]: The arguments of the partitioning strategy.
+ """
+ return [NamedReference.field(field_name=self._field_name)]
diff --git a/clients/client-python/gravitino/dto/rel/partitioning/truncate_partitioning_dto.py b/clients/client-python/gravitino/dto/rel/partitioning/truncate_partitioning_dto.py
new file mode 100644
index 0000000000..daed9be667
--- /dev/null
+++ b/clients/client-python/gravitino/dto/rel/partitioning/truncate_partitioning_dto.py
@@ -0,0 +1,72 @@
+# 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 List
+
+from gravitino.api.expressions.expression import Expression
+from gravitino.api.expressions.transforms.transforms import Transforms
+from gravitino.dto.rel.column_dto import ColumnDTO
+from gravitino.dto.rel.partition_utils import PartitionUtils
+from gravitino.dto.rel.partitioning.partitioning import Partitioning
+
+
+class TruncatePartitioningDTO(Partitioning):
+ """Represents the truncate partitioning.
+
+ Attributes:
+ _width (int): The width of the truncate partitioning.
+ _field_name (List[str]): The name of the field to partition.
+ """
+
+ def __init__(self, width: int, field_name: List[str]):
+ self._field_name = field_name
+ self._width = width
+
+ def field_name(self) -> List[str]:
+ """Returns the name of the field to partition.
+
+ Returns:
+ List[str]: The name of the field to partition.
+ """
+ return self._field_name
+
+ def width(self) -> int:
+ """Returns the width of the partitioning.
+
+ Returns:
+ int: The width of the partitioning.
+ """
+ return self._width
+
+ def strategy(self) -> Partitioning.Strategy:
+ return self.Strategy.TRUNCATE
+
+ def validate(self, columns: List[ColumnDTO]) -> None:
+ PartitionUtils.validate_field_existence(columns, self._field_name)
+
+ def name(self) -> str:
+ return self.strategy().name.lower()
+
+ def arguments(self) -> List[Expression]:
+ """Returns the arguments of the partitioning strategy.
+
+ Returns:
+ List[Expression]: The arguments of the partitioning strategy.
+ """
+ return Transforms.truncate(
+ width=self._width, field_name=self._field_name
+ ).arguments()
diff --git a/clients/client-python/tests/unittests/dto/rel/test_non_single_field_partitioning_dto.py b/clients/client-python/tests/unittests/dto/rel/test_non_single_field_partitioning_dto.py
new file mode 100644
index 0000000000..633d34d3db
--- /dev/null
+++ b/clients/client-python/tests/unittests/dto/rel/test_non_single_field_partitioning_dto.py
@@ -0,0 +1,296 @@
+# 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 itertools import chain
+
+from gravitino.api.expressions.literals.literals import Literals
+from gravitino.api.expressions.named_reference import NamedReference
+from gravitino.api.types.types import Types
+from gravitino.dto.rel.column_dto import ColumnDTO
+from gravitino.dto.rel.expressions.literal_dto import LiteralDTO
+from gravitino.dto.rel.partitioning.bucket_partitioning_dto import BucketPartitioningDTO
+from gravitino.dto.rel.partitioning.function_partitioning_dto import (
+ FunctionPartitioningDTO,
+)
+from gravitino.dto.rel.partitioning.list_partitioning_dto import ListPartitioningDTO
+from gravitino.dto.rel.partitioning.partitioning import Partitioning
+from gravitino.dto.rel.partitioning.range_partitioning_dto import RangePartitioningDTO
+from gravitino.dto.rel.partitioning.truncate_partitioning_dto import (
+ TruncatePartitioningDTO,
+)
+from gravitino.dto.rel.partitions.list_partition_dto import ListPartitionDTO
+from gravitino.dto.rel.partitions.range_partition_dto import RangePartitionDTO
+from gravitino.exceptions.base import IllegalArgumentException
+
+
+class TestBucketPartitioningDTO(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ cls.columns = [
+ ColumnDTO.builder()
+ .with_name("field1")
+ .with_data_type(Types.StringType.get())
+ .with_comment("test field 1")
+ .build(),
+ ColumnDTO.builder()
+ .with_name("field2")
+ .with_data_type(Types.IntegerType.get())
+ .with_comment("test field 2")
+ .build(),
+ ]
+ cls.dto = BucketPartitioningDTO(10, ["field1"], ["field2"])
+
+ def test_bucket_partitioning_dto(self):
+ self.dto.validate(self.columns)
+ self.assertEqual(10, self.dto.num_buckets())
+ self.assertEqual([["field1"], ["field2"]], self.dto.field_names())
+ self.assertEqual(Partitioning.Strategy.BUCKET, self.dto.strategy())
+ self.assertEqual("bucket", self.dto.name())
+
+ def test_validate_non_existing_field(self):
+ dto = BucketPartitioningDTO(6, ["nonexistent"])
+ with self.assertRaises(IllegalArgumentException):
+ dto.validate(self.columns)
+
+ def test_arguments(self):
+ arguments = self.dto.arguments()
+ self.assertIsNotNone(arguments)
+ self.assertIsInstance(arguments, list)
+ self.assertEqual(
+ arguments,
+ [
+ Literals.integer_literal(10),
+ NamedReference.field(field_name=["field1"]),
+ NamedReference.field(field_name=["field2"]),
+ ],
+ )
+
+
+class TestFunctionPartitioningDTO(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ cls.columns = [
+ ColumnDTO.builder()
+ .with_name("field1")
+ .with_data_type(Types.StringType.get())
+ .with_comment("test field 1")
+ .build(),
+ ColumnDTO.builder()
+ .with_name("field2")
+ .with_data_type(Types.IntegerType.get())
+ .with_comment("test field 2")
+ .build(),
+ ]
+
+ def test_function_partitioning_dto(self):
+ field_ref = NamedReference.field(["field1"])
+ literal = Literals.integer_literal(value=10)
+
+ dto = FunctionPartitioningDTO("test_function", field_ref, literal)
+
+ dto.validate(self.columns)
+ self.assertEqual("test_function", dto.function_name())
+ self.assertEqual("test_function", dto.name())
+ self.assertEqual([field_ref, literal], dto.args())
+ self.assertEqual([field_ref, literal], dto.arguments())
+ self.assertEqual(Partitioning.Strategy.FUNCTION, dto.strategy())
+
+ def test_function_partitioning_dto_no_args(self):
+ dto = FunctionPartitioningDTO("simple_function")
+
+ self.assertEqual("simple_function", dto.function_name())
+ self.assertEqual([], dto.args())
+ self.assertEqual([], dto.arguments())
+
+ def test_validate_non_existing_field(self):
+ field_ref = NamedReference.field(["nonexistent"])
+ dto = FunctionPartitioningDTO("test_function", field_ref)
+
+ with self.assertRaises(IllegalArgumentException):
+ dto.validate(self.columns)
+
+
+class TestListPartitioningDTO(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ cls.field_names = [["field1"], ["field2"]]
+ cls.columns = [
+ ColumnDTO.builder()
+ .with_name(field_name)
+ .with_data_type(Types.StringType.get())
+ .with_comment(f"test {field_name}")
+ .build()
+ for field_name in chain.from_iterable(cls.field_names)
+ ]
+ cls.literal_dtos = [
+ [
+ LiteralDTO.builder()
+ .with_data_type(Types.StringType.get())
+ .with_value(value=field_name)
+ .build()
+ ]
+ for field_name in chain.from_iterable(cls.field_names)
+ ]
+
+ def test_init_with_assignments(self):
+ assignments = [
+ ListPartitionDTO(
+ name="list_partition", lists=self.literal_dtos, properties={}
+ )
+ ]
+ dto = ListPartitioningDTO(self.field_names, assignments)
+
+ dto.validate(self.columns)
+ self.assertEqual(self.field_names, dto.field_names())
+ self.assertEqual(assignments, dto.assignments())
+ self.assertEqual(Partitioning.Strategy.LIST, dto.strategy())
+ self.assertEqual("list", dto.name())
+
+ arguments = dto.arguments()
+ self.assertEqual(len(self.field_names), len(arguments))
+ for arg in arguments:
+ self.assertIsInstance(arg, NamedReference)
+
+ def test_init_without_assignments(self):
+ dto = ListPartitioningDTO(self.field_names, None)
+
+ self.assertEqual(self.field_names, dto.field_names())
+ self.assertEqual([], dto.assignments())
+
+ def test_empty_field_names(self):
+ dto = ListPartitioningDTO([], None)
+ self.assertEqual([], dto.field_names())
+ self.assertEqual([], dto.arguments())
+
+ def test_validate_non_existing_field(self):
+ dto = ListPartitioningDTO([["nonexistent"]])
+ with self.assertRaises(IllegalArgumentException):
+ dto.validate(self.columns)
+
+
+class TestRangePartitioningDTO(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ cls.field_names = [["field1", "field2"]]
+ cls.columns = [
+ ColumnDTO.builder()
+ .with_name(field_name)
+ .with_data_type(Types.StringType.get())
+ .with_comment(f"test {field_name}")
+ .build()
+ for field_name in chain.from_iterable(cls.field_names)
+ ]
+ cls.literal_dtos = [
+ LiteralDTO.builder()
+ .with_data_type(Types.IntegerType.get())
+ .with_value(value=str(random_int))
+ .build()
+ for random_int in (random.randint(1, 100), random.randint(100, 200))
+ ]
+
+ def test_init_with_assignments(self):
+ assignments = [
+ RangePartitionDTO(
+ name="range_partition",
+ lower=self.literal_dtos[0],
+ upper=self.literal_dtos[1],
+ properties={},
+ )
+ ]
+ dto = RangePartitioningDTO(
+ field_name=self.field_names[0], assignments=assignments
+ )
+
+ dto.validate(self.columns)
+ self.assertEqual(self.field_names[0], dto.field_name())
+ self.assertEqual(assignments, dto.assignments())
+ self.assertEqual(Partitioning.Strategy.RANGE, dto.strategy())
+ self.assertEqual("range", dto.name())
+
+ arguments = dto.arguments()
+ self.assertEqual(len(self.field_names), len(arguments))
+ for arg in arguments:
+ self.assertIsInstance(arg, NamedReference)
+
+ def test_init_without_assignments(self):
+ dto = RangePartitioningDTO(self.field_names[0])
+
+ self.assertEqual(self.field_names[0], dto.field_name())
+ self.assertEqual([], dto.assignments())
+
+ def test_empty_field_names(self):
+ dto = RangePartitioningDTO([])
+ self.assertEqual([], dto.field_name())
+ self.assertEqual([NamedReference.field(field_name=[])], dto.arguments())
+
+ def test_validate_non_existing_field(self):
+ dto = RangePartitioningDTO(field_name=["nonexistent"])
+ with self.assertRaises(IllegalArgumentException):
+ dto.validate(self.columns)
+
+
+class TestTruncatePartitioningDTO(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ cls.field_names = [["field1", "field2"]]
+ cls.columns = [
+ ColumnDTO.builder()
+ .with_name(field_name)
+ .with_data_type(Types.StringType.get())
+ .with_comment(f"test {field_name}")
+ .build()
+ for field_name in chain.from_iterable(cls.field_names)
+ ]
+ cls.literal_dtos = [
+ LiteralDTO.builder()
+ .with_data_type(Types.IntegerType.get())
+ .with_value(value=str(random_int))
+ .build()
+ for random_int in (random.randint(1, 100), random.randint(100, 200))
+ ]
+
+ def test_init(self):
+ dto = TruncatePartitioningDTO(field_name=self.field_names[0], width=20)
+
+ dto.validate(self.columns)
+ self.assertEqual(self.field_names[0], dto.field_name())
+ self.assertEqual(20, dto.width())
+ self.assertEqual(Partitioning.Strategy.TRUNCATE, dto.strategy())
+ self.assertEqual("truncate", dto.name())
+
+ arguments = dto.arguments()
+ expected_arguments = [
+ Literals.integer_literal(value=dto.width()),
+ NamedReference.field(field_name=dto.field_name()),
+ ]
+ self.assertListEqual(arguments, expected_arguments)
+
+ def test_empty_field_names(self):
+ dto = TruncatePartitioningDTO(field_name=[], width=20)
+ self.assertEqual([], dto.field_name())
+ expected_arguments = [
+ Literals.integer_literal(value=dto.width()),
+ NamedReference.field(field_name=dto.field_name()),
+ ]
+ self.assertListEqual(dto.arguments(), expected_arguments)
+
+ def test_validate_non_existing_field(self):
+ dto = TruncatePartitioningDTO(field_name=["nonexistent"], width=20)
+ with self.assertRaises(IllegalArgumentException):
+ dto.validate(self.columns)
diff --git a/clients/client-python/tests/unittests/dto/rel/test_partitioning.py b/clients/client-python/tests/unittests/dto/rel/test_partitioning.py
new file mode 100644
index 0000000000..370bdec0b1
--- /dev/null
+++ b/clients/client-python/tests/unittests/dto/rel/test_partitioning.py
@@ -0,0 +1,109 @@
+# 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.expressions.named_reference import NamedReference
+from gravitino.api.types.types import Types
+from gravitino.dto.rel.column_dto import ColumnDTO
+from gravitino.dto.rel.partitioning.partitioning import (
+ Partitioning,
+ SingleFieldPartitioning,
+)
+from gravitino.exceptions.base import IllegalArgumentException
+
+
+class MockPartitioningDTO(SingleFieldPartitioning):
+ def strategy(self) -> Partitioning.Strategy:
+ return Partitioning.Strategy.IDENTITY
+
+
+class TestPartitioning(unittest.TestCase):
+ def test_strategy_enum_values(self):
+ self.assertEqual(Partitioning.Strategy.IDENTITY, "identity")
+ self.assertEqual(Partitioning.Strategy.YEAR, "year")
+ self.assertEqual(Partitioning.Strategy.MONTH, "month")
+ self.assertEqual(Partitioning.Strategy.DAY, "day")
+ self.assertEqual(Partitioning.Strategy.HOUR, "hour")
+ self.assertEqual(Partitioning.Strategy.BUCKET, "bucket")
+ self.assertEqual(Partitioning.Strategy.TRUNCATE, "truncate")
+ self.assertEqual(Partitioning.Strategy.LIST, "list")
+ self.assertEqual(Partitioning.Strategy.RANGE, "range")
+ self.assertEqual(Partitioning.Strategy.FUNCTION, "function")
+
+ def test_get_by_name_valid_strategies(self):
+ self.assertEqual(
+ Partitioning.get_by_name("identity"), Partitioning.Strategy.IDENTITY
+ )
+ self.assertEqual(Partitioning.get_by_name("YEAR"), Partitioning.Strategy.YEAR)
+ self.assertEqual(Partitioning.get_by_name("Month"), Partitioning.Strategy.MONTH)
+
+ def test_get_by_name_invalid_strategy(self):
+ invalid_strategy_name = "invalid"
+ with self.assertRaisesRegex(
+ IllegalArgumentException,
+ f"Invalid partitioning strategy: {invalid_strategy_name}",
+ ):
+ Partitioning.get_by_name(invalid_strategy_name)
+
+ def test_empty_partitioning_constant(self):
+ self.assertEqual(len(Partitioning.EMPTY_PARTITIONING), 0)
+
+
+class TestSingleFieldPartitioning(unittest.TestCase):
+ def setUp(self):
+ self.columns = [
+ ColumnDTO.builder()
+ .with_name("col1")
+ .with_data_type(Types.StringType.get())
+ .with_comment("test column")
+ .build()
+ ]
+
+ def test_init_valid_field_name(self):
+ partitioning = MockPartitioningDTO(["col1"])
+ self.assertEqual(partitioning.field_name(), ["col1"])
+
+ def test_init_invalid_field_name(self):
+ with self.assertRaises(IllegalArgumentException):
+ MockPartitioningDTO([])
+
+ with self.assertRaises(IllegalArgumentException):
+ MockPartitioningDTO(None)
+
+ def test_validate_existing_field(self):
+ partitioning = MockPartitioningDTO(["col1"])
+ partitioning.validate(self.columns)
+
+ def test_validate_non_existing_field(self):
+ partitioning = MockPartitioningDTO(["nonexistent"])
+ with self.assertRaises(IllegalArgumentException):
+ partitioning.validate(self.columns)
+
+ def test_arguments(self):
+ partitioning = MockPartitioningDTO(["col1"])
+ args = partitioning.arguments()
+ self.assertEqual(len(args), 1)
+ self.assertIsInstance(args[0], NamedReference)
+
+ def test_strategy(self):
+ partitioning = MockPartitioningDTO(["col1"])
+ self.assertEqual(partitioning.strategy(), Partitioning.Strategy.IDENTITY)
+
+ def test_name(self):
+ partitioning = MockPartitioningDTO(["col1"])
+ self.assertEqual(partitioning.name(), "identity")