google-cloud-bigquery 0.23.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -302,9 +302,6 @@ module Google
302
302
  # length is 1,024 characters.
303
303
  # @param [String] name A descriptive name for the table.
304
304
  # @param [String] description A user-friendly description of the table.
305
- # @param [Array<Schema::Field>] fields An array of Schema::Field objects
306
- # specifying the schema's data types for the table. The schema may
307
- # also be configured when passing a block.
308
305
  # @yield [table] a block for setting the table
309
306
  # @yieldparam [Table] table the table object to be updated
310
307
  #
@@ -326,26 +323,6 @@ module Google
326
323
  # name: "My Table",
327
324
  # description: "A description of table."
328
325
  #
329
- # @example The table's schema fields can be passed as an argument.
330
- # require "google/cloud/bigquery"
331
- #
332
- # bigquery = Google::Cloud::Bigquery.new
333
- # dataset = bigquery.dataset "my_dataset"
334
- #
335
- # schema_fields = [
336
- # Google::Cloud::Bigquery::Schema::Field.new(
337
- # "first_name", :string, mode: :required),
338
- # Google::Cloud::Bigquery::Schema::Field.new(
339
- # "cities_lived", :record, mode: :repeated,
340
- # fields: [
341
- # Google::Cloud::Bigquery::Schema::Field.new(
342
- # "place", :string, mode: :required),
343
- # Google::Cloud::Bigquery::Schema::Field.new(
344
- # "number_of_years", :integer, mode: :required),
345
- # ])
346
- # ]
347
- # table = dataset.create_table "my_table", fields: schema_fields
348
- #
349
326
  # @example Or the table's schema can be configured with the block.
350
327
  # require "google/cloud/bigquery"
351
328
  #
@@ -379,7 +356,7 @@ module Google
379
356
  #
380
357
  # @!group Table
381
358
  #
382
- def create_table table_id, name: nil, description: nil, fields: nil
359
+ def create_table table_id, name: nil, description: nil
383
360
  ensure_service!
