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/21 18:17:55 UTC

[GitHub] [iceberg] yyanyy commented on a change in pull request #2701: Hive: support create table with partition transform through table property

yyanyy commented on a change in pull request #2701:
URL: https://github.com/apache/iceberg/pull/2701#discussion_r655606002



##########
File path: mr/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergPartitionTextParser.java
##########
@@ -0,0 +1,112 @@
+/*
+ * 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.
+ */
+
+package org.apache.iceberg.mr.hive;
+
+import java.util.function.BiConsumer;
+import java.util.function.Consumer;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.exceptions.ValidationException;
+
+/**
+ * Parser for inputs received from config {@link org.apache.iceberg.mr.InputFormatConfig#PARTITIONING}
+ */
+class HiveIcebergPartitionTextParser {
+  private static final int PARTITION_TEXT_MAX_LENGTH = 1000;
+
+  private static final Pattern BUCKET_PATTERN = Pattern.compile("bucket\\((.*?),(.*?)\\)", Pattern.CASE_INSENSITIVE);
+  private static final Pattern TRUNCATE_PATTERN = Pattern.compile(
+      "truncate\\((.*?),(.*?)\\)", Pattern.CASE_INSENSITIVE);
+  private static final Pattern YEAR_PATTERN = Pattern.compile("year\\((.*?)\\)", Pattern.CASE_INSENSITIVE);
+  private static final Pattern MONTH_PATTERN = Pattern.compile("month\\((.*?)\\)", Pattern.CASE_INSENSITIVE);
+  private static final Pattern DAY_PATTERN = Pattern.compile("day\\((.*?)\\)", Pattern.CASE_INSENSITIVE);
+  private static final Pattern HOUR_PATTERN = Pattern.compile("hour\\((.*?)\\)", Pattern.CASE_INSENSITIVE);
+  private static final Pattern ALWAYS_NULL_PATTERN = Pattern.compile(
+      "alwaysNull\\((.*?)\\)", Pattern.CASE_INSENSITIVE);
+  private static final String INTEGER_PATTERN = "\\d+";
+  private static final String PIPE_DELIMITER = "\\|";
+
+  private HiveIcebergPartitionTextParser() {
+  }
+
+  private static boolean build2ArgTransform(Pattern transformPattern, Schema schema,
+                                         BiConsumer<String, Integer> transformBuilder, String trimmedPart) {
+    Matcher matcher = transformPattern.matcher(trimmedPart);
+    if (matcher.find()) {
+      ValidationException.check(matcher.groupCount() == 2 && matcher.group(2).trim().matches(INTEGER_PATTERN),
+          "Cannot parse 2-arg partition transform from text part: %s", trimmedPart);
+
+      String columnName = matcher.group(1).trim();
+      ValidationException.check(schema.findField(columnName) != null,

Review comment:
       Nit: I think we can delegate such validation exception to partition spec builder so that we don't need to test ourselves? That has to catch more complicated cases like unexpected column types anyway

##########
File path: mr/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergPartitionTextParser.java
##########
@@ -0,0 +1,123 @@
+/*
+ * 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.
+ */
+
+package org.apache.iceberg.mr.hive;
+
+import org.apache.iceberg.AssertHelpers;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.types.Types;
+import org.junit.Assert;
+import org.junit.Test;
+
+import static org.apache.iceberg.types.Types.NestedField.optional;
+
+public class TestHiveIcebergPartitionTextParser {
+
+  private static final Schema SCHEMA = new Schema(
+      optional(1, "id", Types.LongType.get()),
+      optional(2, "name", Types.StringType.get()),
+      optional(3, "employee_info", Types.StructType.of(
+          optional(6, "employer", Types.StringType.get()),
+          optional(7, "id", Types.LongType.get()),
+          optional(8, "address", Types.StringType.get())
+      )),
+      optional(4, "created", Types.TimestampType.withoutZone()),
+      optional(5, "updated", Types.TimestampType.withoutZone())
+  );
+
+  @Test
+  public void testParsingColumns() {
+    PartitionSpec expected = PartitionSpec.builderFor(SCHEMA)
+        .identity("id").identity("name").build();
+    Assert.assertEquals(expected, HiveIcebergPartitionTextParser.fromText(SCHEMA, "id|name"));
+    Assert.assertEquals("space on the right of pipe should not matter",
+        expected, HiveIcebergPartitionTextParser.fromText(SCHEMA, "id| name"));
+    Assert.assertEquals("space on the left of pipe should not matter",
+        expected, HiveIcebergPartitionTextParser.fromText(SCHEMA, "id |name"));
+    Assert.assertEquals("space on both sides of pipe should not matter",
+        expected, HiveIcebergPartitionTextParser.fromText(SCHEMA, "id | name"));
+  }
+
+  @Test
+  public void testParsingColumnsFailure() {
+    AssertHelpers.assertThrows("should fail when column does not exist",
+        ValidationException.class,
+        "Cannot find column",
+        () -> HiveIcebergPartitionTextParser.fromText(SCHEMA, "col"));
+  }
+
+  @Test
+  public void testParsing2ArgTransforms() {
+    PartitionSpec expected = PartitionSpec.builderFor(SCHEMA)
+        .bucket("name", 16).truncate("employee_info.address", 8).build();
+    Assert.assertEquals(expected, HiveIcebergPartitionTextParser.fromText(
+        SCHEMA, "bucket(name,16)|truncate(employee_info.address,8)"));
+    Assert.assertEquals("space in args should not matter",
+        expected, HiveIcebergPartitionTextParser.fromText(
+            SCHEMA, "bucket( name, 16)| truncate(employee_info.address ,8 )"));
+  }
+
+  @Test
+  public void testParsing2ArgTransformsFailure() {
+    AssertHelpers.assertThrows("should fail when column does not exist",
+        ValidationException.class,
+        "Cannot find column",
+        () -> HiveIcebergPartitionTextParser.fromText(SCHEMA, "bucket(col, 8)"));
+
+    AssertHelpers.assertThrows("should fail when input to transform is wrong",
+        ValidationException.class,
+        "Cannot parse 2-arg partition transform from text part",
+        () -> HiveIcebergPartitionTextParser.fromText(SCHEMA, "bucket(8, name)"));

Review comment:
       Nit: could add cases like `bucket(name)` for 2-args transforms, and `day(1, created)` for 1-arg transform




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