|
1 | 1 | import time |
| 2 | +import uuid |
2 | 3 | from dataclasses import asdict, dataclass |
3 | | -from datetime import datetime, timedelta |
| 4 | +from datetime import date, datetime, timedelta |
4 | 5 | from typing import List, Optional, Set, Union |
5 | 6 |
|
6 | 7 | import pandas |
@@ -118,7 +119,7 @@ def get_historical_features( |
118 | 119 | entity_df_event_timestamp_col=entity_df_event_timestamp_col, |
119 | 120 | ) |
120 | 121 |
|
121 | | - job = BigQueryRetrievalJob(query=query, client=client) |
| 122 | + job = BigQueryRetrievalJob(query=query, client=client, config=config) |
122 | 123 | return job |
123 | 124 |
|
124 | 125 |
|
@@ -206,15 +207,40 @@ def _infer_event_timestamp_from_dataframe(entity_df: pandas.DataFrame) -> str: |
206 | 207 |
|
207 | 208 |
|
208 | 209 | class BigQueryRetrievalJob(RetrievalJob): |
209 | | - def __init__(self, query, client): |
| 210 | + def __init__(self, query, client, config): |
210 | 211 | self.query = query |
211 | 212 | self.client = client |
| 213 | + self.config = config |
212 | 214 |
|
213 | 215 | def to_df(self): |
214 | 216 | # TODO: Ideally only start this job when the user runs "get_historical_features", not when they run to_df() |
215 | 217 | df = self.client.query(self.query).to_dataframe(create_bqstorage_client=True) |
216 | 218 | return df |
217 | 219 |
|
| 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 | + |
218 | 244 |
|
219 | 245 | @dataclass(frozen=True) |
220 | 246 | class FeatureViewQueryContext: |
|
0 commit comments