google-cloud-bigquery 1.37.0 → 1.39.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1244,6 +1244,12 @@ module Google
1244
1244
  # Flattens all nested and repeated fields in the query results. The
1245
1245
  # default value is `true`. `large_results` parameter must be `true` if
1246
1246
  # this is set to `false`.
1247
+ # @param [Integer] maximum_billing_tier Limits the billing tier for this
1248
+ # job. Queries that have resource usage beyond this tier will fail
1249
+ # (without incurring a charge). WARNING: The billed byte amount can be
1250
+ # multiplied by an amount up to this number! Most users should not need
1251
+ # to alter this setting, and we recommend that you avoid introducing new
1252
+ # uses of it. Deprecated.
1247
1253
  # @param [Integer] maximum_bytes_billed Limits the bytes billed for this
1248
1254
  # job. Queries that will have bytes billed beyond this limit will fail
1249
1255
  # (without incurring a charge). Optional. If unspecified, this will be
@@ -1294,8 +1300,13 @@ module Google
1294
1300
  # For additional information on migrating, see: [Migrating to
1295
1301
  # standard SQL - Differences in user-defined JavaScript
1296
1302
  # functions](https://cloud.google.com/bigquery/docs/reference/standard-sql/migrating-from-legacy-sql#differences_in_user-defined_javascript_functions)
1297
- # @param [Integer] maximum_billing_tier Deprecated: Change the billing
1298
- # tier to allow high-compute queries.
1303
+ # @param [Boolean] create_session If true, creates a new session, where the
1304
+ # session ID will be a server generated random id. If false, runs query
1305
+ # with an existing session ID when one is provided in the `session_id`
1306
+ # param, otherwise runs query in non-session mode. See {Job#session_id}.
1307
+ # The default value is false.
1308
+ # @param [String] session_id The ID of an existing session. See also the
1309
+ # `create_session` param and {Job#session_id}.
1299
1310
  # @yield [job] a job configuration object
1300
1311
  # @yieldparam [Google::Cloud::Bigquery::QueryJob::Updater] job a job
1301
1312
  # configuration object for setting additional options for the query.
@@ -1371,8 +1382,7 @@ module Google
1371
1382
  # bigquery = Google::Cloud::Bigquery.new
1372
1383
  # dataset = bigquery.dataset "my_dataset"
1373
1384
  #
1374
- # job = dataset.query_job "SELECT name FROM my_table " \
1375
- # "WHERE id IN UNNEST(@ids)",
1385
+ # job = dataset.query_job "SELECT name FROM my_table WHERE id IN UNNEST(@ids)",
1376
1386
  # params: { ids: [] },
1377
1387
  # types: { ids: [:INT64] }
1378
1388
  #
@@ -1387,8 +1397,9 @@ module Google
1387
1397
  # require "google/cloud/bigquery"
1388
1398
  #
1389
1399
  # bigquery = Google::Cloud::Bigquery.new
1400
+ # dataset = bigquery.dataset "my_dataset"
1390
1401
  #
1391
- # job = bigquery.query_job "CREATE TABLE my_table (x INT64)"
1402
+ # job = dataset.query_job "CREATE TABLE my_table (x INT64)"
1392
1403
  #
1393
1404
  # job.wait_until_done!
1394
1405
  # if !job.failed?
@@ -1399,16 +1410,28 @@ module Google
1399
1410
  # require "google/cloud/bigquery"
1400
1411
  #
1401
1412
  # bigquery = Google::Cloud::Bigquery.new
1413
+ # dataset = bigquery.dataset "my_dataset"
1402
1414
  #
1403
- # job = bigquery.query_job "UPDATE my_table " \
1404
- # "SET x = x + 1 " \
1405
- # "WHERE x IS NOT NULL"
1415
+ # job = dataset.query_job "UPDATE my_table SET x = x + 1 WHERE x IS NOT NULL"
1406
1416
  #
1407
1417
  # job.wait_until_done!
1408
1418
  # if !job.failed?
1409
1419
  # puts job.num_dml_affected_rows
1410
1420
  # end
1411
1421
  #
