google-cloud-bigquery 1.21.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +16 -0
  3. data/AUTHENTICATION.md +158 -0
  4. data/CHANGELOG.md +397 -0
  5. data/CODE_OF_CONDUCT.md +40 -0
  6. data/CONTRIBUTING.md +188 -0
  7. data/LICENSE +201 -0
  8. data/LOGGING.md +27 -0
  9. data/OVERVIEW.md +463 -0
  10. data/TROUBLESHOOTING.md +31 -0
  11. data/lib/google-cloud-bigquery.rb +139 -0
  12. data/lib/google/cloud/bigquery.rb +145 -0
  13. data/lib/google/cloud/bigquery/argument.rb +197 -0
  14. data/lib/google/cloud/bigquery/convert.rb +383 -0
  15. data/lib/google/cloud/bigquery/copy_job.rb +316 -0
  16. data/lib/google/cloud/bigquery/credentials.rb +50 -0
  17. data/lib/google/cloud/bigquery/data.rb +526 -0
  18. data/lib/google/cloud/bigquery/dataset.rb +2845 -0
  19. data/lib/google/cloud/bigquery/dataset/access.rb +1021 -0
  20. data/lib/google/cloud/bigquery/dataset/list.rb +162 -0
  21. data/lib/google/cloud/bigquery/encryption_configuration.rb +123 -0
  22. data/lib/google/cloud/bigquery/external.rb +2432 -0
  23. data/lib/google/cloud/bigquery/extract_job.rb +368 -0
  24. data/lib/google/cloud/bigquery/insert_response.rb +180 -0
  25. data/lib/google/cloud/bigquery/job.rb +657 -0
  26. data/lib/google/cloud/bigquery/job/list.rb +162 -0
  27. data/lib/google/cloud/bigquery/load_job.rb +1704 -0
  28. data/lib/google/cloud/bigquery/model.rb +740 -0
  29. data/lib/google/cloud/bigquery/model/list.rb +164 -0
  30. data/lib/google/cloud/bigquery/project.rb +1655 -0
  31. data/lib/google/cloud/bigquery/project/list.rb +161 -0
  32. data/lib/google/cloud/bigquery/query_job.rb +1695 -0
  33. data/lib/google/cloud/bigquery/routine.rb +1108 -0
  34. data/lib/google/cloud/bigquery/routine/list.rb +165 -0
  35. data/lib/google/cloud/bigquery/schema.rb +564 -0
  36. data/lib/google/cloud/bigquery/schema/field.rb +668 -0
  37. data/lib/google/cloud/bigquery/service.rb +589 -0
  38. data/lib/google/cloud/bigquery/standard_sql.rb +495 -0
  39. data/lib/google/cloud/bigquery/table.rb +3340 -0
  40. data/lib/google/cloud/bigquery/table/async_inserter.rb +520 -0
  41. data/lib/google/cloud/bigquery/table/list.rb +172 -0
  42. data/lib/google/cloud/bigquery/time.rb +65 -0
  43. data/lib/google/cloud/bigquery/version.rb +22 -0
  44. metadata +297 -0
