google-cloud-bigquery 1.21.2 → 1.26.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.
@@ -94,8 +94,7 @@ module Google
94
94
  # otherwise.
95
95
  #
96
96
  def batch?
97
- val = @gapi.configuration.query.priority
98
- val == "BATCH"
97
+ @gapi.configuration.query.priority == "BATCH"
99
98
  end
100
99
 
101
100
  ##
@@ -528,8 +527,9 @@ module Google
528
527
  # The period for which the destination table will be partitioned, if
529
528
  # any. See [Partitioned Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
530
529
  #
531
- # @return [String, nil] The partition type. Currently the only supported
532
- # value is "DAY", or `nil` if not present.
530
+ # @return [String, nil] The partition type. The supported types are `DAY`,
531
+ # `HOUR`, `MONTH`, and `YEAR`, which will generate one partition per day,
532
+ # hour, month, and year, respectively; or `nil` if not present.
533
533
  #
534
534
  # @!group Attributes
535
535
  #
@@ -1031,12 +1031,21 @@ module Google
1031
1031
  # Sets the labels to use for the job.
1032
1032
  #
1033
1033
  # @param [Hash] value A hash of user-provided labels associated with
1034
- # the job. You can use these to organize and group your jobs. Label
1035
- # keys and values can be no longer than 63 characters, can only
1036
- # contain lowercase letters, numeric characters, underscores and
1037
- # dashes. International characters are allowed. Label values are
1038
- # optional. Label keys must start with a letter and each label in
1039
- # the list must have a different key.
1034
+ # the job. You can use these to organize and group your jobs.
1035
+ #
1036
+ # The labels applied to a resource must meet the following requirements:
1037
+ #
1038
+ # * Each resource can have multiple labels, up to a maximum of 64.
1039
+ # * Each label must be a key-value pair.
1040
+ # * Keys have a minimum length of 1 character and a maximum length of
1041
+ # 63 characters, and cannot be empty. Values can be empty, and have
1042
+ # a maximum length of 63 characters.
1043
+ # * Keys and values can contain only lowercase letters, numeric characters,
1044
+ # underscores, and dashes. All characters must use UTF-8 encoding, and
1045
+ # international characters are allowed.
1046
+ # * The key portion of a label must be unique. However, you can use the
1047
+ # same key with multiple resources.
1048
+ # * Keys must start with a lowercase letter or international character.
1040
1049
  #
1041
1050
  # @!group Attributes
1042
1051
  #
@@ -1293,13 +1302,16 @@ module Google
1293
1302
  ##
1294
1303
  # Sets the partitioning for the destination table. See [Partitioned
1295
1304
  # Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
1305
+ # The supported types are `DAY`, `HOUR`, `MONTH`, and `YEAR`, which will
1306
+ # generate one partition per day, hour, month, and year, respectively.
1296
1307
  #
1297
1308
  # You can only set the partitioning field while creating a table.
1298
1309
  # BigQuery does not allow you to change partitioning on an existing
1299
1310
  # table.
1300
1311
  #
1301
- # @param [String] type The partition type. Currently the only
1302
- # supported value is "DAY".
1312
+ # @param [String] type The partition type. The supported types are `DAY`,
1313
+ # `HOUR`, `MONTH`, and `YEAR`, which will generate one partition per day,
1314
+ # hour, month, and year, respectively.
1303
1315
  #
1304
1316
  # @example
1305
1317
  # require "google/cloud/bigquery"
@@ -43,12 +43,13 @@ module Google
43
43
 
44
44
  ##
45
45
  # Creates a new Service instance.
46
- def initialize project, credentials, retries: nil, timeout: nil, host: nil
46
+ def initialize project, credentials, retries: nil, timeout: nil, host: nil, quota_project: nil
47
47
  @project = project
48
48
  @credentials = credentials
49
49
  @retries = retries
50
50
  @timeout = timeout
51
51
  @host = host
52
+ @quota_project = quota_project
52
53
  end
53
54
 