1422
+ # @example Run query in a session:
1423
+ # require "google/cloud/bigquery"
1424
+ #
1425
+ # bigquery = Google::Cloud::Bigquery.new
1426
+ # dataset = bigquery.dataset "my_dataset"
1427
+ #
1428
+ # job = dataset.query_job "CREATE TEMPORARY TABLE temptable AS SELECT 17 as foo", create_session: true
1429
+ #
1430
+ # job.wait_until_done!
1431
+ #
1432
+ # session_id = job.session_id
1433
+ # data = dataset.query "SELECT * FROM temptable", session_id: session_id
1434
+ #
1412
1435
  # @example Query using external data source, set destination:
1413
1436
  # require "google/cloud/bigquery"
1414
1437
  #
@@ -1435,16 +1458,52 @@ module Google
1435
1458
  #
1436
1459
  # @!group Data
1437
1460
  #
1438
- def query_job query, params: nil, types: nil, external: nil, priority: "INTERACTIVE", cache: true, table: nil,
1439
- create: nil, write: nil, dryrun: nil, standard_sql: nil, legacy_sql: nil, large_results: nil,
1440
- flatten: nil, maximum_billing_tier: nil, maximum_bytes_billed: nil, job_id: nil, prefix: nil,
1441
- labels: nil, udfs: nil
1461
+ def query_job query,
1462
+ params: nil,
1463
+ types: nil,
1464
+ external: nil,
1465
+ priority: "INTERACTIVE",
1466
+ cache: true,
1467
+ table: nil,
1468
+ create: nil,
1469
+ write: nil,
1470
+ dryrun: nil,
1471
+ standard_sql: nil,
1472
+ legacy_sql: nil,
1473
+ large_results: nil,
1474
+ flatten: nil,
1475
+ maximum_billing_tier: nil,
1476
+ maximum_bytes_billed: nil,
1477
+ job_id: nil,
1478
+ prefix: nil,
1479
+ labels: nil,
1480
+ udfs: nil,
1481
+ create_session: nil,
1482
+ session_id: nil
1442
1483
  ensure_service!
1443
- options = { params: params, types: types, external: external, priority: priority, cache: cache, table: table,
1444
- create: create, write: write, dryrun: dryrun, standard_sql: standard_sql, legacy_sql: legacy_sql,
1445
- large_results: large_results, flatten: flatten, maximum_billing_tier: maximum_billing_tier,
1446
- maximum_bytes_billed: maximum_bytes_billed, job_id: job_id, prefix: prefix, labels: labels,
1447
- udfs: udfs }
1484
+ options = {
1485
+ params: params,
1486
+ types: types,
1487
+ external: external,
1488
+ priority: priority,
1489
+ cache: cache,
1490
+ table: table,
1491
+ create: create,
1492
+ write: write,
1493
+ dryrun: dryrun,
1494
+ standard_sql: standard_sql,
1495
+ legacy_sql: legacy_sql,
1496
+ large_results: large_results,
1497
+ flatten: flatten,
1498
+ maximum_billing_tier: maximum_billing_tier,
1499
+ maximum_bytes_billed: maximum_bytes_billed,
1500
+ job_id: job_id,
1501
+ prefix: prefix,
1502
+ labels: labels,
1503
+ udfs: udfs,
1504
+ create_session: create_session,
1505
+ session_id: session_id
1506
+ }
1448
1507
 
1449
1508
  updater = QueryJob::Updater.from_options service, query, options
1450
1509
  updater.dataset = self
@@ -1566,6 +1625,8 @@ module Google
1566
1625
  # When set to false, the values of `large_results` and `flatten` are
1567
1626
  # ignored; the query will be run as if `large_results` is true and
1568
1627
  # `flatten` is false. Optional. The default value is false.
1628
+ # @param [String] session_id The ID of an existing session. See the
1629
+ # `create_session` param in {#query_job} and {Job#session_id}.
1569
1630
  # @yield [job] a job configuration object
1570
1631
  # @yieldparam [Google::Cloud::Bigquery::QueryJob::Updater] job a job
