/
QueryJobConfiguration.java
1097 lines (1014 loc) · 43.5 KB
/
QueryJobConfiguration.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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016 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.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.isNullOrEmpty;
import com.google.api.services.bigquery.model.JobConfigurationQuery;
import com.google.api.services.bigquery.model.QueryParameter;
import com.google.cloud.bigquery.JobInfo.CreateDisposition;
import com.google.cloud.bigquery.JobInfo.SchemaUpdateOption;
import com.google.cloud.bigquery.JobInfo.WriteDisposition;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Google BigQuery Query Job configuration. A Query Job runs a query against BigQuery data. Query
* job configurations have {@link JobConfiguration.Type#QUERY} type.
*/
public final class QueryJobConfiguration extends JobConfiguration {
private static final long serialVersionUID = -1108948249081804890L;
private final String query;
private final ImmutableList<QueryParameterValue> positionalParameters;
private final ImmutableMap<String, QueryParameterValue> namedParameters;
private final TableId destinationTable;
private final Map<String, ExternalTableDefinition> tableDefinitions;
private final List<UserDefinedFunction> userDefinedFunctions;
private final CreateDisposition createDisposition;
private final WriteDisposition writeDisposition;
private final DatasetId defaultDataset;
private final Priority priority;
private final Boolean allowLargeResults;
private final Boolean useQueryCache;
private final Boolean flattenResults;
private final Boolean dryRun;
private final Boolean useLegacySql;
private final Integer maximumBillingTier;
private final Long maximumBytesBilled;
private final List<SchemaUpdateOption> schemaUpdateOptions;
private final EncryptionConfiguration destinationEncryptionConfiguration;
private final TimePartitioning timePartitioning;
private final Clustering clustering;
private final Long jobTimeoutMs;
private final Map<String, String> labels;
private final RangePartitioning rangePartitioning;
private final List<ConnectionProperty> connectionProperties;
// maxResults is only used for fast query path
private final Long maxResults;
/**
* Priority levels for a query. If not specified the priority is assumed to be {@link
* Priority#INTERACTIVE}.
*/
public enum Priority {
/**
* Query is executed as soon as possible and count towards the <a
* href="https://cloud.google.com/bigquery/quota-policy">concurrent rate limit and the daily
* rate limit</a>.
*/
INTERACTIVE,
/**
* Query is queued and started as soon as idle resources are available, usually within a few
* minutes. If the query hasn't started within 3 hours, its priority is changed to {@link
* Priority#INTERACTIVE}.
*/
BATCH
}
public static final class Builder
extends JobConfiguration.Builder<QueryJobConfiguration, Builder> {
private String query;
private List<QueryParameterValue> positionalParameters = Lists.newArrayList();
private Map<String, QueryParameterValue> namedParameters = Maps.newHashMap();
private TableId destinationTable;
private Map<String, ExternalTableDefinition> tableDefinitions;
private List<UserDefinedFunction> userDefinedFunctions;
private CreateDisposition createDisposition;
private WriteDisposition writeDisposition;
private DatasetId defaultDataset;
private Priority priority;
private Boolean allowLargeResults;
private Boolean useQueryCache;
private Boolean flattenResults;
private Boolean dryRun;
private Boolean useLegacySql = false;
private Integer maximumBillingTier;
private Long maximumBytesBilled;
private List<SchemaUpdateOption> schemaUpdateOptions;
private EncryptionConfiguration destinationEncryptionConfiguration;
private TimePartitioning timePartitioning;
private Clustering clustering;
private Long jobTimeoutMs;
private Map<String, String> labels;
private RangePartitioning rangePartitioning;
private List<ConnectionProperty> connectionProperties;
private Long maxResults;
private Builder() {
super(Type.QUERY);
}
private Builder(QueryJobConfiguration jobConfiguration) {
this();
this.query = jobConfiguration.query;
this.namedParameters = jobConfiguration.namedParameters;
this.positionalParameters = jobConfiguration.positionalParameters;
this.destinationTable = jobConfiguration.destinationTable;
this.tableDefinitions = jobConfiguration.tableDefinitions;
this.userDefinedFunctions = jobConfiguration.userDefinedFunctions;
this.createDisposition = jobConfiguration.createDisposition;
this.writeDisposition = jobConfiguration.writeDisposition;
this.defaultDataset = jobConfiguration.defaultDataset;
this.priority = jobConfiguration.priority;
this.allowLargeResults = jobConfiguration.allowLargeResults;
this.useQueryCache = jobConfiguration.useQueryCache;
this.flattenResults = jobConfiguration.flattenResults;
this.dryRun = jobConfiguration.dryRun;
this.useLegacySql = jobConfiguration.useLegacySql;
this.maximumBillingTier = jobConfiguration.maximumBillingTier;
this.maximumBytesBilled = jobConfiguration.maximumBytesBilled;
this.schemaUpdateOptions = jobConfiguration.schemaUpdateOptions;
this.destinationEncryptionConfiguration = jobConfiguration.destinationEncryptionConfiguration;
this.timePartitioning = jobConfiguration.timePartitioning;
this.clustering = jobConfiguration.clustering;
this.jobTimeoutMs = jobConfiguration.jobTimeoutMs;
this.labels = jobConfiguration.labels;
this.rangePartitioning = jobConfiguration.rangePartitioning;
this.connectionProperties = jobConfiguration.connectionProperties;
this.maxResults = jobConfiguration.maxResults;
}
private Builder(com.google.api.services.bigquery.model.JobConfiguration configurationPb) {
this();
JobConfigurationQuery queryConfigurationPb = configurationPb.getQuery();
this.query = queryConfigurationPb.getQuery();
if (queryConfigurationPb.getQueryParameters() != null
&& !queryConfigurationPb.getQueryParameters().isEmpty()) {
if (queryConfigurationPb.getQueryParameters().get(0).getName() == null) {
setPositionalParameters(
Lists.transform(
queryConfigurationPb.getQueryParameters(),
POSITIONAL_PARAMETER_FROM_PB_FUNCTION));
} else {
Map<String, QueryParameterValue> values = Maps.newHashMap();
for (QueryParameter queryParameterPb : queryConfigurationPb.getQueryParameters()) {
checkNotNull(queryParameterPb.getName());
QueryParameterValue value =
QueryParameterValue.fromPb(
queryParameterPb.getParameterValue(), queryParameterPb.getParameterType());
values.put(queryParameterPb.getName(), value);
}
setNamedParameters(values);
}
}
allowLargeResults = queryConfigurationPb.getAllowLargeResults();
useQueryCache = queryConfigurationPb.getUseQueryCache();
flattenResults = queryConfigurationPb.getFlattenResults();
useLegacySql = queryConfigurationPb.getUseLegacySql();
if (queryConfigurationPb.getMaximumBillingTier() != null) {
maximumBillingTier = queryConfigurationPb.getMaximumBillingTier();
}
if (queryConfigurationPb.getMaximumBytesBilled() != null) {
maximumBytesBilled = queryConfigurationPb.getMaximumBytesBilled();
}
dryRun = configurationPb.getDryRun();
if (queryConfigurationPb.getDestinationTable() != null) {
destinationTable = TableId.fromPb(queryConfigurationPb.getDestinationTable());
}
if (queryConfigurationPb.getDefaultDataset() != null) {
defaultDataset = DatasetId.fromPb(queryConfigurationPb.getDefaultDataset());
}
if (queryConfigurationPb.getPriority() != null) {
priority = Priority.valueOf(queryConfigurationPb.getPriority());
}
if (queryConfigurationPb.getTableDefinitions() != null) {
tableDefinitions =
Maps.transformValues(
queryConfigurationPb.getTableDefinitions(),
ExternalTableDefinition.FROM_EXTERNAL_DATA_FUNCTION);
}
if (queryConfigurationPb.getUserDefinedFunctionResources() != null) {
userDefinedFunctions =
Lists.transform(
queryConfigurationPb.getUserDefinedFunctionResources(),
UserDefinedFunction.FROM_PB_FUNCTION);
}
if (queryConfigurationPb.getCreateDisposition() != null) {
createDisposition = CreateDisposition.valueOf(queryConfigurationPb.getCreateDisposition());
}
if (queryConfigurationPb.getWriteDisposition() != null) {
writeDisposition = WriteDisposition.valueOf(queryConfigurationPb.getWriteDisposition());
}
if (queryConfigurationPb.getSchemaUpdateOptions() != null) {
ImmutableList.Builder<JobInfo.SchemaUpdateOption> schemaUpdateOptionsBuilder =
new ImmutableList.Builder<>();
for (String rawSchemaUpdateOption : queryConfigurationPb.getSchemaUpdateOptions()) {
schemaUpdateOptionsBuilder.add(JobInfo.SchemaUpdateOption.valueOf(rawSchemaUpdateOption));
}
this.schemaUpdateOptions = schemaUpdateOptionsBuilder.build();
}
if (queryConfigurationPb.getDestinationEncryptionConfiguration() != null) {
this.destinationEncryptionConfiguration =
new EncryptionConfiguration.Builder(
queryConfigurationPb.getDestinationEncryptionConfiguration())
.build();
}
if (queryConfigurationPb.getTimePartitioning() != null) {
this.timePartitioning = TimePartitioning.fromPb(queryConfigurationPb.getTimePartitioning());
}
if (queryConfigurationPb.getClustering() != null) {
this.clustering = Clustering.fromPb(queryConfigurationPb.getClustering());
}
if (configurationPb.getJobTimeoutMs() != null) {
this.jobTimeoutMs = configurationPb.getJobTimeoutMs();
}
if (configurationPb.getLabels() != null) {
this.labels = configurationPb.getLabels();
}
if (queryConfigurationPb.getRangePartitioning() != null) {
this.rangePartitioning =
RangePartitioning.fromPb(queryConfigurationPb.getRangePartitioning());
}
if (queryConfigurationPb.getConnectionProperties() != null) {
this.connectionProperties =
Lists.transform(
queryConfigurationPb.getConnectionProperties(),
ConnectionProperty.FROM_PB_FUNCTION);
}
}
/** Sets the BigQuery SQL query to execute. */
public Builder setQuery(String query) {
this.query = query;
return this;
}
/**
* Adds a positional query parameter to the list of query parameters. See {@link
* #setPositionalParameters(Iterable)} for more details on the input requirements.
*
* <p>A positional parameter cannot be added after named parameters have been added.
*/
public Builder addPositionalParameter(QueryParameterValue value) {
checkNotNull(value);
if (!namedParameters.isEmpty()) {
throw new IllegalStateException(
"Positional parameters can't be combined with named parameters");
}
positionalParameters.add(value);
return this;
}
/**
* Sets the query parameters to a list of positional query parameters to use in the query.
*
* <p>The set of query parameters must either be all positional or all named parameters.
* Positional parameters are denoted in the query with a question mark (?).
*
* <p>Additionally, useLegacySql must be set to false; query parameters cannot be used with
* legacy SQL.
*
* <p>The values parameter can be set to null to clear out the positional parameters so that
* named parameters can be used instead.
*/
public Builder setPositionalParameters(Iterable<QueryParameterValue> values) {
if (values == null || Iterables.isEmpty(values)) {
positionalParameters = Lists.newArrayList();
} else {
if (!this.namedParameters.isEmpty()) {
throw new IllegalStateException(
"Positional parameters can't be combined with named parameters");
}
this.positionalParameters = Lists.newArrayList(values);
}
return this;
}
/**
* Adds a named query parameter to the set of query parameters. See {@link
* #setNamedParameters(Map)} for more details on the input requirements.
*
* <p>A named parameter cannot be added after positional parameters have been added.
*/
public Builder addNamedParameter(String name, QueryParameterValue value) {
checkNotNull(value);
if (!this.positionalParameters.isEmpty()) {
throw new IllegalStateException(
"Named parameters can't be combined with positional parameters");
}
namedParameters.put(name, value);
return this;
}
/**
* Sets the query parameters to a set of named query parameters to use in the query.
*
* <p>The set of query parameters must either be all positional or all named parameters. Named
* parameters are denoted using an @ prefix, e.g. @myParam for a parameter named "myParam".
*
* <p>Additionally, useLegacySql must be set to false; query parameters cannot be used with
* legacy SQL.
*
* <p>The values parameter can be set to null to clear out the named parameters so that
* positional parameters can be used instead.
*/
public Builder setNamedParameters(Map<String, QueryParameterValue> values) {
if (values == null || values.isEmpty()) {
namedParameters = Maps.newHashMap();
} else {
if (!this.positionalParameters.isEmpty()) {
throw new IllegalStateException(
"Named parameters can't be combined with positional parameters");
}
this.namedParameters = Maps.newHashMap(values);
}
return this;
}
/**
* Sets the table where to put query results. If not provided a new table is created. This value
* is required if {@link Builder#setAllowLargeResults(Boolean)} is set to {@code true}.
*/
public Builder setDestinationTable(TableId destinationTable) {
this.destinationTable = destinationTable;
return this;
}
public Builder setDestinationEncryptionConfiguration(
EncryptionConfiguration encryptionConfiguration) {
this.destinationEncryptionConfiguration = encryptionConfiguration;
return this;
}
/**
* Sets the external tables definitions. If querying external data sources outside of BigQuery,
* this value describes the data format, location and other properties of the data sources. By
* defining these properties, the data sources can be queried as if they were standard BigQuery
* tables.
*/
public Builder setTableDefinitions(Map<String, ExternalTableDefinition> tableDefinitions) {
this.tableDefinitions = tableDefinitions != null ? Maps.newHashMap(tableDefinitions) : null;
return this;
}
/**
* Adds a new external table definition. If a definition already exists for {@code tableName} it
* is updated.
*
* @param tableName name of the table
* @param tableDefinition external data configuration for the table used by this query
*/
public Builder addTableDefinition(String tableName, ExternalTableDefinition tableDefinition) {
if (this.tableDefinitions == null) {
this.tableDefinitions = Maps.newHashMap();
}
this.tableDefinitions.put(checkNotNull(tableName), checkNotNull(tableDefinition));
return this;
}
/**
* Sets user defined function resources that can be used by this query. Function resources can
* either be defined inline ({@link UserDefinedFunction#inline(String)}) or loaded from a Google
* Cloud Storage URI ({@link UserDefinedFunction#fromUri(String)}.
*/
public Builder setUserDefinedFunctions(List<UserDefinedFunction> userDefinedFunctions) {
this.userDefinedFunctions =
userDefinedFunctions != null ? ImmutableList.copyOf(userDefinedFunctions) : null;
return this;
}
/**
* Sets whether the job is allowed to create tables.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.query.createDisposition">
* Create Disposition</a>
*/
public Builder setCreateDisposition(CreateDisposition createDisposition) {
this.createDisposition = createDisposition;
return this;
}
/**
* Sets the action that should occur if the destination table already exists.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.query.writeDisposition">
* Write Disposition</a>
*/
public Builder setWriteDisposition(WriteDisposition writeDisposition) {
this.writeDisposition = writeDisposition;
return this;
}
/**
* Sets the default dataset. This dataset is used for all unqualified table names used in the
* query.
*/
public Builder setDefaultDataset(DatasetId defaultDataset) {
this.defaultDataset = defaultDataset;
return this;
}
/**
* Sets the default dataset. This dataset is used for all unqualified table names used in the
* query.
*/
public Builder setDefaultDataset(String defaultDataset) {
return setDefaultDataset(DatasetId.of(defaultDataset));
}
/**
* Sets a priority for the query. If not specified the priority is assumed to be {@link
* Priority#INTERACTIVE}.
*/
public Builder setPriority(Priority priority) {
this.priority = priority;
return this;
}
/**
* Sets whether the job is enabled to create arbitrarily large results. If {@code true} the
* query is allowed to create large results at a slight cost in performance. If {@code true}
* {@link Builder#setDestinationTable(TableId)} must be provided.
*
* @see <a href="https://cloud.google.com/bigquery/querying-data#largequeryresults">Returning
* Large Query Results</a>
*/
public Builder setAllowLargeResults(Boolean allowLargeResults) {
this.allowLargeResults = allowLargeResults;
return this;
}
/**
* Sets whether to look for the result in the query cache. The query cache is a best-effort
* cache that will be flushed whenever tables in the query are modified. Moreover, the query
* cache is only available when {@link Builder#setDestinationTable(TableId)} is not set.
*
* @see <a href="https://cloud.google.com/bigquery/querying-data#querycaching">Query Caching</a>
*/
public Builder setUseQueryCache(Boolean useQueryCache) {
this.useQueryCache = useQueryCache;
return this;
}
/**
* Sets whether nested and repeated fields should be flattened. If set to {@code false} {@link
* Builder#setAllowLargeResults(Boolean)} must be {@code true}. By default results are
* flattened.
*
* @see <a href="https://cloud.google.com/bigquery/docs/data#flatten">Flatten</a>
*/
public Builder setFlattenResults(Boolean flattenResults) {
this.flattenResults = flattenResults;
return this;
}
/**
* Sets whether the job has to be dry run or not. If set, the job is not executed. A valid query
* will return a mostly empty response with some processing statistics, while an invalid query
* will return the same error it would if it wasn't a dry run.
*/
public Builder setDryRun(Boolean dryRun) {
this.dryRun = dryRun;
return this;
}
/**
* Sets whether to use BigQuery's legacy SQL dialect for this query. By default this property is
* set to {@code false}. If set to {@code false}, the query will use BigQuery's <a
* href="https://cloud.google.com/bigquery/sql-reference/">Standard SQL</a>. When set to {@code
* false}, the values of {@link #setAllowLargeResults(Boolean)} and {@link
* #setFlattenResults(Boolean)} are ignored; query will be run as if {@link
* #setAllowLargeResults(Boolean)} is {@code true} and {@link #setFlattenResults(Boolean)} is
* {@code false}.
*
* <p>If set to {@code null} or {@code true}, legacy SQL dialect is used. This property is
* experimental and might be subject to change.
*/
public Builder setUseLegacySql(Boolean useLegacySql) {
this.useLegacySql = useLegacySql;
return this;
}
/**
* Limits the billing tier for this job. Queries that have resource usage beyond this tier will
* fail (without incurring a charge). If unspecified, this will be set to your project default.
*
* @param maximumBillingTier maximum billing tier for this job
*/
public Builder setMaximumBillingTier(Integer maximumBillingTier) {
this.maximumBillingTier = maximumBillingTier;
return this;
}
/**
* Limits the bytes billed for this job. Queries that will have bytes billed beyond this limit
* will fail (without incurring a charge). If unspecified, this will be set to your project
* default.
*
* @param maximumBytesBilled maximum bytes billed for this job
*/
public Builder setMaximumBytesBilled(Long maximumBytesBilled) {
this.maximumBytesBilled = maximumBytesBilled;
return this;
}
/**
* [Experimental] Sets options allowing the schema of the destination table to be updated as a
* side effect of the query job. Schema update options are supported in two cases: when
* writeDisposition is WRITE_APPEND; when writeDisposition is WRITE_TRUNCATE and the destination
* table is a partition of a table, specified by partition decorators. For normal tables,
* WRITE_TRUNCATE will always overwrite the schema.
*/
public Builder setSchemaUpdateOptions(List<SchemaUpdateOption> schemaUpdateOptions) {
this.schemaUpdateOptions = schemaUpdateOptions;
return this;
}
/** Sets the time partitioning specification for the destination table. */
public Builder setTimePartitioning(TimePartitioning timePartitioning) {
this.timePartitioning = timePartitioning;
return this;
}
/** Sets the clustering specification for the destination table. */
public Builder setClustering(Clustering clustering) {
this.clustering = clustering;
return this;
}
/**
* [Optional] Job timeout in milliseconds. If this time limit is exceeded, BigQuery may attempt
* to terminate the job.
*
* @param jobTimeoutMs jobTimeoutMs or {@code null} for none
*/
public Builder setJobTimeoutMs(Long jobTimeoutMs) {
this.jobTimeoutMs = jobTimeoutMs;
return this;
}
/**
* The labels associated with this job. You can use these to organize and group your jobs. Label
* keys and values can be no longer than 63 characters, can only contain lowercase letters,
* numeric characters, underscores and dashes. International characters are allowed. Label
* values are optional. Label keys must start with a letter and each label in the list must have
* a different key.
*
* @param labels labels or {@code null} for none
*/
public Builder setLabels(Map<String, String> labels) {
this.labels = labels;
return this;
}
/**
* Range partitioning specification for this table. Only one of timePartitioning and
* rangePartitioning should be specified.
*
* @param rangePartitioning rangePartitioning or {@code null} for none
*/
public Builder setRangePartitioning(RangePartitioning rangePartitioning) {
this.rangePartitioning = rangePartitioning;
return this;
}
/**
* A connection-level property to customize query behavior. Under JDBC, these correspond
* directly to connection properties passed to the DriverManager. Under ODBC, these correspond
* to properties in the connection string. Currently, the only supported connection property is
* "time_zone", whose value represents the default timezone used to run the query. Additional
* properties are allowed, but ignored. Specifying multiple connection properties with the same
* key is an error.
*
* @param connectionProperties connectionProperties or {@code null} for none
*/
public Builder setConnectionProperties(List<ConnectionProperty> connectionProperties) {
this.connectionProperties = ImmutableList.copyOf(connectionProperties);
return this;
}
/**
* This is only supported in the fast query path [Optional] The maximum number of rows of data
* to return per page of results. Setting this flag to a small value such as 1000 and then
* paging through results might improve reliability when the query result set is large. In
* addition to this limit, responses are also limited to 10 MB. By default, there is no maximum
* row count, and only the byte limit applies.
*
* @param maxResults maxResults or {@code null} for none
*/
public Builder setMaxResults(Long maxResults) {
this.maxResults = maxResults;
return this;
}
public QueryJobConfiguration build() {
return new QueryJobConfiguration(this);
}
}
private QueryJobConfiguration(Builder builder) {
super(builder);
this.query = checkNotNull(builder.query);
checkNotNull(builder.positionalParameters);
checkNotNull(builder.namedParameters);
if (!builder.positionalParameters.isEmpty()) {
checkArgument(builder.namedParameters.isEmpty());
}
if (!builder.namedParameters.isEmpty()) {
checkArgument(builder.positionalParameters.isEmpty());
}
positionalParameters = ImmutableList.copyOf(builder.positionalParameters);
namedParameters = ImmutableMap.copyOf(builder.namedParameters);
this.allowLargeResults = builder.allowLargeResults;
this.createDisposition = builder.createDisposition;
this.defaultDataset = builder.defaultDataset;
this.destinationTable = builder.destinationTable;
this.flattenResults = builder.flattenResults;
this.priority = builder.priority;
this.useQueryCache = builder.useQueryCache;
this.userDefinedFunctions = builder.userDefinedFunctions;
this.writeDisposition = builder.writeDisposition;
this.tableDefinitions =
builder.tableDefinitions != null ? ImmutableMap.copyOf(builder.tableDefinitions) : null;
this.dryRun = builder.dryRun;
this.useLegacySql = builder.useLegacySql;
this.maximumBillingTier = builder.maximumBillingTier;
this.maximumBytesBilled = builder.maximumBytesBilled;
this.schemaUpdateOptions = builder.schemaUpdateOptions;
this.destinationEncryptionConfiguration = builder.destinationEncryptionConfiguration;
this.timePartitioning = builder.timePartitioning;
this.clustering = builder.clustering;
this.jobTimeoutMs = builder.jobTimeoutMs;
this.labels = builder.labels;
this.rangePartitioning = builder.rangePartitioning;
this.connectionProperties = builder.connectionProperties;
this.maxResults = builder.maxResults;
}
/**
* Returns whether the job is enabled to create arbitrarily large results. If {@code true} the
* query is allowed to create large results at a slight cost in performance. the query is allowed
* to create large results at a slight cost in performance.
*
* @see <a href="https://cloud.google.com/bigquery/querying-data#largequeryresults">Returning
* Large Query Results</a>
*/
public Boolean allowLargeResults() {
return allowLargeResults;
}
/**
* Returns whether the job is allowed to create new tables.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.query.createDisposition">
* Create Disposition</a>
*/
public CreateDisposition getCreateDisposition() {
return createDisposition;
}
/**
* Returns the default dataset. This dataset is used for all unqualified table names used in the
* query.
*/
public DatasetId getDefaultDataset() {
return defaultDataset;
}
/**
* Returns the table where to put query results. If not provided a new table is created. This
* value is required if {@link #allowLargeResults()} is {@code true}.
*/
public TableId getDestinationTable() {
return destinationTable;
}
public EncryptionConfiguration getDestinationEncryptionConfiguration() {
return destinationEncryptionConfiguration;
}
/**
* Returns whether nested and repeated fields should be flattened. If set to {@code false} {@link
* Builder#setAllowLargeResults(Boolean)} must be {@code true}.
*
* @see <a href="https://cloud.google.com/bigquery/docs/data#flatten">Flatten</a>
*/
public Boolean flattenResults() {
return flattenResults;
}
/** Returns the query priority. */
public Priority getPriority() {
return priority;
}
/** Returns the Google BigQuery SQL query. */
public String getQuery() {
return query;
}
/** Returns the positional query parameters to use for the query. */
public List<QueryParameterValue> getPositionalParameters() {
return positionalParameters;
}
/** Returns the named query parameters to use for the query. */
public Map<String, QueryParameterValue> getNamedParameters() {
return namedParameters;
}
/**
* Returns the external tables definitions. If querying external data sources outside of BigQuery,
* this value describes the data format, location and other properties of the data sources. By
* defining these properties, the data sources can be queried as if they were standard BigQuery
* tables.
*/
public Map<String, ExternalTableDefinition> getTableDefinitions() {
return tableDefinitions;
}
/**
* Returns whether to look for the result in the query cache. The query cache is a best-effort
* cache that will be flushed whenever tables in the query are modified. Moreover, the query cache
* is only available when {@link Builder#setDestinationTable(TableId)} is not set.
*
* @see <a href="https://cloud.google.com/bigquery/querying-data#querycaching">Query Caching</a>
*/
public Boolean useQueryCache() {
return useQueryCache;
}
/**
* Returns user defined function resources that can be used by this query. Function resources can
* either be defined inline ({@link UserDefinedFunction.Type#INLINE}) or loaded from a Google
* Cloud Storage URI ({@link UserDefinedFunction.Type#FROM_URI}.
*/
public List<UserDefinedFunction> getUserDefinedFunctions() {
return userDefinedFunctions;
}
/**
* Returns the action that should occur if the destination table already exists.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.query.writeDisposition">
* Write Disposition</a>
*/
public WriteDisposition getWriteDisposition() {
return writeDisposition;
}
/**
* Returns whether the job has to be dry run or not. If set, the job is not executed. A valid
* query will return a mostly empty response with some processing statistics, while an invalid
* query will return the same error it would if it wasn't a dry run.
*/
public Boolean dryRun() {
return dryRun;
}
/**
* Returns whether to use BigQuery's legacy SQL dialect for this query. By default this property
* is set to {@code false}. If set to {@code false}, the query will use BigQuery's <a
* href="https://cloud.google.com/bigquery/sql-reference/">Standard SQL</a>. When set to {@code
* false}, the values of {@link #allowLargeResults()} and {@link #flattenResults()} are ignored;
* query will be run as if {@link #allowLargeResults()} is {@code true} and {@link
* #flattenResults()} is {@code false}. If set to {@code null} or {@code true}, legacy SQL dialect
* is used. This property is experimental and might be subject to change.
*/
public Boolean useLegacySql() {
return useLegacySql;
}
/** Returns the optional billing tier limit for this job. */
public Integer getMaximumBillingTier() {
return maximumBillingTier;
}
/** Returns the optional bytes billed limit for this job. */
public Long getMaximumBytesBilled() {
return maximumBytesBilled;
}
/**
* [Experimental] Returns options allowing the schema of the destination table to be updated as a
* side effect of the query job. Schema update options are supported in two cases: when
* writeDisposition is WRITE_APPEND; when writeDisposition is WRITE_TRUNCATE and the destination
* table is a partition of a table, specified by partition decorators. For normal tables,
* WRITE_TRUNCATE will always overwrite the schema.
*/
public List<SchemaUpdateOption> getSchemaUpdateOptions() {
return schemaUpdateOptions;
}
/** Returns the time partitioning specification for the destination table. */
public TimePartitioning getTimePartitioning() {
return timePartitioning;
}
/** Returns the clustering specification for the destination table. */
public Clustering getClustering() {
return clustering;
}
/** Returns the timeout associated with this job */
public Long getJobTimeoutMs() {
return jobTimeoutMs;
}
/** Returns the labels associated with this job */
public Map<String, String> getLabels() {
return labels;
}
/** Returns the range partitioning specification for the table */
public RangePartitioning getRangePartitioning() {
return rangePartitioning;
}
/** Returns the connection properties for connection string with this job */
public List<ConnectionProperty> getConnectionProperties() {
return connectionProperties;
}
/**
* This is only supported in the fast query path [Optional] The maximum number of rows of data to
* return per page of results. Setting this flag to a small value such as 1000 and then paging
* through results might improve reliability when the query result set is large. In addition to
* this limit, responses are also limited to 10 MB. By default, there is no maximum row count, and
* only the byte limit applies.
*
* @return value or {@code null} for none
*/
public Long getMaxResults() {
return maxResults;
}
@Override
public Builder toBuilder() {
return new Builder(this);
}
@Override
ToStringHelper toStringHelper() {
return super.toStringHelper()
.add("query", query)
.add("positionalParameters", positionalParameters)
.add("namedParameters", namedParameters)
.add("destinationTable", destinationTable)
.add("destinationEncryptionConfiguration", destinationEncryptionConfiguration)
.add("defaultDataset", defaultDataset)
.add("allowLargeResults", allowLargeResults)
.add("flattenResults", flattenResults)
.add("priority", priority)
.add("tableDefinitions", tableDefinitions)
.add("useQueryCache", useQueryCache)
.add("userDefinedFunctions", userDefinedFunctions)
.add("createDisposition", createDisposition)
.add("writeDisposition", writeDisposition)
.add("dryRun", dryRun)
.add("useLegacySql", useLegacySql)
.add("maximumBillingTier", maximumBillingTier)
.add("maximumBytesBilled", maximumBytesBilled)
.add("schemaUpdateOptions", schemaUpdateOptions)
.add("timePartitioning", timePartitioning)
.add("clustering", clustering)
.add("jobTimeoutMs", jobTimeoutMs)
.add("labels", labels)
.add("rangePartitioning", rangePartitioning)
.add("connectionProperties", connectionProperties);
}
@Override
public boolean equals(Object obj) {
return obj == this
|| obj instanceof QueryJobConfiguration && baseEquals((QueryJobConfiguration) obj);
}
@Override
public int hashCode() {
return Objects.hash(
baseHashCode(),
allowLargeResults,
createDisposition,
destinationTable,
defaultDataset,
flattenResults,
priority,
query,
positionalParameters,
namedParameters,
tableDefinitions,
useQueryCache,
userDefinedFunctions,
writeDisposition,
dryRun,
useLegacySql,
maximumBillingTier,
maximumBytesBilled,
schemaUpdateOptions,
timePartitioning,
clustering,
jobTimeoutMs,
labels,
rangePartitioning,
connectionProperties);
}
@Override
QueryJobConfiguration setProjectId(String projectId) {
Builder builder = toBuilder();
if (getDestinationTable() != null && isNullOrEmpty(getDestinationTable().getProject())) {
builder.setDestinationTable(getDestinationTable().setProjectId(projectId));
}
if (getDefaultDataset() != null) {
builder.setDefaultDataset(getDefaultDataset().setProjectId(projectId));
}
return builder.build();
}
@Override
com.google.api.services.bigquery.model.JobConfiguration toPb() {
com.google.api.services.bigquery.model.JobConfiguration configurationPb =
new com.google.api.services.bigquery.model.JobConfiguration();
JobConfigurationQuery queryConfigurationPb = new JobConfigurationQuery();
queryConfigurationPb.setQuery(query);
if (!positionalParameters.isEmpty()) {
List<QueryParameter> queryParametersPb =
Lists.transform(positionalParameters, POSITIONAL_PARAMETER_TO_PB_FUNCTION);
queryConfigurationPb.setQueryParameters(queryParametersPb);
} else if (!namedParameters.isEmpty()) {
List<QueryParameter> queryParametersPb =
Lists.transform(namedParameters.entrySet().asList(), NAMED_PARAMETER_TO_PB_FUNCTION);
queryConfigurationPb.setQueryParameters(queryParametersPb);
}
configurationPb.setDryRun(dryRun());
if (allowLargeResults != null) {
queryConfigurationPb.setAllowLargeResults(allowLargeResults);
}
if (createDisposition != null) {
queryConfigurationPb.setCreateDisposition(createDisposition.toString());
}
if (destinationTable != null) {
queryConfigurationPb.setDestinationTable(destinationTable.toPb());
}
if (defaultDataset != null) {
queryConfigurationPb.setDefaultDataset(defaultDataset.toPb());
}
if (flattenResults != null) {
queryConfigurationPb.setFlattenResults(flattenResults);
}
if (priority != null) {
queryConfigurationPb.setPriority(priority.toString());
}
if (tableDefinitions != null) {
queryConfigurationPb.setTableDefinitions(
Maps.transformValues(
tableDefinitions, ExternalTableDefinition.TO_EXTERNAL_DATA_FUNCTION));
}
if (useQueryCache != null) {
queryConfigurationPb.setUseQueryCache(useQueryCache);
}
if (userDefinedFunctions != null) {
queryConfigurationPb.setUserDefinedFunctionResources(
Lists.transform(userDefinedFunctions, UserDefinedFunction.TO_PB_FUNCTION));
}
if (writeDisposition != null) {
queryConfigurationPb.setWriteDisposition(writeDisposition.toString());
}
if (useLegacySql != null) {