diff --git a/pyiceberg/catalog/sql.py b/pyiceberg/catalog/sql.py index 94c7965a88..31f39c29aa 100644 --- a/pyiceberg/catalog/sql.py +++ b/pyiceberg/catalog/sql.py @@ -25,6 +25,7 @@ from sqlalchemy import ( ColumnElement, + Engine, String, create_engine, delete, @@ -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: diff --git a/tests/catalog/test_sql.py b/tests/catalog/test_sql.py index df5e30e25f..8e0348cba6 100644 --- a/tests/catalog/test_sql.py +++ b/tests/catalog/test_sql.py @@ -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(