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 e916d09c12 [core][python] Match scalar global indexes by primary field 
(#11316)
e916d09c12 is described below

commit e916d09c123c00804d8ebd941aa822470fcb98f1
Author: Jingsong Lee <[email protected]>
AuthorDate: Sat Nov 21 14:13:41 2026 +0800

    [core][python] Match scalar global indexes by primary field (#11316)
---
 .../DataEvolutionGlobalIndexScanner.java           | 23 +-------
 .../paimon/globalindex/GlobalIndexQueryTest.java   | 48 ++++++++++++++-
 .../data_evolution_global_index_coverage.py        |  2 -
 .../data_evolution_global_index_scanner.py         | 33 +----------
 paimon-python/pypaimon/tests/global_index_test.py  | 21 ++++++-
 .../pypaimon/tests/vector_search_filter_test.py    | 69 +++++++++-------------
 6 files changed, 97 insertions(+), 99 deletions(-)

diff --git 
a/paimon-core/src/main/java/org/apache/paimon/globalindex/DataEvolutionGlobalIndexScanner.java
 
b/paimon-core/src/main/java/org/apache/paimon/globalindex/DataEvolutionGlobalIndexScanner.java
index b188268e41..97b22143d9 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/globalindex/DataEvolutionGlobalIndexScanner.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/globalindex/DataEvolutionGlobalIndexScanner.java
@@ -167,15 +167,6 @@ public class DataEvolutionGlobalIndexScanner implements 
Closeable {
         for (IndexMetaFileGroup group : primaryGroups.values()) {
             groupsByField.computeIfAbsent(group.indexFieldId, k -> new 
ArrayList<>()).add(group);
         }
-        for (IndexMetaFileGroup group : primaryGroups.values()) {
-            for (int fieldId : group.fieldIds.subList(1, 
group.fieldIds.size())) {
-                List<IndexMetaFileGroup> groups =
-                        groupsByField.computeIfAbsent(fieldId, k -> new 
ArrayList<>());
-                if (!groups.contains(group)) {
-                    groups.add(group);
-                }
-            }
-        }
         return groupsByField;
     }
 
@@ -362,19 +353,7 @@ public class DataEvolutionGlobalIndexScanner implements 
Closeable {
                             || !isIndexInSchema(table.rowType(), 
entry.indexFile())) {
                         return false;
                     }
-                    // Collect indexes whose primary column is filtered, and 
also multi-column
-                    // indexes that have a filtered column as an extra.
-                    if (filterFieldIds.contains(globalIndex.indexFieldId())) {
-                        return true;
-                    }
-                    if (globalIndex.extraFieldIds() != null) {
-                        for (int id : globalIndex.extraFieldIds()) {
-                            if (filterFieldIds.contains(id)) {
-                                return true;
-                            }
-                        }
-                    }
-                    return false;
+                    return filterFieldIds.contains(globalIndex.indexFieldId());
                 };
         return indexFileFilter;
     }
diff --git 
a/paimon-core/src/test/java/org/apache/paimon/globalindex/GlobalIndexQueryTest.java
 
b/paimon-core/src/test/java/org/apache/paimon/globalindex/GlobalIndexQueryTest.java
index e22a7ff455..a04876c6a0 100644
--- 
a/paimon-core/src/test/java/org/apache/paimon/globalindex/GlobalIndexQueryTest.java
+++ 
b/paimon-core/src/test/java/org/apache/paimon/globalindex/GlobalIndexQueryTest.java
@@ -31,12 +31,15 @@ import org.apache.paimon.index.IndexFileMeta;
 import org.apache.paimon.index.IndexPathFactory;
 import org.apache.paimon.io.DataFileMeta;
 import org.apache.paimon.io.DataOutputSerializer;
+import org.apache.paimon.manifest.FileKind;
+import org.apache.paimon.manifest.IndexManifestEntry;
 import org.apache.paimon.options.Options;
 import org.apache.paimon.predicate.And;
 import org.apache.paimon.predicate.CompoundPredicate;
 import org.apache.paimon.predicate.Predicate;
 import org.apache.paimon.predicate.PredicateBuilder;
 import org.apache.paimon.stats.SimpleStats;
+import org.apache.paimon.table.FileStoreTable;
 import org.apache.paimon.table.source.DataSplit;
 import org.apache.paimon.table.source.SplitSerializer;
 import org.apache.paimon.types.DataField;
