forked from googleapis/google-cloud-ruby
-
Notifications
You must be signed in to change notification settings - Fork 2
/
query_job.rb
1356 lines (1274 loc) · 52 KB
/
query_job.rb
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 2015 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
#
# https://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.
require "google/cloud/bigquery/service"
require "google/cloud/bigquery/data"
require "google/cloud/bigquery/encryption_configuration"
require "google/apis/bigquery_v2"
module Google
module Cloud
module Bigquery
##
# # QueryJob
#
# A {Job} subclass representing a query operation that may be performed
# on a {Table}. A QueryJob instance is created when you call
# {Project#query_job}, {Dataset#query_job}.
#
# @see https://cloud.google.com/bigquery/querying-data Querying Data
# @see https://cloud.google.com/bigquery/docs/reference/v2/jobs Jobs API
# reference
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
#
# job = bigquery.query_job "SELECT COUNT(word) as count FROM " \
# "`bigquery-public-data.samples.shakespeare`"
#
# job.wait_until_done!
#
# if job.failed?
# puts job.error
# else
# puts job.data.first
# end
#
class QueryJob < Job
##
# Checks if the priority for the query is `BATCH`.
#
# @return [Boolean] `true` when the priority is `BATCH`, `false`
# otherwise.
#
def batch?
val = @gapi.configuration.query.priority
val == "BATCH"
end
##
# Checks if the priority for the query is `INTERACTIVE`.
#
# @return [Boolean] `true` when the priority is `INTERACTIVE`, `false`
# otherwise.
#
def interactive?
val = @gapi.configuration.query.priority
return true if val.nil?
val == "INTERACTIVE"
end
##
# Checks if the the query job allows arbitrarily large results at a
# slight cost to performance.
#
# @return [Boolean] `true` when large results are allowed, `false`
# otherwise.
#
def large_results?
val = @gapi.configuration.query.allow_large_results
return false if val.nil?
val
end
##
# Checks if the query job looks for an existing result in the query
# cache. For more information, see [Query
# Caching](https://cloud.google.com/bigquery/querying-data#querycaching).
#
# @return [Boolean] `true` when the query cache will be used, `false`
# otherwise.
#
def cache?
val = @gapi.configuration.query.use_query_cache
return false if val.nil?
val
end
##
# If set, don't actually run this job. 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.
#
# @return [Boolean] `true` when the dry run flag is set for the query
# job, `false` otherwise.
#
def dryrun?
@gapi.configuration.dry_run
end
alias dryrun dryrun?
alias dry_run dryrun?
alias dry_run? dryrun?
##
# Checks if the query job flattens nested and repeated fields in the
# query results. The default is `true`. If the value is `false`,
# #large_results? should return `true`.
#
# @return [Boolean] `true` when the job flattens results, `false`
# otherwise.
#
def flatten?
val = @gapi.configuration.query.flatten_results
return true if val.nil?
val
end
##
# Limits the billing tier for this job. Queries that have resource usage
# beyond this tier will raise (without incurring a charge). If
# unspecified, this will be set to your project default. For more
# information, see [High-Compute
# queries](https://cloud.google.com/bigquery/pricing#high-compute).
#
# @return [Integer, nil] The tier number, or `nil` for the project
# default.
#
def maximum_billing_tier
@gapi.configuration.query.maximum_billing_tier
end
##
# Limits the bytes billed for this job. Queries that will have bytes
# billed beyond this limit will raise (without incurring a charge). If
# `nil`, this will be set to your project default.
#
# @return [Integer, nil] The number of bytes, or `nil` for the project
# default.
#
def maximum_bytes_billed
Integer @gapi.configuration.query.maximum_bytes_billed
rescue StandardError
nil
end
##
# Checks if the query results are from the query cache.
#
# @return [Boolean] `true` when the job statistics indicate a cache hit,
# `false` otherwise.
#
def cache_hit?
return false unless @gapi.statistics.query
@gapi.statistics.query.cache_hit
end
##
# The number of bytes processed by the query.
#
# @return [Integer, nil] Total bytes processed for the job.
#
def bytes_processed
Integer @gapi.statistics.query.total_bytes_processed
rescue StandardError
nil
end
##
# Describes the execution plan for the query.
#
# @return [Array<Google::Cloud::Bigquery::QueryJob::Stage>, nil] An
# array containing the stages of the execution plan.
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
#
# sql = "SELECT word FROM `bigquery-public-data.samples.shakespeare`"
# job = bigquery.query_job sql
#
# job.wait_until_done!
#
# stages = job.query_plan
# stages.each do |stage|
# puts stage.name
# stage.steps.each do |step|
# puts step.kind
# puts step.substeps.inspect
# end
# end
#
def query_plan
return nil unless @gapi.statistics.query &&
@gapi.statistics.query.query_plan
Array(@gapi.statistics.query.query_plan).map do |stage|
Stage.from_gapi stage
end
end
##
# The type of query statement, if valid. Possible values (new values
# might be added in the future):
#
# * "CREATE_MODEL": DDL statement, see [Using Data Definition Language
# Statements](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language)
# * "CREATE_TABLE": DDL statement, see [Using Data Definition Language
# Statements](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language)
# * "CREATE_TABLE_AS_SELECT": DDL statement, see [Using Data Definition
# Language Statements](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language)
# * "CREATE_VIEW": DDL statement, see [Using Data Definition Language
# Statements](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language)
# * "DELETE": DML statement, see [Data Manipulation Language Syntax](https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax)
# * "DROP_MODEL": DDL statement, see [Using Data Definition Language
# Statements](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language)
# * "DROP_TABLE": DDL statement, see [Using Data Definition Language
# Statements](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language)
# * "DROP_VIEW": DDL statement, see [Using Data Definition Language
# Statements](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language)
# * "INSERT": DML statement, see [Data Manipulation Language Syntax](https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax)
# * "MERGE": DML statement, see [Data Manipulation Language Syntax](https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax)
# * "SELECT": SQL query, see [Standard SQL Query Syntax](https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax)
# * "UPDATE": DML statement, see [Data Manipulation Language Syntax](https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax)
#
# @return [String, nil] The type of query statement.
#
def statement_type
return nil unless @gapi.statistics.query
@gapi.statistics.query.statement_type
end
##
# Whether the query is a DDL statement.
#
# @see https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language
# Using Data Definition Language Statements
#
# @return [Boolean]
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# query_job = bigquery.query_job "CREATE TABLE my_table (x INT64)"
#
# query_job.statement_type #=> "CREATE_TABLE"
# query_job.ddl? #=> true
#
def ddl?
%w[CREATE_MODEL CREATE_TABLE CREATE_TABLE_AS_SELECT CREATE_VIEW \
DROP_MODEL DROP_TABLE DROP_VIEW].include? statement_type
end
##
# Whether the query is a DML statement.
#
# @see https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax
# Data Manipulation Language Syntax
#
# @return [Boolean]
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# query_job = bigquery.query_job "UPDATE my_table " \
# "SET x = x + 1 " \
# "WHERE x IS NOT NULL"
#
# query_job.statement_type #=> "UPDATE"
# query_job.dml? #=> true
#
def dml?
%w[INSERT UPDATE MERGE DELETE].include? statement_type
end
##
# The DDL operation performed, possibly dependent on the pre-existence
# of the DDL target. (See {#ddl_target_table}.) Possible values (new
# values might be added in the future):
#
# * "CREATE": The query created the DDL target.
# * "SKIP": No-op. Example cases: the query is
# `CREATE TABLE IF NOT EXISTS` while the table already exists, or the
# query is `DROP TABLE IF EXISTS` while the table does not exist.
# * "REPLACE": The query replaced the DDL target. Example case: the
# query is `CREATE OR REPLACE TABLE`, and the table already exists.
# * "DROP": The query deleted the DDL target.
#
# @return [String, nil] The DDL operation performed.
#
def ddl_operation_performed
return nil unless @gapi.statistics.query
@gapi.statistics.query.ddl_operation_performed
end
##
# The DDL target table, in reference state. (See {Table#reference?}.)
# Present only for `CREATE/DROP TABLE/VIEW` queries. (See
# {#statement_type}.)
#
# @return [Google::Cloud::Bigquery::Table, nil] The DDL target table, in
# reference state.
#
def ddl_target_table
return nil unless @gapi.statistics.query
ensure_service!
table = @gapi.statistics.query.ddl_target_table
return nil unless table
Google::Cloud::Bigquery::Table.new_reference_from_gapi table, service
end
##
# The number of rows affected by a DML statement. Present only for DML
# statements `INSERT`, `UPDATE` or `DELETE`. (See {#statement_type}.)
#
# @return [Integer, nil] The number of rows affected by a DML statement,
# or `nil` if the query is not a DML statement.
#
def num_dml_affected_rows
return nil unless @gapi.statistics.query
@gapi.statistics.query.num_dml_affected_rows
end
##
# The table in which the query results are stored.
#
# @return [Table] A table instance.
#
def destination
table = @gapi.configuration.query.destination_table
return nil unless table
retrieve_table table.project_id,
table.dataset_id,
table.table_id
end
##
# Checks if the query job is using legacy sql.
#
# @return [Boolean] `true` when legacy sql is used, `false` otherwise.
#
def legacy_sql?
val = @gapi.configuration.query.use_legacy_sql
return true if val.nil?
val
end
##
# Checks if the query job is using standard sql.
#
# @return [Boolean] `true` when standard sql is used, `false` otherwise.
#
def standard_sql?
!legacy_sql?
end
##
# The user-defined function resources used in the query. May be either a
# code resource to load from a Google Cloud Storage URI
# (`gs://bucket/path`), or an inline resource that contains code for a
# user-defined function (UDF). Providing an inline code resource is
# equivalent to providing a URI for a file containing the same code. See
# [User-Defined Functions](https://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions).
#
# @return [Array<String>] An array containing Google Cloud Storage URIs
# and/or inline source code.
#
def udfs
udfs_gapi = @gapi.configuration.query.user_defined_function_resources
return nil unless udfs_gapi
Array(udfs_gapi).map do |udf|
udf.inline_code || udf.resource_uri
end
end
##
# The encryption configuration of the destination table.
#
# @return [Google::Cloud::BigQuery::EncryptionConfiguration] Custom
# encryption configuration (e.g., Cloud KMS keys).
#
# @!group Attributes
def encryption
EncryptionConfiguration.from_gapi(
@gapi.configuration.query.destination_encryption_configuration
)
end
###
# Checks if the destination table will be time-partitioned. See
# [Partitioned Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
#
# @return [Boolean, nil] `true` when the table will be time-partitioned,
# or `false` otherwise.
#
# @!group Attributes
#
def time_partitioning?
!@gapi.configuration.query.time_partitioning.nil?
end
###
# The period for which the destination table will be partitioned, if
# any. See [Partitioned Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
#
# @return [String, nil] The partition type. Currently the only supported
# value is "DAY", or `nil` if not present.
#
# @!group Attributes
#
def time_partitioning_type
@gapi.configuration.query.time_partitioning.type if time_partitioning?
end
###
# The field on which the destination table will be partitioned, if any.
# If not set, the destination table will be partitioned by pseudo column
# `_PARTITIONTIME`; if set, the table will be partitioned by this field.
# See [Partitioned Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
#
# @return [String, nil] The partition field, if a field was configured.
# `nil` if not partitioned or not set (partitioned by pseudo column
# '_PARTITIONTIME').
#
# @!group Attributes
#
def time_partitioning_field
return nil unless time_partitioning?
@gapi.configuration.query.time_partitioning.field
end
###
# The expiration for the destination table partitions, if any, in
# seconds. See [Partitioned
# Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
#
# @return [Integer, nil] The expiration time, in seconds, for data in
# partitions, or `nil` if not present.
#
# @!group Attributes
#
def time_partitioning_expiration
tp = @gapi.configuration.query.time_partitioning
tp.expiration_ms / 1_000 if tp && !tp.expiration_ms.nil?
end
###
# If set to true, queries over the destination table will require a
# partition filter that can be used for partition elimination to be
# specified. See [Partitioned
# Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
#
# @return [Boolean] `true` when a partition filter will be required,
# or `false` otherwise.
#
# @!group Attributes
#
def time_partitioning_require_filter?
tp = @gapi.configuration.query.time_partitioning
return false if tp.nil? || tp.require_partition_filter.nil?
tp.require_partition_filter
end
###
# Checks if the destination table will be clustered.
#
# @see https://cloud.google.com/bigquery/docs/clustered-tables
# Introduction to Clustered Tables
#
# @return [Boolean, nil] `true` when the table will be clustered,
# or `false` otherwise.
#
# @!group Attributes
#
def clustering?
!@gapi.configuration.query.clustering.nil?
end
###
# One or more fields on which the destination table should be clustered.
# Must be specified with time-based partitioning, data in the table will
# be first partitioned and subsequently clustered. The order of the
# returned fields determines the sort order of the data.
#
# See {QueryJob::Updater#clustering_fields=}.
#
# @see https://cloud.google.com/bigquery/docs/partitioned-tables
# Partitioned Tables
# @see https://cloud.google.com/bigquery/docs/clustered-tables
# Introduction to Clustered Tables
# @see https://cloud.google.com/bigquery/docs/creating-clustered-tables
# Creating and Using Clustered Tables
#
# @return [Array<String>, nil] The clustering fields, or `nil` if the
# destination table will not be clustered.
#
# @!group Attributes
#
def clustering_fields
@gapi.configuration.query.clustering.fields if clustering?
end
##
# Refreshes the job until the job is `DONE`.
# The delay between refreshes will incrementally increase.
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
#
# sql = "SELECT word FROM `bigquery-public-data.samples.shakespeare`"
# job = bigquery.query_job sql
#
# job.wait_until_done!
# job.done? #=> true
#
def wait_until_done!
return if done?
ensure_service!
loop do
query_results_gapi = service.job_query_results \
job_id, location: location, max: 0
if query_results_gapi.job_complete
@destination_schema_gapi = query_results_gapi.schema
break
end
end
reload!
end
##
# Retrieves the query results for the job.
#
# @param [String] token Page token, returned by a previous call,
# identifying the result set.
# @param [Integer] max Maximum number of results to return.
# @param [Integer] start Zero-based index of the starting row to read.
#
# @return [Google::Cloud::Bigquery::Data] An object providing access to
# data read from the destination table for the job.
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
#
# sql = "SELECT word FROM `bigquery-public-data.samples.shakespeare`"
# job = bigquery.query_job sql
#
# job.wait_until_done!
# data = job.data
# data.each do |row|
# puts row[:word]
# end
# data = data.next if data.next?
#
def data token: nil, max: nil, start: nil
return nil unless done?
if dryrun?
return Data.from_gapi_json({ rows: [] }, nil, @gapi, service)
end
if ddl? || dml?
data_hash = { totalRows: nil, rows: [] }
return Data.from_gapi_json data_hash, nil, @gapi, service
end
ensure_schema!
options = { token: token, max: max, start: start }
data_hash = service.list_tabledata \
destination_table_dataset_id,
destination_table_table_id,
options
Data.from_gapi_json data_hash, destination_table_gapi, @gapi, service
end
alias query_results data
##
# Yielded to a block to accumulate changes for a patch request.
class Updater < QueryJob
##
# @private Create an Updater object.
def initialize service, gapi
@service = service
@gapi = gapi
end
# rubocop:disable all
##
# @private Create an Updater from an options hash.
#
# @return [Google::Cloud::Bigquery::QueryJob::Updater] A job
# configuration object for setting query options.
def self.from_options service, query, options
job_ref = service.job_ref_from options[:job_id], options[:prefix]
dataset_config = service.dataset_ref_from options[:dataset],
options[:project]
req = Google::Apis::BigqueryV2::Job.new(
job_reference: job_ref,
configuration: Google::Apis::BigqueryV2::JobConfiguration.new(
query: Google::Apis::BigqueryV2::JobConfigurationQuery.new(
query: query,
default_dataset: dataset_config,
maximum_billing_tier: options[:maximum_billing_tier]
)
)
)
updater = QueryJob::Updater.new service, req
updater.params = options[:params] if options[:params]
updater.create = options[:create]
updater.write = options[:write]
updater.table = options[:table]
updater.dryrun = options[:dryrun]
updater.maximum_bytes_billed = options[:maximum_bytes_billed]
updater.labels = options[:labels] if options[:labels]
updater.legacy_sql = Convert.resolve_legacy_sql(
options[:standard_sql], options[:legacy_sql])
updater.external = options[:external] if options[:external]
updater.priority = options[:priority]
updater.cache = options[:cache]
updater.large_results = options[:large_results]
updater.flatten = options[:flatten]
updater.udfs = options[:udfs]
updater
end
# rubocop:enable all
##
# Sets the geographic location where the job should run. Required
# except for US and EU.
#
# @param [String] value A geographic location, such as "US", "EU" or
# "asia-northeast1". Required except for US and EU.
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
#
# job = bigquery.query_job "SELECT 1;" do |query|
# query.table = dataset.table "my_table", skip_lookup: true
# query.location = "EU"
# end
#
# @!group Attributes
def location= value
@gapi.job_reference.location = value
return unless value.nil?
# Treat assigning value of nil the same as unsetting the value.
unset = @gapi.job_reference.instance_variables.include? :@location
@gapi.job_reference.remove_instance_variable :@location if unset
end
##
# Sets the priority of the query.
#
# @param [String] value Specifies a priority for the query. Possible
# values include `INTERACTIVE` and `BATCH`.
#
# @!group Attributes
def priority= value
@gapi.configuration.query.priority = priority_value value
end
##
# Specifies to look in the query cache for results.
#
# @param [Boolean] value 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. The default value is
# true. For more information, see [query
# caching](https://developers.google.com/bigquery/querying-data).
#
# @!group Attributes
def cache= value
@gapi.configuration.query.use_query_cache = value
end
##
# Allow large results for a legacy SQL query.
#
# @param [Boolean] value This option is specific to Legacy SQL.
# If `true`, allows the query to produce arbitrarily large result
# tables at a slight cost in performance. Requires `table` parameter
# to be set.
#
# @!group Attributes
def large_results= value
@gapi.configuration.query.allow_large_results = value
end
##
# Flatten nested and repeated fields in legacy SQL queries.
#
# @param [Boolean] value This option is specific to Legacy SQL.
# Flattens all nested and repeated fields in the query results. The
# default value is `true`. `large_results` parameter must be `true`
# if this is set to `false`.
#
# @!group Attributes
def flatten= value
@gapi.configuration.query.flatten_results = value
end
##
# Sets the default dataset of tables referenced in the query.
#
# @param [Dataset] value The default dataset to use for unqualified
# table names in the query.
#
# @!group Attributes
def dataset= value
@gapi.configuration.query.default_dataset =
@service.dataset_ref_from value
end
##
# Sets the query parameters. Standard SQL only.
#
# @param [Array, Hash] params Used to pass query arguments when the
# `query` string contains either positional (`?`) or named
# (`@myparam`) query parameters. If value passed is an array
# `["foo"]`, the query must use positional query parameters. If
# value passed is a hash `{ myparam: "foo" }`, the query must use
# named query parameters. When set, `legacy_sql` will automatically
# be set to false and `standard_sql` to true.
#
# @!group Attributes
def params= params
case params
when Array then
@gapi.configuration.query.use_legacy_sql = false
@gapi.configuration.query.parameter_mode = "POSITIONAL"
@gapi.configuration.query.query_parameters = params.map do |param|
Convert.to_query_param param
end
when Hash then
@gapi.configuration.query.use_legacy_sql = false
@gapi.configuration.query.parameter_mode = "NAMED"
@gapi.configuration.query.query_parameters =
params.map do |name, param|
Convert.to_query_param(param).tap do |named_param|
named_param.name = String name
end
end
else
raise "Query parameters must be an Array or a Hash."
end
end
##
# Sets the create disposition for creating the query results table.
#
# @param [String] value Specifies whether the job is allowed to
# create new tables. The default value is `needed`.
#
# The following values are supported:
#
# * `needed` - Create the table if it does not exist.
# * `never` - The table must already exist. A 'notFound' error is
# raised if the table does not exist.
#
# @!group Attributes
def create= value
@gapi.configuration.query.create_disposition =
Convert.create_disposition value
end
##
# Sets the write disposition for when the query results table exists.
#
# @param [String] value Specifies the action that occurs if the
# destination table already exists. The default value is `empty`.
#
# The following values are supported:
#
# * `truncate` - BigQuery overwrites the table data.
# * `append` - BigQuery appends the data to the table.
# * `empty` - A 'duplicate' error is returned in the job result if
# the table exists and contains data.
#
# @!group Attributes
def write= value
@gapi.configuration.query.write_disposition =
Convert.write_disposition value
end
##
# Sets the dry run flag for the query job.
#
# @param [Boolean] value If set, don't actually run this job. 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..
#
# @!group Attributes
def dryrun= value
@gapi.configuration.dry_run = value
end
alias dry_run= dryrun=
##
# Sets the destination for the query results table.
#
# @param [Table] value The destination table where the query results
# should be stored. If not present, a new table will be created
# according to the create disposition to store the results.
#
# @!group Attributes
def table= value
@gapi.configuration.query.destination_table = table_ref_from value
end
##
# Sets the maximum bytes billed for the query.
#
# @param [Integer] value Limits the bytes billed for this job.
# Queries that will have bytes billed beyond this limit will fail
# (without incurring a charge). Optional. If unspecified, this will
# be set to your project default.
#
# @!group Attributes
def maximum_bytes_billed= value
@gapi.configuration.query.maximum_bytes_billed = value
end
##
# Sets the labels to use for the job.
#
# @param [Hash] value A hash of user-provided labels associated with
# the 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.
#
# @!group Attributes
#
def labels= value
@gapi.configuration.update! labels: value
end
##
# Sets the query syntax to legacy SQL.
#
# @param [Boolean] value Specifies whether to use BigQuery's [legacy
# SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
# dialect for this query. If set to false, the query will use
# BigQuery's [standard
# SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
# dialect. Optional. The default value is false.
#
# @!group Attributes
#
def legacy_sql= value
@gapi.configuration.query.use_legacy_sql = value
end
##
# Sets the query syntax to standard SQL.
#
# @param [Boolean] value Specifies whether to use BigQuery's [standard
# SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
# dialect for this query. If set to true, the query will use
# standard SQL rather than the [legacy
# SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
# dialect. Optional. The default value is true.
#
# @!group Attributes
#
def standard_sql= value
@gapi.configuration.query.use_legacy_sql = !value
end
##
# Sets definitions for external tables used in the query.
#
# @param [Hash<String|Symbol, External::DataSource>] value A Hash
# that represents the mapping of the external tables to the table
# names used in the SQL query. The hash keys are the table names,
# and the hash values are the external table objects.
#
# @!group Attributes
#
def external= value
external_table_pairs = value.map do |name, obj|
[String(name), obj.to_gapi]
end
external_table_hash = Hash[external_table_pairs]
@gapi.configuration.query.table_definitions = external_table_hash
end
##
# Sets user defined functions for the query.
#
# @param [Array<String>, String] value User-defined function resources
# used in the query. May be either a code resource to load from a
# Google Cloud Storage URI (`gs://bucket/path`), or an inline
# resource that contains code for a user-defined function (UDF).
# Providing an inline code resource is equivalent to providing a URI
# for a file containing the same code. See [User-Defined
# Functions](https://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions).
#
# @!group Attributes
def udfs= value
@gapi.configuration.query.user_defined_function_resources =
udfs_gapi_from value
end
##
# Sets the encryption configuration of the destination table.
#
# @param [Google::Cloud::BigQuery::EncryptionConfiguration] val
# Custom encryption configuration (e.g., Cloud KMS keys).
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
#
# key_name = "projects/a/locations/b/keyRings/c/cryptoKeys/d"
# encrypt_config = bigquery.encryption kms_key: key_name
# job = bigquery.query_job "SELECT 1;" do |job|
# job.table = dataset.table "my_table", skip_lookup: true
# job.encryption = encrypt_config
# end
#
# @!group Attributes
def encryption= val
@gapi.configuration.query.update!(
destination_encryption_configuration: val.to_gapi
)
end
##
# Sets the partitioning for the destination table. See [Partitioned
# Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
#
# You can only set the partitioning field while creating a table.
# BigQuery does not allow you to change partitioning on an existing
# table.
#
# @param [String] type The partition type. Currently the only
# supported value is "DAY".
#
# @example
# require "google/cloud/bigquery"
#
# bigquery = Google::Cloud::Bigquery.new
# dataset = bigquery.dataset "my_dataset"
# destination_table = dataset.table "my_destination_table",
# skip_lookup: true
#
# job = dataset.query_job "SELECT * FROM UNNEST(" \
# "GENERATE_TIMESTAMP_ARRAY(" \
# "'2018-10-01 00:00:00', " \
# "'2018-10-10 00:00:00', " \
# "INTERVAL 1 DAY)) AS dob" do |job|
# job.table = destination_table
# job.time_partitioning_type = "DAY"
# end
#
# job.wait_until_done!
# job.done? #=> true
#
# @!group Attributes
#
def time_partitioning_type= type
@gapi.configuration.query.time_partitioning ||= \
Google::Apis::BigqueryV2::TimePartitioning.new
@gapi.configuration.query.time_partitioning.update! type: type
end
##
# Sets the field on which to partition the destination table. If not
# set, the destination table is partitioned by pseudo column
# `_PARTITIONTIME`; if set, the table is partitioned by this field.