Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
skip normalizing problematic timestamp values
Browse files Browse the repository at this point in the history
  • Loading branch information
dlawin committed Nov 30, 2023
1 parent 283bbac commit 0ec8b45
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion data_diff/databases/redshift.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import ClassVar, List, Dict, Type
from typing import ClassVar, List, Dict, Optional, Type

import attrs

Expand All @@ -10,6 +10,7 @@
FractionalType,
DbPath,
TimestampTZ,
UnknownColType,
)
from data_diff.databases.postgresql import (
BaseDialect,
Expand Down Expand Up @@ -54,6 +55,15 @@ def md5_as_hex(self, s: str) -> str:
def normalize_timestamp(self, value: str, coltype: TemporalType) -> str:
if coltype.rounds:
timestamp = f"{value}::timestamp(6)"

# redshift allows some problematic timestamp values, don't normalize those (epoch calcs fail)
case_start = (
f"case when "
+ f"({timestamp} < '1901-01-01 00:00'::timestamp(6) or {timestamp} >= '2038-01-01'::timestamp(6))"
+ f"then {timestamp}::varchar else"
)
case_end = f"end"

# Get seconds since epoch. Redshift doesn't support milli- or micro-seconds.
secs = f"timestamp 'epoch' + round(extract(epoch from {timestamp})::decimal(38)"
# Get the milliseconds from timestamp.
Expand All @@ -65,8 +75,14 @@ def normalize_timestamp(self, value: str, coltype: TemporalType) -> str:
timestamp6 = (
f"to_char({epoch}, -6+{coltype.precision}) * interval '0.000001 seconds', 'YYYY-mm-dd HH24:MI:SS.US')"
)
padded = self._add_padding(coltype, timestamp6)
return f"{case_start} {padded} {case_end}"
else:
timestamp6 = f"to_char({value}::timestamp(6), 'YYYY-mm-dd HH24:MI:SS.US')"
padded = self._add_padding(coltype, timestamp6)
return padded

def _add_padding(self, coltype: TemporalType, timestamp6: str):
return (
f"RPAD(LEFT({timestamp6}, {TIMESTAMP_PRECISION_POS+coltype.precision}), {TIMESTAMP_PRECISION_POS+6}, '0')"
)
Expand Down

0 comments on commit 0ec8b45

Please sign in to comment.