@@ -647,6 +650,43 @@ class GlobalIndexQueryTest {
         }
     }
 
+    @Test
+    void testManifestFilterMatchesOnlyLeadingFields() {
+        RowType rowType = RowType.of(DataTypes.INT(), DataTypes.INT());
+        FileStoreTable table = mock(FileStoreTable.class);
+        when(table.rowType()).thenReturn(rowType);
+        PredicateBuilder builder = new PredicateBuilder(rowType);
+        int firstId = rowType.getFields().get(0).id();
+        int secondId = rowType.getFields().get(1).id();
+        IndexManifestEntry composite =
+                new IndexManifestEntry(
+                        FileKind.ADD,
+                        BinaryRow.EMPTY_ROW,
+                        0,
+                        indexFile("btree", "composite", 0, 99, firstId, new 
int[] {secondId}));
+        IndexManifestEntry dedicated =
+                new IndexManifestEntry(
+                        FileKind.ADD,
+                        BinaryRow.EMPTY_ROW,
+                        0,
+                        indexFile("bitmap", "dedicated", 0, 99, secondId, 
null));
+
+        Predicate suffix = builder.equal(1, 7);
+        assertThat(
+                        DataEvolutionGlobalIndexScanner.indexFileFilter(table, 
null, suffix)
+                                .test(composite))
+                .isFalse();
+        assertThat(
+                        DataEvolutionGlobalIndexScanner.indexFileFilter(table, 
null, suffix)
+                                .test(dedicated))
+                .isTrue();
+        Predicate prefix = PredicateBuilder.and(builder.equal(0, 1), suffix);
+        assertThat(
+                        DataEvolutionGlobalIndexScanner.indexFileFilter(table, 
null, prefix)
+                                .test(composite))
+                .isTrue();
+    }
+
     @Test
     void testGroupsPreserveIndexTypesRangesAndExtraFields() {
         IndexFileMeta first = indexFile("es-index", "first", 0, 99, 10, new 
int[] {20});
@@ -659,9 +699,11 @@ class GlobalIndexQueryTest {
                         Arrays.asList(first, sameRange, tail, primary, 
bitmap));
         assertThat(groups).containsOnlyKeys(10, 20);
         assertThat(groups.get(10)).hasSize(1);
-        assertThat(groups.get(20)).hasSize(2);
+        assertThat(groups.get(20)).hasSize(1);
         IndexMetaFileGroup extra = groups.get(10).get(0);
-        assertThat(groups.get(20).get(1)).isSameAs(extra);
+        assertThat(extra.extraFields(RowType.of(new DataField(20, "extra", 
DataTypes.INT()))))
+                .extracting(DataField::id)
+                .containsExactly(20);
         assertThat(extra.metas().get("es-index"))
                 .containsOnlyKeys(new Range(0, 99), new Range(100, 199));
         assertThat(extra.metas().get("es-index").get(new Range(0, 99)))
@@ -678,7 +720,7 @@ class GlobalIndexQueryTest {
                                 indexFile("btree", "composite", 0, 99, 10, new 
int[] {20}),
                                 indexFile("btree", "single", 100, 199, 10, 
null)));
         assertThat(groups.get(10)).hasSize(2);
-        assertThat(groups.get(20)).hasSize(1);
+        assertThat(groups).doesNotContainKey(20);
     }
 
     @Test
diff --git 
a/paimon-python/pypaimon/execution/python/globalindex/data_evolution_global_index_coverage.py
 
b/paimon-python/pypaimon/execution/python/globalindex/data_evolution_global_index_coverage.py
index fe051153cf..29ea4c0ca1 100644
--- 
a/paimon-python/pypaimon/execution/python/globalindex/data_evolution_global_index_coverage.py
+++ 
b/paimon-python/pypaimon/execution/python/globalindex/data_evolution_global_index_coverage.py
@@ -49,8 +49,6 @@ class DataEvolutionGlobalIndexCoverage:
                 continue
             row_range = Range(meta.row_range_start, meta.row_range_end)
             self._add_coverage(meta.index_field_id, row_range)