1571
1632
  # configuration object for setting additional options for the query.
@@ -1641,8 +1702,7 @@ module Google
1641
1702
  # bigquery = Google::Cloud::Bigquery.new
1642
1703
  # dataset = bigquery.dataset "my_dataset"
1643
1704
  #
1644
- # data = dataset.query "SELECT name FROM my_table " \
1645
- # "WHERE id IN UNNEST(@ids)",
1705
+ # data = dataset.query "SELECT name FROM my_table WHERE id IN UNNEST(@ids)",
1646
1706
  # params: { ids: [] },
1647
1707
  # types: { ids: [:INT64] }
1648
1708
  #
@@ -1657,8 +1717,9 @@ module Google
1657
1717
  # require "google/cloud/bigquery"
1658
1718
  #
1659
1719
  # bigquery = Google::Cloud::Bigquery.new
1720
+ # dataset = bigquery.dataset "my_dataset"
1660
1721
  #
1661
- # data = bigquery.query "CREATE TABLE my_table (x INT64)"
1722
+ # data = dataset.query "CREATE TABLE my_table (x INT64)"
1662
1723
  #
1663
1724
  # table_ref = data.ddl_target_table # Or ddl_target_routine for CREATE/DROP FUNCTION/PROCEDURE
1664
1725
  #
@@ -1666,13 +1727,25 @@ module Google
1666
1727
  # require "google/cloud/bigquery"
1667
1728
  #
1668
1729
  # bigquery = Google::Cloud::Bigquery.new
1730
+ # dataset = bigquery.dataset "my_dataset"
1669
1731
  #
1670
- # data = bigquery.query "UPDATE my_table " \
1671
- # "SET x = x + 1 " \
1672
- # "WHERE x IS NOT NULL"
1732
+ # data = dataset.query "UPDATE my_table SET x = x + 1 WHERE x IS NOT NULL"
1673
1733
  #
1674
1734
  # puts data.num_dml_affected_rows
1675
1735
  #
1736
+ # @example Run query in a session:
1737
+ # require "google/cloud/bigquery"
1738
+ #
1739
+ # bigquery = Google::Cloud::Bigquery.new
1740
+ # dataset = bigquery.dataset "my_dataset"
1741
+ #
1742
+ # job = dataset.query_job "CREATE TEMPORARY TABLE temptable AS SELECT 17 as foo", create_session: true
1743
+ #
1744
+ # job.wait_until_done!
1745
+ #
1746
+ # session_id = job.session_id
1747
+ # data = dataset.query "SELECT * FROM temptable", session_id: session_id
1748
+ #
1676
1749
  # @example Query using external data source, set destination:
1677
1750
  # require "google/cloud/bigquery"
1678
1751
  #
@@ -1699,10 +1772,25 @@ module Google
1699
1772
  #
1700
1773
  # @!group Data
1701
1774
  #
1702
- def query query, params: nil, types: nil, external: nil, max: nil, cache: true,
1703
- standard_sql: nil, legacy_sql: nil, &block
1704
- job = query_job query, params: params, types: types, external: external, cache: cache,
1705
- standard_sql: standard_sql, legacy_sql: legacy_sql, &block
1775
+ def query query,
1776
+ params: nil,
1777
+ types: nil,
1778
+ external: nil,
1779
+ max: nil,
1780
+ cache: true,
1781
+ standard_sql: nil,
1782
+ legacy_sql: nil,
1783
+ session_id: nil,
1784
+ &block
1785
+ job = query_job query,
1786
+ params: params,
1787
+ types: types,
1788
+ external: external,
1789
+ cache: cache,
1790
+ standard_sql: standard_sql,
1791
+ legacy_sql: legacy_sql,
1792
+ session_id: session_id,
1793
+ &block
1706
1794
  job.wait_until_done!
1707
1795
  ensure_job_succeeded! job
1708
1796
 
@@ -2666,7 +2754,7 @@ module Google
2666
2754
  def patch_gapi! *attributes
2667
2755
  return if attributes.empty?
2668
2756
  ensure_service!
