Skip to content

Commit 4cb2f01

Browse files
committed
Add to_bigquery() function for bq retrieval job
Signed-off-by: Vivian Tao <vivian.tao@shopify.com>
1 parent b32e766 commit 4cb2f01

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

‎sdk/python/feast/infra/offline_stores/bigquery.py‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
2+
import uuid
23
from dataclasses import asdict, dataclass
3-
from datetime import datetime, timedelta
4+
from datetime import date, datetime, timedelta
45
from typing import List, Optional, Set, Union
56

67
import pandas
@@ -118,7 +119,7 @@ def get_historical_features(
118119
entity_df_event_timestamp_col=entity_df_event_timestamp_col,
119120
)
120121

121-
job = BigQueryRetrievalJob(query=query, client=client)
122+
job = BigQueryRetrievalJob(query=query, client=client, config=config)
122123
return job
123124

124125

@@ -206,15 +207,40 @@ def _infer_event_timestamp_from_dataframe(entity_df: pandas.DataFrame) -> str:
206207

207208

208209
class BigQueryRetrievalJob(RetrievalJob):
209-
def __init__(self, query, client):
210+
def __init__(self, query, client, config):
210211
self.query = query
211212
self.client = client
213+
self.config = config
212214

213215
def to_df(self):
214216
# TODO: Ideally only start this job when the user runs "get_historical_features", not when they run to_df()
215217
df = self.client.query(self.query).to_dataframe(create_bqstorage_client=True)
216218
return df
217219

220+
def to_bigquery(self, dry_run=False) -> Optional[str]:
221+
today = date.today().strftime("%Y%m%d")
222+
rand_id = str(uuid.uuid4())[:7]
223+
path = f"{self.client.project}.{self.config.offline_store.dataset}.training_{today}_{rand_id} "
224+
225+
job_config = bigquery.QueryJobConfig(destination=path, dry_run=dry_run)
226+
bq_job = self.client.query(self.query, job_config=job_config)
227+
228+
if dry_run:
229+
print(
230+
"This query will process {} bytes.".format(bq_job.total_bytes_processed)
231+
)
232+
return None
233+
234+
while True:
235+
query_job = self.client.get_job(bq_job.job_id)
236+
if query_job.state in ["PENDING", "RUNNING"]:
237+
print(f"The job is still '{bq_job.state}'. Will wait for 30 seconds")
238+
time.sleep(30)
239+
else:
240+
break
241+
242+
return path
243+
218244

219245
@dataclass(frozen=True)
220246
class FeatureViewQueryContext:

‎sdk/python/tests/test_historical_retrieval.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,22 @@ def test_historical_features_from_bigquery_sources(
441441
],
442442
)
443443

444+
# Just a dry run, should not create table
445+
bq_dry_run = job_from_sql.to_bigquery(dry_run=True)
446+
assert bq_dry_run is None
447+
448+
bq_temp_table_path = job_from_sql.to_bigquery()
449+
assert bq_temp_table_path.split(".")[0] == gcp_project
450+
451+
if provider_type == "gcp_custom_offline_config":
452+
assert bq_temp_table_path.split(".")[1] == "foo"
453+
else:
454+
assert bq_temp_table_path.split(".")[1] == bigquery_dataset
455+
456+
# Check that this table actually exists
457+
actual_bq_temp_table = bigquery.Client().get_table(bq_temp_table_path)
458+
assert actual_bq_temp_table.table_id == bq_temp_table_path.split(".")[-1]
459+
444460
start_time = datetime.utcnow()
445461
actual_df_from_sql_entities = job_from_sql.to_df()
446462
end_time = datetime.utcnow()

0 commit comments

Comments
 (0)