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/23 08:56:30 UTC

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

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



##########
File path: mr/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergPartitionTextParser.java
##########
@@ -0,0 +1,111 @@
+/*
+ * 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.relocated.com.google.common.base.Preconditions;
+
+/**
+ * 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, BiConsumer<String, Integer> transformBuilder,
+                                            String trimmedPart, int intArgPos) {
+    Matcher matcher = transformPattern.matcher(trimmedPart);
+    if (matcher.find()) {
+      Preconditions.checkArgument(matcher.groupCount() == 2,
+          "Cannot parse 2-arg partition transform from text part: %s", trimmedPart);
+
+      String intArg = matcher.group(intArgPos).trim();
+      Preconditions.checkArgument(intArg.matches(INTEGER_PATTERN),
+          "Cannot find integer as argument %s in 2-arg partition transform: %s", intArgPos, trimmedPart);
+      int transformArg = Integer.parseInt(matcher.group(intArgPos).trim());
+
+      String columnName = matcher.group(intArgPos % 2 + 1).trim();
+      transformBuilder.accept(columnName, transformArg);
+      return true;
+    }
+
+    return false;
+  }
+
+  private static boolean build1ArgTransform(Pattern transformPattern, Consumer<String> transformBuilder,
+                                            String trimmedPart) {
+    Matcher matcher = transformPattern.matcher(trimmedPart);
+    if (matcher.find()) {
+      Preconditions.checkArgument(matcher.groupCount() == 1,
+          "Cannot parse 1-arg partition transform from text part: %s", trimmedPart);
+      String columnName = matcher.group(1).trim();
+      transformBuilder.accept(columnName);
+      return true;
+    }
+
+    return false;
+  }
+
+  public static PartitionSpec fromText(Schema schema, String text) {
+    Preconditions.checkArgument(text.length() < PARTITION_TEXT_MAX_LENGTH,
+        "Partition spec text too long: max allowed length %s, but got %s",
+        PARTITION_TEXT_MAX_LENGTH, text.length());
+    String[] parts = text.split(PIPE_DELIMITER);

Review comment:
       As @marton-bod pointed out in one of our discussions, there is a possibility to have columns with special characters in the column names, like `,|` etc. See: [HIVE-25222](https://issues.apache.org/jira/browse/HIVE-25222) - Fix reading Iceberg tables with a comma in column names.




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