2669
- patch_args = Hash[attributes.map { |attr| [attr, @gapi.send(attr)] }]
2757
+ patch_args = attributes.to_h { |attr| [attr, @gapi.send(attr)] }
2670
2758
  patch_gapi = Google::Apis::BigqueryV2::Dataset.new(**patch_args)
2671
2759
  patch_gapi.etag = etag if etag
2672
2760
  @gapi = service.patch_dataset dataset_id, patch_gapi
@@ -221,7 +221,7 @@ module Google
221
221
  # and the counts as values.
222
222
  #
223
223
  def destinations_counts
224
- Hash[destinations.zip destinations_file_counts]
224
+ destinations.zip(destinations_file_counts).to_h
225
225
  end
226
226
 
227
227
  ##
@@ -226,6 +226,16 @@ module Google
226
226
  Array(@gapi.statistics.reservation_usage).map { |g| ReservationUsage.from_gapi g }
227
227
  end
228
228
 
229
+ ##
230
+ # The ID of the session if this job is part of one. See the `create_session` param in {Project#query_job} and
231
+ # {Dataset#query_job}.
232
+ #
233
+ # @return [String, nil] The session ID, or `nil` if not associated with a session.
234
+ #
235
+ def session_id
236
+ @gapi.statistics.session_info&.session_id
237
+ end
238
+
229
239
  ##
230
240
  # The ID of a multi-statement transaction.
231
241
  #
@@ -479,7 +489,7 @@ module Google
479
489
  #
480
490
  def wait_until_done!
481
491
  backoff = lambda do |retries|
482
- delay = [retries**2 + 5, 60].min # Maximum delay is 60
492
+ delay = [(retries**2) + 5, 60].min # Maximum delay is 60
483
493
  sleep delay
484
494
  end
485
495
  retries = 0
@@ -402,6 +402,12 @@ module Google
402
402
  # Flattens all nested and repeated fields in the query results. The
403
403
  # default value is `true`. `large_results` parameter must be `true` if
404
404
  # this is set to `false`.
405
+ # @param [Integer] maximum_billing_tier Limits the billing tier for this
406
+ # job. Queries that have resource usage beyond this tier will fail
407
+ # (without incurring a charge). WARNING: The billed byte amount can be
408
+ # multiplied by an amount up to this number! Most users should not need
409
+ # to alter this setting, and we recommend that you avoid introducing new
410
+ # uses of it. Deprecated.
405
411
  # @param [Integer] maximum_bytes_billed Limits the bytes billed for this
406
412
  # job. Queries that will have bytes billed beyond this limit will fail
407
413
  # (without incurring a charge). Optional. If unspecified, this will be
@@ -455,8 +461,13 @@ module Google
455
461
  # For additional information on migrating, see: [Migrating to
456
462
  # standard SQL - Differences in user-defined JavaScript
457
463
  # functions](https://cloud.google.com/bigquery/docs/reference/standard-sql/migrating-from-legacy-sql#differences_in_user-defined_javascript_functions)
458
- # @param [Integer] maximum_billing_tier Deprecated: Change the billing
459
- # tier to allow high-compute queries.
464
+ # @param [Boolean] create_session If true, creates a new session, where the
465
+ # session ID will be a server generated random id. If false, runs query
466
+ # with an existing session ID when one is provided in the `session_id`
467
+ # param, otherwise runs query in non-session mode. See {Job#session_id}.
468
+ # The default value is false.
469
+ # @param [String] session_id The ID of an existing session. See also the
470
+ # `create_session` param and {Job#session_id}.
460
471
  # @yield [job] a job configuration object
461
472
  # @yieldparam [Google::Cloud::Bigquery::QueryJob::Updater] job a job
462
473
  # configuration object for setting query options.
@@ -468,8 +479,7 @@ module Google
468
479
  #
469
480
  # bigquery = Google::Cloud::Bigquery.new
470
481
  #
471
- # job = bigquery.query_job "SELECT name FROM " \
472
- # "`my_project.my_dataset.my_table`"
482
+ # job = bigquery.query_job "SELECT name FROM `my_project.my_dataset.my_table`"
473
483
  #
