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 6aeb4ee2b5 [python] Filter shard reads and _ROW_ID reads after 
selecting rows (#11024)
6aeb4ee2b5 is described below

commit 6aeb4ee2b57e87b142ed47f8bb283aaa9650f7a0
Author: YangJie <[email protected]>
AuthorDate: Tue Nov 10 04:15:03 2026 -0500

    [python] Filter shard reads and _ROW_ID reads after selecting rows (#11024)
---
 .../pypaimon/execution/python/read/split_read.py   |  13 ++-
 .../tests/read_predicate_positions_test.py         | 114 +++++++++++++++++++++
 2 files changed, 126 insertions(+), 1 deletion(-)

diff --git a/paimon-python/pypaimon/execution/python/read/split_read.py 
b/paimon-python/pypaimon/execution/python/read/split_read.py
index 0ff4a8422c..7dcc6396ba 100644
--- a/paimon-python/pypaimon/execution/python/read/split_read.py
+++ b/paimon-python/pypaimon/execution/python/read/split_read.py
@@ -243,6 +243,9 @@ class SplitRead(ABC):
         else:
             return predicate_for_physical_fields(self.predicate, 
self.table.fields)
 
+    def _assigns_row_id(self) -> bool:
+        return any(f.name == SpecialFields.ROW_ID.name for f in 
self.read_fields)
+
     def _has_row_tracking_predicate(self) -> bool:
         return (self.table.options.row_tracking_enabled()
                 and self.predicate is not None
@@ -272,7 +275,11 @@ class SplitRead(ABC):
         ) = self._get_fields_and_predicate(file.schema_id, read_fields)
         if (file.file_name in self.deletion_file_readers
                 or (for_merge_read and self.row_ranges is not None)
-                or physical_row_ranges is not None):
+                or physical_row_ranges is not None
+                # A shard range and _ROW_ID values are counted on the rows the
+                # format reader returns, so it must not filter them either.
+                or shard_range is not None
+                or self._assigns_row_id()):
             # DVs and indexed PK ranges refer to physical file positions.
             # Filtering or skipping row groups here would renumber those rows.
             # Apply the residual predicate after position selection and 
merging.
@@ -1062,6 +1069,10 @@ class RawFileSplitRead(SplitRead):
                      or 
predicate_field_names(self.predicate).intersection(self.table.partition_keys)
                      or self.deletion_file_readers
                      or self._physical_row_ranges
+                     # file_reader_supplier does not push the predicate into
+                     # shard reads or reads that assign _ROW_ID.
+                     or isinstance(self.split, SlicedSplit)
+                     or self._assigns_row_id()
                      or any(file.schema_id != self.table.table_schema.id
                             for file in self.split.files))):
             reader = FilterRecordBatchReader(
diff --git a/paimon-python/pypaimon/tests/read_predicate_positions_test.py 
b/paimon-python/pypaimon/tests/read_predicate_positions_test.py
new file mode 100644
index 0000000000..085867edbf
--- /dev/null
+++ b/paimon-python/pypaimon/tests/read_predicate_positions_test.py
@@ -0,0 +1,114 @@
+# 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.
+
+import os
+import shutil
+import tempfile
+import unittest
+from importlib.util import find_spec
+
+import pyarrow as pa
+
+from pypaimon import CatalogFactory, Schema
+from pypaimon.common.predicate_builder import PredicateBuilder
+
+HAS_VORTEX = find_spec('vortex') is not None
+
+PA_SCHEMA = pa.schema([('a', pa.int32()), ('b', pa.string())])
+
+
+class ReadPredicatePositionsTest(unittest.TestCase):
+    """Shard ranges and _ROW_ID values are counted on the rows a format reader
+    returns, so a filter must not be applied inside the format reader for
+    those reads; it is applied after the rows are selected."""
+
+    def setUp(self):
+        self.temp_dir = tempfile.mkdtemp(prefix='unittest_read_pred_pos_')
+
+    def tearDown(self):
+        shutil.rmtree(self.temp_dir, ignore_errors=True)
+
+    def _table(self, name, options):
+        catalog = CatalogFactory.create(
+            {'warehouse': os.path.join(self.temp_dir, 'warehouse')})
+        catalog.create_database('default', True)
+        ident = f'default.{name}'
+        catalog.create_table(ident, Schema.from_pyarrow_schema(PA_SCHEMA, 
options=options), False)
+        table = catalog.get_table(ident)
+        wb = table.new_batch_write_builder()
+        w = wb.new_write()
+        w.write_arrow(pa.Table.from_pydict(
+            {'a': list(range(10)), 'b': [f'row{i}' for i in range(10)]},
+            schema=PA_SCHEMA))
+        wb.new_commit().commit(w.prepare_commit())
+        w.close()
+        return table
+
+    def _read(self, rb, scan):
+        reader = rb.new_read()
+        return reader.to_arrow(scan.plan().splits())
+
+    def test_shard_with_predicate_returns_only_shard_rows(self):
+        for file_format in ('parquet', 'orc', 'avro'):
+            with self.subTest(file_format=file_format):
+                table = self._table(
+                    f'shard_pred_{file_format}', {'file.format': file_format})
+                predicate = PredicateBuilder(table.schema().fields).is_in('a', 
[2, 8])
+                rb = table.new_read_builder().with_filter(predicate)
+
+                # The file's rows 0-4 belong to shard 0 and rows 5-9 to shard 
1.
+                shard0 = self._read(rb, rb.new_scan().with_shard(0, 2))
+                self.assertEqual([2], shard0.column('a').to_pylist())
+                shard1 = self._read(rb, rb.new_scan().with_shard(1, 2))
+                self.assertEqual([8], shard1.column('a').to_pylist())
+
+                result = self._read(rb, rb.new_scan().with_slice(0, 5))
+                # row 8 lies outside the slice [0, 5)
+                self.assertEqual([2], result.column('a').to_pylist())
+
+    def test_row_tracking_predicate_keeps_physical_row_ids(self):
+        for file_format in ('parquet', 'orc', 'avro'):
+            with self.subTest(file_format=file_format):
+                table = self._table(
+                    f'rt_pred_{file_format}',
+                    {'row-tracking.enabled': 'true', 'file.format': 
file_format})
+                predicate = 
PredicateBuilder(table.schema().fields).greater_or_equal('a', 5)
+                rb = (table.new_read_builder().with_filter(predicate)
+                      .with_projection(['a', 'b', '_ROW_ID']))
+                result = self._read(rb, rb.new_scan())
+                self.assertEqual([5, 6, 7, 8, 9], 
result.column('a').to_pylist())
+                self.assertEqual([5, 6, 7, 8, 9], 
result.column('_ROW_ID').to_pylist())
+
+    @unittest.skipUnless(HAS_VORTEX, "vortex package not installed")
+    def test_vortex_shard_predicate_keeps_row_ids(self):
+        table = self._table(
+            'vortex_shard', {'row-tracking.enabled': 'true', 'file.format': 
'vortex'})
+        predicate = 
PredicateBuilder(table.schema().fields).greater_or_equal('a', 3)
+        rb = (table.new_read_builder().with_filter(predicate)
+              .with_projection(['a', 'b', '_ROW_ID']))
+
+        shard0 = self._read(rb, rb.new_scan().with_shard(0, 2))
+        self.assertEqual([3, 4], shard0.column('a').to_pylist())
+        self.assertEqual([3, 4], shard0.column('_ROW_ID').to_pylist())
+
+        shard1 = self._read(rb, rb.new_scan().with_shard(1, 2))
+        self.assertEqual([5, 6, 7, 8, 9], shard1.column('a').to_pylist())
+        self.assertEqual([5, 6, 7, 8, 9], shard1.column('_ROW_ID').to_pylist())
+
+
+if __name__ == '__main__':
+    unittest.main()

Reply via email to