google-cloud-bigquery 1.22.0 → 1.27.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
  #
@@ -1302,13 +1302,16 @@ module Google
1302
1302
  ##
1303
1303
  # Sets the partitioning for the destination table. See [Partitioned
1304
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.
1305
1307
  #
1306
1308
  # You can only set the partitioning field while creating a table.
1307
1309
  # BigQuery does not allow you to change partitioning on an existing
1308
1310
  # table.
1309
1311
  #
1310
- # @param [String] type The partition type. Currently the only
1311
- # 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.
1312
1315
  #
1313
1316
  # @example
1314
1317
  # require "google/cloud/bigquery"
@@ -603,6 +603,93 @@ module Google
603
603
  update_gapi!
604
604
  end
605
605
 
606
+ ###
607
+ # The JavaScript UDF determinism level. Optional.
608
+ #
609
+ # * `DETERMINISTIC` - Deterministic indicates that two calls with the same input to a UDF yield the same output.
610
+ # If all JavaScript UDFs are `DETERMINISTIC`, the query result is potentially cachable.
611
+ # * `NOT_DETERMINISTIC` - Not deterministic indicates that the output of the UDF is not guaranteed to yield the
612
+ # same output each time for a given set of inputs. If any JavaScript UDF is `NOT_DETERMINISTIC`, the query
613
+ # result is not cacheable.
614
+ #
615
+ # Even if a JavaScript UDF is deterministic, many other factors can prevent usage of cached query results.
616
+ # Example factors include but not limited to: DDL/DML, non-deterministic SQL function calls, update of
617
+ # referenced tables/views/UDFs or imported JavaScript libraries. SQL UDFs cannot have determinism specified.
618
+ # Their determinism is automatically determined.
619
+ #
620
+ # @return [String, nil] The routine determinism level in upper case, or `nil` if not set or the object is a
621
+ # reference (see {#reference?}).
622
+ #
623
+ # @example
624
+ # require "google/cloud/bigquery"
625
+ #
626
+ # bigquery = Google::Cloud::Bigquery.new
627
+ # dataset = bigquery.dataset "my_dataset"
628
+ # routine = dataset.routine "my_routine"
629
+ #
630
+ # routine.determinism_level #=> "NOT_DETERMINISTIC"
631
+ #
632
+ # @!group Attributes
633
+ #
634
+ def determinism_level
635
+ return nil if reference?
636
+ ensure_full_data!
637
+ @gapi.determinism_level
638
+ end
639
+
640
+ ##
641
+ # Updates the JavaScript UDF determinism level. Optional.
642
+ #
643
+ # * `DETERMINISTIC` - Deterministic indicates that two calls with the same input to a UDF yield the same output.
644
+ # If all JavaScript UDFs are `DETERMINISTIC`, the query result is potentially cachable.
645
+ # * `NOT_DETERMINISTIC` - Not deterministic indicates that the output of the UDF is not guaranteed to yield the
646
+ # same output each time for a given set of inputs. If any JavaScript UDF is `NOT_DETERMINISTIC`, the query
647
+ # result is not cacheable.
648
+ #
649
+ # @param [String, nil] new_determinism_level The new routine determinism level in upper case.
650
+ #
651
+ # @example
652
+ # require "google/cloud/bigquery"
653
+ #
654
+ # bigquery = Google::Cloud::Bigquery.new
655
+ # dataset = bigquery.dataset "my_dataset"
656
+ # routine = dataset.routine "my_routine"
657
+ #
658
+ # routine.determinism_level #=> "NOT_DETERMINISTIC"
659
+ # routine.determinism_level = "DETERMINISTIC"
660
+ #
661
+ # @!group Attributes
662
+ #
663
+ def determinism_level= new_determinism_level
664
+ ensure_full_data!
665
+ @gapi.determinism_level = new_determinism_level
666
+ update_gapi!
667
+ end
668
+
669
+ ##
670
+ # Checks if the value of {#determinism_level} is `DETERMINISTIC`. The default is `false`.
671
+ #
672
+ # @return [Boolean] `true` when `DETERMINISTIC` and the object is not a reference (see {#reference?}), `false`
673
+ # otherwise.
674
+ #
675
+ # @!group Attributes
676
+ #
677
+ def determinism_level_deterministic?
678
+ @gapi.determinism_level == "DETERMINISTIC"
679
+ end
680
+
681
+ ##
682
+ # Checks if the value of {#determinism_level} is `NOT_DETERMINISTIC`. The default is `false`.
683
+ #
684
+ # @return [Boolean] `true` when `NOT_DETERMINISTIC` and the object is not a reference (see {#reference?}),
685
+ # `false` otherwise.
686
+ #
687
+ # @!group Attributes
688
+ #
689
+ def determinism_level_not_deterministic?
690
+ @gapi.determinism_level == "NOT_DETERMINISTIC"
691
+ end
692
+
606
693
  ##
607
694
  # Updates the routine with changes made in the given block in a single update request. The following attributes
608
695
  # may be set: {Updater#routine_type=}, {Updater#language=}, {Updater#arguments=}, {Updater#return_type=},
@@ -999,7 +1086,9 @@ module Google
999
1086
  # routine = dataset.routine "my_routine"
1000
1087
  #
1001
1088
  # routine.return_type.type_kind #=> "INT64"
