JingsongLi commented on code in PR #3068: URL: https://github.com/apache/incubator-paimon/pull/3068#discussion_r1533450695
########## paimon-common/src/main/java/org/apache/paimon/fileindex/FileIndexFunctionVisitor.java: ########## @@ -0,0 +1,108 @@ +/* + * 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.paimon.fileindex; + +import org.apache.paimon.predicate.FieldRef; +import org.apache.paimon.predicate.FunctionVisitor; + +import java.util.List; + +/** + * Read file index from serialized bytes. Return true, means we need to search this file, else means + * needn't. + */ +public interface FileIndexFunctionVisitor extends FunctionVisitor<Boolean> { Review Comment: FileIndexReader ########## paimon-common/src/main/java/org/apache/paimon/fileindex/FileIndexPredicateTester.java: ########## @@ -0,0 +1,78 @@ +/* + * 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.paimon.fileindex; + +import org.apache.paimon.predicate.CompoundPredicate; +import org.apache.paimon.predicate.FieldRef; +import org.apache.paimon.predicate.LeafPredicate; +import org.apache.paimon.predicate.Or; +import org.apache.paimon.predicate.Predicate; +import org.apache.paimon.predicate.PredicateVisitor; + +/** Predicate test. */ +public class FileIndexPredicateTester implements PredicateVisitor<Boolean> { Review Comment: Wrap this class into `FileIndexReader`. ########## paimon-common/src/main/java/org/apache/paimon/fileindex/FileIndexPredicateUtil.java: ########## @@ -0,0 +1,172 @@ +/* + * 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.paimon.fileindex; + +import org.apache.paimon.fs.FileIO; +import org.apache.paimon.fs.Path; +import org.apache.paimon.predicate.CompoundPredicate; +import org.apache.paimon.predicate.LeafPredicate; +import org.apache.paimon.predicate.Predicate; +import org.apache.paimon.predicate.PredicateVisitor; +import org.apache.paimon.types.DataType; +import org.apache.paimon.types.RowType; +import org.apache.paimon.utils.Pair; + +import javax.annotation.Nullable; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInput; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** Utils to check secondary index (e.g. bloom filter) predicate. */ +public class FileIndexPredicateUtil { Review Comment: `FileIndexPredicate` ########## paimon-common/src/main/java/org/apache/paimon/fileindex/FileIndexPredicateUtil.java: ########## @@ -0,0 +1,172 @@ +/* + * 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.paimon.fileindex; + +import org.apache.paimon.fs.FileIO; +import org.apache.paimon.fs.Path; +import org.apache.paimon.predicate.CompoundPredicate; +import org.apache.paimon.predicate.LeafPredicate; +import org.apache.paimon.predicate.Predicate; +import org.apache.paimon.predicate.PredicateVisitor; +import org.apache.paimon.types.DataType; +import org.apache.paimon.types.RowType; +import org.apache.paimon.utils.Pair; + +import javax.annotation.Nullable; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInput; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** Utils to check secondary index (e.g. bloom filter) predicate. */ +public class FileIndexPredicateUtil { + + public static boolean checkPredicate( + Path path, FileIO fileIO, RowType fileRowType, @Nullable Predicate filePredicate) + throws IOException { + try (DataInputStream dataInput = new DataInputStream(fileIO.newInputStream(path))) { + return checkPredicate(dataInput, fileRowType, filePredicate); + } + } + + public static boolean checkPredicate( + byte[] serializedBytes, RowType fileRowType, @Nullable Predicate filePredicate) { + DataInput dataInput = new DataInputStream(new ByteArrayInputStream(serializedBytes)); + return checkPredicate(dataInput, fileRowType, filePredicate); + } + + public static boolean checkPredicate( + DataInput dataInput, RowType fileRowType, @Nullable Predicate filePredicate) { + if (filePredicate == null) { + return true; + } + + Set<String> requiredFields = + filePredicate.visit( + new PredicateVisitor<Set<String>>() { + final Set<String> names = new HashSet<>(); + + @Override + public Set<String> visit(LeafPredicate predicate) { + names.add(predicate.fieldName()); + return names; + } + + @Override + public Set<String> visit(CompoundPredicate predicate) { + for (Predicate child : predicate.children()) { + child.visit(this); + } + return names; + } + }); + + Pair<String, Map<String, byte[]>> pair = deserializeIndexString(dataInput, requiredFields); + Map<String, DataType> fileTypes = new HashMap<>(); + fileRowType.getFields().forEach(field -> fileTypes.put(field.name(), field.type())); + + String type = pair.getLeft(); + Map<String, byte[]> checker = pair.getRight(); + + List<FileIndexPredicateTester> testers = + checker.entrySet().stream() + .map( + entry -> + new FileIndexPredicateTester( + entry.getKey(), + FileIndexer.create( + type, fileTypes.get(entry.getKey())) + .createVisitor() + .recoverFrom(entry.getValue()))) + .collect(Collectors.toList()); + + for (FileIndexPredicateTester tester : testers) { + if (!tester.test(filePredicate)) { + return false; + } + } + return true; + } + + public static byte[] serializeIndexMap(String type, Map<String, byte[]> indexMap) { + try { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); Review Comment: You need to define a file structure to separate offsets and index bytes. ########## paimon-common/src/main/java/org/apache/paimon/fileindex/ObjectToBytesVisitor.java: ########## @@ -0,0 +1,290 @@ +/* + * 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.paimon.fileindex; + +import org.apache.paimon.annotation.VisibleForTesting; +import org.apache.paimon.data.BinaryString; +import org.apache.paimon.data.DataGetters; +import org.apache.paimon.data.Decimal; +import org.apache.paimon.data.InternalArray; +import org.apache.paimon.data.InternalMap; +import org.apache.paimon.data.InternalRow; +import org.apache.paimon.data.Timestamp; +import org.apache.paimon.types.ArrayType; +import org.apache.paimon.types.BigIntType; +import org.apache.paimon.types.BinaryType; +import org.apache.paimon.types.BooleanType; +import org.apache.paimon.types.CharType; +import org.apache.paimon.types.DataTypeVisitor; +import org.apache.paimon.types.DateType; +import org.apache.paimon.types.DecimalType; +import org.apache.paimon.types.DoubleType; +import org.apache.paimon.types.FloatType; +import org.apache.paimon.types.IntType; +import org.apache.paimon.types.LocalZonedTimestampType; +import org.apache.paimon.types.MapType; +import org.apache.paimon.types.MultisetType; +import org.apache.paimon.types.RowType; +import org.apache.paimon.types.SmallIntType; +import org.apache.paimon.types.TimeType; +import org.apache.paimon.types.TimestampType; +import org.apache.paimon.types.TinyIntType; +import org.apache.paimon.types.VarBinaryType; +import org.apache.paimon.types.VarCharType; + +import java.util.Arrays; +import java.util.function.BiFunction; +import java.util.function.Function; + +/** Convert different type object to bytes. */ +public class ObjectToBytesVisitor implements DataTypeVisitor<Function<Object, byte[]>> { Review Comment: Maybe we don't need this class now. -- 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...@paimon.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org