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

Commit 4c3bb93

Browse files
committed
Support querying view schema on Redshift
1 parent b593284 commit 4c3bb93

1 file changed

Lines changed: 36 additions & 1 deletion

File tree

sqeleton/databases/redshift.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,46 @@ def query_external_table_schema(self, path: DbPath) -> Dict[str, tuple]:
104104
assert len(d) == len(rows)
105105
return d
106106

107+
def select_view_columns(self, path: DbPath) -> str:
108+
_, schema, table = self._normalize_table_path(path)
109+
110+
return (
111+
"""select * from pg_get_cols('{}.{}')
112+
cols(view_schema name, view_name name, col_name name, col_type varchar, col_num int)
113+
""".format(schema, table)
114+
)
115+
116+
def query_pg_get_cols(self, path: DbPath) -> Dict[str, tuple]:
117+
rows = self.query(self.select_view_columns(path), list)
118+
119+
if not rows:
120+
raise RuntimeError(f"{self.name}: View '{'.'.join(path)}' does not exist, or has no columns")
121+
122+
output = {}
123+
for r in rows:
124+
col_name = r[2]
125+
type_info = r[3].split('(')
126+
base_type = type_info[0]
127+
precision = None
128+
scale = None
129+
130+
if len(type_info) > 1:
131+
if base_type == 'numeric':
132+
precision, scale = type_info[1][:-1].split(',')
133+
134+
out = [col_name, base_type, None, precision, scale]
135+
output[col_name] = tuple(out)
136+
137+
return output
138+
107139
def query_table_schema(self, path: DbPath) -> Dict[str, tuple]:
108140
try:
109141
return super().query_table_schema(path)
110142
except RuntimeError:
111-
return self.query_external_table_schema(path)
143+
try:
144+
return self.query_external_table_schema(path)
145+
except RuntimeError:
146+
return self.query_pg_get_cols()
112147

113148
def _normalize_table_path(self, path: DbPath) -> DbPath:
114149
if len(path) == 1:

0 commit comments

Comments
 (0)