Content-Length: 474101 | pFad | http://github.com/googleapis/python-bigquery/commit/39d4c1d345cb10b236d81a264d5091b11a3dc74a

7D feat: add property for maxStaleness in table definitions (#2087) · googleapis/python-bigquery@39d4c1d · GitHub
Skip to content

Commit 39d4c1d

Browse files
yu-iskwLinchin
authored andcommitted
feat: add property for maxStaleness in table definitions (#2087)
* feat: add property for maxStaleness in table definitions Signed-off-by: Yu Ishikawa <yu-iskw@users.noreply.github.com> * Update google/cloud/bigquery/table.py --------- Signed-off-by: Yu Ishikawa <yu-iskw@users.noreply.github.com> Co-authored-by: Lingqing Gan <lingqing.gan@gmail.com>
1 parent 1f00e76 commit 39d4c1d

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

google/cloud/bigquery/table.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ class Table(_TableBase):
411411
"require_partition_filter": "requirePartitionFilter",
412412
"table_constraints": "tableConstraints",
413413
"external_catalog_table_options": "externalCatalogTableOptions",
414+
"max_staleness": "maxStaleness",
414415
}
415416

416417
def __init__(self, table_ref, schema=None) -> None:
@@ -1144,6 +1145,40 @@ def __repr__(self):
11441145
def __str__(self):
11451146
return f"{self.project}.{self.dataset_id}.{self.table_id}"
11461147

1148+
@property
1149+
def max_staleness(self):
1150+
"""Union[str, None]: The maximum staleness of data that could be returned when the table is queried.
1151+
1152+
Staleness encoded as a string encoding of sql IntervalValue type.
1153+
This property is optional and defaults to None.
1154+
1155+
According to the BigQuery API documentation, maxStaleness specifies the maximum time
1156+
interval for which stale data can be returned when querying the table.
1157+
It helps control data freshness in scenarios like metadata-cached external tables.
1158+
1159+
Returns:
1160+
Optional[str]: A string representing the maximum staleness interval
1161+
(e.g., '1h', '30m', '15s' for hours, minutes, seconds respectively).
1162+
"""
1163+
return self._properties.get(self._PROPERTY_TO_API_FIELD["max_staleness"])
1164+
1165+
@max_staleness.setter
1166+
def max_staleness(self, value):
1167+
"""Set the maximum staleness for the table.
1168+
1169+
Args:
1170+
value (Optional[str]): A string representing the maximum staleness interval.
1171+
Must be a valid time interval string.
1172+
Examples include '1h' (1 hour), '30m' (30 minutes), '15s' (15 seconds).
1173+
1174+
Raises:
1175+
ValueError: If the value is not None and not a string.
1176+
"""
1177+
if value is not None and not isinstance(value, str):
1178+
raise ValueError("max_staleness must be a string or None")
1179+
1180+
self._properties[self._PROPERTY_TO_API_FIELD["max_staleness"]] = value
1181+
11471182

11481183
class TableListItem(_TableBase):
11491184
"""A read-only table resource from a list operation.

tests/unit/test_table.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,49 @@ def test___str__(self):
14831483
table1 = self._make_one(TableReference(dataset, "table1"))
14841484
self.assertEqual(str(table1), "project1.dataset1.table1")
14851485

1486+
def test_max_staleness_getter(self):
1487+
"""Test getting max_staleness property."""
1488+
dataset = DatasetReference("test-project", "test_dataset")
1489+
table_ref = dataset.table("test_table")
1490+
table = self._make_one(table_ref)
1491+
# Initially None
1492+
self.assertIsNone(table.max_staleness)
1493+
# Set max_staleness using setter
1494+
table.max_staleness = "1h"
1495+
self.assertEqual(table.max_staleness, "1h")
1496+
1497+
def test_max_staleness_setter(self):
1498+
"""Test setting max_staleness property."""
1499+
dataset = DatasetReference("test-project", "test_dataset")
1500+
table_ref = dataset.table("test_table")
1501+
table = self._make_one(table_ref)
1502+
# Set valid max_staleness
1503+
table.max_staleness = "30m"
1504+
self.assertEqual(table.max_staleness, "30m")
1505+
# Set to None
1506+
table.max_staleness = None
1507+
self.assertIsNone(table.max_staleness)
1508+
1509+
def test_max_staleness_setter_invalid_type(self):
1510+
"""Test setting max_staleness with an invalid type raises ValueError."""
1511+
dataset = DatasetReference("test-project", "test_dataset")
1512+
table_ref = dataset.table("test_table")
1513+
table = self._make_one(table_ref)
1514+
# Try setting invalid type
1515+
with self.assertRaises(ValueError):
1516+
table.max_staleness = 123 # Not a string
1517+
1518+
def test_max_staleness_to_api_repr(self):
1519+
"""Test max_staleness is correctly represented in API representation."""
1520+
dataset = DatasetReference("test-project", "test_dataset")
1521+
table_ref = dataset.table("test_table")
1522+
table = self._make_one(table_ref)
1523+
# Set max_staleness
1524+
table.max_staleness = "1h"
1525+
# Convert to API representation
1526+
resource = table.to_api_repr()
1527+
self.assertEqual(resource.get("maxStaleness"), "1h")
1528+
14861529

14871530
class Test_row_from_mapping(unittest.TestCase, _SchemaBase):
14881531
PROJECT = "prahj-ekt"

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/googleapis/python-bigquery/commit/39d4c1d345cb10b236d81a264d5091b11a3dc74a

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy