| # 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 | |
| import pyarrow as pa | |
| import pyarrow.parquet as pq | |
| from pypaimon import CatalogFactory, Schema | |
| class _AppendOnlyNestedBase(unittest.TestCase): | |
| """Append-only table whose ``mv`` column is a nested struct, used to | |
| exercise file-level Parquet/ORC pushdown of nested projection.""" | |
| @classmethod | |
| def setUpClass(cls): | |
| cls.tempdir = tempfile.mkdtemp() | |
| cls.warehouse = os.path.join(cls.tempdir, 'warehouse') | |
| cls.catalog = CatalogFactory.create({'warehouse': cls.warehouse}) | |
| cls.catalog.create_database('default', False) | |
| cls.pa_schema = pa.schema([ | |
| ('id', pa.int64()), | |
| ('mv', pa.struct([ | |
| ('latest_version', pa.int64()), | |
| ('latest_value', pa.string()), | |
| ])), | |
| ('val', pa.string()), | |
| ]) | |
| cls.rows = [ | |
| {'id': 1, 'mv': {'latest_version': 100, 'latest_value': 'a'}, 'val': 'x'}, | |
| {'id': 2, 'mv': {'latest_version': 200, 'latest_value': 'b'}, 'val': 'y'}, | |
| {'id': 3, 'mv': {'latest_version': 300, 'latest_value': 'c'}, 'val': 'z'}, | |
| ] | |
| @classmethod | |
| def tearDownClass(cls): | |
| shutil.rmtree(cls.tempdir, ignore_errors=True) | |
| def _create_table(self, name: str, file_format: str = 'parquet', rows=None): | |
| identifier = 'default.{}'.format(name) | |
| schema = Schema.from_pyarrow_schema( | |
| self.pa_schema, | |
| options={'bucket': '-1', 'file.format': file_format}, | |
| ) | |
| self.catalog.create_table(identifier, schema, False) | |
| table = self.catalog.get_table(identifier) | |
| wb = table.new_batch_write_builder() | |
| w = wb.new_write() | |
| w.write_arrow(pa.Table.from_pylist( | |
| self.rows if rows is None else rows, | |
| schema=self.pa_schema, | |
| )) | |
| wb.new_commit().commit(w.prepare_commit()) | |
| w.close() | |
| return table | |
| class AppendOnlyNestedParquetTest(_AppendOnlyNestedBase): | |
| """Parquet path uses PyArrow's dict-form scanner with ``ds.field(*path)`` | |
| to push the nested column read into the engine.""" | |
| def test_dotted_name_returns_just_the_leaf(self): | |
| table = self._create_table('ao_dotted_leaf') | |
| rb = table.new_read_builder().with_projection(['mv.latest_version']) | |
| got = rb.new_read().to_arrow(rb.new_scan().plan().splits()).to_pylist() | |
| self.assertEqual( | |
| got, | |
| [{'mv_latest_version': 100}, | |
| {'mv_latest_version': 200}, | |
| {'mv_latest_version': 300}]) | |
| def test_nested_leaf_preserves_parent_null(self): | |
| table = self._create_table('ao_nullable_parent', rows=[ | |
| self.rows[0], | |
| {'id': 2, 'mv': None, 'val': 'y'}, | |
| ]) | |
| rb = table.new_read_builder().with_projection(['mv.latest_version']) | |
| got = rb.new_read().to_arrow(rb.new_scan().plan().splits()) | |
| self.assertEqual([100, None], got.column(0).to_pylist()) | |
| def test_required_leaf_under_nullable_row_can_be_exported(self): | |
| schema = pa.schema( | |
| [ | |
| ("r", pa.struct([pa.field("x", pa.int64(), nullable=False)])), | |
| ] | |
| ) | |
| identifier = "default.nullable_row_required_leaf" | |
| self.catalog.create_table( | |
| identifier, | |
| Schema.from_pyarrow_schema(schema, options={"bucket": "-1"}), | |
| False, | |
| ) | |
| table = self.catalog.get_table(identifier) | |
| wb = table.new_batch_write_builder() | |
| writer = wb.new_write() | |
| try: | |
| writer.write_arrow( | |
| pa.Table.from_pylist([{"r": None}, {"r": {"x": 7}}], schema=schema) | |
| ) | |
| wb.new_commit().commit(writer.prepare_commit()) | |
| finally: | |
| writer.close() | |
| rb = table.new_read_builder().with_projection(["r.x"]) | |
| result = rb.new_read().to_arrow(rb.new_scan().plan().splits()) | |
| self.assertEqual([None, 7], result.column(0).to_pylist()) | |
| sink = pa.BufferOutputStream() | |
| pq.write_table(result, sink) | |
| restored = pq.read_table(pa.BufferReader(sink.getvalue())) | |
| self.assertTrue(restored.schema.field("r_x").nullable) | |
| self.assertEqual(result.to_pydict(), restored.to_pydict()) | |
| def test_mixed_nested_and_top_level_preserves_order(self): | |
| table = self._create_table('ao_mixed_order') | |
| rb = table.new_read_builder().with_projection( | |
| ['mv.latest_version', 'val', 'mv.latest_value']) | |
| got = rb.new_read().to_arrow(rb.new_scan().plan().splits()).to_pylist() | |
| self.assertEqual( | |
| got, | |
| [{'mv_latest_version': 100, 'val': 'x', 'mv_latest_value': 'a'}, | |
| {'mv_latest_version': 200, 'val': 'y', 'mv_latest_value': 'b'}, | |
| {'mv_latest_version': 300, 'val': 'z', 'mv_latest_value': 'c'}]) | |
| def test_dotted_top_level_field_kept(self): | |
| pa_schema = pa.schema([ | |
| ('id', pa.int64()), | |
| ('media.left', pa.string()), | |
| ]) | |
| identifier = 'default.ao_dotted_top_level' | |
| self.catalog.create_table( | |
| identifier, | |
| Schema.from_pyarrow_schema(pa_schema, options={'bucket': '-1'}), | |
| False) | |
| table = self.catalog.get_table(identifier) | |
| wb = table.new_batch_write_builder() | |
| w = wb.new_write() | |
| w.write_arrow(pa.Table.from_pylist( | |
| [{'id': 1, 'media.left': 'hello'}], schema=pa_schema)) | |
| wb.new_commit().commit(w.prepare_commit()) | |
| w.close() | |
| rb = table.new_read_builder().with_projection(['id', 'media.left']) | |
| got = rb.new_read().to_arrow(rb.new_scan().plan().splits()).to_pylist() | |
| self.assertEqual(got, [{'id': 1, 'media.left': 'hello'}]) | |
| def test_row_path_precedes_dotted_top_level_prefix(self): | |
| pa_schema = pa.schema([ | |
| ('a', pa.struct([ | |
| ('b', pa.struct([('c', pa.int64())])), | |
| ])), | |
| ('a.b', pa.struct([ | |
| ('c', pa.int64()), | |
| ('d', pa.int64()), | |
| ])), | |
| ('id', pa.int64()), | |
| ]) | |
| identifier = 'default.ao_row_path_precedence' | |
| self.catalog.create_table( | |
| identifier, | |
| Schema.from_pyarrow_schema(pa_schema, options={'bucket': '-1'}), | |
| False) | |
| table = self.catalog.get_table(identifier) | |
| wb = table.new_batch_write_builder() | |
| w = wb.new_write() | |
| w.write_arrow(pa.Table.from_arrays([ | |
| pa.array([{'b': {'c': 1}}], type=pa_schema.field('a').type), | |
| pa.array([{'c': 99, 'd': 88}], | |
| type=pa_schema.field('a.b').type), | |
| pa.array([7], type=pa.int64()), | |
| ], schema=pa_schema)) | |
| wb.new_commit().commit(w.prepare_commit()) | |
| w.close() | |
| rb = table.new_read_builder().with_projection([ | |
| 'a.b.c', 'a.b.d', 'id', | |
| ]) | |
| got = rb.new_read().to_arrow(rb.new_scan().plan().splits()) | |
| self.assertEqual( | |
| {'a_b_c': [1], 'a.b_d': [88], 'id': [7]}, | |
| got.to_pydict(), | |
| ) | |
| def test_unknown_dotted_name_silently_skipped(self): | |
| pa_schema = pa.schema([ | |
| ('id', pa.int64()), | |
| ('plain', pa.string()), | |
| ]) | |
| identifier = 'default.ao_unknown_dotted' | |
| self.catalog.create_table( | |
| identifier, | |
| Schema.from_pyarrow_schema(pa_schema, options={'bucket': '-1'}), | |
| False) | |
| table = self.catalog.get_table(identifier) | |
| wb = table.new_batch_write_builder() | |
| w = wb.new_write() | |
| w.write_arrow(pa.Table.from_pylist( | |
| [{'id': 1, 'plain': 'x'}], schema=pa_schema)) | |
| wb.new_commit().commit(w.prepare_commit()) | |
| w.close() | |
| rb = table.new_read_builder().with_projection(['id', 'nonexistent.foo']) | |
| got = rb.new_read().to_arrow(rb.new_scan().plan().splits()).to_pylist() | |
| self.assertEqual(got, [{'id': 1}]) | |
| def test_top_level_only_projection_unchanged(self): | |
| """A projection without dots must keep the existing top-level | |
| path — file-level pushdown still asks for plain column names, | |
| no dict-form scanner.""" | |
| table = self._create_table('ao_top_level_unchanged') | |
| rb = table.new_read_builder().with_projection(['val', 'id']) | |
| got = rb.new_read().to_arrow(rb.new_scan().plan().splits()).to_pylist() | |
| self.assertEqual( | |
| got, | |
| [{'val': 'x', 'id': 1}, | |
| {'val': 'y', 'id': 2}, | |
| {'val': 'z', 'id': 3}]) | |
| def test_partitioned_table_with_nested_projection(self): | |
| """Partition-aware reads have a separate path-mapping helper from | |
| the non-partitioned fast path; regress the case where it dropped | |
| non-nested top-level columns alongside the projected leaf.""" | |
| identifier = 'default.ao_partitioned' | |
| pa_schema = pa.schema([ | |
| ('part', pa.string()), | |
| ('id', pa.int64()), | |
| ('mv', pa.struct([ | |
| ('latest_version', pa.int64()), | |
| ('latest_value', pa.string()), | |
| ])), | |
| ('val', pa.string()), | |
| ]) | |
| schema = Schema.from_pyarrow_schema( | |
| pa_schema, | |
| partition_keys=['part'], | |
| options={'bucket': '-1', 'file.format': 'parquet'}, | |
| ) | |
| self.catalog.create_table(identifier, schema, False) | |
| table = self.catalog.get_table(identifier) | |
| wb = table.new_batch_write_builder() | |
| w = wb.new_write() | |
| w.write_arrow(pa.Table.from_pylist([ | |
| {'part': 'A', 'id': 1, 'mv': {'latest_version': 100, 'latest_value': 'a'}, 'val': 'x'}, | |
| {'part': 'B', 'id': 2, 'mv': {'latest_version': 200, 'latest_value': 'b'}, 'val': 'y'}, | |
| ], schema=pa_schema)) | |
| wb.new_commit().commit(w.prepare_commit()) | |
| w.close() | |
| # Mixed projection: nested leaf, a non-partition top-level column, | |
| # and the partition column itself. | |
| rb = table.new_read_builder().with_projection( | |
| ['part', 'mv.latest_version', 'val']) | |
| got = rb.new_read().to_arrow(rb.new_scan().plan().splits()).to_pylist() | |
| got_sorted = sorted(got, key=lambda r: r['part']) | |
| self.assertEqual( | |
| got_sorted, | |
| [{'part': 'A', 'mv_latest_version': 100, 'val': 'x'}, | |
| {'part': 'B', 'mv_latest_version': 200, 'val': 'y'}]) | |
| def test_filter_on_projected_nested_leaf(self): | |
| """A predicate on a projected nested leaf must actually filter rows. | |
| The read widens the projection to the top-level struct, which drops | |
| the leaf predicate from push-down (its path is absent from the read | |
| fields); without re-applying it after the leaves are extracted, every | |
| row leaks through.""" | |
| table = self._create_table('ao_nested_leaf_filter') | |
| rb = table.new_read_builder().with_projection(['id', 'mv.latest_version']) | |
| pred = rb.new_predicate_builder().greater_than('mv_latest_version', 150) | |
| rb = rb.with_filter(pred) | |
| got = rb.new_read().to_arrow(rb.new_scan().plan().splits()).to_pylist() | |
| got = sorted(got, key=lambda r: r['id']) | |
| self.assertEqual( | |
| got, | |
| [{'id': 2, 'mv_latest_version': 200}, | |
| {'id': 3, 'mv_latest_version': 300}]) | |
| def test_avro_nested_projection_python_fallback(self): | |
| """Avro has no native nested column pruning; the reader walks | |
| each fastavro record dict by path and assembles the column | |
| client-side.""" | |
| table = self._create_table('ao_avro_nested', file_format='avro') | |
| rb = table.new_read_builder().with_projection(['mv.latest_version', 'val']) | |
| got = rb.new_read().to_arrow(rb.new_scan().plan().splits()).to_pylist() | |
| self.assertEqual( | |
| got, | |
| [{'mv_latest_version': 100, 'val': 'x'}, | |
| {'mv_latest_version': 200, 'val': 'y'}, | |
| {'mv_latest_version': 300, 'val': 'z'}]) | |
| def test_avro_top_level_projection_unchanged(self): | |
| """Top-level-only projection on Avro stays on the existing | |
| ``record.get(name)`` fast path.""" | |
| table = self._create_table('ao_avro_top', file_format='avro') | |
| rb = table.new_read_builder().with_projection(['val', 'id']) | |
| got = rb.new_read().to_arrow(rb.new_scan().plan().splits()).to_pylist() | |
| self.assertEqual( | |
| got, | |
| [{'val': 'x', 'id': 1}, | |
| {'val': 'y', 'id': 2}, | |
| {'val': 'z', 'id': 3}]) | |
| class PrimaryKeyNestedTest(_AppendOnlyNestedBase): | |
| """PK tables go through the merge reader once a split is no longer | |
| raw-convertible (multiple overlapping commits on the same key). The | |
| merge function still needs full ROW sub-structures, so the read | |
| splits inner = full-ROW from outer = flat sub-paths via an | |
| OuterProjectionRecordReader.""" | |
| def _create_pk_table(self, name: str, file_format: str = 'parquet'): | |
| identifier = 'default.{}'.format(name) | |
| schema = Schema.from_pyarrow_schema( | |
| self.pa_schema, | |
| primary_keys=['id'], | |
| options={'bucket': '1', 'file.format': file_format}, | |
| ) | |
| self.catalog.create_table(identifier, schema, False) | |
| table = self.catalog.get_table(identifier) | |
| # Two overlapping commits force the split off the raw-convertible | |
| # fast path into the merge reader. | |
| for batch in (self.rows, self.rows): | |
| wb = table.new_batch_write_builder() | |
| w = wb.new_write() | |
| w.write_arrow(pa.Table.from_pylist(batch, schema=self.pa_schema)) | |
| wb.new_commit().commit(w.prepare_commit()) | |
| w.close() | |
| return table | |
| def _create_pk_raw_table(self, name: str, file_format: str = 'parquet'): | |
| """Single commit keeps the split raw-convertible, so the read stays on | |
| the RawFileSplitRead fast path rather than the merge reader.""" | |
| identifier = 'default.{}'.format(name) | |
| schema = Schema.from_pyarrow_schema( | |
| self.pa_schema, | |
| primary_keys=['id'], | |
| options={'bucket': '1', 'file.format': file_format}, | |
| ) | |
| self.catalog.create_table(identifier, schema, False) | |
| table = self.catalog.get_table(identifier) | |
| wb = table.new_batch_write_builder() | |
| w = wb.new_write() | |
| w.write_arrow(pa.Table.from_pylist(self.rows, schema=self.pa_schema)) | |
| wb.new_commit().commit(w.prepare_commit()) | |
| w.close() | |
| return table | |
| def _read_arrow(self, table, projection): | |
| rb = table.new_read_builder().with_projection(projection) | |
| splits = rb.new_scan().plan().splits() | |
| return rb.new_read().to_arrow(splits) | |
| def test_raw_convertible_filter_on_projected_nested_leaf(self): | |
| """PK raw-convertible split also widens nested projection and so drops | |
| the leaf predicate from push-down. The filter must be re-applied on the | |
| extracted leaves; otherwise all rows are returned (reviewer repro).""" | |
| table = self._create_pk_raw_table('pk_raw_nested_leaf_filter') | |
| rb = table.new_read_builder().with_projection(['id', 'mv.latest_version']) | |
| pred = rb.new_predicate_builder().greater_than('mv_latest_version', 150) | |
| rb = rb.with_filter(pred) | |
| arrow = rb.new_read().to_arrow(rb.new_scan().plan().splits()) | |
| rows = sorted(zip( | |
| arrow.column('id').to_pylist(), | |
| arrow.column('mv_latest_version').to_pylist())) | |
| self.assertEqual(rows, [(2, 200), (3, 300)]) | |
| def test_extracts_single_nested_leaf(self): | |
| table = self._create_pk_table('pk_nested_single') | |
| arrow = self._read_arrow(table, ['mv.latest_version']) | |
| self.assertEqual(arrow.column_names, ['mv_latest_version']) | |
| versions = sorted(arrow.column('mv_latest_version').to_pylist()) | |
| self.assertEqual(versions, [100, 200, 300]) | |
| def test_multiple_sub_paths_under_same_struct(self): | |
| table = self._create_pk_table('pk_nested_double') | |
| arrow = self._read_arrow( | |
| table, ['mv.latest_version', 'mv.latest_value']) | |
| self.assertEqual(arrow.column_names, ['mv_latest_version', 'mv_latest_value']) | |
| pairs = sorted(zip( | |
| arrow.column('mv_latest_version').to_pylist(), | |
| arrow.column('mv_latest_value').to_pylist())) | |
| self.assertEqual(pairs, [(100, 'a'), (200, 'b'), (300, 'c')]) | |
| def test_mixed_nested_and_top_level_preserves_order(self): | |
| table = self._create_pk_table('pk_nested_mixed') | |
| arrow = self._read_arrow( | |
| table, ['id', 'mv.latest_version', 'val']) | |
| self.assertEqual( | |
| arrow.column_names, ['id', 'mv_latest_version', 'val']) | |
| rows = sorted(zip( | |
| arrow.column('id').to_pylist(), | |
| arrow.column('mv_latest_version').to_pylist(), | |
| arrow.column('val').to_pylist())) | |
| self.assertEqual(rows, [(1, 100, 'x'), (2, 200, 'y'), (3, 300, 'z')]) | |
| def test_dotted_top_level_field_kept(self): | |
| pa_schema = pa.schema([ | |
| ('id', pa.int64()), | |
| ('media.left', pa.string()), | |
| ]) | |
| identifier = 'default.pk_dotted_top_level' | |
| schema = Schema.from_pyarrow_schema( | |
| pa_schema, primary_keys=['id'], options={'bucket': '1'}) | |
| self.catalog.create_table(identifier, schema, False) | |
| table = self.catalog.get_table(identifier) | |
| rows = [{'id': 1, 'media.left': 'hello'}] | |
| for _ in range(2): | |
| wb = table.new_batch_write_builder() | |
| w = wb.new_write() | |
| w.write_arrow(pa.Table.from_pylist(rows, schema=pa_schema)) | |
| wb.new_commit().commit(w.prepare_commit()) | |
| w.close() | |
| rb = table.new_read_builder().with_projection(['id', 'media.left']) | |
| got = rb.new_read().to_arrow(rb.new_scan().plan().splits()).to_pylist() | |
| self.assertEqual(got, [{'id': 1, 'media.left': 'hello'}]) | |
| def test_merge_filter_on_projected_nested_leaf(self): | |
| """Non-raw-convertible PK splits go through the merge reader, which | |
| widens the nested projection to the full ROW and so also drops the leaf | |
| predicate from push-down. The filter must be re-applied on the extracted | |
| leaves above the merge; otherwise all rows are returned.""" | |
| table = self._create_pk_table('pk_merge_nested_leaf_filter') | |
| rb = table.new_read_builder().with_projection(['id', 'mv.latest_version']) | |
| pred = rb.new_predicate_builder().greater_than('mv_latest_version', 150) | |
| rb = rb.with_filter(pred) | |
| arrow = rb.new_read().to_arrow(rb.new_scan().plan().splits()) | |
| rows = sorted(zip( | |
| arrow.column('id').to_pylist(), | |
| arrow.column('mv_latest_version').to_pylist())) | |
| self.assertEqual(rows, [(2, 200), (3, 300)]) | |
| def test_avro_extracts_single_nested_leaf(self): | |
| # Avro PK reads resolve DataFields through ``full_fields_map`` which | |
| # historically only covered merge-internal aliases; without the | |
| # alias-safe fix, this projection would raise ``KeyError: 'id'``. | |
| table = self._create_pk_table('pk_avro_nested_single', file_format='avro') | |
| arrow = self._read_arrow(table, ['mv.latest_version']) | |
| self.assertEqual(arrow.column_names, ['mv_latest_version']) | |
| versions = sorted(arrow.column('mv_latest_version').to_pylist()) | |
| self.assertEqual(versions, [100, 200, 300]) | |
| if __name__ == '__main__': | |
| unittest.main() |