Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions pyiceberg/catalog/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
)
from pyiceberg.typedef import EMPTY_DICT, Identifier, Properties
from pyiceberg.types import strtobool
from pyiceberg.utils.properties import get_first_property_value
from pyiceberg.view import View
from pyiceberg.view.metadata import ViewVersion

Expand All @@ -89,6 +90,11 @@
DEFAULT_INIT_CATALOG_TABLES = "true"
ICEBERG_TABLE_TYPE = "TABLE"

SCHEMA_VERSION = "schema_version"
SCHEMA_VERSION_JAVA = "jdbc.schema-version"
INIT_CATALOG_TABLES = "init_catalog_tables"
INIT_CATALOG_TABLES_JAVA = "jdbc.init-catalog-tables"


class SqlCatalogBaseTable(MappedAsDataclass, DeclarativeBase):
pass
Expand Down Expand Up @@ -153,12 +159,16 @@ def _init_catalog(self) -> None:
- Existing v0 catalogs are not migrated unless schema_version='v1' is explicitly set.
"""
# Get schema version from properties, ok if None
schema_version_prop = self.properties.get("schema_version", "v0")
raw_schema_version = get_first_property_value(self.properties, SCHEMA_VERSION, SCHEMA_VERSION_JAVA)
schema_version_prop = "v0" if raw_schema_version is None else str(raw_schema_version).lower()
if schema_version_prop not in ("v0", "v1"):
raise ValueError(f"Invalid schema_version property: '{schema_version_prop}'. Must be 'v0' or 'v1'.")
raise ValueError(f"Invalid schema_version property: '{raw_schema_version}'. Must be 'v0' or 'v1'.")

# Determine if catalog tables should be created if absent
init_catalog_tables = strtobool(self.properties.get("init_catalog_tables", DEFAULT_INIT_CATALOG_TABLES))
init_catalog_tables_prop = get_first_property_value(self.properties, INIT_CATALOG_TABLES, INIT_CATALOG_TABLES_JAVA)
init_catalog_tables = strtobool(
DEFAULT_INIT_CATALOG_TABLES if init_catalog_tables_prop is None else str(init_catalog_tables_prop)
)

with Session(self.engine) as session:
# 1. Check if catalog tables exist; create if absent
Expand Down
64 changes: 64 additions & 0 deletions tests/catalog/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,70 @@ def test_invalid_schema_version_raises(warehouse: Path) -> None:
schema_version="invalid",
)

with pytest.raises(ValueError, match="Invalid schema_version"):
SqlCatalog(
name="test",
engine=None,
uri="sqlite:///:memory:",
warehouse=f"file://{warehouse}",
**{"jdbc.schema-version": "V2"},
)


@pytest.mark.parametrize(
("properties", "expect_tables"),
[
({"jdbc.init-catalog-tables": "false"}, False),
({"init_catalog_tables": "false", "jdbc.init-catalog-tables": "true"}, False),
({"jdbc.init-catalog-tables": "true"}, True),
({"init_catalog_tables": "true", "jdbc.init-catalog-tables": "false"}, True),
],
)
def test_java_jdbc_init_catalog_tables_property(
alchemy_engine: Engine,
catalog_name: str,
catalog_uri: str,
properties: dict[str, str],
expect_tables: bool,
) -> None:
confirm_no_tables_exist(alchemy_engine)

catalog = SqlCatalog(name=catalog_name, engine=None, uri=catalog_uri, **properties)
if expect_tables:
confirm_all_tables_exist(catalog)
else:
confirm_no_tables_exist(alchemy_engine)


@pytest.mark.parametrize(
("properties", "expected_version"),
[
({"schema_version": "v0", "jdbc.schema-version": "V1"}, "v0"),
({"jdbc.schema-version": "V0"}, "v0"),
({"jdbc.schema-version": "V1"}, "v1"),
({"schema_version": "v1", "jdbc.schema-version": "V0"}, "v1"),
],
)
def test_java_jdbc_schema_version_property(
tmp_path: Path,
warehouse: Path,
properties: dict[str, str],
expected_version: str,
) -> None:
uri = f"sqlite:///{tmp_path.as_posix()}/test-jdbc-schema-version.db"
engine = _create_v0_db(uri)
assert "iceberg_type" not in get_columns(engine)

catalog = SqlCatalog(
name="test",
engine=None,
uri=uri,
warehouse=f"file://{warehouse}",
**{"jdbc.init-catalog-tables": "false", **properties},
)
assert catalog._schema_version == expected_version
assert ("iceberg_type" in get_columns(catalog.engine)) == (expected_version == "v1")


def test_list_tables_works_on_v0_schema(warehouse: Path) -> None:
"""list_tables should work on V0 schemas without iceberg_type column."""
Expand Down
Loading