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

Commit 24016cc

Browse files
committed
fix unit tests by handling nulls
nulls were getting greatest()'d during materialize
1 parent 9b32890 commit 24016cc

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

data_diff/databases/postgresql.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,14 @@ def md5_as_hex(self, s: str) -> str:
102102
return f"md5({s})"
103103

104104
def normalize_timestamp(self, value: str, coltype: TemporalType) -> str:
105+
def _add_padding(coltype: TemporalType, timestamp6: str):
106+
return f"RPAD(LEFT({timestamp6}, {TIMESTAMP_PRECISION_POS+coltype.precision}), {TIMESTAMP_PRECISION_POS+6}, '0')"
107+
105108
if coltype.rounds:
109+
# NULL value expected to return NULL after normalization
110+
null_case_begin = f"CASE WHEN {value} IS NULL THEN NULL ELSE "
111+
null_case_end = "END"
112+
106113
# 294277 or 4714 BC would be out of range, make sure we can't round to that
107114
# TODO test timezones for overflow?
108115
max_timestamp = "294276-12-31 23:59:59.0000"
@@ -112,20 +119,23 @@ def normalize_timestamp(self, value: str, coltype: TemporalType) -> str:
112119

113120
interval = format((0.5 * (10 ** (-coltype.precision))), f".{coltype.precision+1}f")
114121

115-
final_timestamp = (
122+
rounded_timestamp = (
116123
f"left(to_char(least('{max_timestamp}'::timestamp, {timestamp})"
117124
f"+ interval '{interval}', 'YYYY-mm-dd HH24:MI:SS.US'),"
118125
f"length(to_char(least('{max_timestamp}'::timestamp, {timestamp})"
119126
f"+ interval '{interval}', 'YYYY-mm-dd HH24:MI:SS.US')) - (6-{coltype.precision}))"
120127
)
121128

129+
padded = _add_padding(coltype, rounded_timestamp)
130+
return f"{null_case_begin} {padded} {null_case_end}"
131+
122132
# TODO years with > 4 digits not padded correctly
123133
# current w/ precision 6: 294276-12-31 23:59:59.0000
124134
# should be 294276-12-31 23:59:59.000000
125135
else:
126-
final_timestamp = f"to_char({value}::timestamp(6), 'YYYY-mm-dd HH24:MI:SS.US')"
127-
128-
return f"RPAD(LEFT({final_timestamp}, {TIMESTAMP_PRECISION_POS+coltype.precision}), {TIMESTAMP_PRECISION_POS+6}, '0')"
136+
rounded_timestamp = f"to_char({value}::timestamp(6), 'YYYY-mm-dd HH24:MI:SS.US')"
137+
padded = _add_padding(coltype, rounded_timestamp)
138+
return padded
129139

130140
def normalize_number(self, value: str, coltype: FractionalType) -> str:
131141
return self.to_string(f"{value}::decimal(38, {coltype.precision})")

0 commit comments

Comments
 (0)