Skip to content

Commit cc6f7c4

Browse files
committed
Accept an existing SQLAlchemy engine in SqlCatalog
1 parent aafad6e commit cc6f7c4

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎pyiceberg/catalog/sql.py‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from sqlalchemy import (
2727
ColumnElement,
28+
Engine,
2829
String,
2930
create_engine,
3031
delete,
@@ -126,17 +127,21 @@ class SqlCatalog(MetastoreCatalog):
126127
The `SqlCatalog` has a different convention where a `TableIdentifier` requires a `Namespace`.
127128
"""
128129

129-
def __init__(self, name: str, **properties: str):
130+
def __init__(self, name: str, engine: Engine | None = None, **properties: str):
130131
super().__init__(name, **properties)
131132

132-
if not (uri_prop := self.properties.get(URI)):
133-
raise NoSuchPropertyException("SQL connection URI is required")
133+
if engine is not None:
134+
self.engine = engine
135+
else:
136+
if not (uri_prop := self.properties.get(URI)):
137+
raise NoSuchPropertyException("SQL connection URI is required")
134138

135-
echo_str = str(self.properties.get("echo", DEFAULT_ECHO_VALUE)).lower()
136-
echo = strtobool(echo_str) if echo_str != "debug" else "debug"
137-
pool_pre_ping = strtobool(self.properties.get("pool_pre_ping", DEFAULT_POOL_PRE_PING_VALUE))
139+
echo_str = str(self.properties.get("echo", DEFAULT_ECHO_VALUE)).lower()
140+
echo = strtobool(echo_str) if echo_str != "debug" else "debug"
141+
pool_pre_ping = strtobool(self.properties.get("pool_pre_ping", DEFAULT_POOL_PRE_PING_VALUE))
142+
143+
self.engine = create_engine(uri_prop, echo=echo, pool_pre_ping=pool_pre_ping)
138144

139-
self.engine = create_engine(uri_prop, echo=echo, pool_pre_ping=pool_pre_ping)
140145
self._init_catalog()
141146

142147
def _init_catalog(self) -> None:

‎tests/catalog/test_sql.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,29 @@ def test_creation_with_pool_pre_ping_parameter(catalog_name: str, warehouse: Pat
135135
)
136136

137137

138+
def test_creation_with_existing_engine(catalog_name: str, warehouse: Path) -> None:
139+
engine = create_engine(f"sqlite:///{warehouse.as_posix()}/sql-catalog-engine")
140+
141+
catalog = SqlCatalog(catalog_name, engine=engine, warehouse=f"file://{warehouse}")
142+
143+
assert catalog.engine is engine
144+
confirm_all_tables_exist(catalog)
145+
146+
catalog.create_namespace("default")
147+
catalog.create_table("default.t", Schema(NestedField(1, "x", StringType())))
148+
assert catalog.table_exists("default.t")
149+
150+
151+
def test_creation_with_existing_engine_without_uri(catalog_name: str, warehouse: Path) -> None:
152+
engine = create_engine(f"sqlite:///{warehouse.as_posix()}/sql-catalog-engine-no-uri")
153+
154+
catalog = SqlCatalog(catalog_name, engine=engine, warehouse=f"file://{warehouse}")
155+
156+
catalog.create_namespace("default")
157+
catalog.create_table("default.t2", Schema(NestedField(1, "x", StringType())))
158+
assert catalog.table_exists("default.t2")
159+
160+
138161
def test_creation_from_impl(catalog_name: str, warehouse: Path) -> None:
139162
assert isinstance(
140163
load_catalog(

0 commit comments

Comments
 (0)