Skip to content

Commit f4c9b6a

Browse files
committed
feat: support load table with picosecond timestamp
1 parent 8d5785a commit f4c9b6a

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

google/cloud/bigquery/job/load.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,37 @@ def column_name_character_map(self, value: Optional[str]):
760760
self._set_sub_prop("columnNameCharacterMap", value)
761761

762762

763+
@property
764+
def timestamp_target_precision(self) -> Optional[list[int]]:
765+
"""Optional[list[int]]: [Private Preview] Precisions (maximum number of
766+
total digits in base 10) for seconds of TIMESTAMP types that are
767+
allowed to the destination table for autodetection mode.
768+
769+
Available for the formats: CSV.
770+
771+
For the CSV Format, Possible values include:
772+
None, [], or [6]: timestamp(6) for all auto detected TIMESTAMP
773+
columns.
774+
[6, 12]: timestamp(6) for all auto detected TIMESTAMP columns that
775+
have less than 6 digits of subseconds. timestamp(12) for all auto
776+
detected TIMESTAMP columns that have more than 6 digits of
777+
subseconds.
778+
[12]: timestamp(12) for all auto detected TIMESTAMP columns.
779+
780+
The order of the elements in this array is ignored. Inputs that have
781+
higher precision than the highest target precision in this array will
782+
be truncated.
783+
"""
784+
return self._get_sub_prop("timestampTargetPrecision")
785+
786+
@timestamp_target_precision.setter
787+
def timestamp_target_precision(self, value: Optional[list[int]]):
788+
if value is not None:
789+
self._set_sub_prop("timestampTargetPrecision", value)
790+
else:
791+
self._del_sub_prop("timestampTargetPrecision")
792+
793+
763794
class LoadJob(_AsyncJob):
764795
"""Asynchronous job for loading data into a table.
765796

tests/unit/job/test_load_config.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,9 +1061,33 @@ def test_column_name_character_map_none(self):
10611061
"parquetOptions": {"enableListInference": True},
10621062
"columnNameCharacterMap": "V2",
10631063
"someNewField": "some-value",
1064+
"timestampTargetPrecision": [6, 12],
10641065
}
10651066
}
10661067

1068+
def test_timestamp_target_precision_missing(self):
1069+
config = self._get_target_class()()
1070+
self.assertIsNone(config.timestamp_target_precision)
1071+
1072+
def test_timestamp_target_precision_hit(self):
1073+
timestamp_target_precision = [6, 12]
1074+
config = self._get_target_class()()
1075+
config._properties["load"]["timestampTargetPrecision"] = timestamp_target_precision
1076+
self.assertEqual(config.timestamp_target_precision, timestamp_target_precision)
1077+
1078+
def test_timestamp_target_precision_setter(self):
1079+
timestamp_target_precision = [6, 12]
1080+
config = self._get_target_class()()
1081+
config.timestamp_target_precision = timestamp_target_precision
1082+
self.assertEqual(config._properties["load"]["timestampTargetPrecision"], timestamp_target_precision)
1083+
1084+
def test_timestamp_target_precision_setter_w_none(self):
1085+
timestamp_target_precision = [6, 12]
1086+
config = self._get_target_class()()
1087+
config._properties["load"]["timestampTargetPrecision"] = timestamp_target_precision
1088+
config.timestamp_target_precision = None
1089+
self.assertFalse("timestampTargetPrecision" in config._properties["load"])
1090+
10671091
def test_from_api_repr(self):
10681092
from google.cloud.bigquery.job import (
10691093
CreateDisposition,
@@ -1103,6 +1127,7 @@ def test_from_api_repr(self):
11031127
self.assertTrue(config.parquet_options.enable_list_inference)
11041128
self.assertEqual(config.column_name_character_map, ColumnNameCharacterMap.V2)
11051129
self.assertEqual(config._properties["load"]["someNewField"], "some-value")
1130+
self.assertEqual(config.timestamp_target_precision, [6, 12])
11061131

11071132
def test_to_api_repr(self):
11081133
from google.cloud.bigquery.job import (
@@ -1140,6 +1165,7 @@ def test_to_api_repr(self):
11401165
config.parquet_options = parquet_options
11411166
config.column_name_character_map = ColumnNameCharacterMap.V2
11421167
config._properties["load"]["someNewField"] = "some-value"
1168+
config.timestamp_target_precision = [6, 12]
11431169

11441170
api_repr = config.to_api_repr()
11451171

0 commit comments

Comments
 (0)