474
484
  # job.wait_until_done!
475
485
  # if !job.failed?
@@ -483,8 +493,7 @@ module Google
483
493
  #
484
494
  # bigquery = Google::Cloud::Bigquery.new
485
495
  #
486
- # job = bigquery.query_job "SELECT name FROM " \
487
- # " [my_project:my_dataset.my_table]",
496
+ # job = bigquery.query_job "SELECT name FROM [my_project:my_dataset.my_table]",
488
497
  # legacy_sql: true
489
498
  #
490
499
  # job.wait_until_done!
@@ -499,9 +508,7 @@ module Google
499
508
  #
500
509
  # bigquery = Google::Cloud::Bigquery.new
501
510
  #
502
- # job = bigquery.query_job "SELECT name FROM " \
503
- # "`my_dataset.my_table` " \
504
- # "WHERE id = ?",
511
+ # job = bigquery.query_job "SELECT name FROM `my_dataset.my_table` WHERE id = ?",
505
512
  # params: [1]
506
513
  #
507
514
  # job.wait_until_done!
@@ -516,9 +523,7 @@ module Google
516
523
  #
517
524
  # bigquery = Google::Cloud::Bigquery.new
518
525
  #
519
- # job = bigquery.query_job "SELECT name FROM " \
520
- # "`my_dataset.my_table` " \
521
- # "WHERE id = @id",
526
+ # job = bigquery.query_job "SELECT name FROM `my_dataset.my_table` WHERE id = @id",
522
527
  # params: { id: 1 }
523
528
  #
524
529
  # job.wait_until_done!
@@ -533,9 +538,7 @@ module Google
533
538
  #
534
539
  # bigquery = Google::Cloud::Bigquery.new
535
540
  #
536
- # job = bigquery.query_job "SELECT name FROM " \
537
- # "`my_dataset.my_table` " \
538
- # "WHERE id IN UNNEST(@ids)",
541
+ # job = bigquery.query_job "SELECT name FROM `my_dataset.my_table` WHERE id IN UNNEST(@ids)",
539
542
  # params: { ids: [] },
540
543
  # types: { ids: [:INT64] }
541
544
  #
@@ -551,9 +554,7 @@ module Google
551
554
  #
552
555
  # bigquery = Google::Cloud::Bigquery.new
553
556
  #
554
- # job = bigquery.query_job "CREATE TABLE " \
555
- # "`my_dataset.my_table` " \
556
- # "(x INT64)"
557
+ # job = bigquery.query_job "CREATE TABLE`my_dataset.my_table` (x INT64)"
557
558
  #
558
559
  # job.wait_until_done!
559
560
  # if !job.failed?
@@ -565,10 +566,7 @@ module Google
565
566
  #
566
567
  # bigquery = Google::Cloud::Bigquery.new
567
568
  #
568
- # job = bigquery.query_job "UPDATE " \
569
- # "`my_dataset.my_table` " \
570
- # "SET x = x + 1 " \
571
- # "WHERE x IS NOT NULL"
569
+ # job = bigquery.query_job "UPDATE `my_dataset.my_table` SET x = x + 1 WHERE x IS NOT NULL"
572
570
  #
573
571
  # job.wait_until_done!
574
572
  # if !job.failed?
@@ -599,17 +597,56 @@ module Google
599
597
  # end
600
598
  # end
601
599
  #
602
- def query_job query, params: nil, types: nil, external: nil, priority: "INTERACTIVE", cache: true, table: nil,
603
- create: nil, write: nil, dryrun: nil, dataset: nil, project: nil, standard_sql: nil,
604
- legacy_sql: nil, large_results: nil, flatten: nil, maximum_billing_tier: nil,
605
- maximum_bytes_billed: nil, job_id: nil, prefix: nil, labels: nil, udfs: nil
600
+ def query_job query,
601
+ params: nil,
602
+ types: nil,
603
+ external: nil,
604
+ priority: "INTERACTIVE",
605
+ cache: true,
606
+ table: nil,
607
+ create: nil,
608
+ write: nil,
609
+ dryrun: nil,
610
+ dataset: nil,
611
+ project: nil,
612
+ standard_sql: nil,
613
+ legacy_sql: nil,
614
+ large_results: nil,
615
+ flatten: nil,
616
+ maximum_billing_tier: nil,
617
+ maximum_bytes_billed: nil,
618
+ job_id: nil,
619
+ prefix: nil,
620
+ labels: nil,
621
+ udfs: nil,
622
+ create_session: nil,
623
+ session_id: nil
606
624
  ensure_service!
