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

Commit f521d8a

Browse files
committed
Fixed Redshift
1 parent b28066f commit f521d8a

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

data_diff/database.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,21 @@ class Redshift(Postgres):
534534
def md5_to_int(self, s: str) -> str:
535535
return f"strtol(substring(md5({s}), {1+MD5_HEXDIGITS-CHECKSUM_HEXDIGITS}), 16)::decimal(38)"
536536

537+
def normalize_value_by_type(self, value: str, coltype: ColType) -> str:
538+
if isinstance(coltype, TemporalType):
539+
if coltype.rounds:
540+
timestamp = f"{value}::timestamp(6)"
541+
secs = f"timestamp 'epoch' + round(extract(epoch from {timestamp})::decimal(38)"
542+
ms = f"extract(ms from {timestamp})"
543+
us = f"extract(us from {timestamp})"
544+
epoch = f"{secs}*1000000 + {ms}*1000 + {us}"
545+
timestamp6 = f"to_char({epoch}, -6+{coltype.precision}) * interval '0.000001 seconds', 'YYYY-mm-dd HH24:MI:SS.US')"
546+
else:
547+
timestamp6 = f"to_char({value}::timestamp(6), 'YYYY-mm-dd HH24:MI:SS.US')"
548+
return f"RPAD(LEFT({timestamp6}, {TIMESTAMP_PRECISION_POS+coltype.precision}), {TIMESTAMP_PRECISION_POS+6}, '0')"
549+
550+
return self.to_string(f"{value}")
551+
537552

538553
class MsSQL(ThreadedDatabase):
539554
"AKA sql-server"

tests/test_database_types.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
"int": [127, -3, -9, 37, 15, 127],
1818
"datetime_no_timezone": [
1919
"2020-01-01 15:10:10",
20-
"2020-01-01 9:9:9",
21-
"2022-01-01 15:10:01.139",
22-
"2022-01-01 15:10:02.020409",
23-
"2022-01-01 15:10:03.003030",
24-
"2022-01-01 15:10:05.009900",
20+
"2020-02-01 9:9:9",
21+
"2022-03-01 15:10:01.139",
22+
"2022-04-01 15:10:02.020409",
23+
"2022-05-01 15:10:03.003030",
24+
"2022-06-01 15:10:05.009900",
2525
],
2626
"float": [0.0, 0.1, 0.10, 10.0, 100.98],
2727
}
@@ -100,7 +100,7 @@
100100
# "int",
101101
],
102102
"datetime_no_timezone": [
103-
# "TIMESTAMP",
103+
"TIMESTAMP",
104104
],
105105
# https://docs.aws.amazon.com/redshift/latest/dg/r_Numeric_types201.html#r_Numeric_types201-floating-point-types
106106
"float": [

0 commit comments

Comments
 (0)