You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2021/06/10 12:16:46 UTC

[GitHub] [iceberg] TGooch44 commented on a change in pull request #2689: [Python] support custom target name in partition spec builder

TGooch44 commented on a change in pull request #2689:
URL: https://github.com/apache/iceberg/pull/2689#discussion_r649099139



##########
File path: python/iceberg/api/partition_spec.py
##########
@@ -213,106 +214,145 @@ def with_spec_id(self, spec_id):
         self.spec_id = spec_id
         return self
 
-    def check_and_add_partition_name(self, name):
+    def check_and_add_partition_name(self, name, source_column_id=None):
+        schema_field = self.schema.find_field(name)
+        if source_column_id is not None:
+            if schema_field is not None and schema_field.field_id != source_column_id:
+                raise ValueError("Cannot create identity partition sourced from different field in schema: %s" % name)
+        else:
+            if schema_field is not None:
+                raise ValueError("Cannot create partition from name that exists in schema: %s" % name)
+
         if name is None or name == "":
-            raise RuntimeError("Cannot use empty or null partition name")
+            raise ValueError("Cannot use empty or null partition name: %s" % name)
         if name in self.partition_names:
-            raise RuntimeError("Cannot use partition names more than once: %s" % name)
+            raise ValueError("Cannot use partition names more than once: %s" % name)
 
         self.partition_names.add(name)
         return self
 
+    def check_for_redundant_partitions(self, field):

Review comment:
       Do you think it's worthwhile introducing a function that checks for redundant partitions and adds the field?  Each transform method does the same block creating the partition field and then adding it.  Something like this:
   ```
   def check_redundant_and_add_field(self, field_id: int, name: str, transform: 'Transform') -> None:
           part_field = (PartitionField(field_id,
                                        self.__next_field_id(),
                                        name,
                                        transform))
           self.check_for_redundant_partitions(part_field)
           self.fields.append(part_field)
   ```

##########
File path: python/tests/api/test_partition_spec.py
##########
@@ -0,0 +1,84 @@
+# 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 iceberg.api import PartitionSpec
+from iceberg.api.schema import Schema
+from iceberg.api.types import (BinaryType,
+                               DateType,
+                               DecimalType,
+                               FixedType,
+                               IntegerType,
+                               LongType,
+                               NestedField,
+                               StringType,
+                               TimestampType,
+                               TimeType,
+                               UUIDType)
+from tests.api.test_helpers import TestHelpers
+
+
+class TestConversions(unittest.TestCase):
+
+    def test_transforms(self):
+        schema = Schema(NestedField.required(1, "i", IntegerType.get()),
+                        NestedField.required(2, "l", LongType.get()),
+                        NestedField.required(3, "d", DateType.get()),
+                        NestedField.required(4, "t", TimeType.get()),
+                        NestedField.required(5, "ts", TimestampType.without_timezone()),
+                        NestedField.required(6, "dec", DecimalType.of(9, 2)),
+                        NestedField.required(7, "s", StringType.get()),
+                        NestedField.required(8, "u", UUIDType.get()),
+                        NestedField.required(9, "f", FixedType.of_length(3)),
+                        NestedField.required(10, "b", BinaryType.get()))
+        specs = [PartitionSpec.builder_for(schema).identity("i").build(),
+                 PartitionSpec.builder_for(schema).identity("l").build(),
+                 PartitionSpec.builder_for(schema).identity("d").build(),
+                 PartitionSpec.builder_for(schema).identity("t").build(),
+                 PartitionSpec.builder_for(schema).identity("ts").build(),
+                 PartitionSpec.builder_for(schema).identity("dec").build(),
+                 PartitionSpec.builder_for(schema).identity("s").build(),
+                 PartitionSpec.builder_for(schema).identity("u").build(),
+                 PartitionSpec.builder_for(schema).identity("f").build(),
+                 PartitionSpec.builder_for(schema).identity("b").build(),
+                 PartitionSpec.builder_for(schema).bucket("i", 128).build(),
+                 PartitionSpec.builder_for(schema).bucket("l", 128).build(),
+                 PartitionSpec.builder_for(schema).bucket("d", 128).build(),
+                 PartitionSpec.builder_for(schema).bucket("t", 128).build(),
+                 PartitionSpec.builder_for(schema).bucket("ts", 128).build(),
+                 PartitionSpec.builder_for(schema).bucket("dec", 128).build(),
+                 PartitionSpec.builder_for(schema).bucket("s", 128).build(),
+                 # todo support them
+                 # PartitionSpec.builder_for(schema).bucket("u", 128).build(),
+                 # PartitionSpec.builder_for(schema).bucket("f", 128).build(),
+                 # PartitionSpec.builder_for(schema).bucket("b", 128).build(),
+                 PartitionSpec.builder_for(schema).year("d").build(),
+                 PartitionSpec.builder_for(schema).month("d").build(),
+                 PartitionSpec.builder_for(schema).day("d").build(),
+                 PartitionSpec.builder_for(schema).year("ts").build(),
+                 PartitionSpec.builder_for(schema).month("ts").build(),
+                 PartitionSpec.builder_for(schema).day("ts").build(),
+                 PartitionSpec.builder_for(schema).hour("ts").build(),
+                 PartitionSpec.builder_for(schema).truncate("i", 10).build(),
+                 PartitionSpec.builder_for(schema).truncate("l", 10).build(),
+                 PartitionSpec.builder_for(schema).truncate("dec", 10).build(),
+                 PartitionSpec.builder_for(schema).truncate("s", 10).build(),
+                 # todo support them
+                 # PartitionSpec.builder_for(schema).add_without_field_id(6, "dec_unsupported", "unsupported").build(),

Review comment:
       Is this not supported?




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org