This is an automated email from the ASF dual-hosted git repository.

JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new b8cae592c6 [core] Support OR filters on centroid shard bitmaps (#11329)
b8cae592c6 is described below

commit b8cae592c6ab9663f25f0720b78a35e93efd8129
Author: QuakeWang <[email protected]>
AuthorDate: Sun Nov 22 10:38:26 2026 +0800

    [core] Support OR filters on centroid shard bitmaps (#11329)
---
 docs/docs/multimodal-table/global-index/vector.mdx |   6 +-
 .../globalindex/bitmap/CentroidBitmapIndex.java    | 116 ++++++++++++++-------
 .../bitmap/LazyFilteredBitmapIndexReaderTest.java  |  17 +--
 .../paimon/table/source/CentroidBitmapFilter.java  |  89 +++++++++++-----
 .../paimon/spark/sql/CentroidVectorIndexTest.scala |  47 +++++++++
 5 files changed, 201 insertions(+), 74 deletions(-)

diff --git a/docs/docs/multimodal-table/global-index/vector.mdx 
b/docs/docs/multimodal-table/global-index/vector.mdx
index 62920e0694..9c676bc0b7 100644
--- a/docs/docs/multimodal-table/global-index/vector.mdx
+++ b/docs/docs/multimodal-table/global-index/vector.mdx
@@ -676,8 +676,10 @@ order. Each entry stores the index type, section length, 
and original Bitmap met
 stores the Bitmap sections consecutively, so the reader derives offsets from 
the section lengths.
 
 A vector search can use these Bitmaps for equality, `IN`, `IS NULL`, `IS NOT 
NULL`, and
-conjunctions of these predicates. It applies the exact matches before ranking 
and skips opening
-the ANN file when no visible vector matches. Other predicates use the existing 
scalar-filter path.
+combinations of these predicates with `AND` or `OR`, including across columns. 
It applies the
+exact matches before ranking and skips opening the ANN file when no visible 
vector matches.
+If any branch cannot be evaluated exactly by the shard Bitmaps, the filter 
uses the existing
+scalar-filter path.
 
 To change the scalar columns, run the centroid build again with the new 
`extra_fields`. For the selected
 partitions, the build replaces the indexes of the same type on the vector 
column. Pass just the
diff --git 
a/paimon-common/src/main/java/org/apache/paimon/globalindex/bitmap/CentroidBitmapIndex.java
 
b/paimon-common/src/main/java/org/apache/paimon/globalindex/bitmap/CentroidBitmapIndex.java
index 20b357a9e7..2547088786 100644
--- 
a/paimon-common/src/main/java/org/apache/paimon/globalindex/bitmap/CentroidBitmapIndex.java
+++ 
b/paimon-common/src/main/java/org/apache/paimon/globalindex/bitmap/CentroidBitmapIndex.java
@@ -28,12 +28,18 @@ import org.apache.paimon.globalindex.ResultEntry;
 import org.apache.paimon.globalindex.io.GlobalIndexFileReader;
 import org.apache.paimon.globalindex.io.GlobalIndexFileWriter;
 import org.apache.paimon.options.Options;
+import org.apache.paimon.predicate.And;
+import org.apache.paimon.predicate.CompoundPredicate;
 import org.apache.paimon.predicate.Equal;
+import org.apache.paimon.predicate.In;
 import org.apache.paimon.predicate.IsNotNull;
 import org.apache.paimon.predicate.IsNull;
 import org.apache.paimon.predicate.LeafPredicate;
+import org.apache.paimon.predicate.Or;
+import org.apache.paimon.predicate.Predicate;
 import org.apache.paimon.types.DataField;
 import org.apache.paimon.types.RowType;
+import org.apache.paimon.utils.IOFunction;
 import org.apache.paimon.utils.RoaringNavigableMap64;
 
 import java.io.ByteArrayOutputStream;
@@ -41,7 +47,7 @@ import java.io.DataOutputStream;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.HashMap;
-import java.util.List;
+import java.util.Iterator;
 import java.util.Map;
 
 import static org.apache.paimon.utils.Preconditions.checkArgument;
@@ -72,51 +78,85 @@ public final class CentroidBitmapIndex {
             int[] extraFieldIds,
             RowType rowType,
             RowType buildType,
-            List<LeafPredicate> predicates)
+            Predicate predicate)
             throws IOException {
-        checkArgument(!predicates.isEmpty(), "Centroid bitmap lookup requires 
a predicate.");
         ByteBuffer metadata = metadataPayload(bitmap.metadata());
         metadata.getLong(); // build schema ID
         Map<Integer, Section> sections = readDirectory(metadata, 
extraFieldIds, bitmap.fileSize());
         try (SeekableInputStream input = files.getInputStream(bitmap)) {
-            RoaringNavigableMap64 result = null;
-            for (LeafPredicate predicate : predicates) {
-                int fieldId = 
rowType.getField(predicate.fieldRefOptional().get().name()).id();
-                Section section = sections.get(fieldId);
-                checkState(section != null, "Missing field %s in %s.", 
fieldId, bitmap.filePath());
-                checkArgument(
-                        section.indexType == BITMAP,
-                        "Unsupported centroid scalar index type %s for field 
%s.",
-                        section.indexType,
-                        fieldId);
-                KeySerializer serializer = 
KeySerializer.create(buildType.getField(fieldId).type());
-                GlobalIndexIOMeta sectionMeta =
-                        new GlobalIndexIOMeta(
-                                bitmap.filePath(),
-                                section.length,
-                                bitmap.rowCount(),
-                                section.metadata);
-                try (BitmapIndexReader reader =
-                        new BitmapIndexReader(
-                                serializer, ignored -> section.open(input), 
sectionMeta)) {
-                    RoaringNavigableMap64 rows;
-                    if (Equal.INSTANCE.equals(predicate.function())) {
-                        rows = 
reader.visitEqual(predicate.literals().get(0)).get().results();
-                    } else if (IsNull.INSTANCE.equals(predicate.function())) {
-                        rows = reader.visitIsNull().get().results();
-                    } else if 
(IsNotNull.INSTANCE.equals(predicate.function())) {
-                        rows = reader.visitIsNotNull().get().results();
-                    } else {
-                        rows = 
reader.visitIn(predicate.literals()).get().results();
-                    }
-                    result = result == null ? rows : 
RoaringNavigableMap64.and(result, rows);
-                    if (result.isEmpty()) {
-                        break;
-                    }
+            return evaluate(
+                    predicate,
+                    fieldName -> {
+                        int fieldId = rowType.getField(fieldName).id();
+                        Section section = sections.get(fieldId);
+                        checkState(
+                                section != null,
+                                "Missing field %s in %s.",
+                                fieldId,
+                                bitmap.filePath());
+                        checkArgument(
+                                section.indexType == BITMAP,
+                                "Unsupported centroid scalar index type %s for 
field %s.",
+                                section.indexType,
+                                fieldId);
+                        KeySerializer serializer =
+                                
KeySerializer.create(buildType.getField(fieldId).type());
+                        GlobalIndexIOMeta sectionMeta =
+                                new GlobalIndexIOMeta(
+                                        bitmap.filePath(),
+                                        section.length,
+                                        bitmap.rowCount(),
+                                        section.metadata);
+                        return new BitmapIndexReader(
+                                serializer, ignored -> section.open(input), 
sectionMeta);
+                    });
+        }
+    }
+
+    private static RoaringNavigableMap64 evaluate(
+            Predicate predicate, IOFunction<String, BitmapIndexReader> 
openReader)
+            throws IOException {
+        if (predicate instanceof LeafPredicate) {
+            LeafPredicate leaf = (LeafPredicate) predicate;
+            try (BitmapIndexReader reader =
+                    openReader.apply(leaf.fieldRefOptional().get().name())) {
+                if (Equal.INSTANCE.equals(leaf.function())) {
+                    return 
reader.visitEqual(leaf.literals().get(0)).get().results();
+                }
+                if (IsNull.INSTANCE.equals(leaf.function())) {
+                    return reader.visitIsNull().get().results();
                 }
+                if (IsNotNull.INSTANCE.equals(leaf.function())) {
+                    return reader.visitIsNotNull().get().results();
+                }
+                checkArgument(
+                        In.INSTANCE.equals(leaf.function()),
+                        "Unsupported centroid bitmap predicate %s.",
+                        leaf.function());
+                return reader.visitIn(leaf.literals()).get().results();
+            }
+        }
+
+        checkArgument(
+                predicate instanceof CompoundPredicate, "Unsupported centroid 
bitmap predicate.");
+        CompoundPredicate compound = (CompoundPredicate) predicate;
+        boolean conjunction = And.INSTANCE.equals(compound.function());
+        checkArgument(
+                conjunction || Or.INSTANCE.equals(compound.function()),
+                "Unsupported centroid bitmap predicate %s.",
+                compound.function());
+        Iterator<Predicate> children = compound.children().iterator();
+        checkArgument(children.hasNext(), "Centroid bitmap lookup requires a 
predicate.");
+        RoaringNavigableMap64 rows = evaluate(children.next(), openReader);
+        while (children.hasNext() && (!conjunction || !rows.isEmpty())) {
+            RoaringNavigableMap64 childRows = evaluate(children.next(), 
openReader);
+            if (conjunction) {
+                rows.and(childRows);
+            } else {
+                rows.or(childRows);
             }
-            return result;
         }
+        return rows;
     }
 
     private static Map<Integer, Section> readDirectory(
diff --git 
a/paimon-common/src/test/java/org/apache/paimon/globalindex/bitmap/LazyFilteredBitmapIndexReaderTest.java
 
b/paimon-common/src/test/java/org/apache/paimon/globalindex/bitmap/LazyFilteredBitmapIndexReaderTest.java
index faff123e8b..c72795abda 100644
--- 
a/paimon-common/src/test/java/org/apache/paimon/globalindex/bitmap/LazyFilteredBitmapIndexReaderTest.java
+++ 
b/paimon-common/src/test/java/org/apache/paimon/globalindex/bitmap/LazyFilteredBitmapIndexReaderTest.java
@@ -34,7 +34,7 @@ import org.apache.paimon.globalindex.io.GlobalIndexFileReader;
 import org.apache.paimon.globalindex.io.GlobalIndexFileWriter;
 import org.apache.paimon.options.Options;
 import org.apache.paimon.predicate.FieldRef;
-import org.apache.paimon.predicate.LeafPredicate;
+import org.apache.paimon.predicate.Predicate;
 import org.apache.paimon.predicate.PredicateBuilder;
 import org.apache.paimon.types.DataField;
 import org.apache.paimon.types.DataTypes;
@@ -126,14 +126,17 @@ public class LazyFilteredBitmapIndexReaderTest {
 
         int[] extraFieldIds = {state.id(), dataField.id()};
         PredicateBuilder builder = new PredicateBuilder(fields);
-        List<LeafPredicate> predicates =
-                Arrays.asList(
-                        (LeafPredicate) builder.equal(0, 
BinaryString.fromString("red")),
-                        (LeafPredicate) builder.isNull(1));
+        Predicate red = builder.equal(0, BinaryString.fromString("red"));
+        Predicate conjunction = PredicateBuilder.and(red, builder.isNull(1));
         assertThat(
                         CentroidBitmapIndex.match(
-                                fileReader, meta, extraFieldIds, fields, 
fields, predicates))
+                                fileReader, meta, extraFieldIds, fields, 
fields, conjunction))
                 .containsExactly(90_000L);
+        Predicate disjunction = PredicateBuilder.or(red, builder.isNotNull(1), 
builder.isNull(1));
+        assertThat(
+                        CentroidBitmapIndex.match(
+                                fileReader, meta, extraFieldIds, fields, 
fields, disjunction))
+                .containsExactly(90L, 90_000L);
         GlobalIndexIOMeta incomplete =
                 new GlobalIndexIOMeta(path, meta.fileSize() - 1, 2, 
result.meta());
         assertThatThrownBy(
@@ -147,7 +150,7 @@ public class LazyFilteredBitmapIndexReaderTest {
                                         extraFieldIds,
                                         fields,
                                         fields,
-                                        predicates))
+                                        conjunction))
                 .hasMessageContaining("Invalid centroid bitmap section");
     }
 
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/table/source/CentroidBitmapFilter.java
 
b/paimon-core/src/main/java/org/apache/paimon/table/source/CentroidBitmapFilter.java
index fe9016b432..af6fc851e8 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/table/source/CentroidBitmapFilter.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/table/source/CentroidBitmapFilter.java
@@ -24,12 +24,16 @@ import 
org.apache.paimon.globalindex.bitmap.CentroidBitmapIndex;
 import org.apache.paimon.index.GlobalIndexMeta;
 import org.apache.paimon.index.IndexFileMeta;
 import org.apache.paimon.index.IndexPathFactory;
+import org.apache.paimon.predicate.And;
+import org.apache.paimon.predicate.CompoundFunction;
+import org.apache.paimon.predicate.CompoundPredicate;
 import org.apache.paimon.predicate.Equal;
 import org.apache.paimon.predicate.FieldRef;
 import org.apache.paimon.predicate.In;
 import org.apache.paimon.predicate.IsNotNull;
 import org.apache.paimon.predicate.IsNull;
 import org.apache.paimon.predicate.LeafPredicate;
+import org.apache.paimon.predicate.Or;
 import org.apache.paimon.predicate.Predicate;
 import org.apache.paimon.predicate.PredicateBuilder;
 import org.apache.paimon.table.FileStoreTable;
@@ -69,29 +73,10 @@ final class CentroidBitmapFilter {
             return null;
         }
         RowType rowType = table.rowType();
-        List<LeafPredicate> predicates = new ArrayList<>();
         List<DataField> fields = new ArrayList<>();
-        for (Predicate predicate : PredicateBuilder.splitAnd(filter)) {
-            LeafPredicate leaf = pointLookup(predicate);
-            if (leaf == null) {
-                return null;
-            }
-            Optional<FieldRef> fieldRef = leaf.fieldRefOptional();
-            if (!fieldRef.isPresent()
-                    || (!Equal.INSTANCE.equals(leaf.function())
-                            && !In.INSTANCE.equals(leaf.function())
-                            && !IsNotNull.INSTANCE.equals(leaf.function())
-                            && !IsNull.INSTANCE.equals(leaf.function()))
-                    || !rowType.containsField(fieldRef.get().name())) {
-                return null;
-            }
-            DataField field = rowType.getField(fieldRef.get().name());
-            if (!field.type().equalsIgnoreNullable(fieldRef.get().type())
-                    || Arrays.stream(extraFieldIds).noneMatch(id -> id == 
field.id())) {
-                return null;
-            }
-            predicates.add(leaf);
-            fields.add(field);
+        Predicate predicate = bind(filter, rowType, extraFieldIds, fields);
+        if (predicate == null) {
+            return null;
         }
 
         IndexPathFactory paths = 
table.store().pathFactory().globalIndexFileFactory();
@@ -154,18 +139,68 @@ final class CentroidBitmapFilter {
                         extraFieldIds,
                         rowType,
                         buildType,
-                        predicates);
+                        predicate);
     }
 
     @Nullable
-    private static LeafPredicate pointLookup(Predicate predicate) {
-        if (predicate instanceof LeafPredicate) {
-            return (LeafPredicate) predicate;
+    private static Predicate bind(
+            Predicate predicate, RowType rowType, int[] extraFieldIds, 
List<DataField> fields) {
+        if (predicate instanceof CompoundPredicate) {
+            CompoundFunction function = ((CompoundPredicate) 
predicate).function();
+            List<Predicate> children;
+            if (Or.INSTANCE.equals(function)) {
+                children = PredicateBuilder.splitOr(predicate);
+                LeafPredicate in = sameColumnIn(children);
+                if (in != null) {
+                    return bind(in, rowType, extraFieldIds, fields);
+                }
+            } else if (And.INSTANCE.equals(function)) {
+                children = PredicateBuilder.splitAnd(predicate);
+            } else {
+                return null;
+            }
+            if (children.isEmpty()) {
+                return null;
+            }
+
+            List<Predicate> boundChildren = new ArrayList<>(children.size());
+            for (Predicate child : children) {
+                Predicate bound = bind(child, rowType, extraFieldIds, fields);
+                if (bound == null) {
+                    return null;
+                }
+                boundChildren.add(bound);
+            }
+            return new CompoundPredicate(function, boundChildren);
+        }
+        if (!(predicate instanceof LeafPredicate)) {
+            return null;
+        }
+        LeafPredicate leaf = (LeafPredicate) predicate;
+        Optional<FieldRef> fieldRef = leaf.fieldRefOptional();
+        if (!fieldRef.isPresent()
+                || (!Equal.INSTANCE.equals(leaf.function())
+                        && !In.INSTANCE.equals(leaf.function())
+                        && !IsNotNull.INSTANCE.equals(leaf.function())
+                        && !IsNull.INSTANCE.equals(leaf.function()))
+                || !rowType.containsField(fieldRef.get().name())) {
+            return null;
         }
+        DataField field = rowType.getField(fieldRef.get().name());
+        if (!field.type().equalsIgnoreNullable(fieldRef.get().type())
+                || Arrays.stream(extraFieldIds).noneMatch(id -> id == 
field.id())) {
+            return null;
+        }
+        fields.add(field);
+        return leaf;
+    }
+
+    @Nullable
+    private static LeafPredicate sameColumnIn(List<Predicate> children) {
         // PredicateBuilder represents short IN lists as an OR of equalities.
         LeafPredicate first = null;
         List<Object> values = new ArrayList<>();
-        for (Predicate child : PredicateBuilder.splitOr(predicate)) {
+        for (Predicate child : children) {
             if (!(child instanceof LeafPredicate)) {
                 return null;
             }
diff --git 
a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/CentroidVectorIndexTest.scala
 
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/CentroidVectorIndexTest.scala
index ac2055229d..6383e85655 100644
--- 
a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/CentroidVectorIndexTest.scala
+++ 
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/CentroidVectorIndexTest.scala
@@ -174,6 +174,53 @@ class CentroidVectorIndexTest extends PaimonSparkTestBase {
     }
   }
 
+  test("centroid shard bitmaps apply cross-column OR before top K") {
+    withTable("T") {
+      spark.sql("""CREATE TABLE T (id INT, v ARRAY<FLOAT>, tenant_id INT, 
status STRING, p INT)
+                  |PARTITIONED BY (p) TBLPROPERTIES (
+                  |'bucket'='-1', 'row-tracking.enabled'='true', 
'data-evolution.enabled'='true',
+                  |'global-index.thread-num'='1',
+                  
|'vector-index.post-filter.candidates-factor'='1')""".stripMargin)
+      spark.sql("""INSERT INTO T VALUES
+                  |(0, array(0.0f, 0.0f), 7, 'private', 0),
+                  |(1, array(1.0f, 0.0f), NULL, 'public', 0),
+                  |(2, array(2.0f, 0.0f), 42, NULL, 0),
+                  |(3, array(3.0f, 0.0f), 42, 'public', 0),
+                  |(4, array(4.0f, 0.0f), NULL, NULL, 0),
+                  |(5, array(0.1f, 0.0f), 7, 'private', 1)""".stripMargin)
+      spark
+        .sql("""CALL sys.create_global_index(
+               |table => 'test.T', index_column => 'v', index_type => 
'ivf-flat',
+               |options => 
'ivf-flat.sharding=centroid,ivf-flat.dimension=2,ivf-flat.metric=l2,ivf-flat.centroid.nlist=1,ivf-flat.nlist=1',
+               |extra_fields => 
'{"tenant_id":"bitmap","status":"bitmap"}')""".stripMargin)
+        .collect()
+
+      def nearest(predicate: String, limit: Int = 1): Seq[Int] =
+        spark
+          .sql(s"""SELECT id FROM 
vector_search('T','v',array(0.0f,0.0f),$limit,
+                  |map('centroid.nprobe','1','ivf.nprobe','1')) WHERE 
$predicate ORDER BY id""".stripMargin)
+          .collect()
+          .map(_.getInt(0))
+          .toSeq
+
+      val crossColumnOr = "tenant_id = 42 OR status = 'public'"
+      for (distributed <- Seq(false, true)) {
+        withSparkSQLConf("spark.paimon.vector-search.distribute.enabled" -> 
distributed.toString) {
+          assert(nearest(crossColumnOr) == Seq(1))
+          assert(nearest("status = 'public' OR tenant_id = 42", 4) == Seq(1, 
2, 3))
+          assert(nearest(s"($crossColumnOr) AND status IS NULL") == Seq(2))
+          assert(
+            nearest("(tenant_id = 42 AND status IS NULL) OR status = 
'public'", 3) ==
+              Seq(1, 2, 3))
+          assert(nearest("tenant_id = 99 OR status = 'public'") == Seq(1))
+          assert(nearest("tenant_id = 99 OR status = 'missing'").isEmpty)
+          assert(nearest("tenant_id = 42 OR id = 0") == Seq(0))
+          assert(nearest("status LIKE 'priv%' OR tenant_id = 42") == Seq(0))
+        }
+      }
+    }
+  }
+
   private def build(): Unit = {
     spark
       .sql(

Reply via email to