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 cc4cad038c [python] Skip redundant identity output projections (#11169)
cc4cad038c is described below
commit cc4cad038c0df90740e2becac49aa86bdcd01399
Author: zhigang <[email protected]>
AuthorDate: Mon Nov 16 13:43:35 2026 +0800
[python] Skip redundant identity output projections (#11169)
---
paimon-python/pypaimon/read/table_read.py | 18 ++++++++++++++----
.../pypaimon/tests/projection_predicate_index_test.py | 17 +++++++++++++++++
.../pypaimon/tests/read_builder_explain_test.py | 4 ++++
.../tests/test_read_builder_nested_projection.py | 16 ++++++++++++++++
4 files changed, 51 insertions(+), 4 deletions(-)
diff --git a/paimon-python/pypaimon/read/table_read.py
b/paimon-python/pypaimon/read/table_read.py
index 74a52f32a4..175a645597 100644
--- a/paimon-python/pypaimon/read/table_read.py
+++ b/paimon-python/pypaimon/read/table_read.py
@@ -63,8 +63,17 @@ class TableRead:
self.predicate = predicate
self.read_type = read_type
self.output_projection = output_projection
- adapter = (self._reader_adapter() if output_projection is not None
- else (read_type, [[field.name] for field in read_type]))
+ adapter = (read_type, [[field.name] for field in read_type])
+ if output_projection is not None:
+ plain_adapter = reader_adapter(read_type,
self._table_read_fields())
+ # A pruned ROW or selected MAP can still need a flat adapter.
+ # Elide only when the reader already supplies the complete output.
+ if (not output_projection.named
+ and output_projection.columns == [(field.name,
[field.name]) for field in read_type]
+ and plain_adapter == adapter):
+ self.output_projection = None
+ else:
+ adapter = self._reader_adapter(plain_adapter)
self._adapter_read_type = adapter[0]
physical_names = {field.name for field in self._table_read_fields()}
self.predicate = self._bind_predicate_to_adapter(self.predicate,
*adapter, physical_names)
@@ -219,8 +228,9 @@ class TableRead:
_, paths = self._reader_adapter()
return paths if any(len(path) > 1 for path in paths) else None
- def _reader_adapter(self):
- adapter = reader_adapter(self.read_type, self._table_read_fields())
+ def _reader_adapter(self, adapter=None):
+ if adapter is None:
+ adapter = reader_adapter(self.read_type, self._table_read_fields())
if (self.predicate is None or self.output_projection is None
or self.output_projection.named):
return adapter
diff --git a/paimon-python/pypaimon/tests/projection_predicate_index_test.py
b/paimon-python/pypaimon/tests/projection_predicate_index_test.py
index b319090bdb..32b43a458e 100644
--- a/paimon-python/pypaimon/tests/projection_predicate_index_test.py
+++ b/paimon-python/pypaimon/tests/projection_predicate_index_test.py
@@ -203,6 +203,23 @@ class ProjectionPredicateIndexTest(unittest.TestCase):
self.assertEqual(actual.column_names, ['id'])
self.assertEqual(actual.column('id').to_pylist(), [1])
+ def
test_top_level_projection_preserves_order_duplicates_and_empty_rows(self):
+ table = self._populate_append('test_top_level_projection')
+ for columns in (['name', 'id'], ['id', 'id'], []):
+ with self.subTest(columns=columns):
+ builder = table.new_read_builder().with_projection(columns)
+ builder.with_filter('value >= 20')
+ read = builder.new_read()
+ splits = builder.new_scan().plan().splits()
+ expected = self.data.slice(1).select(columns)
+ with read.to_arrow_batch_reader(splits) as reader:
+ actual = reader.read_all()
+ self.assertEqual(actual.num_rows, expected.num_rows)
+ self.assertEqual(actual.schema, pa.schema([
+ (name, self.pa_schema.field(name).type) for name in
columns]))
+ for index in range(expected.num_columns):
+
self.assertTrue(actual.column(index).equals(expected.column(index)))
+
def test_append_only_filter_with_projection_unchanged(self):
table = self._populate_append('test_append_proj_filter')
diff --git a/paimon-python/pypaimon/tests/read_builder_explain_test.py
b/paimon-python/pypaimon/tests/read_builder_explain_test.py
index 59d1063861..67b234723f 100644
--- a/paimon-python/pypaimon/tests/read_builder_explain_test.py
+++ b/paimon-python/pypaimon/tests/read_builder_explain_test.py
@@ -131,6 +131,10 @@ class ReadBuilderExplainTest(unittest.TestCase):
self.assertIsNone(result.file_skipping)
self.assertIsNone(result.predicate)
+ read = rb.with_projection(['val', 'id']).new_read()
+ self.assertIsNone(read.output_projection)
+ self.assertEqual(['val', 'id'], rb.explain().projection)
+
# ---- 2. PK + partition + bucket pruning ----------------------------
@pytest.mark.python_plan
diff --git
a/paimon-python/pypaimon/tests/test_read_builder_nested_projection.py
b/paimon-python/pypaimon/tests/test_read_builder_nested_projection.py
index 2c4d128044..81d3260262 100644
--- a/paimon-python/pypaimon/tests/test_read_builder_nested_projection.py
+++ b/paimon-python/pypaimon/tests/test_read_builder_nested_projection.py
@@ -27,6 +27,8 @@ from pypaimon import CatalogFactory, Schema
from pypaimon.execution.python.read.table_read import PythonTableRead
from pypaimon.read.datasource.split_provider import (
CatalogSplitProvider, PreResolvedSplitProvider)
+from pypaimon.read.read_type import OutputProjection
+from pypaimon.read.table_read import TableRead
from pypaimon.read.read_builder import ReadBuilder
from pypaimon.read.stream_read_builder import StreamReadBuilder
from pypaimon.execution.python.read.table_read import _RemainingRows
@@ -274,6 +276,20 @@ class ReadBuilderProjectionStateTest(_ReadBuilderTestBase):
# No nested paths derived; only names are stored.
self.assertIsNone(rb._nested_name_paths())
+ def test_direct_read_elides_identity_but_retains_nested_adapter(self):
+ fields = [self.table.fields[2], self.table.fields[0]]
+ projection = OutputProjection([(field.name, [field.name]) for field in
fields])
+ read = TableRead(self.table, None, fields,
output_projection=projection)
+ batch = pa.record_batch([pa.array(['x']), pa.array([7],
type=pa.int64())], names=['val', 'pk'])
+ self.assertIs(read._project_batch_to_output(batch), batch)
+
+ builder =
self.table.new_read_builder().with_projection(['mv.latest_version'])
+ fields = builder.read_type()
+ projection = OutputProjection([('mv', ['mv'])])
+ read = TableRead(self.table, None, fields,
output_projection=projection)
+ self.assertEqual(projection, read.output_projection)
+ self.assertEqual([['mv', 'latest_version']], read.nested_name_paths)
+
def test_named_ordinary_columns_keep_aliases_without_native_read(self):
read = self.table.new_read_builder().with_projection({
'value': 'val', 'key': 'pk'}).new_read()