-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathcolumn.py
300 lines (246 loc) · 11.1 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
from dataclasses import dataclass
from typing import Any, Dict, Iterable, List, Optional, Type, TypeVar, Union
from google.cloud.bigquery import SchemaField
from dbt.adapters.base.column import Column
_PARENT_DATA_TYPE_KEY = "__parent_data_type"
Self = TypeVar("Self", bound="BigQueryColumn")
@dataclass(init=False)
class BigQueryColumn(Column):
TYPE_LABELS = {
"TEXT": "STRING",
"FLOAT": "FLOAT64",
"INTEGER": "INT64",
}
fields: List[Self] # type: ignore
mode: str
def __init__(
self,
column: str,
dtype: str,
fields: Optional[Iterable[SchemaField]] = None,
mode: str = "NULLABLE",
) -> None:
super().__init__(column, dtype)
if fields is None:
fields = []
self.fields = self.wrap_subfields(fields)
self.mode = mode
@classmethod
def wrap_subfields(cls: Type[Self], fields: Iterable[SchemaField]) -> List[Self]:
return [cls.create_from_field(field) for field in fields]
@classmethod
def create_from_field(cls: Type[Self], field: SchemaField) -> Self:
return cls(
field.name,
cls.translate_type(field.field_type),
field.fields,
field.mode,
)
@classmethod
def _flatten_recursive(cls: Type[Self], col: Self, prefix: Optional[str] = None) -> List[Self]:
if prefix is None:
prefix = [] # type: ignore[assignment]
if len(col.fields) == 0:
prefixed_name = ".".join(prefix + [col.column]) # type: ignore[operator]
new_col = cls(prefixed_name, col.dtype, col.fields, col.mode)
return [new_col]
new_fields = []
for field in col.fields:
new_prefix = prefix + [col.column] # type: ignore[operator]
new_fields.extend(cls._flatten_recursive(field, new_prefix))
return new_fields
def flatten(self):
return self._flatten_recursive(self)
@property
def quoted(self):
return "`{}`".format(self.column)
def literal(self, value):
return "cast({} as {})".format(value, self.dtype)
@property
def data_type(self) -> str:
if self.dtype.upper() == "RECORD":
subcols = [
"{} {}".format(col.quoted, col.data_type) for col in self.fields # type: ignore[attr-defined]
]
field_type = "STRUCT<{}>".format(", ".join(subcols))
else:
field_type = self.dtype
if self.mode.upper() == "REPEATED":
return "ARRAY<{}>".format(field_type)
else:
return field_type
@classmethod
def numeric_type(cls, dtype: str, precision: Any, scale: Any) -> str:
# BigQuery makes life much harder if precision + scale are specified
# even if they're fed in here, just return the data type by itself
return dtype
def is_string(self) -> bool:
return self.dtype.lower() == "string"
def is_integer(self) -> bool:
return self.dtype.lower() == "int64"
def is_numeric(self) -> bool:
return self.dtype.lower() == "numeric"
def is_float(self):
return self.dtype.lower() == "float64"
def can_expand_to(self: Self, other_column: Self) -> bool:
"""returns True if both columns are strings"""
return self.is_string() and other_column.is_string()
def __repr__(self) -> str:
return "<BigQueryColumn {} ({}, {})>".format(self.name, self.data_type, self.mode)
def column_to_bq_schema(self) -> SchemaField:
"""Convert a column to a bigquery schema object."""
kwargs = {}
if len(self.fields) > 0:
fields = [field.column_to_bq_schema() for field in self.fields] # type: ignore[attr-defined]
kwargs = {"fields": fields}
return SchemaField(self.name, self.dtype, self.mode, **kwargs)
def get_nested_column_data_types(
columns: Dict[str, Dict[str, Any]],
constraints: Optional[Dict[str, str]] = None,
) -> Dict[str, Dict[str, Optional[str]]]:
"""
columns:
* Dictionary where keys are of flat columns names and values are dictionary of column attributes
* column names with "." indicate a nested column within a STRUCT type
* e.g. {"a": {"name": "a", "data_type": "string", ...}}
constraints:
* Dictionary where keys are flat column names and values are rendered constraints for the column
* If provided, rendered column is included in returned "data_type" values.
returns:
* Dictionary where keys are root column names and values are corresponding nested data_type values.
* Fields other than "name" and "data_type" are __not__ preserved in the return value for nested columns.
* Fields other than "name" and "data_type" are preserved in the return value for flat columns.
Example:
columns: {
"a": {"name": "a", "data_type": "string", "description": ...},
"b.nested": {"name": "b.nested", "data_type": "string"},
"b.nested2": {"name": "b.nested2", "data_type": "string"}
}
returns: {
"a": {"name": "a", "data_type": "string"},
"b": {"name": "b": "data_type": "struct<nested string, nested2 string>}
}
"""
constraints = constraints or {}
nested_column_data_types: Dict[str, Optional[Union[str, Dict]]] = {}
for column in columns.values():
_update_nested_column_data_types(
column["name"],
column.get("data_type"),
constraints.get(column["name"]),
nested_column_data_types,
)
formatted_nested_column_data_types: Dict[str, Dict[str, Optional[str]]] = {}
for column_name, unformatted_column_type in nested_column_data_types.items():
formatted_nested_column_data_types[column_name] = {
"name": column_name,
"data_type": _format_nested_data_type(unformatted_column_type),
}
# add column configs back to flat columns
for column_name in formatted_nested_column_data_types:
if column_name in columns:
formatted_nested_column_data_types[column_name].update(
{
k: v
for k, v in columns[column_name].items()
if k not in formatted_nested_column_data_types[column_name]
}
)
return formatted_nested_column_data_types
def _update_nested_column_data_types(
column_name: str,
column_data_type: Optional[str],
column_rendered_constraint: Optional[str],
nested_column_data_types: Dict[str, Optional[Union[str, Dict]]],
) -> None:
"""
Recursively update nested_column_data_types given a column_name, column_data_type, and optional column_rendered_constraint.
Examples:
>>> nested_column_data_types = {}
>>> BigQueryAdapter._update_nested_column_data_types("a", "string", "not_null", nested_column_data_types)
>>> nested_column_data_types
{"a": "string not null"}
>>> BigQueryAdapter._update_nested_column_data_types("b.c", "string", "not_null", nested_column_data_types)
>>> nested_column_data_types
{"a": "string not null", "b": {"c": "string not null"}}
>>> BigQueryAdapter._update_nested_column_data_types("b.d", "string", None, nested_column_data_types)
>>> nested_column_data_types
{"a": "string not null", "b": {"c": "string not null", "d": "string"}}
"""
column_name_parts = column_name.split(".")
root_column_name = column_name_parts[0]
if len(column_name_parts) == 1:
# Base case: column is not nested - store its data_type concatenated with constraint if provided.
column_data_type_and_constraints = (
(
column_data_type
if column_rendered_constraint is None
else f"{column_data_type} {column_rendered_constraint}"
)
if column_data_type
else None
)
if existing_nested_column_data_type := nested_column_data_types.get(root_column_name):
assert isinstance(existing_nested_column_data_type, dict) # keeping mypy happy
# entry could already exist if this is a parent column -- preserve the parent data type under "_PARENT_DATA_TYPE_KEY"
existing_nested_column_data_type.update(
{_PARENT_DATA_TYPE_KEY: column_data_type_and_constraints}
)
else:
nested_column_data_types.update({root_column_name: column_data_type_and_constraints})
else:
parent_data_type = nested_column_data_types.get(root_column_name)
if isinstance(parent_data_type, dict):
# nested dictionary already initialized
pass
elif parent_data_type is None:
# initialize nested dictionary
nested_column_data_types.update({root_column_name: {}})
else:
# a parent specified its base type -- preserve its data_type and potential rendered constraints
# this is used to specify a top-level 'struct' or 'array' field with its own description, constraints, etc
nested_column_data_types.update(
{root_column_name: {_PARENT_DATA_TYPE_KEY: parent_data_type}}
)
# Recursively process rest of remaining column name
remaining_column_name = ".".join(column_name_parts[1:])
remaining_column_data_types = nested_column_data_types[root_column_name]
assert isinstance(remaining_column_data_types, dict) # keeping mypy happy
_update_nested_column_data_types(
remaining_column_name,
column_data_type,
column_rendered_constraint,
remaining_column_data_types,
)
def _format_nested_data_type(
unformatted_nested_data_type: Optional[Union[str, Dict[str, Any]]]
) -> Optional[str]:
"""
Recursively format a (STRUCT) data type given an arbitrarily nested data type structure.
Examples:
>>> BigQueryAdapter._format_nested_data_type("string")
'string'
>>> BigQueryAdapter._format_nested_data_type({'c': 'string not_null', 'd': 'string'})
'struct<c string not_null, d string>'
>>> BigQueryAdapter._format_nested_data_type({'c': 'string not_null', 'd': {'e': 'string'}})
'struct<c string not_null, d struct<e string>>'
"""
if unformatted_nested_data_type is None:
return None
elif isinstance(unformatted_nested_data_type, str):
return unformatted_nested_data_type
else:
parent_data_type, *parent_constraints = unformatted_nested_data_type.pop(
_PARENT_DATA_TYPE_KEY, ""
).split() or [None]
formatted_nested_types = [
f"{column_name} {_format_nested_data_type(column_type) or ''}".strip()
for column_name, column_type in unformatted_nested_data_type.items()
]
formatted_nested_type = f"""struct<{", ".join(formatted_nested_types)}>"""
if parent_data_type and parent_data_type.lower() == "array":
formatted_nested_type = f"""array<{formatted_nested_type}>"""
if parent_constraints:
parent_constraints = " ".join(parent_constraints)
formatted_nested_type = f"""{formatted_nested_type} {parent_constraints}"""
return formatted_nested_type