JingsongLi commented on code in PR #6028: URL: https://github.com/apache/paimon/pull/6028#discussion_r2275236164
########## paimon-common/src/main/java/org/apache/paimon/fileindex/FileIndexPredicate.java: ########## @@ -83,6 +88,22 @@ public FileIndexResult evaluate(@Nullable Predicate predicate) { return result; } + public FileIndexResult evaluate(@Nullable TopN topN, FileIndexResult result) { Review Comment: evaluateTopN ########## paimon-common/src/main/java/org/apache/paimon/fileindex/FileIndexPredicate.java: ########## @@ -83,6 +88,22 @@ public FileIndexResult evaluate(@Nullable Predicate predicate) { return result; } + public FileIndexResult evaluate(@Nullable TopN topN, FileIndexResult result) { + if (topN == null || !result.remain()) { + return result; + } + + // for now we only support single column. + List<SortValue> orders = topN.orders(); + if (orders.size() != 1) { + return result; + } + + String requiredName = orders.get(0).field().name(); + Set<FileIndexReader> readers = reader.readColumnIndex(requiredName); + return new FileIndexTopNTest(readers, result).test(topN); Review Comment: Don't need to introduce `FileIndexTopNTest`. Just introduce a static method `evaluateTopN(Set<FileIndexReader> readers, , FileIndexResult result)`. ########## paimon-common/src/main/java/org/apache/paimon/predicate/TopNVisitor.java: ########## @@ -0,0 +1,25 @@ +/* + * 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.predicate; + +/** A visitor to visit {@link TopN}. */ +public interface TopNVisitor<T> { Review Comment: Don't need to introduce this interface. ########## paimon-core/src/main/java/org/apache/paimon/io/FileIndexEvaluator.java: ########## @@ -36,40 +37,62 @@ public static FileIndexResult evaluate( FileIO fileIO, TableSchema dataSchema, List<Predicate> dataFilter, + TopN topN, DataFilePathFactory dataFilePathFactory, DataFileMeta file) throws IOException { - if (dataFilter != null && !dataFilter.isEmpty()) { + FileIndexResult result = FileIndexResult.REMAIN; + if ((dataFilter == null || dataFilter.isEmpty()) && topN == null) { + return result; + } + + FileIndexPredicate predicate = null; + try { byte[] embeddedIndex = file.embeddedIndex(); if (embeddedIndex != null) { - try (FileIndexPredicate predicate = - new FileIndexPredicate(embeddedIndex, dataSchema.logicalRowType())) { - return predicate.evaluate( - PredicateBuilder.and(dataFilter.toArray(new Predicate[0]))); + predicate = new FileIndexPredicate(embeddedIndex, dataSchema.logicalRowType()); + } else { + List<String> indexFiles = + file.extraFiles().stream() + .filter( + name -> + name.endsWith( + DataFilePathFactory.INDEX_PATH_SUFFIX)) + .collect(Collectors.toList()); + if (indexFiles.isEmpty()) { + return result; } - } - - List<String> indexFiles = - file.extraFiles().stream() - .filter(name -> name.endsWith(DataFilePathFactory.INDEX_PATH_SUFFIX)) - .collect(Collectors.toList()); - if (!indexFiles.isEmpty()) { if (indexFiles.size() > 1) { throw new RuntimeException( "Found more than one index file for one data file: " + String.join(" and ", indexFiles)); } - // go to file index check - try (FileIndexPredicate predicate = + predicate = new FileIndexPredicate( dataFilePathFactory.toAlignedPath(indexFiles.get(0), file), fileIO, - dataSchema.logicalRowType())) { - return predicate.evaluate( - PredicateBuilder.and(dataFilter.toArray(new Predicate[0]))); - } + dataSchema.logicalRowType()); + } + + // data filter + if (dataFilter != null && !dataFilter.isEmpty()) { + result = + predicate.evaluate( + PredicateBuilder.and(dataFilter.toArray(new Predicate[0]))); + } + + // todo: We need to do more if the index filter supports pushdown. + // e.g. All pushdown index filters must exist and be evaluated. + // topN filter + if (dataFilter == null && topN != null) { Review Comment: You may need to use if else? ########## paimon-core/src/main/java/org/apache/paimon/io/FileIndexEvaluator.java: ########## @@ -36,40 +37,62 @@ public static FileIndexResult evaluate( FileIO fileIO, TableSchema dataSchema, List<Predicate> dataFilter, + TopN topN, DataFilePathFactory dataFilePathFactory, DataFileMeta file) throws IOException { - if (dataFilter != null && !dataFilter.isEmpty()) { + FileIndexResult result = FileIndexResult.REMAIN; + if ((dataFilter == null || dataFilter.isEmpty()) && topN == null) { Review Comment: Introduce a isNullOrEmpty in ListUtils, and use it. ########## paimon-common/src/main/java/org/apache/paimon/utils/RoaringBitmap32.java: ########## @@ -105,6 +106,22 @@ public boolean intersects(long minimum, long supremum) { return roaringBitmap.intersects(minimum, supremum); } + public RoaringBitmap32 limit(int k) { + return new RoaringBitmap32(roaringBitmap.limit(k)); + } + + public void remove(RoaringBitmap32 from, long n) { Review Comment: This method name is very confusing. Just introduce a private method in `BitSliceIndexBitmap`. ########## paimon-core/src/main/java/org/apache/paimon/io/FileIndexEvaluator.java: ########## @@ -36,40 +37,62 @@ public static FileIndexResult evaluate( FileIO fileIO, TableSchema dataSchema, List<Predicate> dataFilter, + TopN topN, DataFilePathFactory dataFilePathFactory, DataFileMeta file) throws IOException { - if (dataFilter != null && !dataFilter.isEmpty()) { + FileIndexResult result = FileIndexResult.REMAIN; + if ((dataFilter == null || dataFilter.isEmpty()) && topN == null) { + return result; + } + + FileIndexPredicate predicate = null; + try { byte[] embeddedIndex = file.embeddedIndex(); if (embeddedIndex != null) { - try (FileIndexPredicate predicate = - new FileIndexPredicate(embeddedIndex, dataSchema.logicalRowType())) { - return predicate.evaluate( - PredicateBuilder.and(dataFilter.toArray(new Predicate[0]))); + predicate = new FileIndexPredicate(embeddedIndex, dataSchema.logicalRowType()); + } else { + List<String> indexFiles = + file.extraFiles().stream() + .filter( + name -> + name.endsWith( + DataFilePathFactory.INDEX_PATH_SUFFIX)) + .collect(Collectors.toList()); + if (indexFiles.isEmpty()) { + return result; } - } - - List<String> indexFiles = - file.extraFiles().stream() - .filter(name -> name.endsWith(DataFilePathFactory.INDEX_PATH_SUFFIX)) - .collect(Collectors.toList()); - if (!indexFiles.isEmpty()) { if (indexFiles.size() > 1) { throw new RuntimeException( "Found more than one index file for one data file: " + String.join(" and ", indexFiles)); } - // go to file index check - try (FileIndexPredicate predicate = + predicate = new FileIndexPredicate( dataFilePathFactory.toAlignedPath(indexFiles.get(0), file), fileIO, - dataSchema.logicalRowType())) { - return predicate.evaluate( - PredicateBuilder.and(dataFilter.toArray(new Predicate[0]))); - } + dataSchema.logicalRowType()); + } + + // data filter + if (dataFilter != null && !dataFilter.isEmpty()) { Review Comment: isNullOrEmpty -- 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