danny0405 commented on code in PR #8102:
URL: https://github.com/apache/hudi/pull/8102#discussion_r1157170289


##########
hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/source/prune/PartitionPruners.java:
##########
@@ -0,0 +1,161 @@
+/*
+ * 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.hudi.source.prune;
+
+import org.apache.hudi.source.ExpressionEvaluators;
+import org.apache.hudi.source.stats.ColumnStats;
+import org.apache.hudi.table.format.FilePathUtils;
+import org.apache.hudi.util.DataTypeUtils;
+
+import org.apache.flink.table.types.DataType;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Tools to prune partitions.
+ */
+public class PartitionPruners {
+
+  public interface PartitionPruner extends Serializable {
+
+    /**
+     * Applies partition pruning on the given partition list, return remained 
partitions.
+     */
+    Set<String> filter(Collection<String> partitions);
+  }
+
+  /**
+   * Dynamic partition pruner for hoodie table source which partitions list is 
available in runtime phase. Note: the data of new partitions created after the 
job starts could be read if they match the
+   * filter conditions.
+   */
+  public static class DynamicPartitionPruner implements PartitionPruner {
+
+    private static final long serialVersionUID = 1L;
+
+    private final List<ExpressionEvaluators.Evaluator> partitionEvaluator;
+
+    private final String[] partitionKeys;
+
+    private final List<DataType> partitionTypes;
+
+    private final String defaultParName;
+
+    private final boolean hivePartition;
+
+    private DynamicPartitionPruner(
+        List<ExpressionEvaluators.Evaluator> partitionEvaluators,
+        List<String> partitionKeys,
+        List<DataType> partitionTypes,
+        String defaultParName,
+        boolean hivePartition) {
+      this.partitionEvaluator = partitionEvaluators;
+      this.partitionKeys = partitionKeys.toArray(new String[] {});
+      this.partitionTypes = partitionTypes;
+      this.defaultParName = defaultParName;
+      this.hivePartition = hivePartition;
+    }
+
+    public Set<String> filter(Collection<String> partitions) {
+      return 
partitions.stream().filter(this::evaluate).collect(Collectors.toSet());
+    }
+
+    private boolean evaluate(String partition) {
+      String[] partStrArray = FilePathUtils.extractPartitionKeyValues(
+          new org.apache.hadoop.fs.Path(partition),
+          hivePartition,
+          partitionKeys).values().toArray(new String[] {});
+      Map<String, ColumnStats> partStats = new LinkedHashMap<>();
+      for (int idx = 0; idx < partitionKeys.length; idx++) {
+        String partKey = partitionKeys[idx];
+        Object partVal = partKey.equals(defaultParName)
+            ? null : DataTypeUtils.resolvePartition(partStrArray[idx], 
partitionTypes.get(idx));
+        ColumnStats columnStats = new ColumnStats(partVal, partVal, partVal == 
null ? 1 : 0);
+        partStats.put(partKey, columnStats);
+      }
+      return partitionEvaluator.stream().allMatch(evaluator -> 
evaluator.eval(partStats));
+    }
+  }
+
+  /**
+   * Static partition pruner for hoodie table source which partitions list is 
available in compile phase.
+   * After applied this partition pruner, hoodie source could not read the 
data from other partitions during runtime.
+   * Note: the data of new partitions created after the job starts would never 
be read.
+   */
+  public static class StaticPartitionPruner implements PartitionPruner {
+
+    private static final long serialVersionUID = 1L;
+
+    private final Set<String> candidatePartitions;
+
+    private StaticPartitionPruner(Set<String> candidatePartitions) {
+      this.candidatePartitions = candidatePartitions;
+    }
+
+    public Set<String> filter(Collection<String> partitions) {
+      return partitions.stream()
+          
.filter(this.candidatePartitions::contains).collect(Collectors.toSet());
+    }
+  }
+
+  /**
+   * Fake partition pruner which would not prune any partitions.
+   */
+  public static class FakePartitionPruner implements PartitionPruner {
+
+    private static final long serialVersionUID = 1L;
+
+    private FakePartitionPruner() {
+
+    }
+
+    @Override
+    public Set<String> filter(Collection<String> partitions) {
+      return new HashSet<>(partitions);
+    }
+  }
+
+  public static PartitionPruner getInstance(
+      List<ExpressionEvaluators.Evaluator> partitionEvaluators,
+      List<String> partitionKeys,
+      List<DataType> partitionTypes,
+      String defaultParName,
+      boolean hivePartition) {
+    if (partitionEvaluators.isEmpty()) {
+      return new FakePartitionPruner();

Review Comment:
   In which case the `partitionEvaluators` can be empty?



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to