Skip to content

Commit 3a15895

Browse files
authored
Add test to capture Duckdb behavior (#389)
* Add test to capture Duckdb behavior * Fix timezone issue
1 parent 8ec62c1 commit 3a15895

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

tests/catalog/test_sql.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import pyarrow as pa
2323
import pytest
24-
from pytest import TempPathFactory
2524
from pytest_lazyfixture import lazy_fixture
2625
from sqlalchemy.exc import ArgumentError, IntegrityError
2726

@@ -51,11 +50,6 @@
5150
from pyiceberg.types import IntegerType
5251

5352

54-
@pytest.fixture(name="warehouse", scope="session")
55-
def fixture_warehouse(tmp_path_factory: TempPathFactory) -> Path:
56-
return tmp_path_factory.mktemp("test_sql")
57-
58-
5953
@pytest.fixture(name="random_identifier")
6054
def fixture_random_identifier(warehouse: Path, database_name: str, table_name: str) -> Identifier:
6155
os.makedirs(f"{warehouse}/{database_name}.db/{table_name}/metadata/", exist_ok=True)

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import string
3232
import uuid
3333
from datetime import datetime
34+
from pathlib import Path
3435
from random import choice
3536
from tempfile import TemporaryDirectory
3637
from typing import (
@@ -1929,6 +1930,11 @@ def example_task(data_file: str) -> FileScanTask:
19291930
)
19301931

19311932

1933+
@pytest.fixture(scope="session")
1934+
def warehouse(tmp_path_factory: pytest.TempPathFactory) -> Path:
1935+
return tmp_path_factory.mktemp("test_sql")
1936+
1937+
19321938
@pytest.fixture
19331939
def table_v1(example_table_metadata_v1: Dict[str, Any]) -> Table:
19341940
table_metadata = TableMetadataV1(**example_table_metadata_v1)

tests/integration/test_writes.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
# pylint:disable=redefined-outer-name
18+
import os
19+
import time
1820
import uuid
1921
from datetime import date, datetime
22+
from pathlib import Path
2023
from typing import Any, Dict, List
2124
from urllib.parse import urlparse
2225

2326
import pyarrow as pa
2427
import pyarrow.parquet as pq
2528
import pytest
29+
import pytz
2630
from pyarrow.fs import S3FileSystem
2731
from pyspark.sql import SparkSession
2832
from pytest_mock.plugin import MockerFixture
2933

3034
from pyiceberg.catalog import Catalog, Properties, Table, load_catalog
35+
from pyiceberg.catalog.sql import SqlCatalog
3136
from pyiceberg.exceptions import NamespaceAlreadyExistsError, NoSuchTableError
3237
from pyiceberg.schema import Schema
3338
from pyiceberg.types import (
@@ -573,3 +578,59 @@ def test_summaries_with_only_nulls(
573578
'total-position-deletes': '0',
574579
'total-records': '0',
575580
}
581+
582+
583+
@pytest.mark.integration
584+
def test_duckdb_url_import(warehouse: Path, arrow_table_with_null: pa.Table) -> None:
585+
os.environ['TZ'] = 'Etc/UTC'
586+
time.tzset()
587+
tz = pytz.timezone(os.environ['TZ'])
588+
589+
catalog = SqlCatalog("test_sql_catalog", uri="sqlite:///:memory:", warehouse=f"/{warehouse}")
590+
catalog.create_namespace("default")
591+
592+
identifier = "default.arrow_table_v1_with_null"
593+
tbl = _create_table(catalog, identifier, {}, [arrow_table_with_null])
594+
location = tbl.metadata_location
595+
596+
import duckdb
597+
598+
duckdb.sql('INSTALL iceberg; LOAD iceberg;')
599+
result = duckdb.sql(
600+
f"""
601+
SELECT *
602+
FROM iceberg_scan('{location}')
603+
"""
604+
).fetchall()
605+
606+
assert result == [
607+
(
608+
False,
609+
'a',
610+
'aaaaaaaaaaaaaaaaaaaaaa',
611+
1,
612+
1,
613+
0.0,
614+
0.0,
615+
datetime(2023, 1, 1, 19, 25),
616+
datetime(2023, 1, 1, 19, 25, tzinfo=tz),
617+
date(2023, 1, 1),
618+
b'\x01',
619+
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
620+
),
621+
(None, None, None, None, None, None, None, None, None, None, None, None),
622+
(
623+
True,
624+
'z',
625+
'zzzzzzzzzzzzzzzzzzzzzz',
626+
9,
627+
9,
628+
0.8999999761581421,
629+
0.9,
630+
datetime(2023, 3, 1, 19, 25),
631+
datetime(2023, 3, 1, 19, 25, tzinfo=tz),
632+
date(2023, 3, 1),
633+
b'\x12',
634+
b'\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11',
635+
),
636+
]

0 commit comments

Comments
 (0)