54
55
  def service
@@ -64,6 +65,9 @@ module Google
64
65
  service.request_options.header ||= {}
65
66
  service.request_options.header["x-goog-api-client"] = \
66
67
  "gl-ruby/#{RUBY_VERSION} gccl/#{Google::Cloud::Bigquery::VERSION}"
68
+ service.request_options.query ||= {}
69
+ service.request_options.query["prettyPrint"] = false
70
+ service.request_options.quota_project = @quota_project if @quota_project
67
71
  service.authorization = @credentials.client
68
72
  service.root_url = host if host
69
73
  service
@@ -138,6 +142,8 @@ module Google
138
142
  end
139
143
  end
140
144
 
145
+ ##
146
+ # Gets the specified table resource by full table reference.
141
147
  def get_project_table project_id, dataset_id, table_id
142
148
  # The get operation is considered idempotent
143
149
  execute backoff: true do
@@ -151,10 +157,7 @@ module Google
151
157
  # it only returns the table resource,
152
158
  # which describes the structure of this table.
153
159
  def get_table dataset_id, table_id
154
- # The get operation is considered idempotent
155
- execute backoff: true do
156
- get_project_table @project, dataset_id, table_id
157
- end
160
+ get_project_table @project, dataset_id, table_id
158
161
  end
159
162
 
160
163
  ##
@@ -179,6 +182,34 @@ module Google
179
182
  end
180
183
  end
181
184
 
185
+ ##
186
+ # Returns Google::Apis::BigqueryV2::Policy
187
+ def get_table_policy dataset_id, table_id
188
+ policy_options = API::GetPolicyOptions.new requested_policy_version: 1
189
+ execute do
190
+ service.get_table_iam_policy table_path(dataset_id, table_id),
191
+ API::GetIamPolicyRequest.new(options: policy_options)
192
+ end
193
+ end
194
+
195
+ ##
196
+ # @param [Google::Apis::BigqueryV2::Policy] new_policy
197
+ def set_table_policy dataset_id, table_id, new_policy
198
+ execute do
199
+ service.set_table_iam_policy table_path(dataset_id, table_id),
200
+ API::SetIamPolicyRequest.new(policy: new_policy)
201
+ end
202
+ end
203
+
204
+ ##
205
+ # Returns Google::Apis::BigqueryV2::TestIamPermissionsResponse
206
+ def test_table_permissions dataset_id, table_id, permissions
207
+ execute do
208
+ service.test_table_iam_permissions table_path(dataset_id, table_id),
209
+ API::TestIamPermissionsRequest.new(permissions: permissions)
210
+ end
211
+ end
212
+
182
213
  ##
183
214
  # Deletes the table specified by tableId from the dataset.
184
215
  # If the table contains data, all the data will be deleted.
@@ -250,18 +281,21 @@ module Google
250
281
  end
251
282
  end
252
283
 
253
- # Gets the specified model resource by model ID.
254
- # This method does not return the data in the model,
255
- # it only returns the model resource,
256
- # which describes the structure of this model.
257
- def get_model dataset_id, model_id
284
+ # Gets the specified model resource by full model reference.
285
+ def get_project_model project_id, dataset_id, model_id
258
286
  # The get operation is considered idempotent
259
287
  execute backoff: true do
260
- json_txt = service.get_model @project, dataset_id, model_id, options: { skip_deserialization: true }
288
+ json_txt = service.get_model project_id, dataset_id, model_id, options: { skip_deserialization: true }
261
289
  JSON.parse json_txt, symbolize_names: true
262
290
  end
263
291
  end
264
292
 
293
+ # Gets the specified model resource by model ID. This method does not return the data in the model, it only
294
+ # returns the model resource, which describes the structure of this model.
295
+ def get_model dataset_id, model_id
296
+ get_project_model @project, dataset_id, model_id
297
+ end
298
+
265
299
  ##
266
300
  # Updates information in an existing model, replacing fields that
267
301
  # are provided in the submitted model resource.
