Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4ff26cd

Browse files
authoredNov 6, 2023
fix: update default temp table expiration to 7 days (#174)
1 parent dfcc2d3 commit 4ff26cd

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed
 

‎bigframes/constants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626

2727
ABSTRACT_METHOD_ERROR_MESSAGE = f"Abstract method. You have likely encountered a bug. Please share this stacktrace and how you reached it with the BigQuery DataFrames team. {FEEDBACK_LINK}"
2828

29-
DEFAULT_EXPIRATION = datetime.timedelta(days=1)
29+
DEFAULT_EXPIRATION = datetime.timedelta(days=7)

‎bigframes/dataframe.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from __future__ import annotations
1818

19+
import datetime
1920
import re
2021
import textwrap
2122
import typing
@@ -2327,7 +2328,8 @@ def to_gbq(
23272328
self._session.bqclient,
23282329
self._session._anonymous_dataset,
23292330
# TODO(swast): allow custom expiration times, probably via session configuration.
2330-
constants.DEFAULT_EXPIRATION,
2331+
datetime.datetime.now(datetime.timezone.utc)
2332+
+ constants.DEFAULT_EXPIRATION,
23312333
)
23322334

23332335
if if_exists is not None and if_exists != "replace":

‎bigframes/session/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,9 @@ def _read_gbq_query(
430430
index_cols = list(index_col)
431431

432432
destination, query_job = self._query_to_destination(
433-
query, index_cols, api_name="read_gbq_query"
433+
query,
434+
index_cols,
435+
api_name=api_name,
434436
)
435437

436438
# If there was no destination table, that means the query must have

‎bigframes/session/_io/bigquery.py

+28-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
"""Private module: Helpers for I/O operations."""
1616

17+
from __future__ import annotations
18+
1719
import datetime
1820
import textwrap
1921
import types
@@ -69,6 +71,29 @@ def create_export_data_statement(
6971
)
7072

7173

74+
def random_table(dataset: bigquery.DatasetReference) -> bigquery.TableReference:
75+
"""Generate a random table ID with BigQuery DataFrames prefix.
76+
Args:
77+
dataset (google.cloud.bigquery.DatasetReference):
78+
The dataset to make the table reference in. Usually the anonymous
79+
dataset for the session.
80+
Returns:
81+
google.cloud.bigquery.TableReference:
82+
Fully qualified table ID of a table that doesn't exist.
83+
"""
84+
now = datetime.datetime.now(datetime.timezone.utc)
85+
random_id = uuid.uuid4().hex
86+
table_id = TEMP_TABLE_PREFIX.format(
87+
date=now.strftime("%Y%m%d"), random_id=random_id
88+
)
89+
return dataset.table(table_id)
90+
91+
92+
def table_ref_to_sql(table: bigquery.TableReference) -> str:
93+
"""Format a table reference as escaped SQL."""
94+
return f"`{table.project}`.`{table.dataset_id}`.`{table.table_id}`"
95+
96+
7297
def create_snapshot_sql(
7398
table_ref: bigquery.TableReference, current_timestamp: datetime.datetime
7499
) -> str:
@@ -95,17 +120,12 @@ def create_snapshot_sql(
95120
def create_temp_table(
96121
bqclient: bigquery.Client,
97122
dataset: bigquery.DatasetReference,
98-
expiration: datetime.timedelta,
123+
expiration: datetime.datetime,
99124
) -> str:
100125
"""Create an empty table with an expiration in the desired dataset."""
101-
now = datetime.datetime.now(datetime.timezone.utc)
102-
random_id = uuid.uuid4().hex
103-
table_id = TEMP_TABLE_PREFIX.format(
104-
date=now.strftime("%Y%m%d"), random_id=random_id
105-
)
106-
table_ref = dataset.table(table_id)
126+
table_ref = random_table(dataset)
107127
destination = bigquery.Table(table_ref)
108-
destination.expires = now + expiration
128+
destination.expires = expiration
109129
bqclient.create_table(destination)
110130
return f"{table_ref.project}.{table_ref.dataset_id}.{table_ref.table_id}"
111131

‎tests/unit/session/test_io_bigquery.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ def test_create_temp_table_default_expiration():
5656
"""Make sure the created table has an expiration."""
5757
bqclient = mock.create_autospec(bigquery.Client)
5858
dataset = bigquery.DatasetReference("test-project", "test_dataset")
59-
now = datetime.datetime.now(datetime.timezone.utc)
60-
expiration = datetime.timedelta(days=3)
61-
expected_expires = now + expiration
59+
expiration = datetime.datetime(
60+
2023, 11, 2, 13, 44, 55, 678901, datetime.timezone.utc
61+
)
6262

6363
bigframes.session._io.bigquery.create_temp_table(bqclient, dataset, expiration)
6464

@@ -69,9 +69,9 @@ def test_create_temp_table_default_expiration():
6969
assert table.dataset_id == "test_dataset"
7070
assert table.table_id.startswith("bqdf")
7171
assert (
72-
(expected_expires - datetime.timedelta(minutes=1))
72+
(expiration - datetime.timedelta(minutes=1))
7373
< table.expires
74-
< (expected_expires + datetime.timedelta(minutes=1))
74+
< (expiration + datetime.timedelta(minutes=1))
7575
)
7676

7777

0 commit comments

Comments
 (0)
Failed to load comments.