-
Notifications
You must be signed in to change notification settings - Fork 118
/
RoutineInfo.java
546 lines (477 loc) · 17.5 KB
/
RoutineInfo.java
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
* 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 com.google.cloud.bigquery;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.api.client.util.Data;
import com.google.api.services.bigquery.model.Routine;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/**
* Google BigQuery routine information. A Routine is an API abstraction that encapsulates several
* related concepts inside the BigQuery service, including scalar user defined functions (UDFS) and
* stored procedures.
*
* <p>For more information about the REST representation of routines, see:
* https://cloud.google.com/bigquery/docs/reference/rest/v2/routines
*
* <p>For more information about working with scalar functions, see:
* https://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions
*/
public class RoutineInfo implements Serializable {
static final Function<Routine, RoutineInfo> FROM_PB_FUNCTION =
new Function<Routine, RoutineInfo>() {
@Override
public RoutineInfo apply(Routine pb) {
return RoutineInfo.fromPb(pb);
}
};
static final Function<RoutineInfo, Routine> TO_PB_FUNCTION =
new Function<RoutineInfo, Routine>() {
@Override
public Routine apply(RoutineInfo routineInfo) {
return routineInfo.toPb();
}
};
private final RoutineId routineId;
private final String etag;
private final String routineType;
private final Long creationTime;
private final String description;
private final String determinismLevel;
private final Long lastModifiedTime;
private final String language;
private final List<RoutineArgument> argumentList;
private final StandardSQLDataType returnType;
private final StandardSQLTableType returnTableType;
private final List<String> importedLibrariesList;
private final String body;
private final RemoteFunctionOptions remoteFunctionOptions;
private final String dataGovernanceType;
public abstract static class Builder {
abstract Builder setRoutineId(RoutineId id);
abstract Builder setEtag(String etag);
/**
* Sets the routine type for the Builder (e.g. SCALAR_FUNCTION).
*
* <p>See https://cloud.google.com/bigquery/docs/reference/rest/v2/routines
*/
public abstract Builder setRoutineType(String routineType);
abstract Builder setCreationTime(Long creationMillis);
/** Sets the description for the routine. */
abstract Builder setDescription(String description);
abstract Builder setLastModifiedTime(Long lastModifiedMillis);
/**
* Sets the JavaScript UDF determinism levels (e.g. DETERMINISM_LEVEL_UNSPECIFIED,
* DETERMINISTIC, NOT_DETERMINISTIC) only applicable to Javascript UDFs.
*/
public abstract Builder setDeterminismLevel(String determinismLevel);
/** Sets the language for the routine (e.g. SQL or JAVASCRIPT) */
public abstract Builder setLanguage(String language);
/** Specifies the list of input/output arguments for the routine. */
public abstract Builder setArguments(List<RoutineArgument> argumentList);
/**
* Sets the return type of the routine.
*
* <p>Optional if language = "SQL"; required otherwise.
*
* <p>If absent, the return type is inferred from definitionBody at query time in each query
* that references this routine. If present, then the evaluated result will be cast to the
* specified returned type at query time.
*/
public abstract Builder setReturnType(StandardSQLDataType returnType);
/** Optional. Set only if Routine is a "TABLE_VALUED_FUNCTION". */
public abstract Builder setReturnTableType(StandardSQLTableType returnTableType);
/**
* Optional. If language = "JAVASCRIPT", this field stores the path of the imported JAVASCRIPT
* libraries as a list of gs:// URLs.
*/
public abstract Builder setImportedLibraries(List<String> importedLibrariesList);
/**
* Required. The body of the routine.
*
* <p>For functions, this is the expression in the AS clause.
*
* <p>If language=SQL, it is the substring inside (but excluding) the parentheses. For example,
* for the function created with the following statement:
*
* <p>CREATE FUNCTION JoinLines(x string, y string) as (concat(x, "\n", y))
*
* <p>The definitionBody is concat(x, "\n", y) (\n is not replaced with linebreak).
*
* <p>If language=JAVASCRIPT, it is the evaluated string in the AS clause. For example, for the
* function created with the following statement:
*
* <p>CREATE FUNCTION f() RETURNS STRING LANGUAGE js AS 'return "\n";\n'
*
* <p>The definitionBody is
*
* <p>return "\n";\n
*
* <p>Note that both \n are replaced with linebreaks.
*/
public abstract Builder setBody(String body);
/**
* Optional. Remote function specific options.
*
* @param remoteFunctionOptions
* @return
*/
public abstract Builder setRemoteFunctionOptions(RemoteFunctionOptions remoteFunctionOptions);
/**
* Sets the data governance type for the Builder (e.g. DATA_MASKING).
*
* <p>See https://cloud.google.com/bigquery/docs/reference/rest/v2/routines
*/
public abstract Builder setDataGovernanceType(String dataGovernanceType);
/** Creates a {@code RoutineInfo} object. */
public abstract RoutineInfo build();
}
static class BuilderImpl extends Builder {
private RoutineId routineId;
private String etag;
private String routineType;
private Long creationTime;
private String description;
private String determinismLevel;
private Long lastModifiedTime;
private String language;
private List<RoutineArgument> argumentList;
private StandardSQLDataType returnType;
private StandardSQLTableType returnTableType;
private List<String> importedLibrariesList;
private String body;
private RemoteFunctionOptions remoteFunctionOptions;
private String dataGovernanceType;
BuilderImpl() {}
BuilderImpl(RoutineInfo routineInfo) {
this.routineId = routineInfo.routineId;
this.etag = routineInfo.etag;
this.routineType = routineInfo.routineType;
this.creationTime = routineInfo.creationTime;
this.description = routineInfo.description;
this.determinismLevel = routineInfo.determinismLevel;
this.lastModifiedTime = routineInfo.lastModifiedTime;
this.language = routineInfo.language;
this.argumentList = routineInfo.argumentList;
this.returnType = routineInfo.returnType;
this.returnTableType = routineInfo.returnTableType;
this.importedLibrariesList = routineInfo.importedLibrariesList;
this.body = routineInfo.body;
this.remoteFunctionOptions = routineInfo.remoteFunctionOptions;
this.dataGovernanceType = routineInfo.dataGovernanceType;
}
BuilderImpl(Routine routinePb) {
this.routineId = RoutineId.fromPb(routinePb.getRoutineReference());
this.etag = routinePb.getEtag();
this.routineType = routinePb.getRoutineType();
this.creationTime = routinePb.getCreationTime();
this.description = routinePb.getDescription();
this.determinismLevel = routinePb.getDeterminismLevel();
this.lastModifiedTime = routinePb.getLastModifiedTime();
this.language = routinePb.getLanguage();
if (routinePb.getArguments() != null) {
this.argumentList =
Lists.transform(routinePb.getArguments(), RoutineArgument.FROM_PB_FUNCTION);
}
if (routinePb.getReturnType() != null) {
this.returnType = StandardSQLDataType.fromPb(routinePb.getReturnType());
}
if (routinePb.getReturnTableType() != null) {
this.returnTableType = StandardSQLTableType.fromPb(routinePb.getReturnTableType());
}
if (routinePb.getImportedLibraries() == null) {
this.importedLibrariesList = Collections.emptyList();
} else {
this.importedLibrariesList = routinePb.getImportedLibraries();
}
this.body = routinePb.getDefinitionBody();
if (routinePb.getRemoteFunctionOptions() != null) {
this.remoteFunctionOptions =
RemoteFunctionOptions.fromPb(routinePb.getRemoteFunctionOptions());
}
this.dataGovernanceType = routinePb.getDataGovernanceType();
}
@Override
Builder setRoutineId(RoutineId id) {
this.routineId = id;
return this;
}
@Override
Builder setEtag(String etag) {
this.etag = etag;
return this;
}
@Override
public Builder setRoutineType(String routineType) {
this.routineType = routineType;
return this;
}
@Override
Builder setCreationTime(Long creationMillis) {
this.creationTime = creationMillis;
return this;
}
@Override
public Builder setDescription(String description) {
this.description = firstNonNull(description, Data.<String>nullOf(String.class));
return this;
}
@Override
public Builder setDeterminismLevel(String determinismLevel) {
this.determinismLevel = determinismLevel;
return this;
}
@Override
Builder setLastModifiedTime(Long lastModifiedMillis) {
this.lastModifiedTime = lastModifiedMillis;
return this;
}
@Override
public Builder setLanguage(String language) {
this.language = language;
return this;
}
@Override
public Builder setArguments(List<RoutineArgument> argumentList) {
this.argumentList = argumentList;
return this;
}
@Override
public Builder setReturnType(StandardSQLDataType returnType) {
this.returnType = returnType;
return this;
}
@Override
public Builder setReturnTableType(StandardSQLTableType returnTableType) {
this.returnTableType = returnTableType;
return this;
}
@Override
public Builder setImportedLibraries(List<String> importedLibrariesList) {
this.importedLibrariesList = importedLibrariesList;
return this;
}
@Override
public Builder setBody(String body) {
this.body = body;
return this;
}
@Override
public Builder setRemoteFunctionOptions(RemoteFunctionOptions remoteFunctionOptions) {
this.remoteFunctionOptions = remoteFunctionOptions;
return this;
}
@Override
public Builder setDataGovernanceType(String dataGovernanceType) {
this.dataGovernanceType = dataGovernanceType;
return this;
}
@Override
public RoutineInfo build() {
return new RoutineInfo(this);
}
}
RoutineInfo(BuilderImpl builder) {
this.routineId = checkNotNull(builder.routineId);
this.etag = builder.etag;
this.routineType = builder.routineType;
this.creationTime = builder.creationTime;
this.description = builder.description;
this.determinismLevel = builder.determinismLevel;
this.lastModifiedTime = builder.lastModifiedTime;
this.language = builder.language;
this.argumentList = builder.argumentList;
this.returnType = builder.returnType;
this.returnTableType = builder.returnTableType;
this.importedLibrariesList = builder.importedLibrariesList;
this.body = builder.body;
this.remoteFunctionOptions = builder.remoteFunctionOptions;
this.dataGovernanceType = builder.dataGovernanceType;
}
/** Returns the RoutineId identified for the routine resource. * */
public RoutineId getRoutineId() {
return routineId;
}
/** Returns the hash of the routine resource. */
public String getEtag() {
return etag;
}
/** Returns the type of the routine, e.g. SCALAR_FUNCTION. */
public String getRoutineType() {
return routineType;
}
/** Returns the creation time of the routine, represented as milliseconds since the epoch. */
public Long getCreationTime() {
return creationTime;
}
/** Returns the description of the routine. */
public String getDescription() {
return description;
}
/** Returns the determinism level of the JavaScript UDF if defined. */
public String getDeterminismLevel() {
return determinismLevel;
}
/**
* Returns the last modification time of the routine, represented as milliseconds since the epoch.
*/
public Long getLastModifiedTime() {
return lastModifiedTime;
}
/**
* Returns the language of the routine. Currently supported languages include SQL and JAVASCRIPT.
*/
public String getLanguage() {
return language;
}
/** Returns the list of arguments for the routine. */
public List<RoutineArgument> getArguments() {
return argumentList;
}
/** If specified, returns the data type returned from the routine. */
public StandardSQLDataType getReturnType() {
return returnType;
}
/** If specified, returns the table type returned from the routine. */
public StandardSQLTableType getReturnTableType() {
return returnTableType;
}
/**
* Returns the list of imported libraries for the routine. Only relevant for routines implemented
* using the JAVASCRIPT language.
*/
public List<String> getImportedLibraries() {
return importedLibrariesList;
}
/** Returns the definition body of the routine. */
public String getBody() {
return body;
}
/** Returns the Remote function specific options. */
public RemoteFunctionOptions getRemoteFunctionOptions() {
return remoteFunctionOptions;
};
/** Returns the data governance type of the routine, e.g. DATA_MASKING. */
public String getDataGovernanceType() {
return dataGovernanceType;
}
/** Returns a builder pre-populated using the current values of this routine. */
public Builder toBuilder() {
return new BuilderImpl(this);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("routineId", routineId)
.add("etag", etag)
.add("routineType", routineType)
.add("creationTime", creationTime)
.add("description", description)
.add("determinismLevel", determinismLevel)
.add("lastModifiedTime", lastModifiedTime)
.add("language", language)
.add("arguments", argumentList)
.add("returnType", returnType)
.add("returnTableType", returnTableType)
.add("importedLibrariesList", importedLibrariesList)
.add("body", body)
.add("remoteFunctionOptions", remoteFunctionOptions)
.add("dataGovernanceType", dataGovernanceType)
.toString();
}
@Override
public int hashCode() {
return Objects.hash(
routineId,
etag,
routineType,
creationTime,
description,
determinismLevel,
lastModifiedTime,
language,
argumentList,
returnType,
returnTableType,
importedLibrariesList,
body,
remoteFunctionOptions,
dataGovernanceType);
}
@Override
public boolean equals(Object obj) {
return obj == this
|| obj != null
&& obj.getClass().equals(RoutineInfo.class)
&& Objects.equals(toPb(), ((RoutineInfo) obj).toPb());
}
/** Returns a builder for a {@code RoutineInfo} object given routine identity. */
public static Builder newBuilder(RoutineId routineId) {
return new BuilderImpl().setRoutineId(routineId);
}
/** Returns a {@code RoutineInfo} object given routine identity. */
public static RoutineInfo of(RoutineId routineId) {
return newBuilder(routineId).build();
}
RoutineInfo setProjectId(String projectId) {
if (Strings.isNullOrEmpty(getRoutineId().getProject())) {
return toBuilder().setRoutineId(getRoutineId().setProjectId(projectId)).build();
}
return this;
}
Routine toPb() {
Routine routinePb =
new Routine()
.setEtag(getEtag())
.setRoutineType(getRoutineType())
.setDefinitionBody(getBody())
.setCreationTime(getCreationTime())
.setDescription(getDescription())
.setDeterminismLevel(getDeterminismLevel())
.setLastModifiedTime(getLastModifiedTime())
.setLanguage(getLanguage())
.setDataGovernanceType(getDataGovernanceType());
if (getRoutineId() != null) {
routinePb.setRoutineReference(getRoutineId().toPb());
}
if (getArguments() != null) {
routinePb.setArguments(Lists.transform(getArguments(), RoutineArgument.TO_PB_FUNCTION));
}
if (getReturnType() != null) {
routinePb.setReturnType(getReturnType().toPb());
}
if (getReturnTableType() != null) {
routinePb.setReturnTableType(getReturnTableType().toPb());
}
if (getRemoteFunctionOptions() != null) {
routinePb.setRemoteFunctionOptions(getRemoteFunctionOptions().toPb());
}
if (getImportedLibraries() != null) {
routinePb.setImportedLibraries(getImportedLibraries());
}
return routinePb;
}
static RoutineInfo fromPb(Routine routinePb) {
return new BuilderImpl(routinePb).build();
}
}