marton-bod commented on a change in pull request #2701:
URL: https://github.com/apache/iceberg/pull/2701#discussion_r656904131



##########
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:
       One other thing to consider is that users will be used to the `,` comma 
delimiter as the syntax from Spark SQL/Hive4/Impala, e.g. `partitioned by 
(bucket(16, id), category)`, so the pipe could feel unnatural.
   I know this would complicate the parsing logic, but something to consider 
also.




-- 
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:
us...@infra.apache.org



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

Reply via email to