|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | + |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +import pyarrow as pa |
| 22 | +import pytest |
| 23 | +from datafusion import SessionContext |
| 24 | + |
| 25 | +from pyiceberg.catalog import Catalog, load_catalog |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope="session") |
| 29 | +def warehouse(tmp_path_factory: pytest.TempPathFactory) -> Path: |
| 30 | + return tmp_path_factory.mktemp("warehouse") |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope="session") |
| 34 | +def catalog(warehouse: Path) -> Catalog: |
| 35 | + catalog = load_catalog( |
| 36 | + "default", |
| 37 | + uri=f"sqlite:///{warehouse}/pyiceberg_catalog.db", |
| 38 | + warehouse=f"file://{warehouse}", |
| 39 | + ) |
| 40 | + return catalog |
| 41 | + |
| 42 | + |
| 43 | +def test_datafusion_register_pyiceberg_table(catalog: Catalog, arrow_table_with_null: pa.Table) -> None: |
| 44 | + catalog.create_namespace_if_not_exists("default") |
| 45 | + iceberg_table = catalog.create_table_if_not_exists( |
| 46 | + "default.dataset", |
| 47 | + schema=arrow_table_with_null.schema, |
| 48 | + ) |
| 49 | + iceberg_table.append(arrow_table_with_null) |
| 50 | + |
| 51 | + ctx = SessionContext() |
| 52 | + ctx.register_table_provider("test", iceberg_table) |
| 53 | + |
| 54 | + datafusion_table = ctx.table("test") |
| 55 | + assert datafusion_table is not None |
| 56 | + |
| 57 | + assert datafusion_table.to_arrow_table().to_pylist() == iceberg_table.scan().to_arrow().to_pylist() |
| 58 | + |
| 59 | + from pandas.testing import assert_frame_equal |
| 60 | + |
| 61 | + assert_frame_equal( |
| 62 | + datafusion_table.to_arrow_table().to_pandas(), |
| 63 | + iceberg_table.scan().to_arrow().to_pandas(), |
| 64 | + ) |
0 commit comments