1002
- # routine.return_type = "STRING"
1089
+ # routine.update do |r|
1090
+ # r.return_type = "STRING"
1091
+ # end
1003
1092
  #
1004
1093
  def return_type= new_return_type
1005
1094
  @gapi.return_type = StandardSql::DataType.gapi_from_string_or_data_type new_return_type
@@ -1019,9 +1108,11 @@ module Google
1019
1108
  # dataset = bigquery.dataset "my_dataset"
1020
1109
  # routine = dataset.routine "my_routine"
1021
1110
  #
1022
- # routine.imported_libraries = [
1023
- # "gs://cloud-samples-data/bigquery/udfs/max-value.js"
1024
- # ]
1111
+ # routine.update do |r|
1112
+ # r.imported_libraries = [
1113
+ # "gs://cloud-samples-data/bigquery/udfs/max-value.js"
1114
+ # ]
1115
+ # end
1025
1116
  #
1026
1117
  def imported_libraries= new_imported_libraries
1027
1118
  @gapi.imported_libraries = new_imported_libraries
@@ -1069,12 +1160,43 @@ module Google
1069
1160
  # routine = dataset.routine "my_routine"
1070
1161
  #
1071
1162
  # routine.description #=> "My routine description"
1072
- # routine.description = "My updated routine description"
1163
+ # routine.update do |r|
1164
+ # r.description = "My updated routine description"
1165
+ # end
1073
1166
  #
1074
1167
  def description= new_description
1075
1168
  @gapi.description = new_description
1076
1169
  end
1077
1170
 
1171
+ ##
1172
+ # Updates the JavaScript UDF determinism level. Optional.
1173
+ #
1174
+ # * `DETERMINISTIC` - Deterministic indicates that two calls with the same input to a UDF yield the same
1175
+ # output. If all JavaScript UDFs are `DETERMINISTIC`, the query result is potentially cachable.
1176
+ # * `NOT_DETERMINISTIC` - Not deterministic indicates that the output of the UDF is not guaranteed to yield
1177
+ # the same output each time for a given set of inputs. If any JavaScript UDF is `NOT_DETERMINISTIC`, the
1178
+ # query result is not cacheable.
1179
+ #
1180
+ # @param [String, nil] new_determinism_level The new routine determinism level in upper case.
1181
+ #
1182
+ # @example
1183
+ # require "google/cloud/bigquery"
1184
+ #
1185
+ # bigquery = Google::Cloud::Bigquery.new
1186
+ # dataset = bigquery.dataset "my_dataset"
1187
+ # routine = dataset.routine "my_routine"
1188
+ #
1189
+ # routine.determinism_level #=> "NOT_DETERMINISTIC"
1190
+ # routine.update do |r|
1191
+ # r.determinism_level = "DETERMINISTIC"
1192
+ # end
1193
+ #
1194
+ # @!group Attributes
1195
+ #
1196
+ def determinism_level= new_determinism_level
1197
+ @gapi.determinism_level = new_determinism_level
1198
+ end
1199
+
1078
1200
  def update
1079
1201
  raise "not implemented in #{self.class}"
1080
1202
  end
@@ -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
@@ -178,6 +182,34 @@ module Google
178
182
  end
179
183
  end
180
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
+
181
213
  ##
182
214
  # Deletes the table specified by tableId from the dataset.
183
215
  # If the table contains data, all the data will be deleted.
@@ -504,6 +536,11 @@ module Google
504
536
 
505
537
  protected
506
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
+
507
544
  # Generate a random string similar to the BigQuery service job IDs.
508
545
  def generate_id
509
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"
@@ -1270,6 +1275,107 @@ module Google
1270
1275
  Array(udfs_gapi).map { |udf| udf.inline_code || udf.resource_uri }
1271
1276
  end
1272
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
+
1273
1379
  ##
1274
1380
  # Retrieves data from the table.
1275
1381
  #
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Bigquery
19
- VERSION = "1.22.0".freeze
19
+ VERSION = "1.27.0".freeze
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-bigquery
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.22.0
4
+ version: 1.27.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-09-10 00:00:00.000000000 Z
12
+ date: 2021-02-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: concurrent-ruby
@@ -26,19 +26,19 @@ dependencies:
26
26
  - !ruby/object:Gem::Version
27
27
  version: '1.0'
28
28
  - !ruby/object:Gem::Dependency
29
- name: google-api-client
29
+ name: google-apis-bigquery_v2
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '0.33'
34
+ version: '0.1'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '0.33'
41
+ version: '0.1'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: google-cloud-core
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -257,6 +257,7 @@ files:
257
257
  - lib/google/cloud/bigquery/load_job.rb
258
258
  - lib/google/cloud/bigquery/model.rb
259
259
  - lib/google/cloud/bigquery/model/list.rb
260
+ - lib/google/cloud/bigquery/policy.rb
260
261
  - lib/google/cloud/bigquery/project.rb
261
262
  - lib/google/cloud/bigquery/project/list.rb
262
263
  - lib/google/cloud/bigquery/query_job.rb
@@ -290,7 +291,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
290
291
  - !ruby/object:Gem::Version
291
292
  version: '0'
292
293
  requirements: []
293
- rubygems_version: 3.1.3
294
+ rubygems_version: 3.2.6
294
295
  signing_key:
295
296
  specification_version: 4
296
297
  summary: API Client library for Google BigQuery