@@ -0,0 +1,495 @@
1
+ # Copyright 2019 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ module Google
17
+ module Cloud
18
+ module Bigquery
19
+ ##
20
+ # BigQuery standard SQL is compliant with the SQL 2011 standard and has
21
+ # extensions that support querying nested and repeated data. See {Routine} and {Argument}.
22
+ #
23
+ # @example
24
+ # require "google/cloud/bigquery"
25
+ #
26
+ # bigquery = Google::Cloud::Bigquery.new
27
+ # dataset = bigquery.dataset "my_dataset"
28
+ # routine = dataset.create_routine "my_routine" do |r|
29
+ # r.routine_type = "SCALAR_FUNCTION"
30
+ # r.language = :SQL
31
+ # r.body = "(SELECT SUM(IF(elem.name = \"foo\",elem.val,null)) FROM UNNEST(arr) AS elem)"
32
+ # r.arguments = [
33
+ # Google::Cloud::Bigquery::Argument.new(
34
+ # name: "arr",
35
+ # argument_kind: "FIXED_TYPE",
36
+ # data_type: Google::Cloud::Bigquery::StandardSql::DataType.new(
37
+ # type_kind: "ARRAY",
38
+ # array_element_type: Google::Cloud::Bigquery::StandardSql::DataType.new(
39
+ # type_kind: "STRUCT",
40
+ # struct_type: Google::Cloud::Bigquery::StandardSql::StructType.new(
41
+ # fields: [
42
+ # Google::Cloud::Bigquery::StandardSql::Field.new(
43
+ # name: "name",
44
+ # type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "STRING")
45
+ # ),
46
+ # Google::Cloud::Bigquery::StandardSql::Field.new(
47
+ # name: "val",
48
+ # type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "INT64")
49
+ # )
50
+ # ]
51
+ # )
52
+ # )
53
+ # )
54
+ # )
55
+ # ]
56
+ # end
57
+ #
58
+ module StandardSql
59
+ ##
60
+ # A field or a column. See {Routine} and {Argument}.
61
+ #
62
+ # @example
63
+ # require "google/cloud/bigquery"
64
+ #
65
+ # bigquery = Google::Cloud::Bigquery.new
66
+ # dataset = bigquery.dataset "my_dataset"
67
+ # routine = dataset.create_routine "my_routine" do |r|
68
+ # r.routine_type = "SCALAR_FUNCTION"
69
+ # r.language = :SQL
70
+ # r.body = "(SELECT SUM(IF(elem.name = \"foo\",elem.val,null)) FROM UNNEST(arr) AS elem)"
71
+ # r.arguments = [
72
+ # Google::Cloud::Bigquery::Argument.new(
73
+ # name: "arr",
74
+ # argument_kind: "FIXED_TYPE",
75
+ # data_type: Google::Cloud::Bigquery::StandardSql::DataType.new(
76
+ # type_kind: "ARRAY",
77
+ # array_element_type: Google::Cloud::Bigquery::StandardSql::DataType.new(
78
+ # type_kind: "STRUCT",
79
+ # struct_type: Google::Cloud::Bigquery::StandardSql::StructType.new(
80
+ # fields: [
81
+ # Google::Cloud::Bigquery::StandardSql::Field.new(
82
+ # name: "name",
83
+ # type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "STRING")
84
+ # ),
85
+ # Google::Cloud::Bigquery::StandardSql::Field.new(
86
+ # name: "val",
87
+ # type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "INT64")
88
+ # )
89
+ # ]
90
+ # )
91
+ # )
92
+ # )
93
+ # )
94
+ # ]
95
+ # end
96
+ #
97
+ class Field
98
+ ##
99
+ # Creates a new, immutable StandardSql::Field object.
100
+ #
101
+ # @overload initialize(name, type)
102
+ # @param [String] name The name of the field. Optional. Can be absent for struct fields.
103
+ # @param [StandardSql::DataType, String] type The type of the field. Optional. Absent if not explicitly
104
+ # specified (e.g., `CREATE FUNCTION` statement can omit the return type; in this case the output parameter
105
+ # does not have this "type" field).
106
+ #
107
+ def initialize **kwargs
108
+ # Convert client object kwargs to a gapi object
109
+ kwargs[:type] = DataType.gapi_from_string_or_data_type kwargs[:type] if kwargs[:type]
110
+ @gapi = Google::Apis::BigqueryV2::StandardSqlField.new(**kwargs)
111
+ end
112
+
113
+ ##
114
+ # The name of the field. Optional. Can be absent for struct fields.
115
+ #
116
+ # @return [String, nil]
117
+ #
118
+ def name
119
+ return if @gapi.name == "".freeze
120
+ @gapi.name
121
+ end
122
+
123
+ ##
124
+ # The type of the field. Optional. Absent if not explicitly specified (e.g., `CREATE FUNCTION` statement can
125
+ # omit the return type; in this case the output parameter does not have this "type" field).
126
+ #
127
+ # @return [DataType, nil] The type of the field.
128
+ #
129
+ def type
130
+ DataType.from_gapi @gapi.type if @gapi.type
131
+ end
132
+
133
+ ##
134
+ # @private New Google::Apis::BigqueryV2::StandardSqlField object.
135
+ def to_gapi
136
+ @gapi
137
+ end
138
+
139
+ ##
140
+ # @private New StandardSql::Field from a Google::Apis::BigqueryV2::StandardSqlField object.
141
+ def self.from_gapi gapi
142
+ new.tap do |f|
143
+ f.instance_variable_set :@gapi, gapi
144
+ end
145
+ end
146
+ end
147
+
148
+ ##
149
+ # The type of a variable, e.g., a function argument. See {Routine} and {Argument}.
150
+ #
151
+ # @example
152
+ # require "google/cloud/bigquery"
153
+ #
154
+ # bigquery = Google::Cloud::Bigquery.new
155
+ # dataset = bigquery.dataset "my_dataset"
156
+ # routine = dataset.create_routine "my_routine" do |r|
157
+ # r.routine_type = "SCALAR_FUNCTION"
158
+ # r.language = :SQL
159
+ # r.body = "(SELECT SUM(IF(elem.name = \"foo\",elem.val,null)) FROM UNNEST(arr) AS elem)"
160
+ # r.arguments = [
161
+ # Google::Cloud::Bigquery::Argument.new(
162
+ # name: "arr",
163
+ # argument_kind: "FIXED_TYPE",
164
+ # data_type: Google::Cloud::Bigquery::StandardSql::DataType.new(
165
+ # type_kind: "ARRAY",
166
+ # array_element_type: Google::Cloud::Bigquery::StandardSql::DataType.new(
167
+ # type_kind: "STRUCT",
168
+ # struct_type: Google::Cloud::Bigquery::StandardSql::StructType.new(
169
+ # fields: [
170
+ # Google::Cloud::Bigquery::StandardSql::Field.new(
171
+ # name: "name",
172
+ # type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "STRING")
173
+ # ),
174
+ # Google::Cloud::Bigquery::StandardSql::Field.new(
175
+ # name: "val",
176
+ # type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "INT64")
177
+ # )
178
+ # ]
179
+ # )
180
+ # )
181
+ # )
182
+ # )
183
+ # ]
184
+ # end
185
+ #
186
+ # @see https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types Standard SQL Data Types
187
+ #
188
+ class DataType
189
+ ##
190
+ # Creates a new, immutable StandardSql::DataType object.
191
+ #
192
+ # @overload initialize(type_kind, array_element_type, struct_type)
193
+ # @param [String] type_kind The top level type of this field. Required. Can be [any standard SQL data
194
+ # type](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types) (e.g., `INT64`, `DATE`,
195
+ # `ARRAY`).
196
+ # @param [DataType, String] array_element_type The type of the array's elements, if {#type_kind} is `ARRAY`.
197
+ # See {#array?}. Optional.
198
+ # @param [StructType] struct_type The fields of the struct, in order, if {#type_kind} is `STRUCT`. See
199
+ # {#struct?}. Optional.
200
+ #
201
+ def initialize **kwargs
202
+ # Convert client object kwargs to a gapi object
203
+ if kwargs[:array_element_type]
204
+ kwargs[:array_element_type] = self.class.gapi_from_string_or_data_type kwargs[:array_element_type]
205
+ end
206
+ kwargs[:struct_type] = kwargs[:struct_type].to_gapi if kwargs[:struct_type]
207
+
208
+ @gapi = Google::Apis::BigqueryV2::StandardSqlDataType.new(**kwargs)
209
+ end
210
+
211
+ ##
212
+ # The top level type of this field. Required. Can be any standard SQL data type (e.g., `INT64`, `DATE`,
213
+ # `ARRAY`).
214
+ #
215
+ # @see https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types Standard SQL Data Types
216
+ #
217
+ # @return [String] The upper case type.
218
+ #
219
+ def type_kind
220
+ @gapi.type_kind
221
+ end
222
+
223
+ ##
224
+ # The type of the array's elements, if {#type_kind} is `ARRAY`. See {#array?}. Optional.
225
+ #
226
+ # @return [DataType, nil]
227
+ #
228
+ def array_element_type
229
+ return if @gapi.array_element_type.nil?
230
+ DataType.from_gapi @gapi.array_element_type
231
+ end
232
+
233
+ ##
234
+ # The fields of the struct, in order, if {#type_kind} is `STRUCT`. See {#struct?}. Optional.
235
+ #
236
+ # @return [StructType, nil]
237
+ #
238
+ def struct_type
239
+ return if @gapi.struct_type.nil?
240
+ StructType.from_gapi @gapi.struct_type
241
+ end
242
+
243
+ ##
244
+ # Checks if the {#type_kind} of the field is `INT64`.
245
+ #
246
+ # @return [Boolean] `true` when `INT64`, `false` otherwise.
247
+ #
248
+ # @!group Helpers
249
+ #
250
+ def int?
251
+ type_kind == "INT64".freeze
252
+ end
253
+
254
+ ##
255
+ # Checks if the {#type_kind} of the field is `FLOAT64`.
256
+ #
257
+ # @return [Boolean] `true` when `FLOAT64`, `false` otherwise.
258
+ #
259
+ # @!group Helpers
260
+ #
261
+ def float?
262
+ type_kind == "FLOAT64".freeze
263
+ end
264
+
265
+ ##
266
+ # Checks if the {#type_kind} of the field is `NUMERIC`.
267
+ #
268
+ # @return [Boolean] `true` when `NUMERIC`, `false` otherwise.
269
+ #
270
+ # @!group Helpers
271
+ #
272
+ def numeric?
273
+ type_kind == "NUMERIC".freeze
274
+ end
275
+
276
+ ##
277
+ # Checks if the {#type_kind} of the field is `BOOL`.
278
+ #
279
+ # @return [Boolean] `true` when `BOOL`, `false` otherwise.
280
+ #
281
+ # @!group Helpers
282
+ #
283
+ def boolean?
284
+ type_kind == "BOOL".freeze
285
+ end
286
+
287
+ ##
288
+ # Checks if the {#type_kind} of the field is `STRING`.
289
+ #
290
+ # @return [Boolean] `true` when `STRING`, `false` otherwise.
291
+ #
292
+ # @!group Helpers
293
+ #
294
+ def string?
295
+ type_kind == "STRING".freeze
296
+ end
297
+
298
+ ##
299
+ # Checks if the {#type_kind} of the field is `BYTES`.
300
+ #
301
+ # @return [Boolean] `true` when `BYTES`, `false` otherwise.
302
+ #
303
+ # @!group Helpers
304
+ #
305
+ def bytes?
306
+ type_kind == "BYTES".freeze
307
+ end
308
+
309
+ ##
310
+ # Checks if the {#type_kind} of the field is `DATE`.
311
+ #
312
+ # @return [Boolean] `true` when `DATE`, `false` otherwise.
313
+ #
314
+ # @!group Helpers
315
+ #
316
+ def date?
317
+ type_kind == "DATE".freeze
318
+ end
319
+
320
+ ##
321
+ # Checks if the {#type_kind} of the field is `DATETIME`.
322
+ #
323
+ # @return [Boolean] `true` when `DATETIME`, `false` otherwise.
324
+ #
325
+ # @!group Helpers
326
+ #
327
+ def datetime?
328
+ type_kind == "DATETIME".freeze
329
+ end
330
+
331
+ ##
332
+ # Checks if the {#type_kind} of the field is `GEOGRAPHY`.
333
+ #
334
+ # @return [Boolean] `true` when `GEOGRAPHY`, `false` otherwise.
335
+ #
336
+ # @!group Helpers
337
+ #
338
+ def geography?
339
+ type_kind == "GEOGRAPHY".freeze
340
+ end
341
+
342
+ ##
343
+ # Checks if the {#type_kind} of the field is `TIME`.
344
+ #
345
+ # @return [Boolean] `true` when `TIME`, `false` otherwise.
346
+ #
347
+ # @!group Helpers
348
+ #
349
+ def time?
350
+ type_kind == "TIME".freeze
351
+ end
352
+
353
+ ##
354
+ # Checks if the {#type_kind} of the field is `TIMESTAMP`.
355
+ #
356
+ # @return [Boolean] `true` when `TIMESTAMP`, `false` otherwise.
357
+ #
358
+ # @!group Helpers
359
+ #
360
+ def timestamp?
361
+ type_kind == "TIMESTAMP".freeze
362
+ end
363
+
364
+ ##
365
+ # Checks if the {#type_kind} of the field is `ARRAY`.
366
+ #
367
+ # @return [Boolean] `true` when `ARRAY`, `false` otherwise.
368
+ #
369
+ # @!group Helpers
370
+ #
371
+ def array?
372
+ type_kind == "ARRAY".freeze
373
+ end
374
+
375
+ ##
376
+ # Checks if the {#type_kind} of the field is `STRUCT`.
377
+ #
378
+ # @return [Boolean] `true` when `STRUCT`, `false` otherwise.
379
+ #
380
+ # @!group Helpers
381
+ #
382
+ def struct?
383
+ type_kind == "STRUCT".freeze
384
+ end
385
+
386
+ ##
387
+ # @private New Google::Apis::BigqueryV2::StandardSqlDataType object.
388
+ def to_gapi
389
+ @gapi
390
+ end
391
+
392
+ ##
393
+ # @private New StandardSql::DataType from a Google::Apis::BigqueryV2::StandardSqlDataType object.
394
+ def self.from_gapi gapi
395
+ new.tap do |f|
396
+ f.instance_variable_set :@gapi, gapi
397
+ end
398
+ end
399
+
400
+ ##
401
+ # @private New Google::Apis::BigqueryV2::StandardSqlDataType from a String or StandardSql::DataType object.
402
+ def self.gapi_from_string_or_data_type data_type
403
+ return if data_type.nil?
404
+ if data_type.is_a? StandardSql::DataType
405
+ data_type.to_gapi
406
+ elsif data_type.is_a? Hash
407
+ data_type
408
+ elsif data_type.is_a?(String) || data_type.is_a?(Symbol)
409
+ Google::Apis::BigqueryV2::StandardSqlDataType.new type_kind: data_type.to_s.upcase
410
+ else
411
+ raise ArgumentError, "Unable to convert #{data_type} to Google::Apis::BigqueryV2::StandardSqlDataType"
412
+ end
413
+ end
414
+ end
415
+
416
+ ##
417
+ # The fields of a `STRUCT` type. See {DataType#struct_type}. See {Routine} and {Argument}.
418
+ #
419
+ # @example
420
+ # require "google/cloud/bigquery"
421
+ #
422
+ # bigquery = Google::Cloud::Bigquery.new
423
+ # dataset = bigquery.dataset "my_dataset"
424
+ # routine = dataset.create_routine "my_routine" do |r|
425
+ # r.routine_type = "SCALAR_FUNCTION"
426
+ # r.language = :SQL
427
+ # r.body = "(SELECT SUM(IF(elem.name = \"foo\",elem.val,null)) FROM UNNEST(arr) AS elem)"
428
+ # r.arguments = [
429
+ # Google::Cloud::Bigquery::Argument.new(
430
+ # name: "arr",
431
+ # argument_kind: "FIXED_TYPE",
432
+ # data_type: Google::Cloud::Bigquery::StandardSql::DataType.new(
433
+ # type_kind: "ARRAY",
434
+ # array_element_type: Google::Cloud::Bigquery::StandardSql::DataType.new(
435
+ # type_kind: "STRUCT",
436
+ # struct_type: Google::Cloud::Bigquery::StandardSql::StructType.new(
437
+ # fields: [
438
+ # Google::Cloud::Bigquery::StandardSql::Field.new(
439
+ # name: "name",
440
+ # type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "STRING")
441
+ # ),
442
+ # Google::Cloud::Bigquery::StandardSql::Field.new(
443
+ # name: "val",
444
+ # type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "INT64")
445
+ # )
446
+ # ]
447
+ # )
448
+ # )
449
+ # )
450
+ # )
451
+ # ]
452
+ # end
453
+ #
454
+ class StructType
455
+ ##
456
+ # Creates a new, immutable StandardSql::StructType object.
457
+ #
458
+ # @overload initialize(fields)
459
+ # @param [Array<Field>] fields The fields of the struct. Required.
460
+ #
461
+ def initialize **kwargs
462
+ # Convert each field client object to gapi object, if fields given (self.from_gapi does not pass kwargs)
463
+ kwargs[:fields] = kwargs[:fields]&.map(&:to_gapi) if kwargs[:fields]
464
+ @gapi = Google::Apis::BigqueryV2::StandardSqlStructType.new(**kwargs)
465
+ end
466
+
467
+ ##
468
+ # The fields of the struct.
469
+ #
470
+ # @return [Array<Field>] A frozen array of fields.
471
+ #
472
+ def fields
473
+ Array(@gapi.fields).map do |field_gapi|
474
+ Field.from_gapi field_gapi
475
+ end.freeze
476
+ end
477
+
478
+ ##
479
+ # @private New Google::Apis::BigqueryV2::StandardSqlStructType object.
480
+ def to_gapi
481
+ @gapi
482
+ end
483
+
484
+ ##
485
+ # @private New StandardSql::StructType from a Google::Apis::BigqueryV2::StandardSqlStructType object.
486
+ def self.from_gapi gapi
487
+ new.tap do |f|
488
+ f.instance_variable_set :@gapi, gapi
489
+ end
490
+ end
491
+ end
492
+ end
493
+ end
494
+ end
495
+ end