Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scd2 extension: active record literal changes #1275

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions dlt/common/schema/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

from dlt.common.data_types import TDataType
from dlt.common.normalizers.typing import TNormalizersConfig
from dlt.common.typing import TSortOrder
from dlt.common.typing import TSortOrder, TAnyDateTime
from dlt.common.pendulum import pendulum

try:
from pydantic import BaseModel as _PydanticBaseModel
Expand All @@ -33,8 +34,6 @@
LOADS_TABLE_NAME = "_dlt_loads"
STATE_TABLE_NAME = "_dlt_pipeline_state"
DLT_NAME_PREFIX = "_dlt"
DEFAULT_VALIDITY_COLUMN_NAMES = ["_dlt_valid_from", "_dlt_valid_to"]
"""Default values for validity column names used in `scd2` merge strategy."""

TColumnProp = Literal[
"name",
Expand Down Expand Up @@ -161,6 +160,9 @@ class NormalizerInfo(TypedDict, total=True):
WRITE_DISPOSITIONS: Set[TWriteDisposition] = set(get_args(TWriteDisposition))
MERGE_STRATEGIES: Set[TLoaderMergeStrategy] = set(get_args(TLoaderMergeStrategy))

DEFAULT_VALIDITY_COLUMN_NAMES = ["_dlt_valid_from", "_dlt_valid_to"]
"""Default values for validity column names used in `scd2` merge strategy."""


class TWriteDispositionDict(TypedDict):
disposition: TWriteDisposition
Expand All @@ -169,6 +171,7 @@ class TWriteDispositionDict(TypedDict):
class TMergeDispositionDict(TWriteDispositionDict, total=False):
strategy: Optional[TLoaderMergeStrategy]
validity_column_names: Optional[List[str]]
active_record_timestamp: Optional[TAnyDateTime]
row_version_column_name: Optional[str]


Expand Down
12 changes: 11 additions & 1 deletion dlt/common/schema/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from copy import deepcopy, copy
from typing import Dict, List, Sequence, Tuple, Type, Any, cast, Iterable, Optional, Union

from dlt.common.pendulum import pendulum
from dlt.common.time import ensure_pendulum_datetime
from dlt.common.json import json
from dlt.common.data_types import TDataType
from dlt.common.exceptions import DictValidationException
Expand Down Expand Up @@ -481,7 +483,8 @@ def get_columns_names_with_prop(
return [
c["name"]
for c in table["columns"].values()
if bool(c.get(column_prop, False)) and (include_incomplete or is_complete_column(c))
if (bool(c.get(column_prop, False)) or c.get(column_prop, False) is None)
and (include_incomplete or is_complete_column(c))
]


Expand Down Expand Up @@ -525,6 +528,13 @@ def get_validity_column_names(table: TTableSchema) -> List[Optional[str]]:
]


def get_active_record_timestamp(table: TTableSchema) -> Optional[pendulum.DateTime]:
# method assumes a column with "x-active-record-timestamp" property exists
cname = get_first_column_name_with_prop(table, "x-active-record-timestamp")
hint_val = table["columns"][cname]["x-active-record-timestamp"] # type: ignore[typeddict-item]
return None if hint_val is None else ensure_pendulum_datetime(hint_val)


def merge_schema_updates(schema_updates: Sequence[TSchemaUpdate]) -> TSchemaTables:
aggregated_update: TSchemaTables = {}
for schema_update in schema_updates:
Expand Down
19 changes: 12 additions & 7 deletions dlt/destinations/sql_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
get_first_column_name_with_prop,
get_dedup_sort_tuple,
get_validity_column_names,
get_active_record_timestamp,
DEFAULT_MERGE_STRATEGY,
)
from dlt.common.storages.load_storage import ParsedLoadJobFileName
Expand All @@ -24,10 +25,6 @@
from dlt.pipeline.current import load_package as current_load_package


HIGH_TS = pendulum.datetime(9999, 12, 31)
"""High timestamp used to indicate active records in `scd2` merge strategy."""


class SqlJobParams(TypedDict, total=False):
replace: Optional[bool]
table_chain_create_table_statements: Dict[str, Sequence[str]]
Expand Down Expand Up @@ -537,12 +534,20 @@ def gen_scd2_sql(
current_load_package()["state"]["created_at"],
caps.timestamp_precision,
)
active_record_ts = format_datetime_literal(HIGH_TS, caps.timestamp_precision)
active_record_timestamp = get_active_record_timestamp(root_table)
if active_record_timestamp is None:
active_record_literal = "NULL"
is_active_clause = f"{to} IS NULL"
else: # it's a datetime
active_record_literal = format_datetime_literal(
active_record_timestamp, caps.timestamp_precision
)
is_active_clause = f"{to} = {active_record_literal}"

# retire updated and deleted records
sql.append(f"""
UPDATE {root_table_name} SET {to} = {boundary_ts}
WHERE {to} = {active_record_ts}
WHERE {is_active_clause}
AND {hash_} NOT IN (SELECT {hash_} FROM {staging_root_table_name});
""")

Expand All @@ -551,7 +556,7 @@ def gen_scd2_sql(
col_str = ", ".join([c for c in columns if c not in (from_, to)])
sql.append(f"""
INSERT INTO {root_table_name} ({col_str}, {from_}, {to})
SELECT {col_str}, {boundary_ts} AS {from_}, {active_record_ts} AS {to}
SELECT {col_str}, {boundary_ts} AS {from_}, {active_record_literal} AS {to}
FROM {staging_root_table_name} AS s
WHERE {hash_} NOT IN (SELECT {hash_} FROM {root_table_name});
""")
Expand Down
6 changes: 2 additions & 4 deletions dlt/extract/hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,9 @@ def _merge_merge_disposition_dict(dict_: Dict[str, Any]) -> None:
"data_type": "timestamp",
"nullable": True,
"x-valid-to": True,
"x-active-record-timestamp": mddict.get("active_record_timestamp"),
}
if mddict.get("row_version_column_name") is None:
hash_ = "_dlt_id"
else:
hash_ = mddict["row_version_column_name"]
hash_ = mddict.get("row_version_column_name", "_dlt_id")
dict_["columns"][hash_] = {
"name": hash_,
"nullable": False,
Expand Down
Loading
Loading