Skip to content

Commit cf723d9

Browse files
committed
feat: add support for dataset.default_rounding_mode
1 parent 5ceed05 commit cf723d9

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

google/cloud/bigquery/dataset.py

+33
Original file line numberDiff line numberDiff line change
@@ -527,13 +527,46 @@ class Dataset(object):
527527
"friendly_name": "friendlyName",
528528
"default_encryption_configuration": "defaultEncryptionConfiguration",
529529
"storage_billing_model": "storageBillingModel",
530+
"default_rounding_mode": "defaultRoundingMode",
530531
}
531532

532533
def __init__(self, dataset_ref) -> None:
533534
if isinstance(dataset_ref, str):
534535
dataset_ref = DatasetReference.from_string(dataset_ref)
535536
self._properties = {"datasetReference": dataset_ref.to_api_repr(), "labels": {}}
536537

538+
@property
539+
def default_rounding_mode(self):
540+
"""
541+
optional[str]: it could have one of the following possible value.
542+
ROUNDING_MODE_UNSPECIFIED: Unspecified will default to using ROUND_HALF_AWAY_FROM_ZERO.
543+
ROUND_HALF_AWAY_FROM_ZERO: ROUND_HALF_AWAY_FROM_ZERO rounds half values away from zero when applying precision
544+
and scale upon writing of NUMERIC and BIGNUMERIC values.
545+
For Scale: 0 1.1, 1.2, 1.3, 1.4 => 1 1.5, 1.6, 1.7, 1.8, 1.9 => 2
546+
ROUND_HALF_EVEN: ROUND_HALF_EVEN rounds half values to the nearest even value when applying precision and scale
547+
upon writing of NUMERIC and BIGNUMERIC values.
548+
For Scale: 0 1.1, 1.2, 1.3, 1.4 => 1 1.5 => 2 1.6, 1.7, 1.8, 1.9 => 2 2.5 => 2
549+
"""
550+
return self._properties.get("defaultRoundingMode")
551+
552+
@default_rounding_mode.setter
553+
def default_rounding_mode(self, value):
554+
possible_values = [
555+
"ROUNDING_MODE_UNSPECIFIED",
556+
"ROUND_HALF_AWAY_FROM_ZERO",
557+
"ROUND_HALF_EVEN",
558+
]
559+
if not isinstance(value, str) and value is not None:
560+
raise ValueError("Pass a string, or None")
561+
if value not in possible_values:
562+
raise ValueError(
563+
f'rounding mode needs to be one of {",".join(possible_values)}'
564+
)
565+
if value:
566+
self._properties["defaultRoundingMode"] = value
567+
else:
568+
self._properties["defaultRoundingMode"] = "ROUNDING_MODE_UNSPECIFIED"
569+
537570
@property
538571
def project(self):
539572
"""str: Project ID of the project bound to the dataset."""

tests/system/test_client.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ def test_get_dataset(self):
265265
self.assertEqual(got.friendly_name, "Friendly")
266266
self.assertEqual(got.description, "Description")
267267

268+
def test_create_dataset_with_default_rounding_mode(self):
269+
DATASET_ID = _make_dataset_id("create_dataset_rounding_mode")
270+
dataset = self.temp_dataset(DATASET_ID, default_rounding_mode="ROUND_HALF_EVEN")
271+
272+
self.assertTrue(_dataset_exists(dataset))
273+
self.assertEqual(dataset.default_rounding_mode, "ROUND_HALF_EVEN")
274+
268275
def test_update_dataset(self):
269276
dataset = self.temp_dataset(_make_dataset_id("update_dataset"))
270277
self.assertTrue(_dataset_exists(dataset))
@@ -2286,12 +2293,17 @@ def test_nested_table_to_arrow(self):
22862293
self.assertTrue(pyarrow.types.is_list(record_col[1].type))
22872294
self.assertTrue(pyarrow.types.is_int64(record_col[1].type.value_type))
22882295

2289-
def temp_dataset(self, dataset_id, location=None):
2296+
def temp_dataset(self, dataset_id, *args, **kwargs):
22902297
project = Config.CLIENT.project
22912298
dataset_ref = bigquery.DatasetReference(project, dataset_id)
22922299
dataset = Dataset(dataset_ref)
2293-
if location:
2294-
dataset.location = location
2300+
if kwargs.get("location"):
2301+
dataset.location = kwargs.get("location")
2302+
if kwargs.get("max_time_travel_hours"):
2303+
dataset.max_time_travel_hours = kwargs.get("max_time_travel_hours")
2304+
if kwargs.get("default_rounding_mode"):
2305+
dataset.default_rounding_mode = kwargs.get("default_rounding_mode")
2306+
22952307
dataset = helpers.retry_403(Config.CLIENT.create_dataset)(dataset)
22962308
self.to_delete.append(dataset)
22972309
return dataset

0 commit comments

Comments
 (0)