alexeykudinkin commented on code in PR #6725: URL: https://github.com/apache/hudi/pull/6725#discussion_r1029747120
########## hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/util/PartitionFilterGenerator.java: ########## @@ -0,0 +1,212 @@ +/* + * 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.hive.util; + +import org.apache.hudi.common.util.ReflectionUtils; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.hive.HiveSyncConfig; +import org.apache.hudi.hive.HoodieHiveSyncException; +import org.apache.hudi.hive.expression.And; +import org.apache.hudi.hive.expression.AttributeReferenceExpression; +import org.apache.hudi.hive.expression.EqualTo; +import org.apache.hudi.hive.expression.Expression; +import org.apache.hudi.hive.expression.GreatThanOrEqual; +import org.apache.hudi.hive.expression.LessThanOrEqual; +import org.apache.hudi.hive.expression.Literal; +import org.apache.hudi.hive.expression.Or; +import org.apache.hudi.sync.common.model.FieldSchema; +import org.apache.hudi.sync.common.model.Partition; +import org.apache.hudi.sync.common.model.PartitionValueExtractor; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.apache.hudi.hive.HiveSyncConfig.HIVE_SYNC_FILTER_PUSHDOWN_MAX_SIZE; +import static org.apache.hudi.sync.common.HoodieSyncConfig.META_SYNC_PARTITION_EXTRACTOR_CLASS; + +public class PartitionFilterGenerator { + + private static final Set<String> SUPPORT_TYPES = new HashSet<String>() { + { + add(HiveSchemaUtil.INT_TYPE_NAME); + add(HiveSchemaUtil.BIGINT_TYPE_NAME); + add(HiveSchemaUtil.DATE_TYPE_NAME); + add(HiveSchemaUtil.STRING_TYPE_NAME); + } + }; + + /** + * Build expression from the Partition list. Here we're trying to match all partitions. + * + * ex. partitionSchema(date, hour) [Partition(2022-09-01, 12), Partition(2022-09-02, 13)] => + * Or(And(Equal(Attribute(date), Literal(2022-09-01)), Equal(Attribute(hour), Literal(12))), + * And(Equal(Attribute(date), Literal(2022-09-02)), Equal(Attribute(hour), Literal(13)))) + */ + private static Expression buildPartitionExpression(List<Partition> partitions, List<FieldSchema> partitionFields) { + return partitions.stream().map(partition -> { + List<String> partitionValues = partition.getValues(); + Expression root = null; + + for (int i = 0; i < partitionFields.size(); i++) { + FieldSchema field = partitionFields.get(i); + String value = partitionValues.get(i); + EqualTo exp = new EqualTo(new AttributeReferenceExpression(field.getName()), + new Literal(value, field.getType())); + if (root != null) { + root = new And(root, exp); Review Comment: @boneanxs i'd suggest to instead of creating separate classes just do static methods in the BinaryOperator like following: ``` BinaryOperator.and(left, right); BinaryOperator.gteq(left, right); // ... ``` This will make it easier for us to reconcile this in the near future w/ RFC-64 (having individual classes compounds the overhead of handling them) ########## hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/HiveSyncTool.java: ########## @@ -309,14 +311,36 @@ private boolean syncSchema(String tableName, boolean tableExists, boolean useRea return schemaChanged; } + /** + * Fetch partitions from meta service, will try to push down more filters to avoid fetching + * too many unnecessary partitions. + */ + private List<Partition> getTablePartitions(String tableName, List<String> writtenPartitionsSince) { Review Comment: Thank you! I think we can just drop "since" suffix in this context, since it's bringing more confusion than clarity ########## hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/util/PartitionFilterGenerator.java: ########## @@ -0,0 +1,191 @@ +/* + * 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.hive.util; + +import org.apache.hudi.common.util.ReflectionUtils; +import org.apache.hudi.common.util.collection.Pair; +import org.apache.hudi.hive.HiveSyncConfig; +import org.apache.hudi.hive.HoodieHiveSyncException; +import org.apache.hudi.hive.expression.BinaryOperator; +import org.apache.hudi.hive.expression.Expression; +import org.apache.hudi.hive.expression.LeafExpression; +import org.apache.hudi.sync.common.model.FieldSchema; +import org.apache.hudi.sync.common.model.Partition; +import org.apache.hudi.sync.common.model.PartitionValueExtractor; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.apache.hudi.hive.HiveSyncConfig.HIVE_SYNC_FILTER_PUSHDOWN_MAX_SIZE; +import static org.apache.hudi.sync.common.HoodieSyncConfig.META_SYNC_PARTITION_EXTRACTOR_CLASS; + +public class PartitionFilterGenerator { + + /** + * Build expression from the Partition list. + * + * ex. partitionSchema(date, hour) [Partition(2022-09-01, 12), Partition(2022-09-02, 13)] => + * Or(And(Equal(Attribute(date), Literal(2022-09-01)), Equal(Attribute(hour), Literal(12))), + * And(Equal(Attribute(date), Literal(2022-09-02)), Equal(Attribute(hour), Literal(13)))) + */ + private static Expression buildPartitionExpression(List<Partition> partitions, List<FieldSchema> partitionFields) { + return partitions.stream().map(partition -> { + List<String> partitionValues = partition.getValues(); + Expression root = null; + + for (int i = 0; i < partitionFields.size(); i++) { + FieldSchema field = partitionFields.get(i); + String value = partitionValues.get(i); + BinaryOperator.EqualTo exp = new BinaryOperator.EqualTo(new LeafExpression.AttributeReferenceExpression(field.getName()), + new LeafExpression.Literal(value, field.getType())); + if (root != null) { + root = new BinaryOperator.And(root, exp); + } else { + root = exp; + } + } + return root; + }).reduce(null, (result, expr) -> { + if (result == null) { + return expr; + } else { + return new BinaryOperator.Or(result, expr); + } + }); + } + + /** + * Extract partition values from the {@param partitions}, and binding to + * corresponding partition fieldSchemas. + */ + private static List<Pair<FieldSchema, String[]>> extractFieldValues(List<Partition> partitions, List<FieldSchema> partitionFields) { + return IntStream.range(0, partitionFields.size()) + .mapToObj(i -> { + Set<String> values = new HashSet<String>(); + for (int j = 0; j < partitions.size(); j++) { + values.add(partitions.get(j).getValues().get(i)); + } + return Pair.of(partitionFields.get(i), values.toArray(new String[0])); + }) + .collect(Collectors.toList()); + } + + private static class ValueComparator implements Comparator<String> { + + private final String valueType; + public ValueComparator(String type) { + this.valueType = type; + } + + @Override + public int compare(String s1, String s2) { + switch (valueType.toLowerCase(Locale.ROOT)) { + case HiveSchemaUtil.INT_TYPE_NAME: + int i1 = Integer.parseInt(s1); + int i2 = Integer.parseInt(s2); + return i1 - i2; + case HiveSchemaUtil.BIGINT_TYPE_NAME: + long l1 = Long.parseLong(s1); + long l2 = Long.parseLong(s2); + long result = l1 - l2; + if (result > 0) { + return 1; + } + + if (result < 0) { + return -1; + } + + return 0; + default: + return s1.compareTo(s2); Review Comment: Make sense. Please add it as a comment in the code as well, so that whoever reading this won't be guessing -- 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]