-            for field_id in meta.extra_field_ids or []:
-                self._add_coverage(field_id, row_range)
 
     def unindexed_ranges(
         self,
diff --git 
a/paimon-python/pypaimon/execution/python/globalindex/data_evolution_global_index_scanner.py
 
b/paimon-python/pypaimon/execution/python/globalindex/data_evolution_global_index_scanner.py
index 3ae8540a5d..7d2f9081b7 100644
--- 
a/paimon-python/pypaimon/execution/python/globalindex/data_evolution_global_index_scanner.py
+++ 
b/paimon-python/pypaimon/execution/python/globalindex/data_evolution_global_index_scanner.py
@@ -83,7 +83,6 @@ class DataEvolutionGlobalIndexScanner:
 
     def _create_evaluator(self, fields, file_io, index_path, index_files):
         index_metas = {}
-        extra_index_metas = {}
         for index_file in index_files:
             global_index_meta = index_file.global_index_meta
             if global_index_meta is None:
@@ -108,8 +107,6 @@ class DataEvolutionGlobalIndexScanner:
             if group is None:
                 group = _IndexMetaFileGroup(index_field_id, field_ids)
                 index_metas[index_field_id] = group
-                for extra_field_id in field_ids[1:]:
-                    extra_index_metas.setdefault(extra_field_id, 
[]).append(group)
             elif group.field_ids != tuple(field_ids):
                 raise ValueError(
                     "Primary field %s owns multiple indexes with different "
@@ -124,27 +121,10 @@ class DataEvolutionGlobalIndexScanner:
         def readers_function(field: DataField) -> 
Collection[GlobalIndexReader]:
             from 
pypaimon.execution.python.globalindex.union_global_index_reader import 
UnionGlobalIndexReader
 
-            groups = []
             group = index_metas.get(field.id)
-            if group is not None:
-                groups.append(group)
-
-            extra_groups = extra_index_metas.get(field.id)
-            if extra_groups:
-                groups.extend(
-                    extra_group
-                    for extra_group in extra_groups
-                    if extra_group not in groups
-                )
-            if not groups:
+            if group is None:
                 return []
-            # Like Java, union every reader for the field. Missing coverage
-            # belongs to raw fallback, not to the matches returned by an index.
-            readers = []
-            for group in groups:
-                readers.extend(
-                    _create_readers(
-                        file_io, index_path, group.metas, field, executor, 
options))
+            readers = _create_readers(file_io, index_path, group.metas, field, 
executor, options)
             return [UnionGlobalIndexReader(readers)]
 
         return GlobalIndexEvaluator(fields, readers_function)
@@ -202,14 +182,7 @@ class DataEvolutionGlobalIndexScanner:
             global_index_meta = entry.index_file.global_index_meta
             if global_index_meta is None:
                 return False
-            if global_index_meta.index_field_id in filter_field_ids:
-                return True
-            if global_index_meta.extra_field_ids is not None:
-                return any(
-                    field_id in filter_field_ids
-                    for field_id in global_index_meta.extra_field_ids
-                )
-            return False
+            return global_index_meta.index_field_id in filter_field_ids
 
         if snapshot is None:
             snapshot = _resolve_snapshot(table, None)
diff --git a/paimon-python/pypaimon/tests/global_index_test.py 
b/paimon-python/pypaimon/tests/global_index_test.py
index 6dc655d574..95dcf736bd 100644
--- a/paimon-python/pypaimon/tests/global_index_test.py
+++ b/paimon-python/pypaimon/tests/global_index_test.py
@@ -191,9 +191,9 @@ class _CoverageSnapshot:
         self.next_row_id = next_row_id
 
 
-def _coverage_index_file(field_id, start, end, extra_field_ids=None):
+def _coverage_index_file(field_id, start, end, extra_field_ids=None, 
index_type="btree"):
     return IndexFileMeta(
-        index_type="btree",
+        index_type=index_type,
         file_name="idx-%s-%s-%s" % (field_id, start, end),
         file_size=1,
         row_count=end - start + 1,
@@ -299,6 +299,23 @@ class 
DataEvolutionGlobalIndexCoverageTest(unittest.TestCase):
 
         self.assertEqual([Range(0, 9)], coverage.unindexed_ranges(1))
 
+    def test_extra_field_does_not_extend_dedicated_scalar_coverage(self):
+        from 
pypaimon.execution.python.globalindex.data_evolution_global_index_coverage 
import (
+            DataEvolutionGlobalIndexCoverage,
+        )
+
+        coverage = DataEvolutionGlobalIndexCoverage(
+            _CoverageTable("full"),
+            _CoverageSnapshot(10),
+            None,
+            [
+                _coverage_index_file(1, 0, 4),
+                _coverage_index_file(0, 5, 9, extra_field_ids=[1], 
index_type="bitmap"),
+            ],
+        )
+
+        self.assertEqual([Range(5, 9)], coverage.unindexed_ranges(1))
+
     def test_detail_mode_uses_table_data_ranges(self):
         from 
pypaimon.execution.python.globalindex.data_evolution_global_index_coverage 
import (
             DataEvolutionGlobalIndexCoverage,
diff --git a/paimon-python/pypaimon/tests/vector_search_filter_test.py 
b/paimon-python/pypaimon/tests/vector_search_filter_test.py
index 26024e16b5..e8dfaf4d6e 100644
--- a/paimon-python/pypaimon/tests/vector_search_filter_test.py
+++ b/paimon-python/pypaimon/tests/vector_search_filter_test.py
@@ -2104,7 +2104,7 @@ class VectorSearchMultiShardScalarTest(unittest.TestCase):
         self.assertEqual([1], sorted(evaluation.result.results()))
         self.assertEqual([Range(5, 9)], fallback)
 
-    def test_extra_field_groups_union_matches_without_padding(self):
+    def test_scalar_reader_uses_only_the_primary_field(self):
         from pypaimon.globalindex.global_index_reader import GlobalIndexReader
         from 
pypaimon.execution.python.globalindex.data_evolution_global_index_scanner 
import (
             DataEvolutionGlobalIndexScanner,
@@ -2114,15 +2114,13 @@ class 
VectorSearchMultiShardScalarTest(unittest.TestCase):
         b_field = _field(1, "b")
         c_field = _field(2, "c")
 
-        # Mocked non-BTree readers exercise shared extra-field reader grouping.
         short = _entry(None, field_id=0, index_type="bitmap",
                        file_name="a-c.index",
                        row_range_start=0, row_range_end=4).index_file
         short.global_index_meta.extra_field_ids = [2]
-        long = _entry(None, field_id=1, index_type="bitmap",
-                      file_name="b-c.index",
+        long = _entry(None, field_id=2, index_type="bitmap",
+                      file_name="c.index",
                       row_range_start=0, row_range_end=9).index_file
-        long.global_index_meta.extra_field_ids = [2]
 
         class _StubReader(GlobalIndexReader):
             def __init__(self_inner, file_name):
@@ -2163,12 +2161,10 @@ class 
VectorSearchMultiShardScalarTest(unittest.TestCase):
                 scanner.close()
 
         self.assertIsNotNone(result)
-        # Java unions both groups: row 4 survives from the short group. Rows
-        # 5, 6 and 9 were not returned by either index and must not be added.
-        self.assertEqual([1, 3, 4, 7, 8], sorted(list(result.results())))
+        self.assertEqual([1, 3, 7, 8], sorted(list(result.results())))
         self.assertTrue(result.is_exact())
 
-    def test_extra_field_groups_do_not_turn_missing_coverage_into_hits(self):
+    def test_scalar_reader_unions_disjoint_primary_field_shards(self):
         from pypaimon.globalindex.global_index_reader import GlobalIndexReader
         from 
pypaimon.execution.python.globalindex.data_evolution_global_index_scanner 
import (
             DataEvolutionGlobalIndexScanner,
@@ -2178,15 +2174,12 @@ class 
VectorSearchMultiShardScalarTest(unittest.TestCase):
         b_field = _field(1, "b")
         c_field = _field(2, "c")
 
-        # Reader stubs exercise disjoint coverage independently of BTree 
support.
-        a_early = _entry(None, field_id=0, index_type="bitmap",
-                         file_name="a-c-early.index",
+        c_early = _entry(None, field_id=2, index_type="bitmap",
+                         file_name="c-early.index",
                          row_range_start=2, row_range_end=3).index_file
-        a_early.global_index_meta.extra_field_ids = [2]
-        a_late = _entry(None, field_id=0, index_type="bitmap",
-                        file_name="a-c-late.index",
+        c_late = _entry(None, field_id=2, index_type="bitmap",
+                        file_name="c-late.index",
                         row_range_start=7, row_range_end=9).index_file
-        a_late.global_index_meta.extra_field_ids = [2]
         b_full = _entry(None, field_id=1, index_type="bitmap",
                         file_name="b-c-full.index",
                         row_range_start=0, row_range_end=9).index_file
@@ -2198,9 +2191,9 @@ class VectorSearchMultiShardScalarTest(unittest.TestCase):
 
             def visit_equal(self_inner, field_ref, literal):
                 bm = RoaringBitmap64()
-                if self_inner._file_name == "a-c-early.index":
+                if self_inner._file_name == "c-early.index":
                     bm.add(0)  # global 2 after offset
-                elif self_inner._file_name == "a-c-late.index":
+                elif self_inner._file_name == "c-late.index":
                     for row_id in [0, 1]:  # global 7, 8 after offset
                         bm.add(row_id)
                 else:
@@ -2223,7 +2216,7 @@ class VectorSearchMultiShardScalarTest(unittest.TestCase):
                 fields=[a_field, b_field, c_field],
                 file_io=object(),
                 index_path="/unused",
-                index_files=[a_early, a_late, b_full],
+                index_files=[c_early, c_late, b_full],
             )
             try:
                 result = scanner.scan(
@@ -2233,12 +2226,10 @@ class 
VectorSearchMultiShardScalarTest(unittest.TestCase):
                 scanner.close()
 
         self.assertIsNotNone(result)
-        # Coverage gaps in the first group are not matches. Only real hits
-        # from either group participate in the union, as in Java.
-        self.assertEqual([1, 2, 5, 7, 8], sorted(list(result.results())))
+        self.assertEqual([2, 7, 8], sorted(list(result.results())))
         self.assertTrue(result.is_exact())
 
-    def test_extra_field_groups_preserve_unknown_results(self):
+    def test_scalar_shards_preserve_unknown_results(self):
         from pypaimon.globalindex.global_index_reader import GlobalIndexReader
         from 
pypaimon.execution.python.globalindex.data_evolution_global_index_scanner 
import (
             DataEvolutionGlobalIndexScanner,
@@ -2248,14 +2239,12 @@ class 
VectorSearchMultiShardScalarTest(unittest.TestCase):
         b_field = _field(1, "b", "STRING")
         c_field = _field(2, "c", "STRING")
 
-        short = _entry(None, field_id=0, index_type="bitmap",
-                       file_name="a-c.index",
+        short = _entry(None, field_id=2, index_type="bitmap",
+                       file_name="c-short.index",
                        row_range_start=0, row_range_end=4).index_file
-        short.global_index_meta.extra_field_ids = [2]
-        long = _entry(None, field_id=1, index_type="bitmap",
-                      file_name="b-c.index",
-                      row_range_start=0, row_range_end=9).index_file
-        long.global_index_meta.extra_field_ids = [2]
+        long = _entry(None, field_id=2, index_type="bitmap",
+                      file_name="c-long.index",
+                      row_range_start=5, row_range_end=9).index_file
 
         class _StubReader(GlobalIndexReader):
             def visit_contains(self_inner, field_ref, literal):
@@ -2431,7 +2420,7 @@ class VectorSearchMultiShardScalarTest(unittest.TestCase):
 
         self.assertEqual([Range(5, 9)], result.results().to_range_list())
 
-    def test_scanner_create_excludes_composite_btree_extra_fields(self):
+    def test_scanner_create_does_not_match_extra_fields(self):
         from 
pypaimon.execution.python.globalindex.data_evolution_global_index_scanner 
import (
             DataEvolutionGlobalIndexScanner,
         )
@@ -2452,15 +2441,15 @@ class 
VectorSearchMultiShardScalarTest(unittest.TestCase):
         )
         _patch_snapshot(self, table._entries)
 
-        with mock.patch(
-                
"pypaimon.execution.python.globalindex.data_evolution_global_index_scanner."
-                "_create_inner_readers") as create_readers:
-            scanner = DataEvolutionGlobalIndexScanner.create(
-                table,
-                predicate=Predicate(method="equal", index=1, field="id", 
literals=[3]),
-            )
-            self.assertIsNone(scanner)
-            create_readers.assert_not_called()
+        predicate = Predicate(method="equal", index=1, field="id", 
literals=[3])
+        for index_type in ("btree", "bitmap"):
+            with self.subTest(index_type=index_type):
+                multi.index_type = index_type
+                scanner = DataEvolutionGlobalIndexScanner.create(
+                    table, predicate=predicate, allow_composite=True)
+                if scanner is not None:
+                    scanner.close()
+                self.assertIsNone(scanner)
 
 
 class VectorSearchPartitionedFilterTest(unittest.TestCase):

Reply via email to