-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathsql.py
421 lines (347 loc) · 13.8 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
"""Sink classes load data to SQL targets."""
from __future__ import annotations
import re
import typing as t
from collections import defaultdict
from copy import copy
from textwrap import dedent
import sqlalchemy as sa
from sqlalchemy.sql import quoted_name
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
from singer_sdk.helpers._util import utc_now
from singer_sdk.sinks.batch import BatchSink
if t.TYPE_CHECKING:
from sqlalchemy.sql import Executable
from singer_sdk.connectors.sql import FullyQualifiedName
from singer_sdk.target_base import Target
_C = t.TypeVar("_C", bound=SQLConnector)
class SQLSink(BatchSink, t.Generic[_C]):
"""SQL-type sink type."""
connector_class: type[_C]
soft_delete_column_name = "_sdc_deleted_at"
version_column_name = "_sdc_table_version"
def __init__(
self,
target: Target,
stream_name: str,
schema: dict,
key_properties: t.Sequence[str] | None,
connector: _C | None = 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: _C
self._connector = connector or self.connector_class(dict(target.config))
super().__init__(target, stream_name, schema, key_properties)
@property
def connector(self) -> _C:
"""The connector object.
Returns:
The connector object.
"""
return self._connector
@property
def connection(self) -> sa.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) -> str | None:
"""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
return self.conform_name(parts[-2], "schema") if len(parts) in {2, 3} else None
@property
def database_name(self) -> str | None:
"""Return the DB name or `None` if using names with no database part."""
# Assumes single-DB target context.
@property
def full_table_name(self) -> FullyQualifiedName:
"""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) -> FullyQualifiedName:
"""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( # noqa: PLR6301
self,
name: str,
object_type: str | None = None, # noqa: ARG002
) -> 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
name = re.sub(r"[^a-zA-Z0-9_\-\.\s]", "", name)
# strip leading/trailing whitespace,
# transform to lowercase and replace - . and spaces to _
name = (
name.lower()
.lstrip()
.rstrip()
.replace(".", "_")
.replace("-", "_")
.replace(" ", "_")
)
# 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']} # noqa: ERA001
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:
msg = (
"Duplicate stream properties produced when conforming property names: "
f"{duplicates}"
)
raise ConformedNameClashException(msg)
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"]
}
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) -> t.Sequence[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 | FullyQualifiedName,
schema: dict,
) -> 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())
column_identifiers = [
self.connector.quote(quoted_name(name, quote=True))
for name in property_names
]
statement = dedent(
f"""\
INSERT INTO {full_table_name}
({", ".join(column_identifiers)})
VALUES ({", ".join([f":{name}" for name in property_names])})
""",
)
return statement.rstrip()
def bulk_insert_records(
self,
full_table_name: str | FullyQualifiedName,
schema: dict,
records: t.Iterable[dict[str, t.Any]],
) -> int | None:
"""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 = sa.text(insert_sql)
conformed_records = [self.conform_record(record) for record in records]
property_names = list(self.conform_schema(schema)["properties"].keys())
# Create new record dicts with missing properties filled in with None
new_records = [
{name: record.get(name) for name in property_names}
for record in conformed_records
]
self.logger.info("Inserting with SQL: %s", insert_sql)
with self.connector._connect() as conn, conn.begin(): # noqa: SLF001
result = conn.execute(insert_sql, new_records)
return result.rowcount
def merge_upsert_from_table(
self,
target_table_name: str,
from_table_name: str,
join_keys: list[str],
) -> int | None:
"""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 = utc_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=sa.types.Integer(),
)
if self.config.get("hard_delete", False):
self.connector.delete_old_versions(
full_table_name=self.full_table_name,
version_column_name=self.version_column_name,
current_version=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=sa.types.DateTime(),
)
query = sa.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_=sa.types.DateTime),
bindparam("version", value=new_version, type_=sa.types.Integer),
)
with self.connector._connect() as conn, conn.begin(): # noqa: SLF001
conn.execute(query)
__all__ = ["SQLConnector", "SQLSink"]