danny0405 commented on code in PR #8437:
URL: https://github.com/apache/hudi/pull/8437#discussion_r1170886349
##########
hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/table/HoodieTableSource.java:
##########
@@ -241,12 +247,12 @@ public String asSummaryString() {
@Override
public Result applyFilters(List<ResolvedExpression> filters) {
List<ResolvedExpression> simpleFilters =
filterSimpleCallExpression(filters);
+ this.predicates =
simpleFilters.stream().map(ParquetFilters::toParquetPredicate).filter(Objects::nonNull).collect(Collectors.toList());
Tuple2<List<ResolvedExpression>, List<ResolvedExpression>> splitFilters =
splitExprByPartitionCall(simpleFilters, this.partitionKeys, this.tableRowType);
Review Comment:
Use `splitFilters.f0` instead.
##########
hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/table/HoodieTableSource.java:
##########
@@ -241,12 +247,12 @@ public String asSummaryString() {
@Override
public Result applyFilters(List<ResolvedExpression> filters) {
List<ResolvedExpression> simpleFilters =
filterSimpleCallExpression(filters);
+ this.predicates =
simpleFilters.stream().map(ParquetFilters::toParquetPredicate).filter(Objects::nonNull).collect(Collectors.toList());
Tuple2<List<ResolvedExpression>, List<ResolvedExpression>> splitFilters =
splitExprByPartitionCall(simpleFilters, this.partitionKeys, this.tableRowType);
this.dataPruner = DataPruner.newInstance(splitFilters.f0);
this.partitionPruner = cratePartitionPruner(splitFilters.f1);
this.dataBucket = getDataBucket(splitFilters.f0);
- // refuse all the filters now
- return SupportsFilterPushDown.Result.of(new ArrayList<>(splitFilters.f1),
new ArrayList<>(filters));
+ return SupportsFilterPushDown.Result.of(new ArrayList<>(filters), new
ArrayList<>(filters));
Review Comment:
unnecessary change ?
##########
hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/table/format/RecordIterators.java:
##########
@@ -49,7 +61,17 @@ public static ClosableIterator<RowData>
getParquetRecordIterator(
int batchSize,
Path path,
long splitStart,
- long splitLength) throws IOException {
+ long splitLength,
+ List<Predicate> predicates) throws IOException {
+ FilterPredicate filterPredicate = getFilterPredicate(conf);
+ for (Predicate predicate : predicates) {
+ FilterPredicate filter = predicate.eval();
+ if (filter != null) {
+ filterPredicate = filterPredicate == null ? filter :
and(filterPredicate, filter);
Review Comment:
Do we need to and all the predicates?
##########
hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/table/format/ParquetFilters.java:
##########
@@ -0,0 +1,646 @@
+/*
+ * 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.table.format;
+
+import org.apache.flink.table.expressions.CallExpression;
+import org.apache.flink.table.expressions.Expression;
+import org.apache.flink.table.expressions.FieldReferenceExpression;
+import org.apache.flink.table.expressions.ValueLiteralExpression;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.FunctionDefinition;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.util.function.TriFunction;
+import org.apache.parquet.filter2.predicate.FilterPredicate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Serializable;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Function;
+
+import static org.apache.hudi.util.ExpressionUtils.getValueFromLiteral;
+import static org.apache.parquet.filter2.predicate.FilterApi.and;
+import static org.apache.parquet.filter2.predicate.FilterApi.binaryColumn;
+import static org.apache.parquet.filter2.predicate.FilterApi.booleanColumn;
+import static org.apache.parquet.filter2.predicate.FilterApi.doubleColumn;
+import static org.apache.parquet.filter2.predicate.FilterApi.eq;
+import static org.apache.parquet.filter2.predicate.FilterApi.floatColumn;
+import static org.apache.parquet.filter2.predicate.FilterApi.gt;
+import static org.apache.parquet.filter2.predicate.FilterApi.gtEq;
+import static org.apache.parquet.filter2.predicate.FilterApi.intColumn;
+import static org.apache.parquet.filter2.predicate.FilterApi.longColumn;
+import static org.apache.parquet.filter2.predicate.FilterApi.lt;
+import static org.apache.parquet.filter2.predicate.FilterApi.ltEq;
+import static org.apache.parquet.filter2.predicate.FilterApi.not;
+import static org.apache.parquet.filter2.predicate.FilterApi.notEq;
+import static org.apache.parquet.filter2.predicate.FilterApi.or;
+import static org.apache.parquet.io.api.Binary.fromConstantByteArray;
+import static org.apache.parquet.io.api.Binary.fromString;
+
+/**
+ * Utility class that provides helper methods to work with Parquet Filter
PushDown.
+ */
+public class ParquetFilters {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(ParquetFilters.class);
+
+ private static final Map<FunctionDefinition, Function<CallExpression,
Predicate>>
+ FILTERS =
+ new HashMap<FunctionDefinition, Function<CallExpression, Predicate>>() {{
+ put(
+ BuiltInFunctionDefinitions.EQUALS,
+ call -> convertBinary(
+ call,
+ Equals::new,
Review Comment:
Just normalize the call expression like
`ExpressionEvaluators#fromExpression`, there is no need to define the redundant
reverse function def.
##########
hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/table/format/ParquetFilters.java:
##########
@@ -0,0 +1,646 @@
+/*
+ * 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.table.format;
+
+import org.apache.flink.table.expressions.CallExpression;
+import org.apache.flink.table.expressions.Expression;
+import org.apache.flink.table.expressions.FieldReferenceExpression;
+import org.apache.flink.table.expressions.ValueLiteralExpression;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.FunctionDefinition;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.util.function.TriFunction;
+import org.apache.parquet.filter2.predicate.FilterPredicate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Serializable;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Function;
+
+import static org.apache.hudi.util.ExpressionUtils.getValueFromLiteral;
+import static org.apache.parquet.filter2.predicate.FilterApi.and;
+import static org.apache.parquet.filter2.predicate.FilterApi.binaryColumn;
+import static org.apache.parquet.filter2.predicate.FilterApi.booleanColumn;
+import static org.apache.parquet.filter2.predicate.FilterApi.doubleColumn;
+import static org.apache.parquet.filter2.predicate.FilterApi.eq;
+import static org.apache.parquet.filter2.predicate.FilterApi.floatColumn;
+import static org.apache.parquet.filter2.predicate.FilterApi.gt;
+import static org.apache.parquet.filter2.predicate.FilterApi.gtEq;
+import static org.apache.parquet.filter2.predicate.FilterApi.intColumn;
+import static org.apache.parquet.filter2.predicate.FilterApi.longColumn;
+import static org.apache.parquet.filter2.predicate.FilterApi.lt;
+import static org.apache.parquet.filter2.predicate.FilterApi.ltEq;
+import static org.apache.parquet.filter2.predicate.FilterApi.not;
+import static org.apache.parquet.filter2.predicate.FilterApi.notEq;
+import static org.apache.parquet.filter2.predicate.FilterApi.or;
+import static org.apache.parquet.io.api.Binary.fromConstantByteArray;
+import static org.apache.parquet.io.api.Binary.fromString;
+
+/**
+ * Utility class that provides helper methods to work with Parquet Filter
PushDown.
+ */
+public class ParquetFilters {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(ParquetFilters.class);
+
+ private static final Map<FunctionDefinition, Function<CallExpression,
Predicate>>
+ FILTERS =
+ new HashMap<FunctionDefinition, Function<CallExpression, Predicate>>() {{
+ put(
+ BuiltInFunctionDefinitions.EQUALS,
+ call -> convertBinary(
+ call,
+ Equals::new,
+ Equals::new));
+ put(
+ BuiltInFunctionDefinitions.NOT_EQUALS,
+ call ->
+ convertBinary(
+ call,
+ NotEquals::new,
+ NotEquals::new));
+ put(
+ BuiltInFunctionDefinitions.GREATER_THAN,
+ call ->
+ convertBinary(
+ call,
+ GreaterThan::new,
+ LessThanOrEqual::new));
+ put(
+ BuiltInFunctionDefinitions.GREATER_THAN_OR_EQUAL,
+ call ->
+ convertBinary(
+ call,
+ GreaterThanOrEqual::new,
+ LessThan::new));
+ put(
+ BuiltInFunctionDefinitions.LESS_THAN,
+ call ->
+ convertBinary(
+ call,
+ LessThan::new,
+ GreaterThanOrEqual::new));
+ put(
+ BuiltInFunctionDefinitions.LESS_THAN_OR_EQUAL,
+ call ->
+ convertBinary(
+ call,
+ LessThanOrEqual::new,
+ GreaterThan::new));
+ put(BuiltInFunctionDefinitions.NOT, ParquetFilters::convertNot);
+ put(BuiltInFunctionDefinitions.OR, ParquetFilters::convertOr);
+ put(BuiltInFunctionDefinitions.AND, ParquetFilters::convertAnd);
+ }};
+
+ public static Predicate convertBinary(
+ CallExpression callExpression,
+ TriFunction<String, LogicalType, Serializable, Predicate> func,
+ TriFunction<String, LogicalType, Serializable, Predicate> reverseFunc) {
+ if (!isBinaryValid(callExpression)) {
+ // not a valid predicate
+ LOG.debug(
+ "Unsupported predicate [{}] cannot be pushed into
ParquetColumnarRowSplitReader.",
+ callExpression);
+ return null;
+ }
+ boolean literalOnRight = literalOnRight(callExpression);
+ String columnName =
+ ((FieldReferenceExpression)
callExpression.getChildren().get(literalOnRight ? 0 : 1)).getName();
+ ValueLiteralExpression valueLiteral =
+ (ValueLiteralExpression)
callExpression.getChildren().get(literalOnRight ? 1 : 0);
+ DataType dataType = valueLiteral.getOutputDataType();
+ LogicalType literalType = dataType.getLogicalType();
+ // fetch literal and ensure it is serializable
+ Object literalObject = getValueFromLiteral(valueLiteral);
+ Serializable literal;
+ // validate that literal is serializable
+ if (literalObject instanceof Serializable) {
+ literal = (Serializable) literalObject;
+ } else {
+ LOG.warn(
+ "Encountered a non-serializable literal of type {}. "
+ + "Cannot push predicate [{}] into FileInputFormat. "
+ + "This is a bug and should be reported.",
+ literalObject.getClass().getCanonicalName(),
+ callExpression);
+ return null;
+ }
+ return literalOnRight
+ ? func.apply(columnName, literalType, literal)
+ : reverseFunc.apply(columnName, literalType, literal);
+ }
+
+ private static Predicate convertNot(CallExpression callExpression) {
+ if (callExpression.getChildren().size() != 1) {
+ // not a valid predicate
+ LOG.debug(
+ "Unsupported predicate [{}] cannot be pushed into
ParquetColumnarRowSplitReader.",
+ callExpression);
+ return null;
+ }
+
+ Predicate predicate =
toParquetPredicate(callExpression.getChildren().get(0));
+ return predicate == null ? null : new Not(predicate);
+ }
+
+ private static Predicate convertOr(CallExpression callExpression) {
+ if (callExpression.getChildren().size() < 2) {
+ return null;
+ }
+ Expression leftExpression = callExpression.getChildren().get(0);
+ Expression rightExpression = callExpression.getChildren().get(1);
+
+ Predicate leftPredicate = toParquetPredicate(leftExpression);
+ Predicate rightPredicate = toParquetPredicate(rightExpression);
+ if (leftPredicate == null || rightPredicate == null) {
+ return null;
+ } else {
+ return new Or(leftPredicate, rightPredicate);
+ }
+ }
+
+ private static Predicate convertAnd(CallExpression callExpression) {
+ if (callExpression.getChildren().size() < 2) {
+ return null;
+ }
+ Expression leftExpression = callExpression.getChildren().get(0);
+ Expression rightExpression = callExpression.getChildren().get(1);
+
+ Predicate leftPredicate = toParquetPredicate(leftExpression);
+ Predicate rightPredicate = toParquetPredicate(rightExpression);
+ if (leftPredicate == null || rightPredicate == null) {
+ return null;
+ } else {
+ return new And(leftPredicate, rightPredicate);
+ }
+ }
+
+ private static boolean isRef(Expression expression) {
+ return expression instanceof FieldReferenceExpression;
+ }
+
+ private static boolean isLit(Expression expression) {
+ return expression instanceof ValueLiteralExpression;
+ }
+
+ private static boolean isBinaryValid(CallExpression callExpression) {
+ return callExpression.getChildren().size() == 2
+ && (isRef(callExpression.getChildren().get(0))
+ && isLit(callExpression.getChildren().get(1))
+ || isLit(callExpression.getChildren().get(0))
+ && isRef(callExpression.getChildren().get(1)));
+ }
+
+ private static boolean literalOnRight(CallExpression callExpression) {
+ if (callExpression.getChildren().size() == 1
+ && callExpression.getChildren().get(0) instanceof
FieldReferenceExpression) {
+ return true;
+ } else if (isLit(callExpression.getChildren().get(0)) &&
isRef(callExpression.getChildren().get(1))) {
+ return false;
+ } else if (isRef(callExpression.getChildren().get(0)) &&
isLit(callExpression.getChildren().get(1))) {
+ return true;
+ } else {
+ throw new RuntimeException("Invalid binary comparison.");
+ }
+ }
+
+ public static Predicate toParquetPredicate(Expression expression) {
+ if (expression instanceof CallExpression) {
+ CallExpression callExp = (CallExpression) expression;
+ if (FILTERS.get(callExp.getFunctionDefinition()) == null) {
+ // unsupported predicate
+ LOG.debug(
+ "Unsupported predicate [{}] cannot be pushed into
ParquetColumnarRowSplitReader.",
+ expression);
+ return null;
+ }
+ return FILTERS.get(callExp.getFunctionDefinition()).apply(callExp);
+ } else {
+ // unsupported predicate
+ LOG.debug(
+ "Unsupported predicate [{}] cannot be pushed into
ParquetColumnarRowSplitReader.",
+ expression);
+ return null;
+ }
+ }
+
+ //
--------------------------------------------------------------------------------------------
+ // Classes to define predicates
+ //
--------------------------------------------------------------------------------------------
+
+ /**
+ * A filter predicate that can be evaluated by the FileInputFormat.
+ */
+ public abstract static class Predicate implements Serializable {
+ public abstract FilterPredicate eval();
+ }
+
+ abstract static class ColumnPredicate extends Predicate {
+ final String columnName;
+ final LogicalType literalType;
+
+ ColumnPredicate(String columnName, LogicalType literalType) {
+ this.columnName = columnName;
+ this.literalType = literalType;
+ }
+ }
+
+ abstract static class BinaryPredicate extends ColumnPredicate {
+ final Serializable literal;
+
+ BinaryPredicate(String columnName, LogicalType literalType, Serializable
literal) {
+ super(columnName, literalType);
+ this.literal = literal;
+ }
+ }
+
+ /**
+ * An EQUALS predicate that can be evaluated by the FileInputFormat.
+ */
+ public static class Equals extends BinaryPredicate {
+ /**
+ * Creates an EQUALS predicate.
+ *
+ * @param columnName The column to check.
+ * @param literalType The type of the literal.
+ * @param literal The literal value to check the column against.
+ */
+ public Equals(String columnName, LogicalType literalType, Serializable
literal) {
+ super(columnName, literalType, literal);
+ }
+
+ @Override
+ public FilterPredicate eval() {
+ switch (literalType.getTypeRoot()) {
+ case BOOLEAN:
+ return eq(booleanColumn(columnName), (Boolean) literal);
+ case TINYINT:
+ case SMALLINT:
Review Comment:
Can we just abstract out the mathing of all these data types, they are
duplicated for each kind of function def.
--
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]