Skip to content

Commit 4e4a165

Browse files
committed
refactor: split temp table support along finalizer lines
1 parent 7e4ea96 commit 4e4a165

File tree

17 files changed

+74
-61
lines changed

17 files changed

+74
-61
lines changed

ibis/backends/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,3 +1829,12 @@ def _from_url(self, url: ParseResult, **kwargs) -> BaseBackend:
18291829
18301830
"""
18311831
return self.connect(**kwargs)
1832+
1833+
1834+
class SupportsTempTables:
1835+
__slots__ = ()
1836+
1837+
supports_temporary_tables = True
1838+
1839+
def _make_memtable_finalizer(self, name: str) -> None:
1840+
"""No-op because temporary tables are automatically cleaned up."""

ibis/backends/athena/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ def create_table(
127127
Iterable of column name and type pairs/mapping/schema by which to
128128
partition the table.
129129
"""
130+
if temp:
131+
raise NotImplementedError(
132+
"Temporary tables are not supported in the Amazon Athena backend"
133+
)
130134
if overwrite is not None:
131135
raise com.UnsupportedOperationError(
132136
"Amazon Athena does not support REPLACE syntax, nor does it "
@@ -137,11 +141,6 @@ def create_table(
137141
if schema is not None:
138142
schema = ibis.schema(schema)
139143

140-
if temp:
141-
raise NotImplementedError(
142-
"Temporary tables are not supported in the Amazon Athena backend"
143-
)
144-
145144
table_loc = self._to_sqlglot_table(database)
146145
catalog, db = self._to_catalog_db_tuple(table_loc)
147146

ibis/backends/bigquery/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
import ibis.expr.schema as sch
2929
import ibis.expr.types as ir
3030
from ibis import util
31-
from ibis.backends import CanCreateDatabase, DirectPyArrowExampleLoader
31+
from ibis.backends import (
32+
CanCreateDatabase,
33+
DirectPyArrowExampleLoader,
34+
SupportsTempTables,
35+
)
3236
from ibis.backends.bigquery.client import (
3337
bigquery_param,
3438
parse_project_and_dataset,
@@ -160,7 +164,9 @@ def _postprocess_arrow(
160164
return table_or_batch.rename_columns(names)
161165

162166

163-
class Backend(SQLBackend, CanCreateDatabase, DirectPyArrowExampleLoader):
167+
class Backend(
168+
SupportsTempTables, SQLBackend, CanCreateDatabase, DirectPyArrowExampleLoader
169+
):
164170
name = "bigquery"
165171
compiler = sc.bigquery.compiler
166172
supports_python_udfs = False

ibis/backends/clickhouse/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626
import ibis.expr.schema as sch
2727
import ibis.expr.types as ir
2828
from ibis import util
29-
from ibis.backends import BaseBackend, CanCreateDatabase, DirectExampleLoader
29+
from ibis.backends import (
30+
BaseBackend,
31+
CanCreateDatabase,
32+
DirectExampleLoader,
33+
SupportsTempTables,
34+
)
3035
from ibis.backends.clickhouse.converter import ClickHousePandasData
3136
from ibis.backends.sql import SQLBackend
3237
from ibis.backends.sql.compilers.base import C
@@ -44,10 +49,9 @@ def _to_memtable(v):
4449
return ibis.memtable(v).op() if not isinstance(v, ops.InMemoryTable) else v
4550

4651

47-
class Backend(SQLBackend, CanCreateDatabase, DirectExampleLoader):
52+
class Backend(SupportsTempTables, SQLBackend, CanCreateDatabase, DirectExampleLoader):
4853
name = "clickhouse"
4954
compiler = sc.clickhouse.compiler
50-
supports_temporary_tables = True
5155

5256
class Options(ibis.config.Config):
5357
"""Clickhouse options.
@@ -774,6 +778,3 @@ def create_view(
774778
with self._safe_raw_sql(src, external_tables=external_tables):
775779
pass
776780
return self.table(name, database=database)
777-
778-
def _make_memtable_finalizer(self, name: str) -> None:
779-
"""No-op because temporary tables are automatically cleaned up."""

ibis/backends/datafusion/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
import inspect
55
import typing
66
from collections.abc import Mapping
7-
from functools import partial
87
from pathlib import Path
9-
from typing import TYPE_CHECKING, Any, Callable
8+
from typing import TYPE_CHECKING, Any
109

1110
import datafusion as df
1211
import pyarrow as pa
@@ -29,6 +28,7 @@
2928
HasCurrentCatalog,
3029
HasCurrentDatabase,
3130
NoUrl,
31+
SupportsTempTables,
3232
)
3333
from ibis.backends.sql import SQLBackend
3434
from ibis.backends.sql.compilers.base import C
@@ -77,6 +77,7 @@ def as_nullable(dtype: dt.DataType) -> dt.DataType:
7777

7878

7979
class Backend(
80+
SupportsTempTables,
8081
SQLBackend,
8182
CanCreateCatalog,
8283
CanCreateDatabase,
@@ -420,9 +421,6 @@ def _register_in_memory_table(self, op: ops.InMemoryTable) -> None:
420421
# of registering the table
421422
self.con.from_arrow(op.data.to_pyarrow(op.schema), op.name)
422423

423-
def _make_memtable_finalizer(self, name: str) -> Callable[..., None]:
424-
return partial(self.con.deregister_table, name)
425-
426424
def read_csv(
427425
self,
428426
paths: str | Path | list[str | Path] | tuple[str | Path],

ibis/backends/duckdb/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
DirectExampleLoader,
2929
HasCurrentCatalog,
3030
HasCurrentDatabase,
31+
SupportsTempTables,
3132
UrlFromPath,
3233
)
3334
from ibis.backends.sql import SQLBackend
@@ -74,6 +75,7 @@ def __repr__(self):
7475

7576

7677
class Backend(
78+
SupportsTempTables,
7779
SQLBackend,
7880
CanListCatalog,
7981
CanCreateDatabase,
@@ -1769,9 +1771,6 @@ def _create_temp_view(self, table_name, source):
17691771
with self._safe_raw_sql(self._get_temp_view_definition(table_name, source)):
17701772
pass
17711773

1772-
def _make_memtable_finalizer(self, name: str) -> None:
1773-
"""No-op because temporary tables are automatically cleaned up."""
1774-
17751774

17761775
@lazy_singledispatch
17771776
def _read_in_memory(source: Any, table_name: str, _conn: Backend, **_: Any):

ibis/backends/flink/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
HasCurrentDatabase,
2222
NoUrl,
2323
PyArrowExampleLoader,
24+
SupportsTempTables,
2425
)
2526
from ibis.backends.flink.ddl import (
2627
CreateDatabase,
@@ -51,6 +52,7 @@
5152

5253

5354
class Backend(
55+
SupportsTempTables,
5456
SQLBackend,
5557
CanCreateDatabase,
5658
HasCurrentCatalog,
@@ -66,9 +68,6 @@ class Backend(
6668
def _register_in_memory_table(self, op: ops.InMemoryTable) -> None:
6769
"""No-op."""
6870

69-
def _make_memtable_finalizer(self, name: str) -> None:
70-
"""No-op."""
71-
7271
def do_connect(self, table_env: TableEnvironment) -> None:
7372
"""Create a Flink `Backend` for use with Ibis.
7473
@@ -398,9 +397,6 @@ def _register_in_memory_table(self, op: ops.InMemoryTable) -> None:
398397
)
399398
self.create_view(op.name, op.data.to_frame(), schema=op.schema, temp=True)
400399

401-
def _make_memtable_finalizer(self, name: str) -> None:
402-
"""No-op for Flink."""
403-
404400
def execute(
405401
self,
406402
expr: ir.Expr,

ibis/backends/mysql/__init__.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
import ibis.expr.schema as sch
2323
import ibis.expr.types as ir
2424
from ibis import util
25-
from ibis.backends import CanCreateDatabase, HasCurrentDatabase, PyArrowExampleLoader
25+
from ibis.backends import (
26+
CanCreateDatabase,
27+
HasCurrentDatabase,
28+
PyArrowExampleLoader,
29+
SupportsTempTables,
30+
)
2631
from ibis.backends.sql import SQLBackend
2732
from ibis.backends.sql.compilers.base import STAR, TRUE, C, RenameTable
2833

@@ -35,11 +40,16 @@
3540
import pyarrow as pa
3641

3742

38-
class Backend(SQLBackend, CanCreateDatabase, HasCurrentDatabase, PyArrowExampleLoader):
43+
class Backend(
44+
SupportsTempTables,
45+
SQLBackend,
46+
CanCreateDatabase,
47+
HasCurrentDatabase,
48+
PyArrowExampleLoader,
49+
):
3950
name = "mysql"
4051
compiler = sc.mysql.compiler
4152
supports_create_or_replace = False
42-
supports_temporary_tables = True
4353

4454
def _from_url(self, url: ParseResult, **kwarg_overrides):
4555
kwargs = {}
@@ -511,6 +521,3 @@ def _fetch_from_cursor(self, cursor, schema: sch.Schema) -> pd.DataFrame:
511521
cursor.fetchall(), columns=schema.names, coerce_float=True
512522
)
513523
return MySQLPandasData.convert_table(df, schema)
514-
515-
def _make_memtable_finalizer(self, name: str) -> None:
516-
"""No-op because temporary tables are automatically cleaned up."""

ibis/backends/oracle/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class Backend(
8989
):
9090
name = "oracle"
9191
compiler = sc.oracle.compiler
92+
supports_temporary_tables = True
9293

9394
@cached_property
9495
def version(self):

ibis/backends/polars/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import ibis.expr.operations as ops
1313
import ibis.expr.schema as sch
1414
import ibis.expr.types as ir
15-
from ibis.backends import BaseBackend, DirectExampleLoader, NoUrl
15+
from ibis.backends import BaseBackend, DirectExampleLoader, NoUrl, SupportsTempTables
1616
from ibis.backends.polars.compiler import translate
1717
from ibis.backends.polars.rewrites import bind_unbound_table, rewrite_join
1818
from ibis.backends.sql.dialects import Polars
@@ -28,7 +28,7 @@
2828
import pyarrow as pa
2929

3030

31-
class Backend(BaseBackend, NoUrl, DirectExampleLoader):
31+
class Backend(SupportsTempTables, BaseBackend, NoUrl, DirectExampleLoader):
3232
name = "polars"
3333
dialect = Polars
3434
supports_temporary_tables = True
@@ -549,9 +549,6 @@ def _create_cached_table(self, name, expr):
549549
def _drop_cached_table(self, name):
550550
self.drop_table(name, force=True)
551551

552-
def _make_memtable_finalizer(self, name: str) -> None:
553-
"""No-op because temporary tables are automatically cleaned up."""
554-
555552

556553
@lazy_singledispatch
557554
def _read_in_memory(source: Any, table_name: str, _conn: Backend, **kwargs: Any):

0 commit comments

Comments
 (0)