|
| 1 | +################################################################################ |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +################################################################################ |
| 18 | + |
| 19 | +import os |
| 20 | +import shutil |
| 21 | +import tempfile |
| 22 | +import unittest |
| 23 | + |
| 24 | +import pyarrow as pa |
| 25 | + |
| 26 | +from pypaimon import CatalogFactory |
| 27 | +from pypaimon import Schema |
| 28 | +from pypaimon.read.split import Split |
| 29 | + |
| 30 | + |
| 31 | +class ReaderPredicateTest(unittest.TestCase): |
| 32 | + @classmethod |
| 33 | + def setUpClass(cls): |
| 34 | + cls.tempdir = tempfile.mkdtemp() |
| 35 | + cls.warehouse = os.path.join(cls.tempdir, 'warehouse') |
| 36 | + cls.catalog = CatalogFactory.create({ |
| 37 | + 'warehouse': cls.warehouse |
| 38 | + }) |
| 39 | + cls.catalog.create_database('default', False) |
| 40 | + |
| 41 | + cls.pa_schema = pa.schema([ |
| 42 | + ('a', pa.int64()), |
| 43 | + ('pt', pa.int64()) |
| 44 | + ]) |
| 45 | + schema = Schema.from_pyarrow_schema(cls.pa_schema, partition_keys=['pt']) |
| 46 | + cls.catalog.create_table('default.test_reader_predicate', schema, False) |
| 47 | + cls.table = cls.catalog.get_table('default.test_reader_predicate') |
| 48 | + |
| 49 | + data1 = pa.Table.from_pydict({ |
| 50 | + 'a': [1, 2], |
| 51 | + 'pt': [1001, 1002]}, schema=cls.pa_schema) |
| 52 | + write_builder = cls.table.new_batch_write_builder() |
| 53 | + table_write = write_builder.new_write() |
| 54 | + table_commit = write_builder.new_commit() |
| 55 | + table_write.write_arrow(data1) |
| 56 | + table_commit.commit(table_write.prepare_commit()) |
| 57 | + table_write.close() |
| 58 | + table_commit.close() |
| 59 | + |
| 60 | + data2 = pa.Table.from_pydict({ |
| 61 | + 'a': [3, 4], |
| 62 | + 'pt': [1003, 1004]}, schema=cls.pa_schema) |
| 63 | + write_builder = cls.table.new_batch_write_builder() |
| 64 | + table_write = write_builder.new_write() |
| 65 | + table_commit = write_builder.new_commit() |
| 66 | + table_write.write_arrow(data2) |
| 67 | + table_commit.commit(table_write.prepare_commit()) |
| 68 | + table_write.close() |
| 69 | + table_commit.close() |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def tearDownClass(cls): |
| 73 | + shutil.rmtree(cls.tempdir, ignore_errors=True) |
| 74 | + |
| 75 | + def test_partition_predicate(self): |
| 76 | + predicate_builder = self.table.new_read_builder().new_predicate_builder() |
| 77 | + predicate = predicate_builder.equal('pt', 1003) |
| 78 | + read_builder = self.table.new_read_builder() |
| 79 | + read_builder.with_filter(predicate) |
| 80 | + splits: list[Split] = read_builder.new_scan().plan().splits() |
| 81 | + self.assertEqual(len(splits), 1) |
| 82 | + self.assertEqual(splits[0].partition.to_dict().get("pt"), 1003) |
0 commit comments