Skip to content

Commit cc98509

Browse files
authored
fix(bigquery): support more timestamp formats for query param (#9236)
BigQuery accepts timestamp parameters in many different formats, so when parsing the query parameters, we need to be able to support all of them. As reported on #9221, if a query is created using bq-cli (or any other system) with the `time.DateTime(2006-01-02 15:04:05)` format, our SDK was failing to parse the Job Config when retrieving it from the backend system. Resolves #9221
1 parent 929d817 commit cc98509

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

bigquery/integration_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,6 +2473,22 @@ func TestIntegration_TimestampFormat(t *testing.T) {
24732473
[]Value{ts},
24742474
ts,
24752475
},
2476+
{
2477+
"SELECT @val",
2478+
[]*bq.QueryParameter{
2479+
{
2480+
Name: "val",
2481+
ParameterType: &bq.QueryParameterType{
2482+
Type: "TIMESTAMP",
2483+
},
2484+
ParameterValue: &bq.QueryParameterValue{
2485+
Value: ts.Format(dateTimeFormat),
2486+
},
2487+
},
2488+
},
2489+
[]Value{ts},
2490+
ts,
2491+
},
24762492
{
24772493
"SELECT @val",
24782494
[]*bq.QueryParameter{

bigquery/params.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ import (
2929
bq "google.golang.org/api/bigquery/v2"
3030
)
3131

32+
// See https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#timestamp-type.
3233
var (
33-
// See https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#timestamp-type.
3434
timestampFormat = "2006-01-02 15:04:05.999999-07:00"
35+
dateTimeFormat = "2006-01-02 15:04:05"
36+
)
3537

38+
var (
3639
// See https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#schema.fields.name
3740
validFieldName = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]{0,127}$")
3841
)
@@ -590,14 +593,17 @@ func convertParamValue(qval *bq.QueryParameterValue, qtype *bq.QueryParameterTyp
590593
if isNullScalar(qval) {
591594
return NullTimestamp{Valid: false}, nil
592595
}
593-
t, err := time.Parse(timestampFormat, qval.Value)
594-
if err != nil {
595-
t, err = time.Parse(time.RFC3339Nano, qval.Value)
596+
formats := []string{timestampFormat, time.RFC3339Nano, dateTimeFormat}
597+
var lastParseErr error
598+
for _, format := range formats {
599+
t, err := time.Parse(format, qval.Value)
596600
if err != nil {
597-
return nil, err
601+
lastParseErr = err
602+
continue
598603
}
604+
return t, nil
599605
}
600-
return t, nil
606+
return nil, lastParseErr
601607

602608
case "DATETIME":
603609
if isNullScalar(qval) {

0 commit comments

Comments
 (0)