-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathdata_source.py
231 lines (205 loc) · 7.6 KB
/
data_source.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
222
223
224
225
226
227
228
229
230
231
from __future__ import annotations
import base64
import ssl
from enum import Enum, StrEnum, auto
from json import loads
from typing import Optional
import ibis
from google.oauth2 import service_account
from ibis import BaseBackend
from app.model import (
BigQueryConnectionInfo,
CannerConnectionInfo,
ClickHouseConnectionInfo,
ConnectionInfo,
MSSqlConnectionInfo,
MySqlConnectionInfo,
PostgresConnectionInfo,
QueryBigQueryDTO,
QueryCannerDTO,
QueryClickHouseDTO,
QueryDTO,
QueryGcsFileDTO,
QueryLocalFileDTO,
QueryMinioFileDTO,
QueryMSSqlDTO,
QueryMySqlDTO,
QueryPostgresDTO,
QueryS3FileDTO,
QuerySnowflakeDTO,
QueryTrinoDTO,
SnowflakeConnectionInfo,
SSLMode,
TrinoConnectionInfo,
)
class DataSource(StrEnum):
bigquery = auto()
canner = auto()
clickhouse = auto()
mssql = auto()
mysql = auto()
postgres = auto()
snowflake = auto()
trino = auto()
local_file = auto()
s3_file = auto()
minio_file = auto()
gcs_file = auto()
def get_connection(self, info: ConnectionInfo) -> BaseBackend:
try:
return DataSourceExtension[self].get_connection(info)
except KeyError:
raise NotImplementedError(f"Unsupported data source: {self}")
def get_dto_type(self):
try:
return DataSourceExtension[self].dto
except KeyError:
raise NotImplementedError(f"Unsupported data source: {self}")
class DataSourceExtension(Enum):
bigquery = QueryBigQueryDTO
canner = QueryCannerDTO
clickhouse = QueryClickHouseDTO
mssql = QueryMSSqlDTO
mysql = QueryMySqlDTO
postgres = QueryPostgresDTO
snowflake = QuerySnowflakeDTO
trino = QueryTrinoDTO
local_file = QueryLocalFileDTO
s3_file = QueryS3FileDTO
minio_file = QueryMinioFileDTO
gcs_file = QueryGcsFileDTO
def __init__(self, dto: QueryDTO):
self.dto = dto
def get_connection(self, info: ConnectionInfo) -> BaseBackend:
try:
if hasattr(info, "connection_url"):
return ibis.connect(info.connection_url.get_secret_value())
if self.name == "local_file":
raise NotImplementedError(
"Local file connection is not implemented to get ibis backend"
)
return getattr(self, f"get_{self.name}_connection")(info)
except KeyError:
raise NotImplementedError(f"Unsupported data source: {self}")
@staticmethod
def get_bigquery_connection(info: BigQueryConnectionInfo) -> BaseBackend:
credits_json = loads(
base64.b64decode(info.credentials.get_secret_value()).decode("utf-8")
)
credentials = service_account.Credentials.from_service_account_info(
credits_json
)
credentials = credentials.with_scopes(
[
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/cloud-platform",
]
)
return ibis.bigquery.connect(
project_id=info.project_id.get_secret_value(),
dataset_id=info.dataset_id.get_secret_value(),
credentials=credentials,
)
@staticmethod
def get_canner_connection(info: CannerConnectionInfo) -> BaseBackend:
return ibis.postgres.connect(
host=info.host.get_secret_value(),
port=int(info.port.get_secret_value()),
database=info.workspace.get_secret_value(),
user=info.user.get_secret_value(),
password=info.pat.get_secret_value(),
)
@staticmethod
def get_clickhouse_connection(info: ClickHouseConnectionInfo) -> BaseBackend:
return ibis.clickhouse.connect(
host=info.host.get_secret_value(),
port=int(info.port.get_secret_value()),
database=info.database.get_secret_value(),
user=info.user.get_secret_value(),
password=(info.password and info.password.get_secret_value()),
)
@classmethod
def get_mssql_connection(cls, info: MSSqlConnectionInfo) -> BaseBackend:
return ibis.mssql.connect(
host=info.host.get_secret_value(),
port=info.port.get_secret_value(),
database=info.database.get_secret_value(),
user=info.user.get_secret_value(),
password=(
info.password
and cls._escape_special_characters_for_odbc(
info.password.get_secret_value()
)
),
driver=info.driver,
TDS_Version=info.tds_version,
**info.kwargs if info.kwargs else dict(),
)
@classmethod
def get_mysql_connection(cls, info: MySqlConnectionInfo) -> BaseBackend:
ssl_context = cls._create_ssl_context(info)
kwargs = {"ssl": ssl_context} if ssl_context else {}
if info.kwargs:
kwargs.update(info.kwargs)
return ibis.mysql.connect(
host=info.host.get_secret_value(),
port=int(info.port.get_secret_value()),
database=info.database.get_secret_value(),
user=info.user.get_secret_value(),
password=(info.password and info.password.get_secret_value()),
**kwargs,
)
@staticmethod
def get_postgres_connection(info: PostgresConnectionInfo) -> BaseBackend:
return ibis.postgres.connect(
host=info.host.get_secret_value(),
port=int(info.port.get_secret_value()),
database=info.database.get_secret_value(),
user=info.user.get_secret_value(),
password=(info.password and info.password.get_secret_value()),
)
@staticmethod
def get_snowflake_connection(info: SnowflakeConnectionInfo) -> BaseBackend:
return ibis.snowflake.connect(
user=info.user.get_secret_value(),
password=info.password.get_secret_value(),
account=info.account.get_secret_value(),
database=info.database.get_secret_value(),
schema=info.sf_schema.get_secret_value(),
)
@staticmethod
def get_trino_connection(info: TrinoConnectionInfo) -> BaseBackend:
return ibis.trino.connect(
host=info.host.get_secret_value(),
port=int(info.port.get_secret_value()),
database=info.catalog.get_secret_value(),
schema=info.trino_schema.get_secret_value(),
user=(info.user and info.user.get_secret_value()),
password=(info.password and info.password.get_secret_value()),
)
@staticmethod
def _escape_special_characters_for_odbc(value: str) -> str:
return "{" + value.replace("}", "}}") + "}"
@staticmethod
def _create_ssl_context(info: ConnectionInfo) -> Optional[ssl.SSLContext]:
ssl_mode = (
info.ssl_mode.get_secret_value()
if hasattr(info, "ssl_mode") and info.ssl_mode
else None
)
if ssl_mode == SSLMode.VERIFY_CA and not info.ssl_ca:
raise ValueError("SSL CA must be provided when SSL mode is VERIFY CA")
if not ssl_mode or ssl_mode == SSLMode.DISABLED:
return None
ctx = ssl.create_default_context()
ctx.check_hostname = False
if ssl_mode == SSLMode.ENABLED:
ctx.verify_mode = ssl.CERT_NONE
elif ssl_mode == SSLMode.VERIFY_CA:
ctx.verify_mode = ssl.CERT_REQUIRED
ctx.load_verify_locations(
cadata=base64.b64decode(info.ssl_ca.get_secret_value()).decode("utf-8")
if info.ssl_ca
else None
)
return ctx