4949import google .api_core .exceptions as core_exceptions
5050from google .api_core import retry as retries
5151
52+ from google .cloud .bigquery import enums
5253from google .cloud .bigquery import job
5354import google .cloud .bigquery .job .query
5455import google .cloud .bigquery .query
@@ -265,6 +266,7 @@ def _to_query_request(
265266 query : str ,
266267 location : Optional [str ] = None ,
267268 timeout : Optional [float ] = None ,
269+ timestamp_precision : Optional [enums .TimestampPrecision ] = None ,
268270) -> Dict [str , Any ]:
269271 """Transform from Job resource to QueryRequest resource.
270272
@@ -290,6 +292,12 @@ def _to_query_request(
290292 request_body .setdefault ("formatOptions" , {})
291293 request_body ["formatOptions" ]["useInt64Timestamp" ] = True # type: ignore
292294
295+ if timestamp_precision == enums .TimestampPrecision .PICOSECOND :
296+ # Cannot specify both use_int64_timestamp and timestamp_output_format.
297+ del request_body ["formatOptions" ]["useInt64Timestamp" ]
298+
299+ request_body ["formatOptions" ]["timestampOutputFormat" ] = "ISO8601_STRING"
300+
293301 if timeout is not None :
294302 # Subtract a buffer for context switching, network latency, etc.
295303 request_body ["timeoutMs" ] = max (0 , int (1000 * timeout ) - _TIMEOUT_BUFFER_MILLIS )
@@ -370,14 +378,19 @@ def query_jobs_query(
370378 retry : retries .Retry ,
371379 timeout : Optional [float ],
372380 job_retry : Optional [retries .Retry ],
381+ timestamp_precision : Optional [enums .TimestampPrecision ] = None ,
373382) -> job .QueryJob :
374383 """Initiate a query using jobs.query with jobCreationMode=JOB_CREATION_REQUIRED.
375384
376385 See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query
377386 """
378387 path = _to_query_path (project )
379388 request_body = _to_query_request (
380- query = query , job_config = job_config , location = location , timeout = timeout
389+ query = query ,
390+ job_config = job_config ,
391+ location = location ,
392+ timeout = timeout ,
393+ timestamp_precision = timestamp_precision ,
381394 )
382395
383396 def do_query ():
0 commit comments