607
- options = { params: params, types: types, external: external, priority: priority, cache: cache, table: table,
608
- create: create, write: write, dryrun: dryrun, dataset: dataset,
609
- project: (project || self.project), standard_sql: standard_sql, legacy_sql: legacy_sql,
610
- large_results: large_results, flatten: flatten, maximum_billing_tier: maximum_billing_tier,
611
- maximum_bytes_billed: maximum_bytes_billed, job_id: job_id, prefix: prefix, labels: labels,
612
- udfs: udfs }
625
+ options = {
626
+ params: params,
627
+ types: types,
628
+ external: external,
629
+ priority: priority,
630
+ cache: cache,
631
+ table: table,
632
+ create: create,
633
+ write: write,
634
+ dryrun: dryrun,
635
+ dataset: dataset,
636
+ project: (project || self.project),
637
+ standard_sql: standard_sql,
638
+ legacy_sql: legacy_sql,
639
+ large_results: large_results,
640
+ flatten: flatten,
641
+ maximum_billing_tier: maximum_billing_tier,
642
+ maximum_bytes_billed: maximum_bytes_billed,
643
+ job_id: job_id,
644
+ prefix: prefix,
645
+ labels: labels,
646
+ udfs: udfs,
647
+ create_session: create_session,
648
+ session_id: session_id
649
+ }
613
650
 
614
651
  updater = QueryJob::Updater.from_options service, query, options
615
652
 
@@ -730,6 +767,8 @@ module Google
730
767
  # When set to false, the values of `large_results` and `flatten` are
731
768
  # ignored; the query will be run as if `large_results` is true and
732
769
  # `flatten` is false. Optional. The default value is false.
770
+ # @param [String] session_id The ID of an existing session. See the
771
+ # `create_session` param in {#query_job} and {Job#session_id}.
733
772
  # @yield [job] a job configuration object
734
773
  # @yieldparam [Google::Cloud::Bigquery::QueryJob::Updater] job a job
735
774
  # configuration object for setting additional options for the query.
@@ -782,9 +821,7 @@ module Google
782
821
  #
783
822
  # bigquery = Google::Cloud::Bigquery.new
784
823
  #
785
- # data = bigquery.query "SELECT name " \
786
- # "FROM `my_dataset.my_table` " \
787
- # "WHERE id = ?",
824
+ # data = bigquery.query "SELECT name FROM `my_dataset.my_table` WHERE id = ?",
788
825
  # params: [1]
789
826
  #
790
827
  # # Iterate over the first page of results
@@ -799,9 +836,7 @@ module Google
799
836
  #
800
837
  # bigquery = Google::Cloud::Bigquery.new
801
838
  #
802
- # data = bigquery.query "SELECT name " \
803
- # "FROM `my_dataset.my_table` " \
804
- # "WHERE id = @id",
839
+ # data = bigquery.query "SELECT name FROM `my_dataset.my_table` WHERE id = @id",
805
840
  # params: { id: 1 }
806
841
  #
807
842
  # # Iterate over the first page of results
@@ -816,9 +851,7 @@ module Google
816
851
  #
817
852
  # bigquery = Google::Cloud::Bigquery.new
818
853
  #
819
- # data = bigquery.query "SELECT name FROM " \
820
- # "`my_dataset.my_table` " \
821
- # "WHERE id IN UNNEST(@ids)",
854
+ # data = bigquery.query "SELECT name FROM `my_dataset.my_table` WHERE id IN UNNEST(@ids)",
822
855
  # params: { ids: [] },
823
856
  # types: { ids: [:INT64] }
