/
routine.go
399 lines (370 loc) · 11.1 KB
/
routine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2019 Google LLC
//
// 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 (
"context"
"errors"
"fmt"
"time"
"cloud.google.com/go/internal/optional"
"cloud.google.com/go/internal/trace"
bq "google.golang.org/api/bigquery/v2"
)
// Routine represents a reference to a BigQuery routine. There are multiple
// types of routines including stored procedures and scalar user-defined functions (UDFs).
// For more information, see the BigQuery documentation at https://cloud.google.com/bigquery/docs/
type Routine struct {
ProjectID string
DatasetID string
RoutineID string
c *Client
}
func (r *Routine) toBQ() *bq.RoutineReference {
return &bq.RoutineReference{
ProjectId: r.ProjectID,
DatasetId: r.DatasetID,
RoutineId: r.RoutineID,
}
}
// FullyQualifiedName returns an identifer for the routine in project.dataset.routine format.
func (r *Routine) FullyQualifiedName() string {
return fmt.Sprintf("%s.%s.%s", r.ProjectID, r.DatasetID, r.RoutineID)
}
// Create creates a Routine in the BigQuery service.
// Pass in a RoutineMetadata to define the routine.
func (r *Routine) Create(ctx context.Context, rm *RoutineMetadata) (err error) {
ctx = trace.StartSpan(ctx, "cloud.google.com/go/bigquery.Routine.Create")
defer func() { trace.EndSpan(ctx, err) }()
routine, err := rm.toBQ()
if err != nil {
return err
}
routine.RoutineReference = &bq.RoutineReference{
ProjectId: r.ProjectID,
DatasetId: r.DatasetID,
RoutineId: r.RoutineID,
}
req := r.c.bqs.Routines.Insert(r.ProjectID, r.DatasetID, routine).Context(ctx)
setClientHeader(req.Header())
_, err = req.Do()
return err
}
// Metadata fetches the metadata for a given Routine.
func (r *Routine) Metadata(ctx context.Context) (rm *RoutineMetadata, err error) {
ctx = trace.StartSpan(ctx, "cloud.google.com/go/bigquery.Routine.Metadata")
defer func() { trace.EndSpan(ctx, err) }()
req := r.c.bqs.Routines.Get(r.ProjectID, r.DatasetID, r.RoutineID).Context(ctx)
setClientHeader(req.Header())
var routine *bq.Routine
err = runWithRetry(ctx, func() (err error) {
routine, err = req.Do()
return err
})
if err != nil {
return nil, err
}
return bqToRoutineMetadata(routine)
}
// Update modifies properties of a Routine using the API.
func (r *Routine) Update(ctx context.Context, upd *RoutineMetadataToUpdate, etag string) (rm *RoutineMetadata, err error) {
ctx = trace.StartSpan(ctx, "cloud.google.com/go/bigquery.Routine.Update")
defer func() { trace.EndSpan(ctx, err) }()
bqr, err := upd.toBQ()
if err != nil {
return nil, err
}
//TODO: remove when routines update supports partial requests.
bqr.RoutineReference = &bq.RoutineReference{
ProjectId: r.ProjectID,
DatasetId: r.DatasetID,
RoutineId: r.RoutineID,
}
call := r.c.bqs.Routines.Update(r.ProjectID, r.DatasetID, r.RoutineID, bqr).Context(ctx)
setClientHeader(call.Header())
if etag != "" {
call.Header().Set("If-Match", etag)
}
var res *bq.Routine
if err := runWithRetry(ctx, func() (err error) {
res, err = call.Do()
return err
}); err != nil {
return nil, err
}
return bqToRoutineMetadata(res)
}
// Delete removes a Routine from a dataset.
func (r *Routine) Delete(ctx context.Context) (err error) {
ctx = trace.StartSpan(ctx, "cloud.google.com/go/bigquery.Model.Delete")
defer func() { trace.EndSpan(ctx, err) }()
req := r.c.bqs.Routines.Delete(r.ProjectID, r.DatasetID, r.RoutineID).Context(ctx)
setClientHeader(req.Header())
return req.Do()
}
// RoutineDeterminism specifies the level of determinism that javascript User Defined Functions
// exhibit.
type RoutineDeterminism string
const (
// Deterministic indicates that two calls with the same input to a UDF yield the same output.
Deterministic RoutineDeterminism = "DETERMINISTIC"
// NotDeterministic indicates that the output of the UDF is not guaranteed to yield the same
// output each time for a given set of inputs.
NotDeterministic RoutineDeterminism = "NOT_DETERMINISTIC"
)
// RoutineMetadata represents details of a given BigQuery Routine.
type RoutineMetadata struct {
ETag string
// Type indicates the type of routine, such as SCALAR_FUNCTION or PROCEDURE.
Type string
CreationTime time.Time
Description string
// DeterminismLevel is only applicable to Javascript UDFs.
DeterminismLevel RoutineDeterminism
LastModifiedTime time.Time
// Language of the routine, such as SQL or JAVASCRIPT.
Language string
// The list of arguments for the the routine.
Arguments []*RoutineArgument
ReturnType *StandardSQLDataType
// For javascript routines, this indicates the paths for imported libraries.
ImportedLibraries []string
// Body contains the routine's body.
// For functions, Body is the expression in the AS clause.
//
// For SQL functions, it is the substring inside the parentheses of a CREATE
// FUNCTION statement.
//
// For JAVASCRIPT function, it is the evaluated string in the AS clause of
// a CREATE FUNCTION statement.
Body string
}
func (rm *RoutineMetadata) toBQ() (*bq.Routine, error) {
r := &bq.Routine{}
if rm == nil {
return r, nil
}
r.Description = rm.Description
r.DeterminismLevel = string(rm.DeterminismLevel)
r.Language = rm.Language
r.RoutineType = rm.Type
r.DefinitionBody = rm.Body
rt, err := rm.ReturnType.toBQ()
if err != nil {
return nil, err
}
r.ReturnType = rt
var args []*bq.Argument
for _, v := range rm.Arguments {
bqa, err := v.toBQ()
if err != nil {
return nil, err
}
args = append(args, bqa)
}
r.Arguments = args
r.ImportedLibraries = rm.ImportedLibraries
if !rm.CreationTime.IsZero() {
return nil, errors.New("cannot set CreationTime on create")
}
if !rm.LastModifiedTime.IsZero() {
return nil, errors.New("cannot set LastModifiedTime on create")
}
if rm.ETag != "" {
return nil, errors.New("cannot set ETag on create")
}
return r, nil
}
// RoutineArgument represents an argument supplied to a routine such as a UDF or
// stored procedured.
type RoutineArgument struct {
// The name of this argument. Can be absent for function return argument.
Name string
// Kind indicates the kind of argument represented.
// Possible values:
// ARGUMENT_KIND_UNSPECIFIED
// FIXED_TYPE - The argument is a variable with fully specified
// type, which can be a struct or an array, but not a table.
// ANY_TYPE - The argument is any type, including struct or array,
// but not a table.
Kind string
// Mode is optional, and indicates whether an argument is input or output.
// Mode can only be set for procedures.
//
// Possible values:
// MODE_UNSPECIFIED
// IN - The argument is input-only.
// OUT - The argument is output-only.
// INOUT - The argument is both an input and an output.
Mode string
// DataType provides typing information. Unnecessary for ANY_TYPE Kind
// arguments.
DataType *StandardSQLDataType
}
func (ra *RoutineArgument) toBQ() (*bq.Argument, error) {
if ra == nil {
return nil, nil
}
a := &bq.Argument{
Name: ra.Name,
ArgumentKind: ra.Kind,
Mode: ra.Mode,
}
if ra.DataType != nil {
dt, err := ra.DataType.toBQ()
if err != nil {
return nil, err
}
a.DataType = dt
}
return a, nil
}
func bqToRoutineArgument(bqa *bq.Argument) (*RoutineArgument, error) {
arg := &RoutineArgument{
Name: bqa.Name,
Kind: bqa.ArgumentKind,
Mode: bqa.Mode,
}
dt, err := bqToStandardSQLDataType(bqa.DataType)
if err != nil {
return nil, err
}
arg.DataType = dt
return arg, nil
}
func bqToArgs(in []*bq.Argument) ([]*RoutineArgument, error) {
var out []*RoutineArgument
for _, a := range in {
arg, err := bqToRoutineArgument(a)
if err != nil {
return nil, err
}
out = append(out, arg)
}
return out, nil
}
func routineArgumentsToBQ(in []*RoutineArgument) ([]*bq.Argument, error) {
var out []*bq.Argument
for _, inarg := range in {
arg, err := inarg.toBQ()
if err != nil {
return nil, err
}
out = append(out, arg)
}
return out, nil
}
// RoutineMetadataToUpdate governs updating a routine.
type RoutineMetadataToUpdate struct {
Arguments []*RoutineArgument
Description optional.String
DeterminismLevel optional.String
Type optional.String
Language optional.String
Body optional.String
ImportedLibraries []string
ReturnType *StandardSQLDataType
}
func (rm *RoutineMetadataToUpdate) toBQ() (*bq.Routine, error) {
r := &bq.Routine{}
forceSend := func(field string) {
r.ForceSendFields = append(r.ForceSendFields, field)
}
nullField := func(field string) {
r.NullFields = append(r.NullFields, field)
}
if rm.Description != nil {
r.Description = optional.ToString(rm.Description)
forceSend("Description")
}
if rm.DeterminismLevel != nil {
processed := false
// Allow either string or RoutineDeterminism, a type based on string.
if x, ok := rm.DeterminismLevel.(RoutineDeterminism); ok {
r.DeterminismLevel = string(x)
processed = true
}
if x, ok := rm.DeterminismLevel.(string); ok {
r.DeterminismLevel = x
processed = true
}
if !processed {
panic(fmt.Sprintf("DeterminismLevel should be either type string or RoutineDetermism in update, got %T", rm.DeterminismLevel))
}
}
if rm.Arguments != nil {
if len(rm.Arguments) == 0 {
nullField("Arguments")
} else {
args, err := routineArgumentsToBQ(rm.Arguments)
if err != nil {
return nil, err
}
r.Arguments = args
forceSend("Arguments")
}
}
if rm.Type != nil {
r.RoutineType = optional.ToString(rm.Type)
forceSend("RoutineType")
}
if rm.Language != nil {
r.Language = optional.ToString(rm.Language)
forceSend("Language")
}
if rm.Body != nil {
r.DefinitionBody = optional.ToString(rm.Body)
forceSend("DefinitionBody")
}
if rm.ImportedLibraries != nil {
if len(rm.ImportedLibraries) == 0 {
nullField("ImportedLibraries")
} else {
r.ImportedLibraries = rm.ImportedLibraries
forceSend("ImportedLibraries")
}
}
if rm.ReturnType != nil {
dt, err := rm.ReturnType.toBQ()
if err != nil {
return nil, err
}
r.ReturnType = dt
forceSend("ReturnType")
}
return r, nil
}
func bqToRoutineMetadata(r *bq.Routine) (*RoutineMetadata, error) {
meta := &RoutineMetadata{
ETag: r.Etag,
Type: r.RoutineType,
CreationTime: unixMillisToTime(r.CreationTime),
Description: r.Description,
DeterminismLevel: RoutineDeterminism(r.DeterminismLevel),
LastModifiedTime: unixMillisToTime(r.LastModifiedTime),
Language: r.Language,
ImportedLibraries: r.ImportedLibraries,
Body: r.DefinitionBody,
}
args, err := bqToArgs(r.Arguments)
if err != nil {
return nil, err
}
meta.Arguments = args
ret, err := bqToStandardSQLDataType(r.ReturnType)
if err != nil {
return nil, err
}
meta.ReturnType = ret
return meta, nil
}