384
361
  new_tb = Google::Apis::BigqueryV2::Table.new(
385
362
  table_reference: Google::Apis::BigqueryV2::TableReference.new(
@@ -388,7 +365,6 @@ module Google
388
365
  updater = Table::Updater.new(new_tb).tap do |tb|
389
366
  tb.name = name unless name.nil?
390
367
  tb.description = description unless description.nil?
391
- tb.schema.fields = fields unless fields.nil?
392
368
  end
393
369
 
394
370
  yield updater if block_given?
@@ -407,6 +383,14 @@ module Google
407
383
  # is referenced.
408
384
  # @param [String] name A descriptive name for the table.
409
385
  # @param [String] description A user-friendly description of the table.
386
+ # @param [Boolean] standard_sql Specifies whether to use BigQuery's
387
+ # [standard
388
+ # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
389
+ # dialect. Optional. The default value is true.
390
+ # @param [Boolean] legacy_sql Specifies whether to use BigQuery's
391
+ # [legacy
392
+ # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
393
+ # dialect. Optional. The default value is false.
410
394
  #
411
395
  # @return [Google::Cloud::Bigquery::View]
412
396
  #
@@ -416,7 +400,7 @@ module Google
416
400
  # bigquery = Google::Cloud::Bigquery.new
417
401
  # dataset = bigquery.dataset "my_dataset"
418
402
  # view = dataset.create_view "my_view",
419
- # "SELECT name, age FROM [proj:dataset.users]"
403
+ # "SELECT name, age FROM proj.dataset.users"
420
404
  #
421
405
  # @example A name and description can be provided:
422
406
  # require "google/cloud/bigquery"
@@ -424,12 +408,13 @@ module Google
424
408
  # bigquery = Google::Cloud::Bigquery.new
425
409
  # dataset = bigquery.dataset "my_dataset"
426
410
  # view = dataset.create_view "my_view",
427
- # "SELECT name, age FROM [proj:dataset.users]",
411
+ # "SELECT name, age FROM proj.dataset.users",
428
412
  # name: "My View", description: "This is my view"
429
413
  #
430
414
  # @!group Table
431
415
  #
432
- def create_view table_id, query, name: nil, description: nil
416
+ def create_view table_id, query, name: nil, description: nil,
417
+ standard_sql: nil, legacy_sql: nil
433
418
  new_view_opts = {
434
419
  table_reference: Google::Apis::BigqueryV2::TableReference.new(
435
420
  project_id: project_id, dataset_id: dataset_id, table_id: table_id
@@ -437,7 +422,9 @@ module Google
437
422
  friendly_name: name,
438
423
  description: description,
439
424
  view: Google::Apis::BigqueryV2::ViewDefinition.new(
440
- query: query
425
+ query: query,
426
+ use_legacy_sql: Convert.resolve_legacy_sql(standard_sql,
427
+ legacy_sql)
441
428
  )
442
429
  }.delete_if { |_, v| v.nil? }
443
430
  new_view = Google::Apis::BigqueryV2::Table.new new_view_opts
@@ -579,31 +566,28 @@ module Google
579
566
  # * `append` - BigQuery appends the data to the table.
580
567
  # * `empty` - A 'duplicate' error is returned in the job result if the
581
568
  # table exists and contains data.
582
- # @param [Boolean] large_results If `true`, allows the query to produce
583
- # arbitrarily large result tables at a slight cost in performance.
584
- # Requires `table` parameter to be set.
585
- # @param [Boolean] flatten Flattens all nested and repeated fields in
586
- # the query results. The default value is `true`. `large_results`
587
- # parameter must be `true` if this is set to `false`.
588
- # @param [Boolean] legacy_sql Specifies whether to use BigQuery's
589
- # [legacy
590
- # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
591
- # dialect for this query. If set to false, the query will use
592
- # BigQuery's [standard
593
- # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
594
- # When set to false, the values of `large_results` and `flatten` are
595
- # ignored; the query will be run as if `large_results` is true and
596
- # `flatten` is false. Optional. The default value is true.
597
569
  # @param [Boolean] standard_sql Specifies whether to use BigQuery's
598
570
  # [standard
599
571
  # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
600
572
  # dialect for this query. If set to true, the query will use standard
601
573
  # SQL rather than the [legacy
602
574
  # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
603
- # dialect. When set to true, the values of `large_results` and
604
- # `flatten` are ignored; the query will be run as if `large_results`
605
- # is true and `flatten` is false. Optional. The default value is
606
- # false.
575
+ # dialect. Optional. The default value is true.
576
+ # @param [Boolean] legacy_sql Specifies whether to use BigQuery's
577
+ # [legacy
578
+ # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
579
+ # dialect for this query. If set to false, the query will use
580
+ # BigQuery's [standard
581
+ # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
582
+ # dialect. Optional. The default value is false.
583
+ # @param [Boolean] large_results This option is specific to Legacy SQL.
584
+ # If `true`, allows the query to produce arbitrarily large result
585
+ # tables at a slight cost in performance. Requires `table` parameter
586
+ # to be set.
587
+ # @param [Boolean] flatten This option is specific to Legacy SQL.
588
+ # Flattens all nested and repeated fields in the query results. The
589
+ # default value is `true`. `large_results` parameter must be `true` if
590
+ # this is set to `false`.
607
591
  #
608
592
  # @return [Google::Cloud::Bigquery::QueryJob]
609
593
  #
@@ -621,13 +605,13 @@ module Google
621
605
  # end
622
606
  # end
623
607
  #
624
- # @example Query using standard SQL:
608
+ # @example Query using legacy SQL:
625
609
  # require "google/cloud/bigquery"
626
610
  #
627
611
  # bigquery = Google::Cloud::Bigquery.new
628
612
  #
629
613
  # job = bigquery.query_job "SELECT name FROM my_table",
630
- # standard_sql: true
614
+ # legacy_sql: true
631
615
  #
632
616
  # job.wait_until_done!
633
617
  # if !job.failed?
@@ -669,8 +653,8 @@ module Google
669
653
  # @!group Data
670
654
  #
671
655
  def query_job query, params: nil, priority: "INTERACTIVE", cache: true,
672
- table: nil, create: nil, write: nil, large_results: nil,
673
- flatten: nil, legacy_sql: nil, standard_sql: nil
656
+ table: nil, create: nil, write: nil, standard_sql: nil,
657
+ legacy_sql: nil, large_results: nil, flatten: nil
674
658
  options = { priority: priority, cache: cache, table: table,
675
659
  create: create, write: write,
676
660
  large_results: large_results, flatten: flatten,
@@ -741,15 +725,6 @@ module Google
741
725
  # whenever tables in the query are modified. The default value is
742
726
  # true. For more information, see [query
743
727
  # caching](https://developers.google.com/bigquery/querying-data).
744
- # @param [Boolean] legacy_sql Specifies whether to use BigQuery's
745
- # [legacy
746
- # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
747
- # dialect for this query. If set to false, the query will use
748
- # BigQuery's [standard
749
- # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
750
- # When set to false, the values of `large_results` and `flatten` are
751
- # ignored; the query will be run as if `large_results` is true and
752
- # `flatten` is false. Optional. The default value is true.
753
728
  # @param [Boolean] standard_sql Specifies whether to use BigQuery's
754
729
  # [standard
755
730
  # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
@@ -759,7 +734,16 @@ module Google
759
734
  # dialect. When set to true, the values of `large_results` and
760
735
  # `flatten` are ignored; the query will be run as if `large_results`
761
736
  # is true and `flatten` is false. Optional. The default value is
762
- # false.
737
+ # true.
738
+ # @param [Boolean] legacy_sql Specifies whether to use BigQuery's
739
+ # [legacy
740
+ # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
741
+ # dialect for this query. If set to false, the query will use
742
+ # BigQuery's [standard
743
+ # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
744
+ # When set to false, the values of `large_results` and `flatten` are
745
+ # ignored; the query will be run as if `large_results` is true and
746
+ # `flatten` is false. Optional. The default value is false.
763
747
  #
764
748
  # @return [Google::Cloud::Bigquery::QueryData]
765
749
  #
@@ -774,13 +758,13 @@ module Google
774
758
  # puts row["name"]
775
759
  # end
776
760
  #
777
- # @example Query using standard SQL:
761
+ # @example Query using legacy SQL:
778
762
  # require "google/cloud/bigquery"
779
763
  #
780
764
  # bigquery = Google::Cloud::Bigquery.new
781
765
  #
782
766
  # data = bigquery.query "SELECT name FROM my_table",
783
- # standard_sql: true
767
+ # legacy_sql: true
784
768
  #
785
769
  # data.each do |row|
786
770
  # puts row["name"]
@@ -813,7 +797,7 @@ module Google
813
797
  # @!group Data
814
798
  #
815
799
  def query query, params: nil, max: nil, timeout: 10000, dryrun: nil,
816
- cache: true, legacy_sql: nil, standard_sql: nil
800
+ cache: true, standard_sql: nil, legacy_sql: nil
817
801
  options = { max: max, timeout: timeout, dryrun: dryrun, cache: cache,
818
802
  legacy_sql: legacy_sql, standard_sql: standard_sql,
819
803
  params: params }
@@ -158,30 +158,28 @@ module Google
158
158
  # @param [Boolean] large_results If `true`, allows the query to produce
159
159
  # arbitrarily large result tables at a slight cost in performance.
160
160
  # Requires `table` parameter to be set.
161
- # @param [Boolean] flatten Flattens all nested and repeated fields in
162
- # the query results. The default value is `true`. `large_results`
163
- # parameter must be `true` if this is set to `false`.
164
- # @param [Dataset, String] dataset Specifies the default dataset to use
165
- # for unqualified table names in the query.
166
- # @param [Boolean] legacy_sql Specifies whether to use BigQuery's
167
- # [legacy
168
- # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
169
- # dialect for this query. If set to false, the query will use
170
- # BigQuery's [standard
171
- # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
172
- # When set to false, the values of `large_results` and `flatten` are
173
- # ignored; the query will be run as if `large_results` is true and
174
- # `flatten` is false. Optional. The default value is true.
175
161
  # @param [Boolean] standard_sql Specifies whether to use BigQuery's
176
162
  # [standard
177
163
  # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
178
164
  # dialect for this query. If set to true, the query will use standard
179
165
  # SQL rather than the [legacy
180
166
  # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
181
- # dialect. When set to true, the values of `large_results` and
182
- # `flatten` are ignored; the query will be run as if `large_results`
183
- # is true and `flatten` is false. Optional. The default value is
184
- # false.
167
+ # dialect. Optional. The default value is true.
168
+ # @param [Boolean] legacy_sql Specifies whether to use BigQuery's
169
+ # [legacy
170
+ # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
171
+ # dialect for this query. If set to false, the query will use
172
+ # BigQuery's [standard
173
+ # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
174
+ # dialect. Optional. The default value is false.
175
+ # @param [Boolean] large_results This option is specific to Legacy SQL.
176
+ # If `true`, allows the query to produce arbitrarily large result
177
+ # tables at a slight cost in performance. Requires `table` parameter
178
+ # to be set.
179
+ # @param [Boolean] flatten This option is specific to Legacy SQL.
180
+ # Flattens all nested and repeated fields in the query results. The
181
+ # default value is `true`. `large_results` parameter must be `true` if
182
+ # this is set to `false`.
185
183
  #
186
184
  # @return [Google::Cloud::Bigquery::QueryJob]
187
185
  #
@@ -200,14 +198,14 @@ module Google
200
198
  # end
201
199
  # end
202
200
  #
203
- # @example Query using standard SQL:
201
+ # @example Query using legacy SQL:
204
202
  # require "google/cloud/bigquery"
205
203
  #
206
204
  # bigquery = Google::Cloud::Bigquery.new
207
205
  #
208
206
  # job = bigquery.query_job "SELECT name FROM " \
209
207
  # "`my_proj.my_data.my_table`",
210
- # standard_sql: true
208
+ # legacy_sql: true
211
209
  #
212
210
  # job.wait_until_done!
213
211
  # if !job.failed?
@@ -251,9 +249,9 @@ module Google
251
249
  # end
252
250
  #
253
251
  def query_job query, params: nil, priority: "INTERACTIVE", cache: true,
254
- table: nil, create: nil, write: nil, large_results: nil,
255
- flatten: nil, dataset: nil, legacy_sql: nil,
256
- standard_sql: nil
252
+ table: nil, create: nil, write: nil, dataset: nil,
253
+ standard_sql: nil, legacy_sql: nil, large_results: nil,
254
+ flatten: nil
257
255
  ensure_service!
258
256
  options = { priority: priority, cache: cache, table: table,
259
257
  create: create, write: write,
@@ -327,15 +325,6 @@ module Google
327
325
  # @param [String] project Specifies the default projectId to assume for
328
326
  # any unqualified table names in the query. Only used if `dataset`
329
327
  # option is set.
330
- # @param [Boolean] legacy_sql Specifies whether to use BigQuery's
331
- # [legacy
332
- # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
333
- # dialect for this query. If set to false, the query will use
334
- # BigQuery's [standard
335
- # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
336
- # When set to false, the values of `large_results` and `flatten` are
337
- # ignored; the query will be run as if `large_results` is true and
338
- # `flatten` is false. Optional. The default value is true.
339
328
  # @param [Boolean] standard_sql Specifies whether to use BigQuery's
340
329
  # [standard
341
330
  # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
@@ -345,7 +334,16 @@ module Google
345
334
  # dialect. When set to true, the values of `large_results` and
346
335
  # `flatten` are ignored; the query will be run as if `large_results`
347
336
  # is true and `flatten` is false. Optional. The default value is
348
- # false.
337
+ # true.
338
+ # @param [Boolean] legacy_sql Specifies whether to use BigQuery's
339
+ # [legacy
340
+ # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
341
+ # dialect for this query. If set to false, the query will use
342
+ # BigQuery's [standard
343
+ # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
344
+ # When set to false, the values of `large_results` and `flatten` are
345
+ # ignored; the query will be run as if `large_results` is true and
346
+ # `flatten` is false. Optional. The default value is false.
349
347
  #
350
348
  # @return [Google::Cloud::Bigquery::QueryData]
351
349
  #
@@ -354,19 +352,19 @@ module Google
354
352
  #
355
353
  # bigquery = Google::Cloud::Bigquery.new
356
354
  #
357
- # data = bigquery.query "SELECT name FROM [my_proj:my_data.my_table]"
355
+ # data = bigquery.query "SELECT name FROM `my_proj.my_data.my_table`"
358
356
  #
359
357
  # data.each do |row|
360
358
  # puts row["name"]
361
359
  # end
362
360
  #
363
- # @example Query using standard SQL:
361
+ # @example Query using legacy SQL:
364
362
  # require "google/cloud/bigquery"
365
363
  #
366
364
  # bigquery = Google::Cloud::Bigquery.new
367
365
  #
368
- # data = bigquery.query "SELECT name FROM `my_proj.my_data.my_table`",
369
- # standard_sql: true
366
+ # data = bigquery.query "SELECT name FROM [my_proj:my_data.my_table]",
367
+ # legacy_sql: true
370
368
  #
371
369
  # data.each do |row|
372
370
  # puts row["name"]
@@ -377,7 +375,7 @@ module Google
377
375
  #
378
376
  # bigquery = Google::Cloud::Bigquery.new
379
377
  #
380
- # data = bigquery.query "SELECT name FROM [my_proj:my_data.my_table]"
378
+ # data = bigquery.query "SELECT name FROM `my_proj.my_data.my_table`"
381
379
  #
382
380
  # data.all do |row|
383
381
  # puts row["name"]
@@ -389,7 +387,7 @@ module Google
389
387
  # bigquery = Google::Cloud::Bigquery.new
390
388
  #
391
389
  # data = bigquery.query "SELECT name " \
392
- # "FROM [my_proj:my_data.my_table]" \
390
+ # "FROM `my_proj.my_data.my_table`" \
393
391
  # "WHERE id = ?",
394
392
  # params: [1]
395
393
  #
@@ -403,7 +401,7 @@ module Google
403
401
  # bigquery = Google::Cloud::Bigquery.new
404
402
  #
405
403
  # data = bigquery.query "SELECT name " \
406
- # "FROM [my_proj:my_data.my_table]" \
404
+ # "FROM `my_proj.my_data.my_table`" \
407
405
  # "WHERE id = @id",
408
406
  # params: { id: 1 }
409
407
  #
@@ -412,8 +410,8 @@ module Google
412
410
  # end
413
411
  #
414
412
  def query query, params: nil, max: nil, timeout: 10000, dryrun: nil,
415
- cache: true, dataset: nil, project: nil, legacy_sql: nil,
416
- standard_sql: nil
413
+ cache: true, dataset: nil, project: nil, standard_sql: nil,
414
+ legacy_sql: nil
417
415
  ensure_service!
418
416
  options = { max: max, timeout: timeout, dryrun: dryrun, cache: cache,
419
417
  dataset: dataset, project: project,
@@ -710,7 +708,7 @@ module Google
710
708
  #
711
709
  # fourpm = bigquery.time 16, 0, 0
712
710
  # data = bigquery.query "SELECT name " \
713
- # "FROM [my_proj:my_data.my_table]" \
711
+ # "FROM `my_proj.my_data.my_table`" \
714
712
  # "WHERE time_of_date = @time",
715
713
  # params: { time: fourpm }
716
714
  #
@@ -725,7 +723,7 @@ module Google
725
723
  #
726
724
  # precise_time = bigquery.time 16, 35, 15.376541
727
725
  # data = bigquery.query "SELECT name " \
728
- # "FROM [my_proj:my_data.my_table]" \
726
+ # "FROM `my_proj.my_data.my_table`" \
729
727
  # "WHERE time_of_date >= @time",
730
728
  # params: { time: precise_time }
731
729
  #
@@ -55,22 +55,24 @@ module Google
55
55
  ##
56
56
  # The schema of the data.
57
57
  def schema
58
- Schema.from_gapi(@gapi.schema).freeze
58
+ @schema ||= begin
59
+ s = Schema.from_gapi(@gapi.schema)
60
+ # call fields so they will be available
61
+ s.fields
62
+ s.freeze
63
+ end
59
64
  end
60
65
 
61
66
  ##
62
67
  # The fields of the data.
63
68
  def fields
64
- f = schema.fields
65
- f = f.to_hash if f.respond_to? :to_hash
66
- f = [] if f.nil?
67
- f
69
+ schema.fields
68
70
  end
69
71
 
70
72
  ##
71
73
  # The name of the columns in the data.
72
74
  def headers
73
- fields.map(&:name)
75
+ schema.headers
74
76
  end
75
77
 
76
78
  ##
@@ -202,8 +204,7 @@ module Google
202
204
  if gapi.schema.nil?
203
205
  formatted_rows = []
204
206
  else
205
- formatted_rows = format_rows gapi.rows,
206
- gapi.schema.fields
207
+ formatted_rows = Convert.format_rows gapi.rows, gapi.schema.fields
207
208
  end
208
209
 
209
210
  data = new formatted_rows