Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix: access MATERIALIZED_VIEW with read_gbq #1070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 6 bigframes/core/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ class GbqTable:
table_id: str = field()
physical_schema: Tuple[bq.SchemaField, ...] = field()
n_rows: int = field()
is_physical_table: bool = field()
is_physically_stored: bool = field()
cluster_cols: typing.Optional[Tuple[str, ...]]

@staticmethod
Expand All @@ -563,7 +563,7 @@ def from_table(table: bq.Table, columns: Sequence[str] = ()) -> GbqTable:
table_id=table.table_id,
physical_schema=schema,
n_rows=table.num_rows,
is_physical_table=(table.table_type == "TABLE"),
is_physically_stored=(table.table_type in ["TABLE", "MATERIALIZED_VIEW"]),
cluster_cols=None
if table.clustering_fields is None
else tuple(table.clustering_fields),
Expand Down Expand Up @@ -641,7 +641,7 @@ def variables_introduced(self) -> int:

@property
def row_count(self) -> typing.Optional[int]:
if self.source.sql_predicate is None and self.source.table.is_physical_table:
if self.source.sql_predicate is None and self.source.table.is_physically_stored:
return self.source.table.n_rows
return None

Expand Down
12 changes: 12 additions & 0 deletions 12 bigframes/session/_io/bigquery/read_gbq_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def validate_table(
table_ref: bigquery.table.TableReference,
columns: Optional[Sequence[str]],
snapshot_time: datetime.datetime,
table_type: str,
filter_str: Optional[str] = None,
) -> bool:
"""Validates that the table can be read, returns True iff snapshot is supported."""
Expand All @@ -124,6 +125,17 @@ def validate_table(
if table_ref.dataset_id.startswith("_"):
return False

# Materialized views锛宒oes not support snapshot
if table_type == "MATERIALIZED_VIEW":
warnings.warn(
Copy link
Contributor

@shobsi shobsi Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a test for this warning

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

"Materialized views do not support FOR SYSTEM_TIME AS OF queries. "
"Attempting query without time travel. Be aware that as materialized views "
"are updated periodically, modifications to the underlying data in the view may "
"result in errors or unexpected behavior.",
category=bigframes.exceptions.TimeTravelDisabledWarning,
)
return False

# Second, try with snapshot to verify table supports this feature
snapshot_sql = bigframes.session._io.bigquery.to_query(
query_or_table=f"{table_ref.project}.{table_ref.dataset_id}.{table_ref.table_id}",
Expand Down
7 changes: 6 additions & 1 deletion 7 bigframes/session/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,12 @@ def read_gbq_table(
)

enable_snapshot = enable_snapshot and bf_read_gbq_table.validate_table(
self._bqclient, table_ref, all_columns, time_travel_timestamp, filter_str
self._bqclient,
table_ref,
all_columns,
time_travel_timestamp,
table.table_type,
filter_str,
)

# ----------------------------
Expand Down
4 changes: 4 additions & 0 deletions 4 tests/system/small/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,10 @@ def test_shape(scalars_dfs):
@pytest.mark.parametrize(
"reference_table, test_table",
[
(
"bigframes-dev.bigframes_tests_sys.base_table",
"bigframes-dev.bigframes_tests_sys.base_table_mat_view",
),
(
"bigframes-dev.bigframes_tests_sys.base_table",
"bigframes-dev.bigframes_tests_sys.base_table_view",
Expand Down
11 changes: 9 additions & 2 deletions 11 tests/system/small/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,16 @@ def test_read_gbq_twice_with_same_timestamp(session, penguins_table_id):
assert df3 is not None


def test_read_gbq_on_linked_dataset_warns(session):
@pytest.mark.parametrize(
"source_table",
[
"bigframes-dev.thelook_ecommerce.orders",
"bigframes-dev.bigframes_tests_sys.base_table_mat_view",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a concerned about hard coding this table. Could we at least include something in the scripts/ directory that can recreate this table if needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, will add that.

],
)
def test_read_gbq_on_linked_dataset_warns(session, source_table):
with warnings.catch_warnings(record=True) as warned:
session.read_gbq("bigframes-dev.thelook_ecommerce.orders")
session.read_gbq(source_table)
assert len(warned) == 1
assert warned[0].category == bigframes.exceptions.TimeTravelDisabledWarning

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.