-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_engine.py
More file actions
378 lines (331 loc) · 12.2 KB
/
sql_engine.py
File metadata and controls
378 lines (331 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
"""Marimo ``mo.sql`` engine integration for :class:`~hotdata_runtime.HotdataClient`."""
from __future__ import annotations
from collections import defaultdict
from typing import Any, Literal
from hotdata_runtime import HotdataClient
from marimo import _loggers
from marimo._data.models import (
Database,
DataSourceConnection,
DataTable,
DataTableColumn,
Schema,
)
from marimo._sql.engines.types import InferenceConfig, SQLConnection
from marimo._sql.utils import convert_to_output, sql_type_to_data_type
from marimo._types.ids import VariableName
LOGGER = _loggers.marimo_logger()
def _table_schema_name(t: Any) -> str:
return str(t.var_schema)
class HotdataMarimoEngine(SQLConnection[HotdataClient]):
"""Marimo :class:`~marimo._sql.engines.types.SQLConnection` backed by Hotdata.
Catalog methods support Marimo's Data Sources panel. ``execute()`` only runs SQL
via :meth:`~hotdata_runtime.HotdataClient.execute_sql` (no catalog calls in that path).
"""
def __init__(
self,
connection: HotdataClient,
engine_name: VariableName | None = None,
*,
default_database: str | None = None,
) -> None:
super().__init__(connection, engine_name)
self._connections_cache: list[Any] | None = None
self._default_database = default_database
@property
def source(self) -> str:
return "hotdata"
@property
def dialect(self) -> str:
# Marimo labels engines as ``{dialect} ({variable_name})``; display_name is patched to "Hotdata".
return "hotdata"
@staticmethod
def is_compatible(var: Any) -> bool:
return isinstance(var, HotdataClient)
@property
def inference_config(self) -> InferenceConfig:
return InferenceConfig(
auto_discover_schemas=True,
auto_discover_tables="auto",
auto_discover_columns="auto",
)
def _resolve_should_auto_discover(
self,
value: bool | Literal["auto"],
) -> bool:
if value == "auto":
return True
return value
def _connection_id(self, connection_name: str) -> str | None:
try:
return self._connection.connection_id_by_name().get(connection_name)
except RuntimeError as e:
LOGGER.warning("%s", e)
return None
def _connections(self) -> list[Any]:
if self._connections_cache is None:
self._connections_cache = list(
self._connection.connections().list_connections().connections
)
return self._connections_cache
def _iter_grouped(
self,
*,
connection_id: str | None,
include_columns: bool,
) -> dict[str, dict[str, list[Any]]]:
grouped: dict[str, dict[str, list[Any]]] = defaultdict(
lambda: defaultdict(list)
)
for t in self._connection.iter_tables(
connection_id=connection_id,
include_columns=include_columns,
):
grouped[str(t.connection)][_table_schema_name(t)].append(t)
return grouped
def get_default_database(self) -> str | None:
listing = self._connections()
if not listing:
return None
return str(listing[0].name)
def get_default_schema(self) -> str | None:
return None
def get_databases(
self,
*,
include_schemas: bool | Literal["auto"],
include_tables: bool | Literal["auto"],
include_table_details: bool | Literal["auto"],
) -> list[Database]:
databases: list[Database] = []
for c in self._connections():
name = str(c.name)
if self._resolve_should_auto_discover(include_schemas):
schemas = self.get_schemas(
database=name,
include_tables=self._resolve_should_auto_discover(
include_tables
),
include_table_details=self._resolve_should_auto_discover(
include_table_details
),
)
else:
schemas = []
databases.append(
Database(
name=name,
dialect=self.dialect,
schemas=schemas,
engine=self._engine_name,
)
)
return databases
def get_schemas(
self,
*,
database: str | None,
include_tables: bool,
include_table_details: bool,
) -> list[Schema]:
if not database:
return []
conn_id = self._connection_id(database)
if conn_id is None:
LOGGER.warning("Unknown Hotdata connection name %r", database)
return []
grouped = self._iter_grouped(
connection_id=conn_id,
include_columns=include_table_details,
)
inner = grouped.get(database, {})
schemas: list[Schema] = []
for schema_name in sorted(inner.keys()):
tables: list[DataTable] = []
if include_tables:
tables = self.get_tables_in_schema(
schema=schema_name,
database=database,
include_table_details=include_table_details,
)
if not tables:
continue
schemas.append(Schema(name=schema_name, tables=tables))
return schemas
def _data_table_from_table_info(self, t: Any) -> DataTable:
cols: list[DataTableColumn] = []
for col in t.columns or []:
cols.append(
DataTableColumn(
name=str(col.name),
type=sql_type_to_data_type(str(col.data_type)),
external_type=str(col.data_type),
sample_values=[],
)
)
return DataTable(
source_type="connection",
source=self.source,
name=str(t.table),
num_rows=None,
num_columns=len(cols) if cols else None,
variable_name=None,
engine=self._engine_name,
type="table",
columns=cols,
primary_keys=None,
indexes=None,
)
def get_tables_in_schema(
self,
*,
schema: str,
database: str,
include_table_details: bool,
) -> list[DataTable]:
conn_id = self._connection_id(database)
if conn_id is None:
return []
grouped = self._iter_grouped(
connection_id=conn_id,
include_columns=include_table_details,
)
tables_info = grouped.get(database, {}).get(schema, [])
out: list[DataTable] = []
for t in sorted(tables_info, key=lambda x: str(x.table)):
if include_table_details:
if t.columns:
out.append(self._data_table_from_table_info(t))
continue
dt = self.get_table_details(
table_name=str(t.table),
schema_name=schema,
database_name=database,
)
if dt is not None:
out.append(dt)
else:
out.append(
DataTable(
source_type="connection",
source=self.source,
name=str(t.table),
num_rows=None,
num_columns=len(t.columns or []) if t.columns else None,
variable_name=None,
engine=self._engine_name,
type="table",
columns=[],
primary_keys=None,
indexes=None,
)
)
return out
def get_table_details(
self,
*,
table_name: str,
schema_name: str,
database_name: str,
) -> DataTable | None:
conn_id = self._connection_id(database_name)
if conn_id is None:
return None
qualified = f"{database_name}.{schema_name}.{table_name}"
try:
cols_raw = self._connection.columns_for_qualified(
qualified, connection_id=conn_id
)
except Exception:
LOGGER.warning(
"Failed to load columns for %s",
qualified,
exc_info=True,
)
return None
cols: list[DataTableColumn] = []
for col in cols_raw:
cols.append(
DataTableColumn(
name=str(col.name),
type=sql_type_to_data_type(str(col.data_type)),
external_type=str(col.data_type),
sample_values=[],
)
)
return DataTable(
source_type="connection",
source=self.source,
name=table_name,
num_rows=None,
num_columns=len(cols),
variable_name=None,
engine=self._engine_name,
type="table",
columns=cols,
primary_keys=None,
indexes=None,
)
def execute(self, query: str) -> Any:
qr = self._connection.execute_sql(query, database=self._default_database)
fmt = self.sql_output_format()
def to_polars() -> Any:
import polars as pl
if not qr.columns:
return pl.DataFrame()
return pl.DataFrame(qr.rows, schema=qr.columns, orient="row")
return convert_to_output(
sql_output_format=fmt,
to_polars=to_polars,
to_pandas=qr.to_pandas,
to_native=to_polars,
)
_HOTDATA_ENGINE_DISPLAY_NAME = "Hotdata"
_ORIGINAL_ENGINE_TO_CONNECTION = None
def _install_hotdata_engine_display_name() -> None:
"""Show ``Hotdata`` in Marimo's SQL engine / Data Sources UI (not ``sql (client)``)."""
global _ORIGINAL_ENGINE_TO_CONNECTION
if _ORIGINAL_ENGINE_TO_CONNECTION is not None:
return
import marimo._sql.get_engines as ge
_ORIGINAL_ENGINE_TO_CONNECTION = ge.engine_to_data_source_connection
def engine_to_data_source_connection(
variable_name: VariableName, engine: object
) -> DataSourceConnection:
conn = _ORIGINAL_ENGINE_TO_CONNECTION(variable_name, engine) # type: ignore[arg-type]
if not isinstance(engine, HotdataMarimoEngine):
return conn
return DataSourceConnection(
source=conn.source,
dialect=conn.dialect,
name=conn.name,
display_name=_HOTDATA_ENGINE_DISPLAY_NAME,
databases=conn.databases,
default_database=conn.default_database,
default_schema=conn.default_schema,
)
_set_engine_to_data_source_connection(engine_to_data_source_connection)
def _set_engine_to_data_source_connection(fn: object) -> None:
"""Marimo imports this helper in multiple modules; patch all bindings."""
import marimo._runtime.runner.hooks_post_execution as hpe
import marimo._runtime.runtime as rt
import marimo._sql.get_engines as ge
ge.engine_to_data_source_connection = fn # type: ignore[assignment]
hpe.engine_to_data_source_connection = fn # type: ignore[assignment]
rt.engine_to_data_source_connection = fn # type: ignore[assignment]
def register_hotdata_sql_engine() -> None:
"""Register :class:`HotdataMarimoEngine` with Marimo's SQL engine registry (idempotent)."""
_install_hotdata_engine_display_name()
from marimo._sql.get_engines import SUPPORTED_ENGINES
if HotdataMarimoEngine in SUPPORTED_ENGINES:
return
SUPPORTED_ENGINES.insert(0, HotdataMarimoEngine)
def unregister_hotdata_sql_engine() -> None:
"""Remove :class:`HotdataMarimoEngine` from Marimo's registry (mostly for tests)."""
global _ORIGINAL_ENGINE_TO_CONNECTION
from marimo._sql.get_engines import SUPPORTED_ENGINES
while HotdataMarimoEngine in SUPPORTED_ENGINES:
SUPPORTED_ENGINES.remove(HotdataMarimoEngine)
if _ORIGINAL_ENGINE_TO_CONNECTION is not None:
_set_engine_to_data_source_connection(_ORIGINAL_ENGINE_TO_CONNECTION)
_ORIGINAL_ENGINE_TO_CONNECTION = None