Repository navigation
Expand file tree
/
Copy pathbigquery_source.py
More file actions
288 lines (237 loc) · 9.99 KB
/
Copy pathbigquery_source.py
File metadata and controls
288 lines (237 loc) · 9.99 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
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
from typing import Callable, Dict, Iterable, List, Optional, Tuple
from typeguard import typechecked
from feast import type_map
from feast.data_source import DataSource
from feast.errors import DataSourceNoNameException, DataSourceNotFoundException
from feast.feature_logging import LoggingDestination
from feast.protos.feast.core.DataSource_pb2 import DataSource as DataSourceProto
from feast.protos.feast.core.FeatureService_pb2 import (
LoggingConfig as LoggingConfigProto,
)
from feast.protos.feast.core.SavedDataset_pb2 import (
SavedDatasetStorage as SavedDatasetStorageProto,
)
from feast.repo_config import RepoConfig
from feast.saved_dataset import SavedDatasetStorage
from feast.utils import get_user_agent
from feast.value_type import ValueType
@typechecked
class BigQuerySource(DataSource):
def __init__(
self,
*,
name: Optional[str] = None,
timestamp_field: Optional[str] = None,
table: Optional[str] = None,
created_timestamp_column: Optional[str] = "",
field_mapping: Optional[Dict[str, str]] = None,
query: Optional[str] = None,
description: Optional[str] = "",
tags: Optional[Dict[str, str]] = None,
owner: Optional[str] = "",
):
"""Create a BigQuerySource from an existing table or query.
Args:
name (optional): Name for the source. Defaults to the table if not specified, in which
case the table must be specified.
timestamp_field (optional): Event timestamp field used for point in time
joins of feature values.
table (optional): BigQuery table where the features are stored. Exactly one of 'table'
and 'query' must be specified.
table (optional): The BigQuery table where features can be found.
created_timestamp_column (optional): Timestamp column when row was created, used for deduplicating rows.
field_mapping (optional): A dictionary mapping of column names in this data source to feature names in a feature table
or view. Only used for feature columns, not entities or timestamp columns.
query (optional): The query to be executed to obtain the features. Exactly one of 'table'
and 'query' must be specified.
description (optional): A human-readable description.
tags (optional): A dictionary of key-value pairs to store arbitrary metadata.
owner (optional): The owner of the bigquery source, typically the email of the primary
maintainer.
Example:
>>> from feast import BigQuerySource
>>> my_bigquery_source = BigQuerySource(table="gcp_project:bq_dataset.bq_table")
"""
if table is None and query is None:
raise ValueError('No "table" or "query" argument provided.')
self.bigquery_options = BigQueryOptions(table=table, query=query)
# If no name, use the table as the default name.
if name is None and table is None:
raise DataSourceNoNameException()
name = name or table
assert name
super().__init__(
name=name,
timestamp_field=timestamp_field,
created_timestamp_column=created_timestamp_column,
field_mapping=field_mapping,
description=description,
tags=tags,
owner=owner,
)
# Note: Python requires redefining hash in child classes that override __eq__
def __hash__(self):
return super().__hash__()
def __eq__(self, other):
if not isinstance(other, BigQuerySource):
raise TypeError(
"Comparisons should only involve BigQuerySource class objects."
)
return (
super().__eq__(other)
and self.table == other.table
and self.query == other.query
)
@property
def table(self):
return self.bigquery_options.table
@property
def query(self):
return self.bigquery_options.query
@staticmethod
def from_proto(data_source: DataSourceProto):
assert data_source.HasField("bigquery_options")
return BigQuerySource(
name=data_source.name,
field_mapping=dict(data_source.field_mapping),
table=data_source.bigquery_options.table,
timestamp_field=data_source.timestamp_field,
created_timestamp_column=data_source.created_timestamp_column,
query=data_source.bigquery_options.query,
description=data_source.description,
tags=dict(data_source.tags),
owner=data_source.owner,
)
def to_proto(self) -> DataSourceProto:
data_source_proto = DataSourceProto(
name=self.name,
type=DataSourceProto.BATCH_BIGQUERY,
field_mapping=self.field_mapping,
bigquery_options=self.bigquery_options.to_proto(),
description=self.description,
tags=self.tags,
owner=self.owner,
timestamp_field=self.timestamp_field,
created_timestamp_column=self.created_timestamp_column,
)
return data_source_proto
def validate(self, config: RepoConfig):
if not self.query:
from google.api_core.exceptions import NotFound
from google.cloud import bigquery
client = bigquery.Client()
try:
client.get_table(self.table)
except NotFound:
raise DataSourceNotFoundException(self.table)
def get_table_query_string(self) -> str:
"""Returns a string that can directly be used to reference this table in SQL"""
if self.table:
return f"`{self.table}`"
else:
return f"({self.query})"
@staticmethod
def source_datatype_to_feast_value_type() -> Callable[[str], ValueType]:
return type_map.bq_to_feast_value_type
def get_table_column_names_and_types(
self, config: RepoConfig
) -> Iterable[Tuple[str, str]]:
try:
from google.api_core import client_info as http_client_info
except ImportError as e:
from feast.errors import FeastExtrasDependencyImportError
raise FeastExtrasDependencyImportError("gcp", str(e))
from google.cloud import bigquery
project_id = (
config.offline_store.billing_project_id or config.offline_store.project_id
)
client = bigquery.Client(
project=project_id,
location=config.offline_store.location,
client_info=http_client_info.ClientInfo(user_agent=get_user_agent()),
)
if self.table:
schema = client.get_table(self.table).schema
if not isinstance(schema[0], bigquery.schema.SchemaField):
raise TypeError("Could not parse BigQuery table schema.")
else:
bq_columns_query = f"SELECT * FROM ({self.query}) LIMIT 0"
query_res = client.query(bq_columns_query).result()
schema = query_res.schema
name_type_pairs: List[Tuple[str, str]] = []
for field in schema:
bq_type_as_str = field.field_type
if field.mode == "REPEATED":
bq_type_as_str = "ARRAY<" + bq_type_as_str + ">"
name_type_pairs.append((field.name, bq_type_as_str))
return name_type_pairs
class BigQueryOptions:
"""
Configuration options for a BigQuery data source.
"""
def __init__(
self,
table: Optional[str],
query: Optional[str],
):
self.table = table or ""
self.query = query or ""
@classmethod
def from_proto(cls, bigquery_options_proto: DataSourceProto.BigQueryOptions):
"""
Creates a BigQueryOptions from a protobuf representation of a BigQuery option
Args:
bigquery_options_proto: A protobuf representation of a DataSource
Returns:
Returns a BigQueryOptions object based on the bigquery_options protobuf
"""
bigquery_options = cls(
table=bigquery_options_proto.table,
query=bigquery_options_proto.query,
)
return bigquery_options
def to_proto(self) -> DataSourceProto.BigQueryOptions:
"""
Converts an BigQueryOptionsProto object to its protobuf representation.
Returns:
BigQueryOptionsProto protobuf
"""
bigquery_options_proto = DataSourceProto.BigQueryOptions(
table=self.table,
query=self.query,
)
return bigquery_options_proto
class SavedDatasetBigQueryStorage(SavedDatasetStorage):
_proto_attr_name = "bigquery_storage"
bigquery_options: BigQueryOptions
def __init__(self, table: str):
self.bigquery_options = BigQueryOptions(table=table, query=None)
@staticmethod
def from_proto(storage_proto: SavedDatasetStorageProto) -> SavedDatasetStorage:
return SavedDatasetBigQueryStorage(
table=BigQueryOptions.from_proto(storage_proto.bigquery_storage).table
)
def to_proto(self) -> SavedDatasetStorageProto:
return SavedDatasetStorageProto(
bigquery_storage=self.bigquery_options.to_proto()
)
def to_data_source(self) -> DataSource:
return BigQuerySource(table=self.bigquery_options.table)
class BigQueryLoggingDestination(LoggingDestination):
_proto_kind = "bigquery_destination"
table: str
def __init__(self, *, table_ref):
self.table = table_ref
@classmethod
def from_proto(cls, config_proto: LoggingConfigProto) -> "LoggingDestination":
return BigQueryLoggingDestination(
table_ref=config_proto.bigquery_destination.table_ref,
)
def to_data_source(self) -> DataSource:
return BigQuerySource(table=self.table)
def to_proto(self) -> LoggingConfigProto:
return LoggingConfigProto(
bigquery_destination=LoggingConfigProto.BigQueryDestination(
table_ref=self.table
)
)