Skip to content
Merged
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
19 changes: 12 additions & 7 deletions pyiceberg/catalog/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from sqlalchemy import (
ColumnElement,
Engine,
String,
create_engine,
delete,
Expand Down Expand Up @@ -126,17 +127,21 @@ class SqlCatalog(MetastoreCatalog):
The `SqlCatalog` has a different convention where a `TableIdentifier` requires a `Namespace`.
"""

def __init__(self, name: str, **properties: str):
def __init__(self, name: str, engine: Engine | None = None, **properties: str):
super().__init__(name, **properties)

if not (uri_prop := self.properties.get(URI)):
raise NoSuchPropertyException("SQL connection URI is required")
if engine is not None:
self.engine = engine
else:
if not (uri_prop := self.properties.get(URI)):
raise NoSuchPropertyException("SQL connection URI is required")

echo_str = str(self.properties.get("echo", DEFAULT_ECHO_VALUE)).lower()
echo = strtobool(echo_str) if echo_str != "debug" else "debug"
pool_pre_ping = strtobool(self.properties.get("pool_pre_ping", DEFAULT_POOL_PRE_PING_VALUE))
echo_str = str(self.properties.get("echo", DEFAULT_ECHO_VALUE)).lower()
echo = strtobool(echo_str) if echo_str != "debug" else "debug"
pool_pre_ping = strtobool(self.properties.get("pool_pre_ping", DEFAULT_POOL_PRE_PING_VALUE))

self.engine = create_engine(uri_prop, echo=echo, pool_pre_ping=pool_pre_ping)

self.engine = create_engine(uri_prop, echo=echo, pool_pre_ping=pool_pre_ping)
self._init_catalog()

def _init_catalog(self) -> None:
Expand Down
23 changes: 23 additions & 0 deletions tests/catalog/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,29 @@ def test_creation_with_pool_pre_ping_parameter(catalog_name: str, warehouse: Pat
)


def test_creation_with_existing_engine(catalog_name: str, warehouse: Path) -> None:
engine = create_engine(f"sqlite:///{warehouse.as_posix()}/sql-catalog-engine")

catalog = SqlCatalog(catalog_name, engine=engine, warehouse=f"file://{warehouse}")

assert catalog.engine is engine
confirm_all_tables_exist(catalog)

catalog.create_namespace("default")
catalog.create_table("default.t", Schema(NestedField(1, "x", StringType())))
assert catalog.table_exists("default.t")


def test_creation_with_existing_engine_without_uri(catalog_name: str, warehouse: Path) -> None:
engine = create_engine(f"sqlite:///{warehouse.as_posix()}/sql-catalog-engine-no-uri")

catalog = SqlCatalog(catalog_name, engine=engine, warehouse=f"file://{warehouse}")

catalog.create_namespace("default")
catalog.create_table("default.t2", Schema(NestedField(1, "x", StringType())))
assert catalog.table_exists("default.t2")


def test_creation_from_impl(catalog_name: str, warehouse: Path) -> None:
assert isinstance(
load_catalog(
Expand Down
Loading