|
| 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 | +from java_based_implementation.java_gateway import get_gateway |
| 20 | +from java_based_implementation.util.java_utils import to_j_catalog_context |
| 21 | +from paimon_python_api import catalog, read_builder, table_scan, split, table_read |
| 22 | +from paimon_python_api import table |
| 23 | +from pyarrow import RecordBatchReader |
| 24 | +from typing import List |
| 25 | +from typing_extensions import Self |
| 26 | + |
| 27 | + |
| 28 | +class Catalog(catalog.Catalog): |
| 29 | + |
| 30 | + def __init__(self, j_catalog): |
| 31 | + self._j_catalog = j_catalog |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + def create(catalog_context: dict) -> 'Catalog': |
| 35 | + j_catalog_context = to_j_catalog_context(catalog_context) |
| 36 | + gateway = get_gateway() |
| 37 | + j_catalog = gateway.jvm.CatalogFactory.createCatalog(j_catalog_context) |
| 38 | + return Catalog(j_catalog) |
| 39 | + |
| 40 | + def get_table(self, identifier: tuple) -> 'Table': |
| 41 | + gateway = get_gateway() |
| 42 | + j_identifier = gateway.jvm.Identifier.fromString(identifier) |
| 43 | + j_table = self._j_catalog.getTable(j_identifier) |
| 44 | + return Table(j_table) |
| 45 | + |
| 46 | + |
| 47 | +class Table(table.Table): |
| 48 | + |
| 49 | + def __init__(self, j_table): |
| 50 | + self._j_table = j_table |
| 51 | + |
| 52 | + def new_read_builder(self) -> 'ReadBuilder': |
| 53 | + j_read_builder = self._j_table.newReadBuilder() |
| 54 | + return ReadBuilder(j_read_builder) |
| 55 | + |
| 56 | + |
| 57 | +class ReadBuilder(read_builder.ReadBuilder): |
| 58 | + |
| 59 | + def __init__(self, j_read_builder): |
| 60 | + self._j_read_builder = j_read_builder |
| 61 | + |
| 62 | + def with_projection(self, projection: List[List[int]]) -> Self: |
| 63 | + self._j_read_builder.withProjection(projection) |
| 64 | + return self |
| 65 | + |
| 66 | + def with_limit(self, limit: int) -> Self: |
| 67 | + self._j_read_builder.withLimit(limit) |
| 68 | + return self |
| 69 | + |
| 70 | + def new_scan(self) -> 'TableScan': |
| 71 | + j_table_scan = self._j_read_builder.newScan() |
| 72 | + return TableScan(j_table_scan) |
| 73 | + |
| 74 | + def new_read(self) -> 'TableRead': |
| 75 | + # TODO |
| 76 | + pass |
| 77 | + |
| 78 | + |
| 79 | +class TableScan(table_scan.TableScan): |
| 80 | + |
| 81 | + def __init__(self, j_table_scan): |
| 82 | + self._j_table_scan = j_table_scan |
| 83 | + |
| 84 | + def plan(self) -> 'Plan': |
| 85 | + j_plan = self._j_table_scan.plan() |
| 86 | + j_splits = j_plan.splits() |
| 87 | + return Plan(j_splits) |
| 88 | + |
| 89 | + |
| 90 | +class Plan(table_scan.Plan): |
| 91 | + |
| 92 | + def __init__(self, j_splits): |
| 93 | + self._j_splits = j_splits |
| 94 | + |
| 95 | + def splits(self) -> List['Split']: |
| 96 | + return list(map(lambda s: Split(s), self._j_splits)) |
| 97 | + |
| 98 | + |
| 99 | +class Split(split.Split): |
| 100 | + |
| 101 | + def __init__(self, j_split): |
| 102 | + self._j_split = j_split |
| 103 | + |
| 104 | + def to_j_split(self): |
| 105 | + return self._j_split |
| 106 | + |
| 107 | + |
| 108 | +class TableRead(table_read.TableRead): |
| 109 | + |
| 110 | + def create_reader(self, split: Split) -> RecordBatchReader: |
| 111 | + # TODO |
| 112 | + pass |
0 commit comments