-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add biquery query parameter option to support parametrize query #39389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,88 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one or more | ||
| // contributor license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright ownership. | ||
| // The ASF licenses this file to You 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 bigqueryio | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/gob" | ||
| "io" | ||
| "math/big" | ||
| "time" | ||
|
|
||
| "cloud.google.com/go/bigquery" | ||
| "cloud.google.com/go/civil" | ||
|
|
||
| "github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors" | ||
| ) | ||
|
|
||
| // init registers the concrete types that bigquery.QueryParameter.Value may hold | ||
| // so gob can encode/decode them through the interface{} field. | ||
| func init() { | ||
| // bigquery's paramType/paramValue only recognize | ||
| // *bigquery.QueryParameterValue as an explicitly-typed parameter. | ||
| gob.Register(&bigquery.QueryParameterValue{}) | ||
| gob.Register(bigquery.NullInt64{}) | ||
| gob.Register(bigquery.NullFloat64{}) | ||
| gob.Register(bigquery.NullString{}) | ||
| gob.Register(bigquery.NullBool{}) | ||
| gob.Register(bigquery.NullTimestamp{}) | ||
| gob.Register(bigquery.NullDate{}) | ||
| gob.Register(bigquery.NullTime{}) | ||
| gob.Register(bigquery.NullDateTime{}) | ||
| gob.Register(bigquery.NullGeography{}) | ||
| gob.Register(bigquery.NullJSON{}) | ||
| gob.Register(civil.Date{}) | ||
| gob.Register(civil.Time{}) | ||
| gob.Register(civil.DateTime{}) | ||
| gob.Register(time.Time{}) | ||
| gob.Register(&big.Rat{}) | ||
| gob.Register(&bigquery.IntervalValue{}) | ||
| gob.Register(&bigquery.RangeValue{}) | ||
| } | ||
|
|
||
| func encodeQueryParameters(params []bigquery.QueryParameter) ([]byte, error) { | ||
| if params == nil { | ||
| return []byte{}, nil | ||
| } | ||
| // validate each element to tell which parameter is unsupported. | ||
| for _, p := range params { | ||
| if err := gob.NewEncoder(io.Discard).Encode([]bigquery.QueryParameter{p}); err != nil { | ||
| return nil, errors.Errorf( | ||
| "bigqueryio: query parameter %q has unsupported value type %T (%v). "+ | ||
| "WithQueryParameters only supports bool, string, numeric types, []byte, "+ | ||
| "time.Time, civil.Date/Time/DateTime, *big.Rat, bigquery.Null* types, "+ | ||
| "*bigquery.IntervalValue, *bigquery.RangeValue, and *bigquery.QueryParameterValue. "+ | ||
| "For STRUCT/ARRAY parameters or other custom types, build a "+ | ||
| "*bigquery.QueryParameterValue explicitly instead", p.Name, p.Value, err) | ||
| } | ||
| } | ||
| var buf bytes.Buffer | ||
| if err := gob.NewEncoder(&buf).Encode(params); err != nil { | ||
| return nil, err | ||
| } | ||
| return buf.Bytes(), nil | ||
| } | ||
|
|
||
| func decodeQueryParameters(data []byte) ([]bigquery.QueryParameter, error) { | ||
| if len(data) == 0 { | ||
| return []bigquery.QueryParameter{}, nil | ||
| } | ||
| var params []bigquery.QueryParameter | ||
| if err := gob.NewDecoder(bytes.NewReader(data)).Decode(¶ms); err != nil { | ||
| return nil, err | ||
| } | ||
| return params, nil | ||
| } | ||
|
i05nagai marked this conversation as resolved.
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.