824
857
  #
@@ -843,9 +876,7 @@ module Google
843
876
  #
844
877
  # bigquery = Google::Cloud::Bigquery.new
845
878
  #
846
- # data = bigquery.query "UPDATE `my_dataset.my_table` " \
847
- # "SET x = x + 1 " \
848
- # "WHERE x IS NOT NULL"
879
+ # data = bigquery.query "UPDATE `my_dataset.my_table` SET x = x + 1 WHERE x IS NOT NULL"
849
880
  #
850
881
  # puts data.num_dml_affected_rows
851
882
  #
@@ -873,10 +904,29 @@ module Google
873
904
  # # Retrieve the next page of results
874
905
  # data = data.next if data.next?
875
906
  #
876
- def query query, params: nil, types: nil, external: nil, max: nil, cache: true, dataset: nil, project: nil,
877
- standard_sql: nil, legacy_sql: nil, &block
878
- job = query_job query, params: params, types: types, external: external, cache: cache, dataset: dataset,
879
- project: project, standard_sql: standard_sql, legacy_sql: legacy_sql, &block
907
+ def query query,
908
+ params: nil,
909
+ types: nil,
910
+ external: nil,
911
+ max: nil,
912
+ cache: true,
913
+ dataset: nil,
914
+ project: nil,
915
+ standard_sql: nil,
916
+ legacy_sql: nil,
917
+ session_id: nil,
918
+ &block
919
+ job = query_job query,
920
+ params: params,
921
+ types: types,
922
+ external: external,
923
+ cache: cache,
924
+ dataset: dataset,
925
+ project: project,
926
+ standard_sql: standard_sql,
927
+ legacy_sql: legacy_sql,
928
+ session_id: session_id,
929
+ &block
880
930
  job.wait_until_done!
881
931
 
882
932
  if job.failed?
@@ -1326,9 +1376,7 @@ module Google
1326
1376
  # bigquery = Google::Cloud::Bigquery.new
1327
1377
  #
1328
1378
  # fourpm = bigquery.time 16, 0, 0
1329
- # data = bigquery.query "SELECT name " \
1330
- # "FROM `my_dataset.my_table`" \
1331
- # "WHERE time_of_date = @time",
1379
+ # data = bigquery.query "SELECT name FROM `my_dataset.my_table` WHERE time_of_date = @time",
1332
1380
  # params: { time: fourpm }
1333
1381
  #
1334
1382
  # # Iterate over the first page of results
@@ -1344,9 +1392,7 @@ module Google
1344
1392
  # bigquery = Google::Cloud::Bigquery.new
1345
1393
  #
1346
1394
  # precise_time = bigquery.time 16, 35, 15.376541
1347
- # data = bigquery.query "SELECT name " \
1348
- # "FROM `my_dataset.my_table`" \
1349
- # "WHERE time_of_date >= @time",
1395
+ # data = bigquery.query "SELECT name FROM `my_dataset.my_table` WHERE time_of_date >= @time",
1350
1396
  # params: { time: precise_time }
1351
1397
  #
1352
1398
  # # Iterate over the first page of results
@@ -250,6 +250,8 @@ module Google
250
250
  # The type of query statement, if valid. Possible values (new values
251
251
  # might be added in the future):
252
252
  #
253
+ # * "ALTER_TABLE": DDL statement, see [Using Data Definition Language
254
+ # Statements](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language)
253
255
  # * "CREATE_MODEL": DDL statement, see [Using Data Definition Language
254
256
  # Statements](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language)
255
257
  # * "CREATE_TABLE": DDL statement, see [Using Data Definition Language
@@ -295,8 +297,16 @@ module Google
295
297
  # query_job.ddl? #=> true
296
298
  #
297
299
  def ddl?
298
- ["CREATE_MODEL", "CREATE_TABLE", "CREATE_TABLE_AS_SELECT", "CREATE_VIEW", "\n", "DROP_MODEL", "DROP_TABLE",
299
- "DROP_VIEW"].include? statement_type
300
+ [
301
+ "ALTER_TABLE",
302
+ "CREATE_MODEL",
303
+ "CREATE_TABLE",
304
+ "CREATE_TABLE_AS_SELECT",
305
+ "CREATE_VIEW",
306
+ "DROP_MODEL",
307
+ "DROP_TABLE",
308
+ "DROP_VIEW"
309
+ ].include? statement_type
300
310
  end
