From f53110e58a59ef81554f4cdcd9b4618d9620833c Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 8 Jan 2024 17:34:55 -0400 Subject: [PATCH 1/3] fix(bigquery): support more timestamp formats for query param --- bigquery/integration_test.go | 16 ++++++++++++++++ bigquery/params.go | 13 ++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/bigquery/integration_test.go b/bigquery/integration_test.go index 232333383da9..115d84c13f7b 100644 --- a/bigquery/integration_test.go +++ b/bigquery/integration_test.go @@ -2473,6 +2473,22 @@ func TestIntegration_TimestampFormat(t *testing.T) { []Value{ts}, ts, }, + { + "SELECT @val", + []*bq.QueryParameter{ + { + Name: "val", + ParameterType: &bq.QueryParameterType{ + Type: "TIMESTAMP", + }, + ParameterValue: &bq.QueryParameterValue{ + Value: ts.Format(time.DateTime), + }, + }, + }, + []Value{ts}, + ts, + }, { "SELECT @val", []*bq.QueryParameter{ diff --git a/bigquery/params.go b/bigquery/params.go index bdbe9a947104..4cc7f48cace7 100644 --- a/bigquery/params.go +++ b/bigquery/params.go @@ -590,14 +590,17 @@ func convertParamValue(qval *bq.QueryParameterValue, qtype *bq.QueryParameterTyp if isNullScalar(qval) { return NullTimestamp{Valid: false}, nil } - t, err := time.Parse(timestampFormat, qval.Value) - if err != nil { - t, err = time.Parse(time.RFC3339Nano, qval.Value) + formats := []string{timestampFormat, time.RFC3339Nano, time.DateTime} + var lastParseErr error + for _, format := range formats { + t, err := time.Parse(format, qval.Value) if err != nil { - return nil, err + lastParseErr = err + continue } + return t, nil } - return t, nil + return nil, lastParseErr case "DATETIME": if isNullScalar(qval) { From 0cf0a3c2101895c2c9f492d58cf8583b3d1f3be4 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Tue, 9 Jan 2024 09:37:00 -0400 Subject: [PATCH 2/3] fix: time.DateTime not available on go 1.18 --- bigquery/params.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bigquery/params.go b/bigquery/params.go index 4cc7f48cace7..cf47e9a23821 100644 --- a/bigquery/params.go +++ b/bigquery/params.go @@ -29,10 +29,13 @@ import ( bq "google.golang.org/api/bigquery/v2" ) +// See https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#timestamp-type. var ( - // See https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#timestamp-type. timestampFormat = "2006-01-02 15:04:05.999999-07:00" + dateTimeFormat = "2006-01-02 15:04:05" +) +var ( // See https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#schema.fields.name validFieldName = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]{0,127}$") ) @@ -590,7 +593,7 @@ func convertParamValue(qval *bq.QueryParameterValue, qtype *bq.QueryParameterTyp if isNullScalar(qval) { return NullTimestamp{Valid: false}, nil } - formats := []string{timestampFormat, time.RFC3339Nano, time.DateTime} + formats := []string{timestampFormat, time.RFC3339Nano, dateTimeFormat} var lastParseErr error for _, format := range formats { t, err := time.Parse(format, qval.Value) From d91b23a5ae1fad4e929b383b4238492b7e2a0592 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Tue, 9 Jan 2024 09:54:56 -0400 Subject: [PATCH 3/3] fix: remove another ref to time.DateTime --- bigquery/integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bigquery/integration_test.go b/bigquery/integration_test.go index 115d84c13f7b..3a749719432e 100644 --- a/bigquery/integration_test.go +++ b/bigquery/integration_test.go @@ -2482,7 +2482,7 @@ func TestIntegration_TimestampFormat(t *testing.T) { Type: "TIMESTAMP", }, ParameterValue: &bq.QueryParameterValue{ - Value: ts.Format(time.DateTime), + Value: ts.Format(dateTimeFormat), }, }, },