danny0405 commented on code in PR #8051: URL: https://github.com/apache/hudi/pull/8051#discussion_r1129057645
########## hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/source/DataPruner.java: ########## @@ -0,0 +1,138 @@ +/* + * 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; + +import org.apache.hudi.source.evaluator.Evaluator; +import org.apache.hudi.source.stats.ColumnStats; +import org.apache.hudi.util.ExpressionUtils; + +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.expressions.ResolvedExpression; +import org.apache.flink.table.types.logical.DecimalType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.RowType; +import org.apache.flink.table.types.logical.TimestampType; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import static org.apache.hudi.util.EvaluatorUtils.fromExpression; + +/** + * Utility to do data skipping. + */ +public class DataPruner { + private final String[] referencedCols; + private final List<Evaluator> evaluators; + + private DataPruner(String[] referencedCols, List<Evaluator> evaluators) { + this.referencedCols = referencedCols; + this.evaluators = evaluators; + } + + /** + * Filters the index row with specific data filters and query fields. + * + * @param indexRow The index row + * @param queryFields The query fields referenced by the filters + * @return true if the index row should be considered as a candidate + */ + public boolean test(RowData indexRow, RowType.RowField[] queryFields) { + Map<String, ColumnStats> columnStatsMap = convertColumnStats(indexRow, queryFields); + for (Evaluator evaluator : evaluators) { + if (!evaluator.eval(columnStatsMap)) { + return false; + } + } + return true; + } + + public String[] getReferencedCols() { + return referencedCols; + } + + public static DataPruner newInstance(List<ResolvedExpression> filters) { + if (filters == null || filters.size() == 0) { + return null; + } + String[] referencedCols = ExpressionUtils.referencedColumns(filters); + if (referencedCols.length == 0) { + return null; + } + List<Evaluator> evaluators = fromExpression(filters); + return new DataPruner(referencedCols, evaluators); + } + + public static Map<String, ColumnStats> convertColumnStats(RowData indexRow, RowType.RowField[] queryFields) { + Map<String, ColumnStats> mapping = new LinkedHashMap<>(); + if (indexRow == null || queryFields == null) { + return mapping; Review Comment: `AssertError` is not a good coding practice, let's not use that. -- 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]
