Skip to content

Commit b9dd955

Browse files
codyjlinwoop
andauthored
Provide the user with more options for setting the to_bigquery config (#1661)
* Provide more options for to_bigquery config Signed-off-by: Cody Lin <codyl@twitter.com> * Fix default job_config when none; remove excessive testing Signed-off-by: Cody Lin <codyl@twitter.com> * Add param type and docstring Signed-off-by: Cody Lin <codyl@twitter.com> * add docstrings and typing Signed-off-by: Cody Lin <codyl@twitter.com> * Apply docstring suggestions from code review Co-authored-by: Willem Pienaar <6728866+woop@users.noreply.github.com> Signed-off-by: Cody Lin <codyjlin@yahoomail.com> Co-authored-by: Willem Pienaar <6728866+woop@users.noreply.github.com>
1 parent 1761b10 commit b9dd955

2 files changed

Lines changed: 35 additions & 31 deletions

File tree

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,31 +239,51 @@ def to_df(self):
239239
df = self.client.query(self.query).to_dataframe(create_bqstorage_client=True)
240240
return df
241241

242-
def to_bigquery(self, dry_run=False) -> Optional[str]:
242+
def to_sql(self) -> str:
243+
"""
244+
Returns the SQL query that will be executed in BigQuery to build the historical feature table.
245+
"""
246+
return self.query
247+
248+
def to_bigquery(self, job_config: bigquery.QueryJobConfig = None) -> Optional[str]:
249+
"""
250+
Triggers the execution of a historical feature retrieval query and exports the results to a BigQuery table.
251+
252+
Args:
253+
job_config: An optional bigquery.QueryJobConfig to specify options like destination table, dry run, etc.
254+
255+
Returns:
256+
Returns the destination table name or returns None if job_config.dry_run is True.
257+
"""
258+
243259
@retry(wait=wait_fixed(10), stop=stop_after_delay(1800), reraise=True)
244260
def _block_until_done():
245261
return self.client.get_job(bq_job.job_id).state in ["PENDING", "RUNNING"]
246262

247-
today = date.today().strftime("%Y%m%d")
248-
rand_id = str(uuid.uuid4())[:7]
249-
dataset_project = self.config.offline_store.project_id or self.client.project
250-
path = f"{dataset_project}.{self.config.offline_store.dataset}.historical_{today}_{rand_id}"
251-
job_config = bigquery.QueryJobConfig(destination=path, dry_run=dry_run)
252-
bq_job = self.client.query(self.query, job_config=job_config)
253-
254-
if dry_run:
255-
print(
256-
"This query will process {} bytes.".format(bq_job.total_bytes_processed)
263+
if not job_config:
264+
today = date.today().strftime("%Y%m%d")
265+
rand_id = str(uuid.uuid4())[:7]
266+
dataset_project = (
267+
self.config.offline_store.project_id or self.client.project
257268
)
258-
return None
269+
path = f"{dataset_project}.{self.config.offline_store.dataset}.historical_{today}_{rand_id}"
270+
job_config = bigquery.QueryJobConfig(destination=path)
271+
272+
bq_job = self.client.query(self.query, job_config=job_config)
259273

260274
_block_until_done()
261275

262276
if bq_job.exception():
263277
raise bq_job.exception()
264278

265-
print(f"Done writing to '{path}'.")
266-
return path
279+
if job_config.dry_run:
280+
print(
281+
"This query will process {} bytes.".format(bq_job.total_bytes_processed)
282+
)
283+
return None
284+
285+
print(f"Done writing to '{job_config.destination}'.")
286+
return str(job_config.destination)
267287

268288

269289
@dataclass(frozen=True)

‎sdk/python/tests/test_historical_retrieval.py‎

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -438,29 +438,13 @@ def test_historical_features_from_bigquery_sources(
438438
],
439439
)
440440

441-
# Just a dry run, should not create table
442-
bq_dry_run = job_from_sql.to_bigquery(dry_run=True)
443-
assert bq_dry_run is None
444-
445-
bq_temp_table_path = job_from_sql.to_bigquery()
446-
assert bq_temp_table_path.split(".")[0] == gcp_project
447-
448-
if provider_type == "gcp_custom_offline_config":
449-
assert bq_temp_table_path.split(".")[1] == "foo"
450-
else:
451-
assert bq_temp_table_path.split(".")[1] == bigquery_dataset
452-
453-
# Check that this table actually exists
454-
actual_bq_temp_table = bigquery.Client().get_table(bq_temp_table_path)
455-
assert actual_bq_temp_table.table_id == bq_temp_table_path.split(".")[-1]
456-
457441
start_time = datetime.utcnow()
458442
actual_df_from_sql_entities = job_from_sql.to_df()
459443
end_time = datetime.utcnow()
460444
with capsys.disabled():
461445
print(
462446
str(
463-
f"\nTime to execute job_from_df.to_df() = '{(end_time - start_time)}'"
447+
f"\nTime to execute job_from_sql.to_df() = '{(end_time - start_time)}'"
464448
)
465449
)
466450

0 commit comments

Comments
 (0)