-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathsql.py
393 lines (325 loc) · 13.2 KB
/
sql.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
"""Sink classes load data to SQL targets."""
import re
from collections import defaultdict
from copy import copy
from textwrap import dedent
from typing import Any, Dict, Iterable, List, Optional, Type, Union
import sqlalchemy
from pendulum import now
from sqlalchemy.sql import Executable
from sqlalchemy.sql.expression import bindparam
from singer_sdk.connectors import SQLConnector
from singer_sdk.exceptions import ConformedNameClashException
from singer_sdk.helpers._conformers import replace_leading_digit, snakecase
from singer_sdk.plugin_base import PluginBase
from singer_sdk.sinks.batch import BatchSink
class SQLSink(BatchSink):
"""SQL-type sink type."""
connector_class: Type[SQLConnector]
soft_delete_column_name = "_sdc_deleted_at"
version_column_name = "_sdc_table_version"
def __init__(
self,
target: PluginBase,
stream_name: str,
schema: Dict,
key_properties: Optional[List[str]],
connector: Optional[SQLConnector] = None,
) -> None:
"""Initialize SQL Sink.
Args:
target: The target object.
stream_name: The source tap's stream name.
schema: The JSON Schema definition.
key_properties: The primary key columns.
connector: Optional connector to reuse.
"""
self._connector: SQLConnector
self._connector = connector or self.connector_class(dict(target.config))
super().__init__(target, stream_name, schema, key_properties)
@property
def connector(self) -> SQLConnector:
"""The connector object.
Returns:
The connector object.
"""
return self._connector
@property
def connection(self) -> sqlalchemy.engine.Connection:
"""Get or set the SQLAlchemy connection for this sink.
Returns:
A connection object.
"""
return self.connector.connection
@property
def table_name(self) -> str:
"""Return the table name, with no schema or database part.
Returns:
The target table name.
"""
parts = self.stream_name.split("-")
table = self.stream_name if len(parts) == 1 else parts[-1]
return self.conform_name(table, "table")
@property
def schema_name(self) -> Optional[str]:
"""Return the schema name or `None` if using names with no schema part.
Returns:
The target schema name.
"""
# Look for a default_target_scheme in the configuraion fle
default_target_schema: str = self.config.get("default_target_schema", None)
parts = self.stream_name.split("-")
# 1) When default_target_scheme is in the configuration use it
# 2) if the streams are in <schema>-<table> format use the
# stream <schema>
# 3) Return None if you don't find anything
if default_target_schema:
return default_target_schema
if len(parts) in {2, 3}:
# Stream name is a two-part or three-part identifier.
# Use the second-to-last part as the schema name.
stream_schema = self.conform_name(parts[-2], "schema")
return stream_schema
# Schema name not detected.
return None
@property
def database_name(self) -> Optional[str]:
"""Return the DB name or `None` if using names with no database part.
Returns:
The target database name.
"""
return None # Assumes single-DB target context.
@property
def full_table_name(self) -> str:
"""Return the fully qualified table name.
Returns:
The fully qualified table name.
"""
return self.connector.get_fully_qualified_name(
table_name=self.table_name,
schema_name=self.schema_name,
db_name=self.database_name,
)
@property
def full_schema_name(self) -> str:
"""Return the fully qualified schema name.
Returns:
The fully qualified schema name.
"""
return self.connector.get_fully_qualified_name(
schema_name=self.schema_name, db_name=self.database_name
)
def conform_name(self, name: str, object_type: Optional[str] = None) -> str:
"""Conform a stream property name to one suitable for the target system.
Transforms names to snake case by default, applicable to most common DBMSs'.
Developers may override this method to apply custom transformations
to database/schema/table/column names.
Args:
name: Property name.
object_type: One of ``database``, ``schema``, ``table`` or ``column``.
Returns:
The name transformed to snake case.
"""
# strip non-alphanumeric characters, keeping - . _ and spaces
name = re.sub(r"[^a-zA-Z0-9_\-\.\s]", "", name)
# convert to snakecase
name = snakecase(name)
# replace leading digit
return replace_leading_digit(name)
@staticmethod
def _check_conformed_names_not_duplicated(
conformed_property_names: Dict[str, str]
) -> None:
"""Check if conformed names produce duplicate keys.
Args:
conformed_property_names: A name:conformed_name dict map.
Raises:
ConformedNameClashException: if duplicates found.
"""
# group: {'_a': ['1_a'], 'abc': ['aBc', 'abC']}
grouped = defaultdict(list)
for k, v in conformed_property_names.items():
grouped[v].append(k)
# filter
duplicates = list(filter(lambda p: len(p[1]) > 1, grouped.items()))
if duplicates:
raise ConformedNameClashException(
"Duplicate stream properties produced when "
+ f"conforming property names: {duplicates}"
)
def conform_schema(self, schema: dict) -> dict:
"""Return schema dictionary with property names conformed.
Args:
schema: JSON schema dictionary.
Returns:
A schema dictionary with the property names conformed.
"""
conformed_schema = copy(schema)
conformed_property_names = {
key: self.conform_name(key) for key in conformed_schema["properties"].keys()
}
self._check_conformed_names_not_duplicated(conformed_property_names)
conformed_schema["properties"] = {
conformed_property_names[key]: value
for key, value in conformed_schema["properties"].items()
}
return conformed_schema
def conform_record(self, record: dict) -> dict:
"""Return record dictionary with property names conformed.
Args:
record: Dictionary representing a single record.
Returns:
New record dictionary with conformed column names.
"""
conformed_property_names = {key: self.conform_name(key) for key in record}
self._check_conformed_names_not_duplicated(conformed_property_names)
return {conformed_property_names[key]: value for key, value in record.items()}
def setup(self) -> None:
"""Set up Sink.
This method is called on Sink creation, and creates the required Schema and
Table entities in the target database.
"""
if self.schema_name:
self.connector.prepare_schema(self.schema_name)
self.connector.prepare_table(
full_table_name=self.full_table_name,
schema=self.conform_schema(self.schema),
primary_keys=self.key_properties,
as_temp_table=False,
)
@property
def key_properties(self) -> List[str]:
"""Return key properties, conformed to target system naming requirements.
Returns:
A list of key properties, conformed with `self.conform_name()`
"""
return [self.conform_name(key, "column") for key in super().key_properties]
def process_batch(self, context: dict) -> None:
"""Process a batch with the given batch context.
Writes a batch to the SQL target. Developers may override this method
in order to provide a more efficient upload/upsert process.
Args:
context: Stream partition or context dictionary.
"""
# If duplicates are merged, these can be tracked via
# :meth:`~singer_sdk.Sink.tally_duplicate_merged()`.
self.bulk_insert_records(
full_table_name=self.full_table_name,
schema=self.schema,
records=context["records"],
)
def generate_insert_statement(
self,
full_table_name: str,
schema: dict,
) -> Union[str, Executable]:
"""Generate an insert statement for the given records.
Args:
full_table_name: the target table name.
schema: the JSON schema for the new table.
Returns:
An insert statement.
"""
property_names = list(self.conform_schema(schema)["properties"].keys())
statement = dedent(
f"""\
INSERT INTO {full_table_name}
({", ".join(property_names)})
VALUES ({", ".join([f":{name}" for name in property_names])})
"""
)
return statement.rstrip()
def bulk_insert_records(
self,
full_table_name: str,
schema: dict,
records: Iterable[Dict[str, Any]],
) -> Optional[int]:
"""Bulk insert records to an existing destination table.
The default implementation uses a generic SQLAlchemy bulk insert operation.
This method may optionally be overridden by developers in order to provide
faster, native bulk uploads.
Args:
full_table_name: the target table name.
schema: the JSON schema for the new table, to be used when inferring column
names.
records: the input records.
Returns:
True if table exists, False if not, None if unsure or undetectable.
"""
insert_sql = self.generate_insert_statement(
full_table_name,
schema,
)
if isinstance(insert_sql, str):
insert_sql = sqlalchemy.text(insert_sql)
conformed_records = (
[self.conform_record(record) for record in records]
if isinstance(records, list)
else (self.conform_record(record) for record in records)
)
self.logger.info("Inserting with SQL: %s", insert_sql)
self.connector.connection.execute(insert_sql, conformed_records)
return len(conformed_records) if isinstance(conformed_records, list) else None
def merge_upsert_from_table(
self, target_table_name: str, from_table_name: str, join_keys: List[str]
) -> Optional[int]:
"""Merge upsert data from one table to another.
Args:
target_table_name: The destination table name.
from_table_name: The source table name.
join_keys: The merge upsert keys, or `None` to append.
Return:
The number of records copied, if detectable, or `None` if the API does not
report number of records affected/inserted.
Raises:
NotImplementedError: if the merge upsert capability does not exist or is
undefined.
"""
raise NotImplementedError()
def activate_version(self, new_version: int) -> None:
"""Bump the active version of the target table.
Args:
new_version: The version number to activate.
"""
# There's nothing to do if the table doesn't exist yet
# (which it won't the first time the stream is processed)
if not self.connector.table_exists(self.full_table_name):
return
deleted_at = now()
if not self.connector.column_exists(
full_table_name=self.full_table_name,
column_name=self.version_column_name,
):
self.connector.prepare_column(
self.full_table_name,
self.version_column_name,
sql_type=sqlalchemy.types.Integer(),
)
if self.config.get("hard_delete", True):
self.connection.execute(
f"DELETE FROM {self.full_table_name} "
f"WHERE {self.version_column_name} <= {new_version}"
)
return
if not self.connector.column_exists(
full_table_name=self.full_table_name,
column_name=self.soft_delete_column_name,
):
self.connector.prepare_column(
self.full_table_name,
self.soft_delete_column_name,
sql_type=sqlalchemy.types.DateTime(),
)
query = sqlalchemy.text(
f"UPDATE {self.full_table_name}\n"
f"SET {self.soft_delete_column_name} = :deletedate \n"
f"WHERE {self.version_column_name} < :version \n"
f" AND {self.soft_delete_column_name} IS NULL\n"
)
query = query.bindparams(
bindparam("deletedate", value=deleted_at, type_=sqlalchemy.types.DateTime),
bindparam("version", value=new_version, type_=sqlalchemy.types.Integer),
)
self.connector.connection.execute(query)
__all__ = ["SQLSink", "SQLConnector"]