@@ -502,6 +536,11 @@ module Google
502
536
 
503
537
  protected
504
538
 
539
+ # Creates a formatted table path.
540
+ def table_path dataset_id, table_id
541
+ "projects/#{@project}/datasets/#{dataset_id}/tables/#{table_id}"
542
+ end
543
+
505
544
  # Generate a random string similar to the BigQuery service job IDs.
506
545
  def generate_id
507
546
  SecureRandom.urlsafe_base64 21
@@ -23,6 +23,7 @@ require "google/cloud/bigquery/external"
23
23
  require "google/cloud/bigquery/insert_response"
24
24
  require "google/cloud/bigquery/table/async_inserter"
25
25
  require "google/cloud/bigquery/convert"
26
+ require "google/cloud/bigquery/policy"
26
27
  require "google/apis/bigquery_v2"
27
28
 
28
29
  module Google
@@ -250,9 +251,10 @@ module Google
250
251
  # The period for which the table is time partitioned, if any. See
251
252
  # [Partitioned Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
252
253
  #
253
- # @return [String, nil] The time partition type. Currently the only supported
254
- # value is "DAY", or `nil` if the object is a reference (see
255
- # {#reference?}).
254
+ # @return [String, nil] The time partition type. The supported types are `DAY`,
255
+ # `HOUR`, `MONTH`, and `YEAR`, which will generate one partition per day,
256
+ # hour, month, and year, respectively; or `nil` if not set or the object is a
257
+ # reference (see {#reference?}).
256
258
  #
257
259
  # @!group Attributes
258
260
  #
@@ -265,13 +267,16 @@ module Google
265
267
  ##
266
268
  # Sets the time partitioning type for the table. See [Partitioned
267
269
  # Tables](https://cloud.google.com/bigquery/docs/partitioned-tables).
270
+ # The supported types are `DAY`, `HOUR`, `MONTH`, and `YEAR`, which will
271
+ # generate one partition per day, hour, month, and year, respectively.
268
272
  #
269
273
  # You can only set time partitioning when creating a table as in
270
274
  # the example below. BigQuery does not allow you to change time partitioning
271
275
  # on an existing table.
272
276
  #
273
- # @param [String] type The time partition type. Currently the only
274
- # supported value is "DAY".
277
+ # @param [String] type The time partition type. The supported types are `DAY`,
278
+ # `HOUR`, `MONTH`, and `YEAR`, which will generate one partition per day,
279
+ # hour, month, and year, respectively.
275
280
  #
276
281
  # @example
277
282
  # require "google/cloud/bigquery"
@@ -820,12 +825,19 @@ module Google
820
825
  # @param [Hash<String, String>] labels A hash containing key/value
821
826
  # pairs.
822
827
  #
823
- # * Label keys and values can be no longer than 63 characters.
824
- # * Label keys and values can contain only lowercase letters, numbers,
825
- # underscores, hyphens, and international characters.
826
- # * Label keys and values cannot exceed 128 bytes in size.
827
- # * Label keys must begin with a letter.
828
- # * Label keys must be unique within a table.
828
+ # The labels applied to a resource must meet the following requirements:
829
+ #
830
+ # * Each resource can have multiple labels, up to a maximum of 64.
831
+ # * Each label must be a key-value pair.
832
+ # * Keys have a minimum length of 1 character and a maximum length of
833
+ # 63 characters, and cannot be empty. Values can be empty, and have
834
+ # a maximum length of 63 characters.
835
+ # * Keys and values can contain only lowercase letters, numeric characters,
836
+ # underscores, and dashes. All characters must use UTF-8 encoding, and
837
+ # international characters are allowed.
838
+ # * The key portion of a label must be unique. However, you can use the
839
+ # same key with multiple resources.
840
+ # * Keys must start with a lowercase letter or international character.
829
841
  #
830
842
  # @example
831
843
  # require "google/cloud/bigquery"
@@ -1263,6 +1275,107 @@ module Google
1263
1275
  Array(udfs_gapi).map { |udf| udf.inline_code || udf.resource_uri }
1264
1276
  end
1265
1277
 
1278
+ ##
1279
+ # Gets the Cloud IAM access control policy for the table. The latest policy will be read from the service. See
1280
+ # also {#update_policy}.
1281
+ #
1282
+ # @see https://cloud.google.com/iam/docs/managing-policies Managing Policies
1283
+ # @see https://cloud.google.com/bigquery/docs/table-access-controls-intro Controlling access to tables
1284
+ #
1285
+ # @return [Policy] The frozen policy for the table.
1286
+ #
1287
+ # @example
1288
+ # require "google/cloud/bigquery"
1289
+ #
1290
+ # bigquery = Google::Cloud::Bigquery.new
1291
+ # dataset = bigquery.dataset "my_dataset"
1292
+ # table = dataset.table "my_table"
1293
+ #
1294
+ # policy = table.policy
1295
+ #
1296
+ # policy.frozen? #=> true
1297
+ # binding_owner = policy.bindings.find { |b| b.role == "roles/owner" }
1298
+ # binding_owner.role #=> "roles/owner"
1299
+ # binding_owner.members #=> ["user:owner@example.com"]
1300
+ # binding_owner.frozen? #=> true
1301
+ # binding_owner.members.frozen? #=> true
1302
+ #
1303
+ def policy
1304
+ raise ArgumentError, "Block argument not supported: Use #update_policy instead." if block_given?
1305
+ ensure_service!
1306
+ gapi = service.get_table_policy dataset_id, table_id
1307
+ Policy.from_gapi(gapi).freeze
1308
+ end
1309
+
1310
+ ##
1311
+ # Updates the Cloud IAM access control policy for the table. The latest policy will be read from the service.
1312
+ # See also {#policy}.
1313
+ #
1314
+ # @see https://cloud.google.com/iam/docs/managing-policies Managing Policies
1315
+ # @see https://cloud.google.com/bigquery/docs/table-access-controls-intro Controlling access to tables
1316
+ #
1317
+ # @yield [policy] A block for updating the policy. The latest policy will be read from the service and passed to
1318
+ # the block. After the block completes, the modified policy will be written to the service.
1319
+ # @yieldparam [Policy] policy The mutable Policy for the table.
1320
+ #
1321
+ # @return [Policy] The updated and frozen policy for the table.
1322
+ #
1323
+ # @example Update the policy by passing a block.
1324
+ # require "google/cloud/bigquery"
1325
+ #
1326
+ # bigquery = Google::Cloud::Bigquery.new
1327
+ # dataset = bigquery.dataset "my_dataset"
1328
+ # table = dataset.table "my_table"
1329
+ #
1330
+ # table.update_policy do |p|
1331
+ # p.grant role: "roles/viewer", members: "user:viewer@example.com"
1332
+ # p.revoke role: "roles/editor", members: "user:editor@example.com"
1333
+ # p.revoke role: "roles/owner"
1334
+ # end # 2 API calls
1335
+ #
1336
+ def update_policy
1337
+ raise ArgumentError, "A block updating the policy must be provided" unless block_given?
1338
+ ensure_service!
1339
+ gapi = service.get_table_policy dataset_id, table_id
1340
+ policy = Policy.from_gapi gapi
1341
+ yield policy
1342
+ # TODO: Check for changes before calling RPC
1343
+ gapi = service.set_table_policy dataset_id, table_id, policy.to_gapi
1344
+ Policy.from_gapi(gapi).freeze
1345
+ end
1346
+
1347
+ ##
1348
+ # Tests the specified permissions against the [Cloud
1349
+ # IAM](https://cloud.google.com/iam/) access control policy.
1350
+ #
1351
+ # @see https://cloud.google.com/iam/docs/managing-policies Managing Policies
1352
+ #
1353
+ # @param [String, Array<String>] permissions The set of permissions
1354
+ # against which to check access. Permissions must be of the format
1355
+ # `bigquery.resource.capability`.
1356
+ # See https://cloud.google.com/bigquery/docs/access-control#bigquery.
1357
+ #
1358
+ # @return [Array<String>] The frozen array of permissions held by the caller.
1359
+ #
1360
+ # @example
1361
+ # require "google/cloud/bigquery"
1362
+ #
1363
+ # bigquery = Google::Cloud::Bigquery.new
1364
+ # dataset = bigquery.dataset "my_dataset"
1365
+ # table = dataset.table "my_table"
1366
+ #
1367
+ # permissions = table.test_iam_permissions "bigquery.tables.get",
1368
+ # "bigquery.tables.delete"
1369
+ # permissions.include? "bigquery.tables.get" #=> true
1370
+ # permissions.include? "bigquery.tables.delete" #=> false
1371
+ #
1372
+ def test_iam_permissions *permissions
1373
+ permissions = Array(permissions).flatten
1374
+ ensure_service!
1375
+ gapi = service.test_table_permissions dataset_id, table_id, permissions
1376
+ gapi.permissions.freeze
1377
+ end
1378
+
1266
1379
  ##
1267
1380
  # Retrieves data from the table.
1268
1381
  #
@@ -1370,13 +1483,21 @@ module Google
1370
1483
  # is 1,024 characters. If `job_id` is provided, then `prefix` will not
1371
1484
  # be used.
1372
1485
  # @param [Hash] labels A hash of user-provided labels associated with
1373
- # the job. You can use these to organize and group your jobs. Label
1374
- # keys and values can be no longer than 63 characters, can only
1375
- # contain lowercase letters, numeric characters, underscores and
1376
- # dashes. International characters are allowed. Label values are
1377
- # optional. Label keys must start with a letter and each label in the
1378
- # list must have a different key. See [Requirements for
1379
- # labels](https://cloud.google.com/bigquery/docs/creating-managing-labels#requirements).
1486
+ # the job. You can use these to organize and group your jobs.
1487
+ #
1488
+ # The labels applied to a resource must meet the following requirements:
1489
+ #
1490
+ # * Each resource can have multiple labels, up to a maximum of 64.
1491
+ # * Each label must be a key-value pair.
1492
+ # * Keys have a minimum length of 1 character and a maximum length of
1493
+ # 63 characters, and cannot be empty. Values can be empty, and have
1494
+ # a maximum length of 63 characters.
1495
+ # * Keys and values can contain only lowercase letters, numeric characters,
1496
+ # underscores, and dashes. All characters must use UTF-8 encoding, and
1497
+ # international characters are allowed.
1498
+ # * The key portion of a label must be unique. However, you can use the
1499
+ # same key with multiple resources.
1500
+ # * Keys must start with a lowercase letter or international character.
1380
1501
  # @param [Boolean] dryrun If set, don't actually run this job. Behavior
1381
1502
  # is undefined however for non-query jobs and may result in an error.
1382
1503
  # Deprecated.
@@ -1511,11 +1632,11 @@ module Google
1511
1632
  # The geographic location for the job ("US", "EU", etc.) can be set via
1512
1633
  # {ExtractJob::Updater#location=} in a block passed to this method. If
1513
1634
  # the table is a full resource representation (see {#resource_full?}),
1514
- # the location of the job will be automatically set to the location of
1635
+ # the location of the job will automatically be set to the location of
1515
1636
  # the table.
1516
1637
  #
1517
- # @see https://cloud.google.com/bigquery/exporting-data-from-bigquery
1518
- # Exporting Data From BigQuery
1638
+ # @see https://cloud.google.com/bigquery/docs/exporting-data
1639
+ # Exporting table data
1519
1640
  #
1520
1641
  # @param [Google::Cloud::Storage::File, String, Array<String>]
1521
1642
  # extract_url The Google Storage file or file URI pattern(s) to which
@@ -1551,13 +1672,21 @@ module Google
1551
1672
  # is 1,024 characters. If `job_id` is provided, then `prefix` will not
1552
1673
  # be used.
1553
1674
  # @param [Hash] labels A hash of user-provided labels associated with
1554
- # the job. You can use these to organize and group your jobs. Label
1555
- # keys and values can be no longer than 63 characters, can only
1556
- # contain lowercase letters, numeric characters, underscores and
1557
- # dashes. International characters are allowed. Label values are
1558
- # optional. Label keys must start with a letter and each label in the
1559
- # list must have a different key. See [Requirements for
1560
- # labels](https://cloud.google.com/bigquery/docs/creating-managing-labels#requirements).
1675
+ # the job. You can use these to organize and group your jobs.
1676
+ #
1677
+ # The labels applied to a resource must meet the following requirements:
1678
+ #
1679
+ # * Each resource can have multiple labels, up to a maximum of 64.
1680
+ # * Each label must be a key-value pair.
1681
+ # * Keys have a minimum length of 1 character and a maximum length of
1682
+ # 63 characters, and cannot be empty. Values can be empty, and have
1683
+ # a maximum length of 63 characters.
1684
+ # * Keys and values can contain only lowercase letters, numeric characters,
1685
+ # underscores, and dashes. All characters must use UTF-8 encoding, and
1686
+ # international characters are allowed.
1687
+ # * The key portion of a label must be unique. However, you can use the
1688
+ # same key with multiple resources.
1689
+ # * Keys must start with a lowercase letter or international character.
1561
1690
  # @param [Boolean] dryrun If set, don't actually run this job. Behavior
1562
1691
  # is undefined however for non-query jobs and may result in an error.
1563
1692
  # Deprecated.
@@ -1609,8 +1738,8 @@ module Google
1609
1738
  # the location of the job will be automatically set to the location of
1610
1739
  # the table.
1611
1740
  #
1612
- # @see https://cloud.google.com/bigquery/exporting-data-from-bigquery
1613
- # Exporting Data From BigQuery
1741
+ # @see https://cloud.google.com/bigquery/docs/exporting-data
1742
+ # Exporting table data
1614
1743
  #
1615
1744
  # @param [Google::Cloud::Storage::File, String, Array<String>]
1616
1745
  # extract_url The Google Storage file or file URI pattern(s) to which
@@ -1791,13 +1920,21 @@ module Google
1791
1920
  # is 1,024 characters. If `job_id` is provided, then `prefix` will not
1792
1921
  # be used.
1793
1922
  # @param [Hash] labels A hash of user-provided labels associated with
1794
- # the job. You can use these to organize and group your jobs. Label
1795
- # keys and values can be no longer than 63 characters, can only
1796
- # contain lowercase letters, numeric characters, underscores and
1797
- # dashes. International characters are allowed. Label values are
1798
- # optional. Label keys must start with a letter and each label in the
1799
- # list must have a different key. See [Requirements for
1800
- # labels](https://cloud.google.com/bigquery/docs/creating-managing-labels#requirements).
1923
+ # the job. You can use these to organize and group your jobs.
1924
+ #
1925
+ # The labels applied to a resource must meet the following requirements:
1926
+ #
1927
+ # * Each resource can have multiple labels, up to a maximum of 64.
1928
+ # * Each label must be a key-value pair.
1929
+ # * Keys have a minimum length of 1 character and a maximum length of
1930
+ # 63 characters, and cannot be empty. Values can be empty, and have
1931
+ # a maximum length of 63 characters.
1932
+ # * Keys and values can contain only lowercase letters, numeric characters,
1933
+ # underscores, and dashes. All characters must use UTF-8 encoding, and
1934
+ # international characters are allowed.
1935
+ # * The key portion of a label must be unique. However, you can use the
1936
+ # same key with multiple resources.
1937
+ # * Keys must start with a lowercase letter or international character.
1801
1938
  # @param [Boolean] dryrun If set, don't actually run this job. Behavior
1802
1939
  # is undefined however for non-query jobs and may result in an error.
1803
1940
  # Deprecated.