AshinGau commented on code in PR #11156:
URL: https://github.com/apache/doris/pull/11156#discussion_r928515893


##########
fe/fe-core/src/main/java/org/apache/doris/catalog/HiveBucketUtil.java:
##########
@@ -0,0 +1,394 @@
+// 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.doris.catalog;
+
+import org.apache.doris.analysis.BinaryPredicate;
+import org.apache.doris.analysis.CompoundPredicate;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.InPredicate;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.thrift.TExprOpcode;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters;
+import 
org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter;
+import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector;
+import org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.FileSplit;
+import org.apache.hadoop.mapred.InputSplit;
+import org.apache.hive.common.util.Murmur3;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class HiveBucketUtil {
+    private static final Logger LOG = 
LogManager.getLogger(HiveBucketUtil.class);
+
+    private static final Set<PrimitiveType> SUPPORTED_TYPES_FOR_BUCKET_FILTER 
= ImmutableSet.of(
+            PrimitiveType.BOOLEAN,
+            PrimitiveType.TINYINT,
+            PrimitiveType.SMALLINT,
+            PrimitiveType.INT,
+            PrimitiveType.BIGINT,
+            PrimitiveType.STRING);
+
+    private static PrimitiveTypeInfo convertToHiveColType(PrimitiveType 
dorisType) throws DdlException {
+        switch (dorisType) {
+            case BOOLEAN:
+                return TypeInfoFactory.booleanTypeInfo;
+            case TINYINT:
+                return TypeInfoFactory.byteTypeInfo;
+            case SMALLINT:
+                return TypeInfoFactory.shortTypeInfo;
+            case INT:
+                return TypeInfoFactory.intTypeInfo;
+            case BIGINT:
+                return TypeInfoFactory.longTypeInfo;
+            case STRING:
+                return TypeInfoFactory.stringTypeInfo;
+            default:
+                throw new DdlException("Unsupported pruning bucket column 
type: " + dorisType);
+        }
+    }
+
+    private static final Pattern BUCKET_WITH_OPTIONAL_ATTEMPT_ID_PATTERN =
+            Pattern.compile("bucket_(\\d+)(_\\d+)?$");
+
+    private static final Iterable<Pattern> BUCKET_PATTERNS = ImmutableList.of(
+            // legacy Presto naming pattern (current version matches Hive)
+            
Pattern.compile("\\d{8}_\\d{6}_\\d{5}_[a-z0-9]{5}_bucket-(\\d+)(?:[-_.].*)?"),
+            // Hive naming pattern per 
`org.apache.hadoop.hive.ql.exec.Utilities#getBucketIdFromFile()`
+            Pattern.compile("(\\d+)_\\d+.*"),
+            // Hive ACID with optional direct insert attempt id
+            BUCKET_WITH_OPTIONAL_ATTEMPT_ID_PATTERN);
+
+    public static List<InputSplit> getPrunedSplitsByBuckets(
+            String tableName, List<InputSplit> splits, Set<Integer> buckets, 
int numBuckets) {
+        if (buckets == null) {
+            return splits;
+        }
+        if (buckets.size() == 0) {
+            return Collections.emptyList();
+        }
+        List<InputSplit> result = new LinkedList<>();
+        boolean valid = true;
+        for (InputSplit split : splits) {
+            String fileName = ((FileSplit) split).getPath().getName();
+            OptionalInt bucket = getBucketNumberFromPath(fileName);
+            if (bucket.isPresent()) {
+                int bucketId = bucket.getAsInt();
+                if (bucketId >= numBuckets) {
+                    valid = false;
+                    LOG.warn("Hive table {} is corrupt for file 
{}(bucketId={}), skip bucket pruning.",
+                            tableName, fileName, bucketId);
+                    break;
+                }
+                if (buckets.contains(bucketId)) {
+                    result.add(split);
+                }
+            } else {
+                valid = false;
+                LOG.warn("File {} is not a bucket file in hive table {}, skip 
bucket pruning.", fileName, tableName);

Review Comment:
   done



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/HiveBucketUtil.java:
##########
@@ -0,0 +1,394 @@
+// 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.doris.catalog;
+
+import org.apache.doris.analysis.BinaryPredicate;
+import org.apache.doris.analysis.CompoundPredicate;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.InPredicate;
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.analysis.SlotRef;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.thrift.TExprOpcode;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters;
+import 
org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter;
+import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector;
+import org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.FileSplit;
+import org.apache.hadoop.mapred.InputSplit;
+import org.apache.hive.common.util.Murmur3;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.nio.ByteBuffer;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class HiveBucketUtil {
+    private static final Logger LOG = 
LogManager.getLogger(HiveBucketUtil.class);
+
+    private static final Set<PrimitiveType> SUPPORTED_TYPES_FOR_BUCKET_FILTER 
= ImmutableSet.of(
+            PrimitiveType.BOOLEAN,
+            PrimitiveType.TINYINT,
+            PrimitiveType.SMALLINT,
+            PrimitiveType.INT,
+            PrimitiveType.BIGINT,
+            PrimitiveType.STRING);
+
+    private static PrimitiveTypeInfo convertToHiveColType(PrimitiveType 
dorisType) throws DdlException {
+        switch (dorisType) {
+            case BOOLEAN:
+                return TypeInfoFactory.booleanTypeInfo;
+            case TINYINT:
+                return TypeInfoFactory.byteTypeInfo;
+            case SMALLINT:
+                return TypeInfoFactory.shortTypeInfo;
+            case INT:
+                return TypeInfoFactory.intTypeInfo;
+            case BIGINT:
+                return TypeInfoFactory.longTypeInfo;
+            case STRING:
+                return TypeInfoFactory.stringTypeInfo;
+            default:
+                throw new DdlException("Unsupported pruning bucket column 
type: " + dorisType);
+        }
+    }
+
+    private static final Pattern BUCKET_WITH_OPTIONAL_ATTEMPT_ID_PATTERN =
+            Pattern.compile("bucket_(\\d+)(_\\d+)?$");
+
+    private static final Iterable<Pattern> BUCKET_PATTERNS = ImmutableList.of(
+            // legacy Presto naming pattern (current version matches Hive)
+            
Pattern.compile("\\d{8}_\\d{6}_\\d{5}_[a-z0-9]{5}_bucket-(\\d+)(?:[-_.].*)?"),
+            // Hive naming pattern per 
`org.apache.hadoop.hive.ql.exec.Utilities#getBucketIdFromFile()`
+            Pattern.compile("(\\d+)_\\d+.*"),
+            // Hive ACID with optional direct insert attempt id
+            BUCKET_WITH_OPTIONAL_ATTEMPT_ID_PATTERN);
+
+    public static List<InputSplit> getPrunedSplitsByBuckets(
+            String tableName, List<InputSplit> splits, Set<Integer> buckets, 
int numBuckets) {
+        if (buckets == null) {
+            return splits;
+        }
+        if (buckets.size() == 0) {
+            return Collections.emptyList();
+        }
+        List<InputSplit> result = new LinkedList<>();
+        boolean valid = true;
+        for (InputSplit split : splits) {
+            String fileName = ((FileSplit) split).getPath().getName();
+            OptionalInt bucket = getBucketNumberFromPath(fileName);
+            if (bucket.isPresent()) {
+                int bucketId = bucket.getAsInt();
+                if (bucketId >= numBuckets) {
+                    valid = false;
+                    LOG.warn("Hive table {} is corrupt for file 
{}(bucketId={}), skip bucket pruning.",
+                            tableName, fileName, bucketId);
+                    break;
+                }
+                if (buckets.contains(bucketId)) {
+                    result.add(split);
+                }
+            } else {
+                valid = false;
+                LOG.warn("File {} is not a bucket file in hive table {}, skip 
bucket pruning.", fileName, tableName);
+                break;
+            }
+        }
+        if (valid) {
+            LOG.info("{} / {} input splits in hive table {} after bucket 
pruning.",

Review Comment:
   done



##########
fe/fe-core/src/main/java/org/apache/doris/planner/external/ExternalHiveScanProvider.java:
##########
@@ -109,12 +112,22 @@ public List<InputSplit> getSplits(List<Expr> exprs)
 
         Configuration configuration = setConfiguration();
         InputFormat<?, ?> inputFormat = HiveUtil.getInputFormat(configuration, 
inputFormatName, false);
+        List<InputSplit> result;
         if (!hivePartitions.isEmpty()) {
-            return hivePartitions.parallelStream()
+            result = hivePartitions.parallelStream()
                     .flatMap(x -> getSplitsByPath(inputFormat, configuration, 
x.getSd().getLocation()).stream())
                     .collect(Collectors.toList());
         } else {
-            return getSplitsByPath(inputFormat, configuration, splitsPath);
+            result = getSplitsByPath(inputFormat, configuration, splitsPath);
+        }
+        Optional<Set<Integer>> prunedBuckets = HiveBucketUtil.getPrunedBuckets(

Review Comment:
   done



-- 
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: commits-unsubscr...@doris.apache.org

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


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

Reply via email to