This repository was archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathredshift.py
More file actions
165 lines (131 loc) · 5.48 KB
/
redshift.py
File metadata and controls
165 lines (131 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from typing import ClassVar, List, Dict, Type
import attrs
from data_diff.abcs.database_types import (
ColType,
Float,
JSON,
TemporalType,
FractionalType,
DbPath,
TimestampTZ,
)
from data_diff.databases.postgresql import (
BaseDialect,
PostgreSQL,
MD5_HEXDIGITS,
CHECKSUM_HEXDIGITS,
CHECKSUM_OFFSET,
TIMESTAMP_PRECISION_POS,
PostgresqlDialect,
)
@attrs.define(frozen=False)
class Dialect(PostgresqlDialect):
name = "Redshift"
TYPE_CLASSES: ClassVar[Dict[str, Type[ColType]]] = {
**PostgresqlDialect.TYPE_CLASSES,
"double": Float,
"real": Float,
"super": JSON,
}
SUPPORTS_INDEXES = False
def concat(self, items: List[str]) -> str:
joined_exprs = " || ".join(items)
return f"({joined_exprs})"
def is_distinct_from(self, a: str, b: str) -> str:
return f"({a} IS NULL != {b} IS NULL) OR ({a}!={b})"
def type_repr(self, t) -> str:
if isinstance(t, TimestampTZ):
return f"timestamptz"
return super().type_repr(t)
def md5_as_int(self, s: str) -> str:
return f"strtol(substring(md5({s}), {1+MD5_HEXDIGITS-CHECKSUM_HEXDIGITS}), 16)::decimal(38) - {CHECKSUM_OFFSET}"
def md5_as_hex(self, s: str) -> str:
return f"md5({s})"
def normalize_number(self, value: str, coltype: FractionalType) -> str:
return self.to_string(f"{value}::decimal(38,{coltype.precision})")
def normalize_json(self, value: str, _coltype: JSON) -> str:
return f"nvl2({value}, json_serialize({value}), NULL)"
@attrs.define(frozen=False, init=False, kw_only=True)
class Redshift(PostgreSQL):
DIALECT_CLASS: ClassVar[Type[BaseDialect]] = Dialect
CONNECT_URI_HELP = "redshift://<user>:<password>@<host>/<database>"
CONNECT_URI_PARAMS = ["database?"]
def select_table_schema(self, path: DbPath) -> str:
database, schema, table = self._normalize_table_path(path)
info_schema_path = ["information_schema", "columns"]
if database:
info_schema_path.insert(0, database)
return (
f"SELECT column_name, data_type, datetime_precision, numeric_precision, numeric_scale FROM {'.'.join(info_schema_path)} "
f"WHERE table_name = '{table.lower()}' AND table_schema = '{schema.lower()}'"
)
def select_external_table_schema(self, path: DbPath) -> str:
database, schema, table = self._normalize_table_path(path)
db_clause = ""
if database:
db_clause = f" AND redshift_database_name = '{database.lower()}'"
return (
f"""SELECT
columnname AS column_name
, CASE WHEN external_type = 'string' THEN 'varchar' ELSE external_type END AS data_type
, NULL AS datetime_precision
, NULL AS numeric_precision
, NULL AS numeric_scale
FROM svv_external_columns
WHERE tablename = '{table.lower()}' AND schemaname = '{schema.lower()}'
"""
+ db_clause
)
def query_external_table_schema(self, path: DbPath) -> Dict[str, tuple]:
rows = self.query(self.select_external_table_schema(path), list)
if not rows:
raise RuntimeError(f"{self.name}: Table '{'.'.join(path)}' does not exist, or has no columns")
schema_dict = self._normalize_schema_info(rows)
return schema_dict
def select_view_columns(self, path: DbPath) -> str:
_, schema, table = self._normalize_table_path(path)
return """select * from pg_get_cols('{}.{}')
cols(col_name name, col_type varchar)
""".format(schema, table)
def query_pg_get_cols(self, path: DbPath) -> Dict[str, tuple]:
rows = self.query(self.select_view_columns(path), list)
if not rows:
raise RuntimeError(f"{self.name}: View '{'.'.join(path)}' does not exist, or has no columns")
schema_dict = self._normalize_schema_info(rows)
return schema_dict
# when using a non-information_schema source, strip (N) from type(N) etc. to match
# typical information_schema output
def _normalize_schema_info(self, rows) -> Dict[str, tuple]:
schema_dict = {}
for r in rows:
col_name = r[0]
type_info = r[1].split("(")
base_type = type_info[0]
precision = None
scale = None
if len(type_info) > 1:
if base_type == "numeric":
precision, scale = type_info[1][:-1].split(",")
precision = int(precision)
scale = int(scale)
out = [col_name, base_type, None, precision, scale]
schema_dict[col_name] = tuple(out)
return schema_dict
def query_table_schema(self, path: DbPath) -> Dict[str, tuple]:
try:
return super().query_table_schema(path)
except RuntimeError:
try:
return self.query_external_table_schema(path)
except RuntimeError:
return self.query_pg_get_cols(path)
def _normalize_table_path(self, path: DbPath) -> DbPath:
if len(path) == 1:
return None, self.default_schema, path[0]
elif len(path) == 2:
return None, path[0], path[1]
elif len(path) == 3:
return path
raise ValueError(
f"{self.name}: Bad table path for {self}: '{'.'.join(path)}'. Expected format: table, schema.table, or database.schema.table"
)