301
311
 
302
312
  ##
@@ -319,7 +329,12 @@ module Google
319
329
  # query_job.dml? #=> true
320
330
  #
321
331
  def dml?
322
- ["INSERT", "UPDATE", "MERGE", "DELETE"].include? statement_type
332
+ [
333
+ "INSERT",
334
+ "UPDATE",
335
+ "MERGE",
336
+ "DELETE"
337
+ ].include? statement_type
323
338
  end
324
339
 
325
340
  ##
@@ -775,6 +790,8 @@ module Google
775
790
  updater = QueryJob::Updater.new service, req
776
791
  updater.set_params_and_types options[:params], options[:types] if options[:params]
777
792
  updater.create = options[:create]
793
+ updater.create_session = options[:create_session]
794
+ updater.session_id = options[:session_id] if options[:session_id]
778
795
  updater.write = options[:write]
779
796
  updater.table = options[:table]
780
797
  updater.dryrun = options[:dryrun]
@@ -1018,6 +1035,37 @@ module Google
1018
1035
  @gapi.configuration.query.create_disposition = Convert.create_disposition value
1019
1036
  end
1020
1037
 
1038
+ ##
1039
+ # Sets the create_session property. If true, creates a new session,
1040
+ # where session id will be a server generated random id. If false,
1041
+ # runs query with an existing {#session_id=}, otherwise runs query in
1042
+ # non-session mode. The default value is `false`.
1043
+ #
1044
+ # @param [Boolean] value The create_session property. The default
1045
+ # value is `false`.
1046
+ #
1047
+ # @!group Attributes
1048
+ def create_session= value
1049
+ @gapi.configuration.query.create_session = value
1050
+ end
1051
+
1052
+ ##
1053
+ # Sets the session ID for a query run in session mode. See {#create_session=}.
1054
+ #
1055
+ # @param [String] value The session ID. The default value is `nil`.
1056
+ #
1057
+ # @!group Attributes
1058
+ def session_id= value
1059
+ @gapi.configuration.query.connection_properties ||= []
1060
+ prop = @gapi.configuration.query.connection_properties.find { |cp| cp.key == "session_id" }
1061
+ if prop
1062
+ prop.value = value
1063
+ else
1064
+ prop = Google::Apis::BigqueryV2::ConnectionProperty.new key: "session_id", value: value
1065
+ @gapi.configuration.query.connection_properties << prop
1066
+ end
1067
+ end
1068
+
1021
1069
  ##
1022
1070
  # Sets the write disposition for when the query results table exists.
1023
1071
  #
@@ -1145,7 +1193,7 @@ module Google
1145
1193
  #
1146
1194
  def external= value
1147
1195
  external_table_pairs = value.map { |name, obj| [String(name), obj.to_gapi] }
1148
- external_table_hash = Hash[external_table_pairs]
1196
+ external_table_hash = external_table_pairs.to_h
1149
1197
  @gapi.configuration.query.table_definitions = external_table_hash
1150
1198
  end
1151
1199
 
@@ -471,7 +471,7 @@ module Google
471
471
  #
472
472
  def param_type
473
473
  param_type = type.to_sym
474
- param_type = Hash[fields.map { |field| [field.name.to_sym, field.param_type] }] if record?
474
+ param_type = fields.to_h { |field| [field.name.to_sym, field.param_type] } if record?
475
475
  param_type = [param_type] if repeated?
476
476
  param_type
477
477
  end
@@ -172,7 +172,7 @@ module Google
172
172
  # schema.param_types
173
173
  #
174
174
  def param_types
175
- Hash[fields.map { |field| [field.name.to_sym, field.param_type] }]
175
+ fields.to_h { |field| [field.name.to_sym, field.param_type] }
176
176
  end
177
177
 
178
178
  ##