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

Commit 0ec8b45

Browse files
committed
skip normalizing problematic timestamp values
#786
1 parent 283bbac commit 0ec8b45

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

data_diff/databases/redshift.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import ClassVar, List, Dict, Type
1+
from typing import ClassVar, List, Dict, Optional, Type
22

33
import attrs
44

@@ -10,6 +10,7 @@
1010
FractionalType,
1111
DbPath,
1212
TimestampTZ,
13+
UnknownColType,
1314
)
1415
from data_diff.databases.postgresql import (
1516
BaseDialect,
@@ -54,6 +55,15 @@ def md5_as_hex(self, s: str) -> str:
5455
def normalize_timestamp(self, value: str, coltype: TemporalType) -> str:
5556
if coltype.rounds:
5657
timestamp = f"{value}::timestamp(6)"
58+
59+
# redshift allows some problematic timestamp values, don't normalize those (epoch calcs fail)
60+
case_start = (
61+
f"case when "
62+
+ f"({timestamp} < '1901-01-01 00:00'::timestamp(6) or {timestamp} >= '2038-01-01'::timestamp(6))"
63+
+ f"then {timestamp}::varchar else"
64+
)
65+
case_end = f"end"
66+
5767
# Get seconds since epoch. Redshift doesn't support milli- or micro-seconds.
5868
secs = f"timestamp 'epoch' + round(extract(epoch from {timestamp})::decimal(38)"
5969
# Get the milliseconds from timestamp.
@@ -65,8 +75,14 @@ def normalize_timestamp(self, value: str, coltype: TemporalType) -> str:
6575
timestamp6 = (
6676
f"to_char({epoch}, -6+{coltype.precision}) * interval '0.000001 seconds', 'YYYY-mm-dd HH24:MI:SS.US')"
6777
)
78+
padded = self._add_padding(coltype, timestamp6)
79+
return f"{case_start} {padded} {case_end}"
6880
else:
6981
timestamp6 = f"to_char({value}::timestamp(6), 'YYYY-mm-dd HH24:MI:SS.US')"
82+
padded = self._add_padding(coltype, timestamp6)
83+
return padded
84+
85+
def _add_padding(self, coltype: TemporalType, timestamp6: str):
7086
return (
7187
f"RPAD(LEFT({timestamp6}, {TIMESTAMP_PRECISION_POS+coltype.precision}), {TIMESTAMP_PRECISION_POS+6}, '0')"
7288
)

0 commit comments

Comments
 (0)