-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcolumn.py
265 lines (221 loc) · 6.29 KB
/
column.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import re
from cube_dbt.dump import dump
# As of 2024-10-17, the valid "Dimension Types" listed on
# https://cube.dev/docs/reference/data-model/types-and-formats#dimension-types
# are: time, string, number, boolean, and geo
VALID_DIMENSION_TYPES = [
"boolean",
"geo",
"number",
"string",
"time",
]
# Other System's Type => Cube Type
# See https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types
BIGQUERY_TYPE_MAPPINGS = {
"array": "string",
"bool": "boolean",
"bytes": "string",
"date": "time",
"datetime": "time",
"geography": "geo",
"interval": "string",
"json": "string",
# numeric types
"int64": "number",
"int": "number",
"smallint": "number",
"integer": "number",
"bigint": "number",
"tinyint": "number",
"byteint": "number",
"numeric": "number",
"decimal": "number",
"bignumeric": "number",
"bigdecimal": "number",
"float64": "number",
"range": "string",
# string does not need to be mapped
"struct": "string",
# time does not need to be mapped
"timestamp": "time",
}
# See https://docs.snowflake.com/en/sql-reference-data-types
SNOWFLAKE_TYPE_MAPPINGS = {
# Numeric data types
# number does not need to be mapped
"decimal": "number",
"dec": "number",
"numeric": "number",
"int": "number",
"integer": "number",
"bigint": "number",
"smallint": "number",
"tinyint": "number",
"byteint": "number",
"float": "number",
"float4": "number",
"float8": "number",
"double": "number",
"double precision": "number",
"real": "number",
# String & binary data types
"varchar": "string",
"char": "string",
"character": "string",
"nchar": "string",
# string does not need to be mapped
"text": "string",
"nvarchar": "string",
"nvarchar2": "string",
"char varying": "string",
"nchar varying": "string",
"binary": "string",
"varbinary": "string",
# Logical data types
# boolean does not need to be mapped
# Date & time data types
"date": "time",
"datetime": "time",
# time does not need to be mapped
"timestamp": "time",
"timestamp_ltz": "time",
"timestamp_ntz": "time",
"timestamp_tz": "time",
# Semi-structured data types
"variant": "string",
"object": "string",
"array": "string",
# Geospatial data types
"geography": "geo",
"geometry": "string",
# Vector data types
"vector": "string",
}
# See https://docs.aws.amazon.com/redshift/latest/dg/c_Supported_data_types.html
REDSHIFT_TYPE_MAPPINGS = {
# Signed two-byte integer
"smallint": "number",
"int2": "number",
# Signed four-byte integer
"integer": "number",
"int": "number",
"int4": "number",
# Signed eight-byte integer
"bigint": "number",
"int8": "number",
# Exact numeric of selectable precision
"decimal": "number",
"numeric": "number",
# Single precision floating-point number
"real": "number",
"float4": "number",
# Double precision floating-point number
"double precision": "number",
"float8": "number",
"float": "number",
# Fixed-length character string
"char": "string",
"character": "string",
"nchar": "string",
"bpchar": "string",
# Variable-length character string with a user-defined limit
"varchar": "string",
"character varying": "string",
"nvarchar": "string",
"text": "string",
# Calendar date (year, month, day)
"date": "time",
# Time of day
"time": "time",
"time without time zone": "time",
# Time of day with time zone
"timetz": "time",
"time with time zone": "time",
# Date and time (without time zone)
"timestamp": "time",
"timestamp without time zone": "time",
# Date and time (with time zone)
"timestamptz": "time",
"timestamp with time zone": "time",
# Time duration in year to month order
"interval year to month": "string",
# Time duration in day to second order
"interval day to second": "string",
# Logical Boolean (true/false)
# boolean does not need to be mapped
"bool": "boolean",
# Type used with HyperLogLog sketches
"hllsketch": "string",
# A superset data type that encompasses all scalar types of Amazon Redshift including complex types such as ARRAY and STRUCTS
"super": "string",
# Variable-length binary value
"varbyte": "string",
"varbinary": "string",
"binary varying": "string",
# Spatial data
"geometry": "geo",
"geography": "string",
}
TYPE_MAPPINGS = {
**BIGQUERY_TYPE_MAPPINGS,
**REDSHIFT_TYPE_MAPPINGS,
**SNOWFLAKE_TYPE_MAPPINGS,
}
class Column:
def __init__(self, model_name: str, column_dict: dict) -> None:
self._model_name = model_name
self._column_dict = column_dict
pass
def __repr__(self) -> str:
return str(self._column_dict)
@property
def name(self) -> str:
return self._column_dict['name']
@property
def description(self) -> str:
return self._column_dict['description']
@property
def sql(self) -> str:
return self._column_dict['name']
@property
def type(self) -> str:
if not 'data_type' in self._column_dict or self._column_dict['data_type'] == None:
return 'string'
# Normalize the data_type value, downcasing it, and removing extra information.
source_data_type = re.sub(r"<.*>", "", re.sub(r"\([^\)]*\)", "", self._column_dict["data_type"].lower()))
if source_data_type in TYPE_MAPPINGS:
cube_data_type = TYPE_MAPPINGS[source_data_type]
else:
cube_data_type = source_data_type
if cube_data_type not in VALID_DIMENSION_TYPES:
raise RuntimeError(f"Unknown column type of {self._model_name}.{self.name}: {self._column_dict['data_type']}")
return cube_data_type
@property
def meta(self) -> dict:
return self._column_dict['meta']
@property
def primary_key(self) -> bool:
"""
Convention: if the column is marked with the 'primary_key' tag,
it will be mapped to a primary key dimension
"""
return 'primary_key' in self._column_dict['tags']
def _as_dimension(self) -> dict:
data = {}
data['name'] = self.name
if self.description:
data['description'] = self.description
data['sql'] = self.sql
data['type'] = self.type
if self.primary_key:
data['primary_key'] = True
if self.meta:
data['meta'] = self.meta
return data
def as_dimension(self) -> str:
"""
For use in Jinja:
{{ dbt.model('name').column('name').as_dimension() }}
"""
return dump(self._as_dimension(), indent=8)