Skip to content

Commit

Permalink
Add temporary try-catch to handle snapshot time parsing issue (#933)
Browse files Browse the repository at this point in the history
* Add temporary try-catch to handle snapshot time parsing issue

* Bump version
  • Loading branch information
mars-lan authored Aug 2, 2024
1 parent e540833 commit 69b8e6e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 15 additions & 5 deletions metaphor/bigquery/extractor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from typing import (
Any,
Collection,
Expand Down Expand Up @@ -224,13 +225,10 @@ def parse_schema(bq_table: bigquery.table.Table) -> DatasetSchema:
table_schema=bq_table.mview_query,
)
elif bq_table.table_type == "SNAPSHOT":

schema.sql_schema = SQLSchema(
materialization=MaterializationType.SNAPSHOT,
snapshot_time=(
bq_table.snapshot_definition.snapshot_time
if bq_table.snapshot_definition
else None
),
snapshot_time=BigQueryExtractor._get_snapshot_time(bq_table),
)
else:
raise ValueError(f"Unexpected table type {bq_table.table_type}")
Expand All @@ -239,6 +237,18 @@ def parse_schema(bq_table: bigquery.table.Table) -> DatasetSchema:

return schema

@staticmethod
def _get_snapshot_time(bq_table: bigquery.table.Table) -> Optional[datetime]:
# bigquery client fails to parse snapshot time sometimes
# See https://github.com/googleapis/python-bigquery/issues/1986
try:
if bq_table.snapshot_definition:
return bq_table.snapshot_definition.snapshot_time
except ValueError:
return None

return None

# See https://googleapis.dev/python/bigquery/latest/generated/google.cloud.bigquery.schema.SchemaField.html#google.cloud.bigquery.schema.SchemaField
@staticmethod
def _parse_fields(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "metaphor-connectors"
version = "0.14.62"
version = "0.14.63"
license = "Apache-2.0"
description = "A collection of Python-based 'connectors' that extract metadata from various sources to ingest into the Metaphor app."
authors = ["Metaphor <[email protected]>"]
Expand Down

0 comments on commit 69b8e6e

Please sign in to comment.