pltbkd commented on code in PR #20374: URL: https://github.com/apache/flink/pull/20374#discussion_r939518327
########## flink-table/flink-table-common/src/main/java/org/apache/flink/table/connector/source/DynamicFilteringData.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.flink.table.connector.source; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.api.common.ExecutionConfig; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.common.typeutils.base.array.BytePrimitiveArrayComparator; +import org.apache.flink.core.memory.DataInputViewStreamWrapper; +import org.apache.flink.table.api.TableException; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.types.logical.LogicalTypeRoot; +import org.apache.flink.table.types.logical.RowType; + +import java.io.ByteArrayInputStream; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** Data for dynamic filtering. */ +public class DynamicFilteringData implements Serializable { + + public static final Set<LogicalTypeRoot> SUPPORTED_TYPES; + + static { + Set<LogicalTypeRoot> supported = new HashSet<>(); + supported.add(LogicalTypeRoot.INTEGER); + supported.add(LogicalTypeRoot.BIGINT); + supported.add(LogicalTypeRoot.SMALLINT); + supported.add(LogicalTypeRoot.TINYINT); + supported.add(LogicalTypeRoot.VARCHAR); + supported.add(LogicalTypeRoot.CHAR); + supported.add(LogicalTypeRoot.DATE); + supported.add(LogicalTypeRoot.TIME_WITHOUT_TIME_ZONE); + supported.add(LogicalTypeRoot.TIMESTAMP_WITH_LOCAL_TIME_ZONE); + supported.add(LogicalTypeRoot.TIMESTAMP_WITHOUT_TIME_ZONE); + SUPPORTED_TYPES = Collections.unmodifiableSet(supported); + } + + private final TypeInformation<RowData> typeInfo; + private final RowType rowType; + + /** The list should be sorted and distinct. */ + private final List<byte[]> serializedData; + + /** Whether the data actually does filter. If false, everything is considered contained. */ + private final boolean isFiltering; + + private transient volatile boolean prepared = false; + private transient Map<Integer, List<RowData>> dataMap; + private transient RowData.FieldGetter[] fieldGetters; + + public DynamicFilteringData( + TypeInformation<RowData> typeInfo, + RowType rowType, + List<byte[]> serializedData, + boolean isFiltering) { + this.typeInfo = checkNotNull(typeInfo); + this.rowType = checkNotNull(rowType); + this.serializedData = checkNotNull(serializedData); + this.isFiltering = isFiltering; + } + + public boolean isFiltering() { + return isFiltering; + } + + public RowType getRowType() { + return rowType; + } + + public boolean contains(RowData row) { + if (!isFiltering) { + return true; + } else if (row.getArity() != rowType.getFieldCount()) { + throw new TableException("The arity of RowData is different"); + } else { + prepare(); + List<RowData> mayMatchRowData = dataMap.get(hash(row)); + if (mayMatchRowData == null) { + return false; + } + for (RowData mayMatch : mayMatchRowData) { + if (matchRow(row, mayMatch)) { + return true; + } + } + return false; + } + } + + private boolean matchRow(RowData row, RowData mayMatch) { + for (int i = 0; i < rowType.getFieldCount(); ++i) { + if (!Objects.equals( + fieldGetters[i].getFieldOrNull(row), + fieldGetters[i].getFieldOrNull(mayMatch))) { Review Comment: In addition, here users mean the users of this data class, rather than flink users. So I suppose the behavior of the users should be under control. -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org