-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathextractor.py
More file actions
426 lines (368 loc) · 15.3 KB
/
Copy pathextractor.py
File metadata and controls
426 lines (368 loc) · 15.3 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
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
422
423
424
425
426
import csv
from abc import ABC, abstractmethod
from collections import OrderedDict
from collections.abc import Iterator
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
from typing import Generic, override
import psycopg
import snowflake.connector
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from loguru import logger
from psycopg.rows import dict_row
from pydantic import TypeAdapter
from snowflake.connector import DictCursor, SnowflakeConnection
from snowflake.snowpark import Session
from constants import DEFAULT_MIN_DATE
from db_utils import get_connection_string
from load_partition import LoadPartition
from model.base_model import (
DbType,
LoadMode,
Source,
T,
format_date_opt,
)
from model.load_progress import LoadProgress
from settings import (
ALLOW_EXTRACTOR_QUERY_LOGGING,
BATCH_MULTIPLIER,
ENABLE_DATE_PARTITIONS,
IDR_ACCOUNT,
IDR_DATABASE,
IDR_PRIVATE_KEY,
IDR_SCHEMA,
IDR_USERNAME,
IDR_WAREHOUSE,
MIN_BATCH_COMPLETION_DATE,
)
from timer import Timer
@dataclass
class CsvFile:
cols: list[str]
table: str
csv_file: Path
def cols_str(self) -> str:
return ",".join(self.cols)
# TODO: UP046 seems to cause issues with pyright
class Extractor(ABC, Generic[T]): # noqa: UP046
def __init__(self, cls: type[T], partition: LoadPartition) -> None:
self.cls = cls
self.type_adapter = TypeAdapter(list[self.cls])
self.partition = partition
self.cursor_execute_timer = Timer("cursor_execute", cls, partition)
self.cursor_fetch_timer = Timer("cursor_fetch", cls, partition)
self.transform_timer = Timer("transform", cls, partition)
@abstractmethod
def extract_many(self, sql: str, params: dict[str, DbType]) -> Iterator[list[T]]:
pass
@abstractmethod
def reconnect(self) -> None:
pass
@abstractmethod
def close(self) -> None:
pass
def _coalesce_dates(self, cols: list[str]) -> list[str]:
return [f"COALESCE({col}, '{DEFAULT_MIN_DATE}')" for col in cols]
def _greatest_col(self, cols: list[str]) -> str:
return f"GREATEST({','.join(cols)})"
def _get_batch_size(self) -> int:
if ENABLE_DATE_PARTITIONS:
# Larger tables take up more memory, so we'll try to normalize
# the total memory used here based on the number of columns
return round(BATCH_MULTIPLIER / len(self.cls.columns_raw()))
# If date partitioning is not enabled, the number of concurrent jobs will be small
return 100_000
def get_query(self, start_time: datetime, source: Source) -> str:
query = self.cls.fetch_query(self.partition, start_time, source)
columns = ",".join(self.cls.column_aliases())
columns_raw = ",".join(self.cls.columns_raw())
return query.replace("{COLUMNS}", columns).replace("{COLUMNS_NO_ALIAS}", columns_raw)
def build_filter_columns(self, progress: LoadProgress | None) -> str:
is_historical = progress is None or progress.is_historical()
# GREATEST doesn't work with nulls so we need to coalesce here
batch_timestamp_cols = self._coalesce_dates(
self.cls.format_aliases(self.cls.batch_timestamp_col(is_historical))
)
update_timestamp_cols = self._coalesce_dates(
self.cls.format_aliases(self.cls.update_timestamp_col())
)
# We need to create batches using the most recent timestamp from all of the
# insert/update timestamps
return self._greatest_col([*batch_timestamp_cols, *update_timestamp_cols])
def extract_idr_data(
self, progress: LoadProgress | None, start_time: datetime, source: Source
) -> Iterator[list[T]]:
fetch_query = self.get_query(start_time, source)
# We need to create batches using the most recent timestamp from all of the
# insert/update timestamps
batch_timestamp_clause = self.build_filter_columns(progress)
min_transaction_date = self.cls.model_type().min_transaction_date
batch_id_clause = ""
batch_id_col = self.cls.batch_id_col_alias()
additional_order_by = list(
OrderedDict.fromkeys(
[x for x in [batch_id_col, *self.cls.ordered_pkeys()] if x is not None]
) # Use an OrderedDict as an ordered set because there is no ordered set in stdlib
)
logger.info("extracting {}", self.cls.table())
order_by = f"ORDER BY {', '.join([batch_timestamp_clause, *additional_order_by])}"
if progress is None:
# No saved progress, process the whole table from the beginning
return self.extract_many(
fetch_query.replace(
"{WHERE_CLAUSE}",
f"WHERE ({batch_timestamp_clause} >= %(timestamp)s)",
)
.replace("{FILTER_OP}", ">=")
.replace("{LAST_TS}", "%(timestamp)s")
.replace("{ORDER_BY}", order_by)
.replace("{TABLESAMPLE}", "")
.replace("{LIMIT}", "")
.replace("{BASE_CLAIMS_WHERE_FILTERS}", ""),
{"timestamp": min_transaction_date},
)
previous_batch_complete = progress.batch_complete_ts >= progress.job_start_ts
min_batch_completion_date = format_date_opt(MIN_BATCH_COMPLETION_DATE)
if (
previous_batch_complete
and min_batch_completion_date
and progress.batch_complete_ts > min_batch_completion_date
):
# If we've set a min completion date, we don't need to reprocess any batches that have
# already completed within the given timeframe.
# This helps for large loads that may have been interrupted recently.
return iter([])
# If we've completed the last batch, there shouldn't be any additional records
# with the same timestamp/id.
# Additionally, if there's a batch_id column, records with the same timestamp will be
# filtered by the batch_id filter.
filter_op = ">" if previous_batch_complete or batch_id_col is not None else ">="
# insertion timestamps aren't always representative of the time the data is available in
# Snowflake, so we should always start loading from the most recent timestamp
# that we've already fetched
compare_timestamp = max(min_transaction_date, progress.last_ts)
if batch_id_col is not None:
batch_id_clause = f"""
OR (
{batch_timestamp_clause} = %(timestamp)s
AND {batch_id_col} {filter_op} {progress.last_id}
)"""
# Saved progress found, start processing from where we left off
return self.extract_many(
fetch_query.replace(
"{WHERE_CLAUSE}",
f"""
WHERE (
{batch_timestamp_clause} {filter_op} %(timestamp)s
{batch_id_clause}
)
""",
)
.replace("{FILTER_OP}", filter_op)
.replace("{LAST_TS}", "%(timestamp)s")
.replace("{ORDER_BY}", order_by)
.replace("{TABLESAMPLE}", "")
.replace("{LIMIT}", "")
.replace("{BASE_CLAIMS_WHERE_FILTERS}", ""),
{"timestamp": compare_timestamp},
)
def extract_full_idr_data(self, source: Source) -> Iterator[list[T]]:
start_time = self.cls.model_type().min_transaction_date
fetch_query = self.get_query(start_time, source)
logger.info("extracting full {}", self.cls.table())
return self.extract_many(
fetch_query.replace("{LAST_TS}", "%(timestamp)s"), {"timestamp": start_time}
)
def _transform(self, batch: list[dict[str, DbType]]) -> list[T]:
self.transform_timer.start()
res = self.type_adapter.validate_python(
[{k.lower(): v for k, v in row.items()} for row in batch]
)
self.transform_timer.stop()
return res
class DbExecutor(ABC):
@abstractmethod
def copy(self, file: CsvFile) -> None:
pass
@abstractmethod
def query(self, sql: str, params: dict[str, DbType] | None = None) -> list[dict[str, DbType]]:
pass
@abstractmethod
def execute(self, sql: str, params: dict[str, DbType] | None = None) -> None:
pass
@abstractmethod
def commit(self) -> None:
pass
class PostgresExtractor(Extractor[T]):
def __init__(self, cls: type[T], partition: LoadPartition, load_mode: LoadMode) -> None:
super().__init__(cls, partition)
self.connection_string = get_connection_string(load_mode)
self.conn = psycopg.connect(self.connection_string)
@override
def reconnect(self) -> None:
self.conn = psycopg.connect(self.connection_string)
@override
def extract_many(
self,
sql: str,
params: dict[str, DbType],
) -> Iterator[list[T]]:
if ALLOW_EXTRACTOR_QUERY_LOGGING:
logger.debug(sql)
batch_size = self._get_batch_size()
with self.conn.cursor(row_factory=dict_row) as cur:
cur.execute(sql, params) # type: ignore
batch = cur.fetchmany(batch_size)
while len(batch) > 0:
yield self._transform(batch)
batch = cur.fetchmany(batch_size)
def extract_single(self, sql: str, params: dict[str, DbType]) -> T | None:
with self.conn.cursor(row_factory=dict_row) as cur:
cur.execute(sql, params) # type: ignore
res = cur.fetchone()
if res:
return self._transform([res])[0]
return None
@override
def close(self) -> None:
self.conn.close()
class PostgresExecutor(DbExecutor):
def __init__(self, conn: psycopg.connection.Connection) -> None:
self.conn = conn
@override
def execute(self, sql: str, params: dict[str, DbType] | None = None) -> None:
cur = self.conn.cursor(row_factory=dict_row)
cur.execute(sql, params) # type: ignore
@override
def query(self, sql: str, params: dict[str, DbType] | None = None) -> list[dict[str, DbType]]:
cur = self.conn.cursor(row_factory=dict_row)
res = cur.execute(sql, params) # type: ignore
return res.fetchall() # type: ignore
@override
def commit(self) -> None:
self.conn.commit()
@override
def copy(self, file: CsvFile) -> None:
with self.conn.cursor(row_factory=dict_row) as cur, file.csv_file.open() as f:
reader = csv.DictReader(f)
# skip empty files
if reader.fieldnames is None:
return
with cur.copy(
f"COPY {file.table} ({file.cols_str()}) FROM STDIN" # type: ignore
) as copy:
for row in reader:
copy.write_row([row[c] or None for c in file.cols])
class SnowflakeExtractor(Extractor[T]):
def __init__(self, cls: type[T], partition: LoadPartition) -> None:
super().__init__(cls, partition)
self.conn = SnowflakeExtractor.connect()
@override
def reconnect(self) -> None:
self.conn = SnowflakeExtractor.connect()
@staticmethod
def connect() -> SnowflakeConnection:
private_key = serialization.load_pem_private_key(
IDR_PRIVATE_KEY.encode(),
password=None,
backend=default_backend(),
)
private_key_bytes = private_key.private_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
)
return snowflake.connector.connect( # type: ignore
user=IDR_USERNAME,
private_key=private_key_bytes,
account=IDR_ACCOUNT,
warehouse=IDR_WAREHOUSE,
database=IDR_DATABASE,
schema=IDR_SCHEMA,
)
@override
def extract_many(
self,
sql: str,
params: dict[str, DbType],
) -> Iterator[list[T]]:
cur = None
if ALLOW_EXTRACTOR_QUERY_LOGGING:
logger.debug(sql)
try:
self.cursor_execute_timer.start()
cur = self.conn.cursor(DictCursor)
cur.execute(sql, params)
self.cursor_execute_timer.stop()
self.cursor_fetch_timer.start()
# fetchmany can return list[dict] or list[tuple] but we'll only use
# queries that return dicts
batch_size = self._get_batch_size()
batch: list[dict[str, DbType]] = cur.fetchmany(batch_size)
self.cursor_fetch_timer.stop()
while len(batch) > 0: # type: ignore
yield self._transform(batch)
self.cursor_fetch_timer.start()
batch = cur.fetchmany(batch_size)
self.cursor_fetch_timer.stop()
return
finally:
if cur:
cur.close()
@override
def close(self) -> None:
self.conn.close()
class SnowflakeExecutor(DbExecutor):
def __init__(self) -> None:
private_key = serialization.load_pem_private_key(
IDR_PRIVATE_KEY.encode(),
password=None,
backend=default_backend(),
)
private_key_bytes = private_key.private_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
)
self.session = Session.builder.configs(
{
"account": IDR_ACCOUNT,
"user": IDR_USERNAME,
"private_key": private_key_bytes, # type: ignore
"warehouse": IDR_WAREHOUSE,
"database": IDR_DATABASE,
"schema": IDR_SCHEMA,
}
).create()
self.conn = SnowflakeExtractor.connect()
@override
def commit(self) -> None:
self.conn.commit()
@override
def copy(self, file: CsvFile) -> None:
self.session.sql("create or replace temp stage source_stage").collect()
self.session.file.put(str(file.csv_file.absolute()), "@source_stage")
self.session.sql(f"""COPY INTO
{file.table}
FROM @source_stage/{file.csv_file.name}
FILE_FORMAT = (
TYPE = 'CSV',
PARSE_HEADER = TRUE
ERROR_ON_COLUMN_COUNT_MISMATCH = FALSE
FIELD_OPTIONALLY_ENCLOSED_BY = '"'
)
MATCH_BY_COLUMN_NAME = CASE_INSENSITIVE
""").collect()
self.session.commit()
@override
def execute(self, sql: str, params: dict[str, DbType] | None = None) -> None:
cur = self.conn.cursor(DictCursor)
cur.execute(sql, params)
@override
def query(self, sql: str, params: dict[str, DbType] | None = None) -> list[dict[str, DbType]]:
cur = self.conn.cursor(DictCursor)
res = cur.execute(sql, params).fetchall() # type: ignore
return [{k.lower(): r[k] for k in r} for r in res] # type: ignore