-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bigquery: support scalar query parameters
Initial support for query parameters. This CL supports only scalar parameters (no arrays or structs). Query parameters are cumbersome to use in the underlying API for two reasons: all scalar values must be encoded as strings, and each value must be accompanied by a type. To improve the ergonomics, this client accepts Go values for parameters, converting them to strings and determining their types. As @c0b pointed out, the parameter mode is unnecessary, so we omit it from the surface and the RPC request. See #390. Change-Id: Id4c1ba740bdee00979efbffe7e0f8276ed80ff00 Reviewed-on: https://code-review.googlesource.com/9557 Reviewed-by: Ross Light <[email protected]>
- Loading branch information
Showing
5 changed files
with
257 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2016 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package bigquery | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"time" | ||
|
||
bq "google.golang.org/api/bigquery/v2" | ||
) | ||
|
||
// See https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#timestamp-type. | ||
var timestampFormat = "2006-01-02 15:04:05.999999-07:00" | ||
|
||
var ( | ||
int64ParamType = &bq.QueryParameterType{Type: "INT64"} | ||
float64ParamType = &bq.QueryParameterType{Type: "FLOAT64"} | ||
boolParamType = &bq.QueryParameterType{Type: "BOOL"} | ||
stringParamType = &bq.QueryParameterType{Type: "STRING"} | ||
bytesParamType = &bq.QueryParameterType{Type: "BYTES"} | ||
timestampParamType = &bq.QueryParameterType{Type: "TIMESTAMP"} | ||
) | ||
|
||
func paramType(x interface{}) (*bq.QueryParameterType, error) { | ||
switch x.(type) { | ||
case int, int8, int16, int32, int64, uint8, uint16, uint32: | ||
return int64ParamType, nil | ||
case float32, float64: | ||
return float64ParamType, nil | ||
case bool: | ||
return boolParamType, nil | ||
case string: | ||
return stringParamType, nil | ||
case time.Time: | ||
return timestampParamType, nil | ||
case []byte: | ||
return bytesParamType, nil | ||
default: | ||
return nil, fmt.Errorf("Go type %T cannot be represented as a parameter type", x) | ||
} | ||
} | ||
|
||
func paramValue(x interface{}) (bq.QueryParameterValue, error) { | ||
// convenience function for scalar value | ||
sval := func(s string) bq.QueryParameterValue { | ||
return bq.QueryParameterValue{Value: s} | ||
} | ||
switch x := x.(type) { | ||
case []byte: | ||
return sval(base64.StdEncoding.EncodeToString(x)), nil | ||
case time.Time: | ||
return sval(x.Format(timestampFormat)), nil | ||
default: | ||
return sval(fmt.Sprint(x)), nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2016 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package bigquery | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"math" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
bq "google.golang.org/api/bigquery/v2" | ||
) | ||
|
||
var scalarTests = []struct { | ||
val interface{} | ||
want string | ||
}{ | ||
{int64(0), "0"}, | ||
{3.14, "3.14"}, | ||
{3.14159e-87, "3.14159e-87"}, | ||
{true, "true"}, | ||
{"string", "string"}, | ||
{"\u65e5\u672c\u8a9e\n", "\u65e5\u672c\u8a9e\n"}, | ||
{math.NaN(), "NaN"}, | ||
{[]byte("foo"), "Zm9v"}, // base64 encoding of "foo" | ||
{time.Date(2016, 3, 20, 4, 22, 9, 5000, time.FixedZone("neg1-2", -3720)), | ||
"2016-03-20 04:22:09.000005-01:02"}, | ||
} | ||
|
||
func TestParamValueScalar(t *testing.T) { | ||
for _, test := range scalarTests { | ||
got, err := paramValue(test.val) | ||
if err != nil { | ||
t.Errorf("%v: got %v, want nil", test.val, err) | ||
continue | ||
} | ||
if got.ArrayValues != nil { | ||
t.Errorf("%v, ArrayValues: got %v, expected nil", test.val, got.ArrayValues) | ||
} | ||
if got.StructValues != nil { | ||
t.Errorf("%v, StructValues: got %v, expected nil", test.val, got.StructValues) | ||
} | ||
if got.Value != test.want { | ||
t.Errorf("%v: got %q, want %q", test.val, got.Value, test.want) | ||
} | ||
} | ||
} | ||
|
||
func TestParamTypeScalar(t *testing.T) { | ||
for _, test := range []struct { | ||
val interface{} | ||
want *bq.QueryParameterType | ||
}{ | ||
{0, int64ParamType}, | ||
{uint32(32767), int64ParamType}, | ||
{3.14, float64ParamType}, | ||
{float32(3.14), float64ParamType}, | ||
{math.NaN(), float64ParamType}, | ||
{true, boolParamType}, | ||
{"", stringParamType}, | ||
{"string", stringParamType}, | ||
{time.Now(), timestampParamType}, | ||
{[]byte("foo"), bytesParamType}, | ||
} { | ||
got, err := paramType(test.val) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if got != test.want { | ||
t.Errorf("%v (%T): got %v, want %v", test.val, test.val, got, test.want) | ||
} | ||
} | ||
} | ||
|
||
func TestIntegration_ScalarParam(t *testing.T) { | ||
ctx := context.Background() | ||
c := getClient(t) | ||
for _, test := range scalarTests { | ||
q := c.Query("select ?") | ||
q.Parameters = []QueryParameter{{Value: test.val}} | ||
it, err := q.Read(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
var val []Value | ||
err = it.Next(&val) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(val) != 1 { | ||
t.Fatalf("got %d values, want 1", len(val)) | ||
} | ||
got := val[0] | ||
if !equal(got, test.val) { | ||
t.Errorf("\ngot %#v (%T)\nwant %#v (%T)", got, got, test.val, test.val) | ||
} | ||
} | ||
} | ||
|
||
func equal(x1, x2 interface{}) bool { | ||
if reflect.TypeOf(x1) != reflect.TypeOf(x2) { | ||
return false | ||
} | ||
switch x1 := x1.(type) { | ||
case float64: | ||
if math.IsNaN(x1) { | ||
return math.IsNaN(x2.(float64)) | ||
} | ||
return x1 == x2 | ||
case time.Time: | ||
// BigQuery is only accurate to the microsecond. | ||
return x1.Round(time.Microsecond).Equal(x2.(time.Time).Round(time.Microsecond)) | ||
case []byte: | ||
return bytes.Equal(x1, x2.([]byte)) | ||
default: | ||
return x1 == x2 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters