google-cloud-bigquery 1.21.1 → 1.25.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
  #
@@ -1286,12 +1399,13 @@ module Google
1286
1399
  # table = dataset.table "my_table"
1287
1400
  #
1288
1401
  # data = table.data
1402
+ #
1403
+ # # Iterate over the first page of results
1289
1404
  # data.each do |row|
1290
- # puts row[:first_name]
1291
- # end
1292
- # if data.next?
1293
- # more_data = data.next if data.next?
1405
+ # puts row[:name]
1294
1406
  # end
1407
+ # # Retrieve the next page of results
1408
+ # data = data.next if data.next?
1295
1409
  #
1296
1410
  # @example Retrieve all rows of data: (See {Data#all})
1297
1411
  # require "google/cloud/bigquery"
@@ -1301,8 +1415,9 @@ module Google
1301
1415
  # table = dataset.table "my_table"
1302
1416
  #
1303
1417
  # data = table.data
1418
+ #
1304
1419
  # data.all do |row|
1305
- # puts row[:first_name]
1420
+ # puts row[:name]
1306
1421
  # end
1307
1422
  #
1308
1423
  # @!group Data
@@ -1368,13 +1483,21 @@ module Google
1368
1483
  # is 1,024 characters. If `job_id` is provided, then `prefix` will not
1369
1484
  # be used.
1370
1485
  # @param [Hash] labels A hash of user-provided labels associated with
1371
- # the job. You can use these to organize and group your jobs. Label
1372
- # keys and values can be no longer than 63 characters, can only
1373
- # contain lowercase letters, numeric characters, underscores and
1374
- # dashes. International characters are allowed. Label values are
1375
- # optional. Label keys must start with a letter and each label in the
1376
- # list must have a different key. See [Requirements for
1377
- # 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.
1378
1501
  # @param [Boolean] dryrun If set, don't actually run this job. Behavior
1379
1502
  # is undefined however for non-query jobs and may result in an error.
1380
1503
  # Deprecated.
@@ -1509,11 +1632,11 @@ module Google
1509
1632
  # The geographic location for the job ("US", "EU", etc.) can be set via
1510
1633
  # {ExtractJob::Updater#location=} in a block passed to this method. If
1511
1634
  # the table is a full resource representation (see {#resource_full?}),
1512
- # 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
1513
1636
  # the table.
1514
1637
  #
1515
- # @see https://cloud.google.com/bigquery/exporting-data-from-bigquery
1516
- # Exporting Data From BigQuery
1638
+ # @see https://cloud.google.com/bigquery/docs/exporting-data
1639
+ # Exporting table data
1517
1640
  #
1518
1641
  # @param [Google::Cloud::Storage::File, String, Array<String>]
1519
1642
  # extract_url The Google Storage file or file URI pattern(s) to which
@@ -1549,13 +1672,21 @@ module Google
1549
1672
  # is 1,024 characters. If `job_id` is provided, then `prefix` will not
1550
1673
  # be used.
1551
1674
  # @param [Hash] labels A hash of user-provided labels associated with
1552
- # the job. You can use these to organize and group your jobs. Label
1553
- # keys and values can be no longer than 63 characters, can only
1554
- # contain lowercase letters, numeric characters, underscores and
1555
- # dashes. International characters are allowed. Label values are
1556
- # optional. Label keys must start with a letter and each label in the
1557
- # list must have a different key. See [Requirements for
1558
- # 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.
1559
1690
  # @param [Boolean] dryrun If set, don't actually run this job. Behavior
1560
1691
  # is undefined however for non-query jobs and may result in an error.
1561
1692
  # Deprecated.
@@ -1607,8 +1738,8 @@ module Google
1607
1738
  # the location of the job will be automatically set to the location of
1608
1739
  # the table.
1609
1740
  #
1610
- # @see https://cloud.google.com/bigquery/exporting-data-from-bigquery
1611
- # Exporting Data From BigQuery
1741
+ # @see https://cloud.google.com/bigquery/docs/exporting-data
1742
+ # Exporting table data
1612
1743
  #
1613
1744
  # @param [Google::Cloud::Storage::File, String, Array<String>]
1614
1745
  # extract_url The Google Storage file or file URI pattern(s) to which
@@ -1789,13 +1920,21 @@ module Google
1789
1920
  # is 1,024 characters. If `job_id` is provided, then `prefix` will not
1790
1921
  # be used.
1791
1922
  # @param [Hash] labels A hash of user-provided labels associated with
1792
- # the job. You can use these to organize and group your jobs. Label
1793
- # keys and values can be no longer than 63 characters, can only
1794
- # contain lowercase letters, numeric characters, underscores and
1795
- # dashes. International characters are allowed. Label values are
1796
- # optional. Label keys must start with a letter and each label in the
1797
- # list must have a different key. See [Requirements for
1798
- # 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.
1799
1938
  # @param [Boolean] dryrun If set, don't actually run this job. Behavior
1800
1939
  # is undefined however for non-query jobs and may result in an error.
1801
1940
  # Deprecated.
@@ -34,9 +34,12 @@ module Google
34
34
  # "WHERE time_of_date = @time",
35
35
  # params: { time: fourpm }
36
36
  #
37
+ # # Iterate over the first page of results
37
38
  # data.each do |row|
38
39
  # puts row[:name]
39
40
  # end
41
+ # # Retrieve the next page of results
42
+ # data = data.next if data.next?
40
43
  #
41
44
  # @example Create Time with fractional seconds:
42
45
  # require "google/cloud/bigquery"
@@ -49,9 +52,12 @@ module Google
49
52
  # "WHERE time_of_date >= @time",
50
53
  # params: { time: precise_time }
51
54
  #
55
+ # # Iterate over the first page of results
52
56
  # data.each do |row|
53
57
  # puts row[:name]
54
58
  # end
59
+ # # Retrieve the next page of results
60
+ # data = data.next if data.next?
55
61
  #
56
62
  Time = Struct.new :value
57
63
  end