-
Notifications
You must be signed in to change notification settings - Fork 227
/
session.py
221 lines (176 loc) · 5.15 KB
/
session.py
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
"""Spark session integration."""
from __future__ import annotations
import datetime as dt
from types import TracebackType
from typing import Any
from dbt.events import AdapterLogger
from dbt.utils import DECIMALS
from pyspark.sql import DataFrame, Row, SparkSession
logger = AdapterLogger("Spark")
NUMBERS = DECIMALS + (int, float)
class Cursor:
"""
Mock a pyodbc cursor.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Cursor
"""
def __init__(self) -> None:
self._df: DataFrame | None = None
self._rows: list[Row] | None = None
def __enter__(self) -> Cursor:
return self
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: Exception | None,
exc_tb: TracebackType | None,
) -> bool:
self.close()
return True
@property
def description(
self,
) -> list[tuple[str, str, None, None, None, None, bool]]:
"""
Get the description.
Returns
-------
out : list[tuple[str, str, None, None, None, None, bool]]
The description.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Cursor#description
"""
if self._df is None:
description = list()
else:
description = [
(
field.name,
field.dataType.simpleString(),
None,
None,
None,
None,
field.nullable,
)
for field in self._df.schema.fields
]
return description
def close(self) -> None:
"""
Close the connection.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Cursor#close
"""
self._df = None
self._rows = None
def execute(self, sql: str, *parameters: Any) -> None:
"""
Execute a sql statement.
Parameters
----------
sql : str
Execute a sql statement.
*parameters : Any
The parameters.
Raises
------
NotImplementedError
If there are parameters given. We do not format sql statements.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Cursor#executesql-parameters
"""
if len(parameters) > 0:
sql = sql % parameters
spark_session = SparkSession.builder.enableHiveSupport().getOrCreate()
self._df = spark_session.sql(sql)
def fetchall(self) -> list[Row] | None:
"""
Fetch all data.
Returns
-------
out : list[Row] | None
The rows.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Cursor#fetchall
"""
if self._rows is None and self._df is not None:
self._rows = self._df.collect()
return self._rows
def fetchone(self) -> Row | None:
"""
Fetch the first output.
Returns
-------
out : Row | None
The first row.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Cursor#fetchone
"""
if self._rows is None and self._df is not None:
self._rows = self._df.collect()
if self._rows is not None and len(self._rows) > 0:
row = self._rows.pop(0)
else:
row = None
return row
class Connection:
"""
Mock a pyodbc connection.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Connection
"""
def cursor(self) -> Cursor:
"""
Get a cursor.
Returns
-------
out : Cursor
The cursor.
"""
return Cursor()
class SessionConnectionWrapper(object):
"""Connection wrapper for the sessoin connection method."""
def __init__(self, handle):
self.handle = handle
self._cursor = None
def cursor(self):
self._cursor = self.handle.cursor()
return self
def cancel(self):
logger.debug("NotImplemented: cancel")
def close(self):
if self._cursor:
self._cursor.close()
def rollback(self, *args, **kwargs):
logger.debug("NotImplemented: rollback")
def fetchall(self):
return self._cursor.fetchall()
def execute(self, sql, bindings=None):
if sql.strip().endswith(";"):
sql = sql.strip()[:-1]
if bindings is None:
self._cursor.execute(sql)
else:
bindings = [self._fix_binding(binding) for binding in bindings]
self._cursor.execute(sql, *bindings)
@property
def description(self):
return self._cursor.description
@classmethod
def _fix_binding(cls, value):
"""Convert complex datatypes to primitives that can be loaded by
the Spark driver"""
if isinstance(value, NUMBERS):
return float(value)
elif isinstance(value, dt.datetime):
return f"'{value.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]}'"
else:
return f"'{value}'"