google-cloud-spanner 1.14.0 → 1.15.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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/lib/google-cloud-spanner.rb +13 -0
  4. data/lib/google/cloud/spanner.rb +2 -1
  5. data/lib/google/cloud/spanner/admin/database.rb +3 -2
  6. data/lib/google/cloud/spanner/admin/database/v1.rb +3 -2
  7. data/lib/google/cloud/spanner/admin/database/v1/database_admin_client.rb +742 -20
  8. data/lib/google/cloud/spanner/admin/database/v1/database_admin_client_config.json +40 -0
  9. data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/field_mask.rb +222 -0
  10. data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/timestamp.rb +113 -0
  11. data/lib/google/cloud/spanner/admin/database/v1/doc/google/spanner/admin/database/v1/backup.rb +325 -0
  12. data/lib/google/cloud/spanner/admin/database/v1/doc/google/spanner/admin/database/v1/spanner_database_admin.rb +197 -30
  13. data/lib/google/cloud/spanner/admin/instance.rb +1 -1
  14. data/lib/google/cloud/spanner/admin/instance/v1.rb +1 -1
  15. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/spanner/admin/instance/v1/spanner_instance_admin.rb +6 -13
  16. data/lib/google/cloud/spanner/admin/instance/v1/instance_admin_client.rb +8 -5
  17. data/lib/google/cloud/spanner/batch_client.rb +5 -3
  18. data/lib/google/cloud/spanner/batch_snapshot.rb +43 -8
  19. data/lib/google/cloud/spanner/client.rb +57 -8
  20. data/lib/google/cloud/spanner/project.rb +31 -6
  21. data/lib/google/cloud/spanner/results.rb +4 -2
  22. data/lib/google/cloud/spanner/service.rb +3 -1
  23. data/lib/google/cloud/spanner/session.rb +37 -6
  24. data/lib/google/cloud/spanner/snapshot.rb +26 -3
  25. data/lib/google/cloud/spanner/transaction.rb +49 -5
  26. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/mutation.rb +8 -1
  27. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/spanner.rb +91 -102
  28. data/lib/google/cloud/spanner/v1/spanner_client.rb +112 -136
  29. data/lib/google/cloud/spanner/version.rb +1 -1
  30. data/lib/google/spanner/admin/database/v1/backup_pb.rb +98 -0
  31. data/lib/google/spanner/admin/database/v1/common_pb.rb +28 -0
  32. data/lib/google/spanner/admin/database/v1/spanner_database_admin_pb.rb +53 -0
  33. data/lib/google/spanner/admin/database/v1/spanner_database_admin_services_pb.rb +82 -12
  34. data/lib/google/spanner/admin/instance/v1/spanner_instance_admin_services_pb.rb +1 -2
  35. data/lib/google/spanner/v1/spanner_pb.rb +5 -0
  36. data/lib/google/spanner/v1/spanner_services_pb.rb +31 -40
  37. metadata +7 -2
@@ -166,6 +166,13 @@ module Google
166
166
  # partitions to return. For example, this may be set to the number of
167
167
  # workers available. This is only a hint and may provide different
168
168
  # results based on the request.
169
+ # @param [Hash] query_options A hash of values to specify the custom
170
+ # query options for executing SQL query. Query options are optional.
171
+ # The following settings can be provided:
172
+ #
173
+ # * `:optimizer_version` (String) The version of optimizer to use.
174
+ # Empty to use database default. "latest" to use the latest
175
+ # available optimizer version.
169
176
  #
170
177
  # @return [Array<Google::Cloud::Spanner::Partition>] The partitions
171
178
  # created by the query partition.
@@ -188,7 +195,8 @@ module Google
188
195
  # batch_snapshot.close
189
196
  #
190
197
  def partition_query sql, params: nil, types: nil,
191
- partition_size_bytes: nil, max_partitions: nil
198
+ partition_size_bytes: nil, max_partitions: nil,
199
+ query_options: nil
192
200
  ensure_session!
193
201
 
194
202
  params, types = Convert.to_input_params_and_types params, types
@@ -197,7 +205,6 @@ module Google
197
205
  sql, tx_selector, params: params, types: types,
198
206
  partition_size_bytes: partition_size_bytes,
199
207
  max_partitions: max_partitions
200
-
201
208
  results.partitions.map do |grpc|
202
209
  # Convert partition protos to execute sql request protos
203
210
  execute_sql_grpc = Google::Spanner::V1::ExecuteSqlRequest.new(
@@ -207,7 +214,8 @@ module Google
207
214
  params: params,
208
215
  param_types: types,
209
216
  transaction: tx_selector,
210
- partition_token: grpc.partition_token
217
+ partition_token: grpc.partition_token,
218
+ query_options: query_options
211
219
  }.delete_if { |_, v| v.nil? }
212
220
  )
213
221
  Partition.from_execute_sql_grpc execute_sql_grpc
@@ -415,6 +423,14 @@ module Google
415
423
  # specified using a {Fields} object.
416
424
  #
417
425
  # Types are optional.
426
+ # @param [Hash] query_options A hash of values to specify the custom
427
+ # query options for executing SQL query. Query options are optional.
428
+ # The following settings can be provided:
429
+ #
430
+ # * `:optimizer_version` (String) The version of optimizer to use.
431
+ # Empty to use database default. "latest" to use the latest
432
+ # available optimizer version.
433
+ #
418
434
  # @return [Google::Cloud::Spanner::Results] The results of the query
419
435
  # execution.
420
436
  #
@@ -518,13 +534,29 @@ module Google
518
534
  # puts "User #{row[:id]} is #{row[:name]}"
519
535
  # end
520
536
  #
521
- def execute_query sql, params: nil, types: nil
537
+ # @example Query using query options:
538
+ # require "google/cloud/spanner"
539
+ #
540
+ # spanner = Google::Cloud::Spanner.new
541
+ # batch_client = spanner.batch_client "my-instance", "my-database"
542
+ # batch_snapshot = batch_client.batch_snapshot
543
+ #
544
+ # results = batch_snapshot.execute_query \
545
+ # "SELECT * FROM users",
546
+ # query_options: { optimizer_version: "1" }
547
+ #
548
+ # results.rows.each do |row|
549
+ # puts "User #{row[:id]} is #{row[:name]}"
550
+ # end
551
+ #
552
+ def execute_query sql, params: nil, types: nil, query_options: nil
522
553
  ensure_session!
523
554
 
524
555
  params, types = Convert.to_input_params_and_types params, types
525
556
 
526
557
  session.execute_query sql, params: params, types: types,
527
- transaction: tx_selector
558
+ transaction: tx_selector,
559
+ query_options: query_options
528
560
  end
529
561
  alias execute execute_query
530
562
  alias query execute_query
@@ -628,7 +660,7 @@ module Google
628
660
  ##
629
661
  # @private Loads the serialized batch snapshot. See
630
662
  # {BatchClient#load_batch_snapshot}.
631
- def self.load data, service: nil
663
+ def self.load data, service: nil, query_options: nil
632
664
  data = JSON.parse data, symbolize_names: true unless data.is_a? Hash
633
665
 
634
666
  session_grpc = Google::Spanner::V1::Session.decode \
@@ -636,7 +668,7 @@ module Google
636
668
  transaction_grpc = Google::Spanner::V1::Transaction.decode \
637
669
  Base64.decode64(data[:transaction])
638
670
 
639
- from_grpc transaction_grpc, Session.from_grpc(session_grpc, service)
671
+ from_grpc transaction_grpc, Session.from_grpc(session_grpc, service, query_options: query_options)
640
672
  end
641
673
 
642
674
  ##
@@ -661,12 +693,15 @@ module Google
661
693
  end
662
694
 
663
695
  def execute_partition_query partition
696
+ query_options = partition.execute.query_options
697
+ query_options = query_options.to_h unless query_options.nil?
664
698
  session.execute_query \
665
699
  partition.execute.sql,
666
700
  params: partition.execute.params,
667
701
  types: partition.execute.param_types.to_h,
668
702
  transaction: partition.execute.transaction,
669
- partition_token: partition.execute.partition_token
703
+ partition_token: partition.execute.partition_token,
704
+ query_options: query_options
670
705
  end
671
706
 
672
707
  def execute_partition_read partition
@@ -53,12 +53,13 @@ module Google
53
53
  ##
54
54
  # @private Creates a new Spanner Client instance.
55
55
  def initialize project, instance_id, database_id, session_labels: nil,
56
- pool_opts: {}
56
+ pool_opts: {}, query_options: nil
57
57
  @project = project
58
58
  @instance_id = instance_id
59
59
  @database_id = database_id
60
60
  @session_labels = session_labels
61
61
  @pool = Pool.new self, pool_opts
62
+ @query_options = query_options
62
63
  end
63
64
 
64
65
  # The unique identifier for the project.
@@ -97,6 +98,13 @@ module Google
97
98
  @project.database instance_id, database_id
98
99
  end
99
100
 
101
+ # A hash of values to specify the custom query options for executing
102
+ # SQL query.
103
+ # @return [Hash]
104
+ def query_options
105
+ @query_options
106
+ end
107
+
100
108
  ##
101
109
  # Executes a SQL query.
102
110
  #
@@ -207,6 +215,13 @@ module Google
207
215
  # Useful for reading the freshest data available at a nearby
208
216
  # replica, while bounding the possible staleness if the local
209
217
  # replica has fallen behind.
218
+ # @param [Hash] query_options A hash of values to specify the custom
219
+ # query options for executing SQL query. Query options are optional.
220
+ # The following settings can be provided:
221
+ #
222
+ # * `:optimizer_version` (String) The version of optimizer to use.
223
+ # Empty to use database default. "latest" to use the latest
224
+ # available optimizer version.
210
225
  #
211
226
  # @return [Google::Cloud::Spanner::Results] The results of the query
212
227
  # execution.
@@ -306,7 +321,22 @@ module Google
306
321
  # puts "User #{row[:id]} is #{row[:name]}"
307
322
  # end
308
323
  #
309
- def execute_query sql, params: nil, types: nil, single_use: nil
324
+ # @example Query using query options:
325
+ # require "google/cloud/spanner"
326
+ #
327
+ # spanner = Google::Cloud::Spanner.new
328
+ #
329
+ # db = spanner.client "my-instance", "my-database"
330
+ #
331
+ # results = db.execute_query \
332
+ # "SELECT * FROM users", query_options: { optimizer_version: "1" }
333
+ #
334
+ # results.rows.each do |row|
335
+ # puts "User #{row[:id]} is #{row[:name]}"
336
+ # end
337
+ #
338
+ def execute_query sql, params: nil, types: nil, single_use: nil,
339
+ query_options: nil
310
340
  validate_single_use_args! single_use
311
341
  ensure_service!
312
342
 
@@ -316,7 +346,8 @@ module Google
316
346
  results = nil
317
347
  @pool.with_session do |session|
318
348
  results = session.execute_query \
319
- sql, params: params, types: types, transaction: single_use_tx
349
+ sql, params: params, types: types, transaction: single_use_tx,
350
+ query_options: query_options
320
351
  end
321
352
  results
322
353
  end
@@ -443,6 +474,14 @@ module Google
443
474
  # value in `params`. In these cases, the `types` hash can be used to
444
475
  # specify the exact SQL type for some or all of the SQL query
445
476
  # parameters.
477
+ # @param [Hash] query_options A hash of values to specify the custom
478
+ # query options for executing SQL query. Query options are optional.
479
+ # The following settings can be provided:
480
+ #
481
+ # * `:optimizer_version` (String) The version of optimizer to use.
482
+ # Empty to use database default. "latest" to use the latest
483
+ # available optimizer version.
484
+ #
446
485
  #
447
486
  # The keys of the hash should be query string parameter placeholders,
448
487
  # minus the "@". The values of the hash should be Cloud Spanner type
@@ -481,16 +520,26 @@ module Google
481
520
  # "UPDATE users SET friends = NULL WHERE active = @active",
482
521
  # params: { active: false }
483
522
  #
484
- def execute_partition_update sql, params: nil, types: nil
523
+ # @example Query using query options:
524
+ # require "google/cloud/spanner"
525
+ #
526
+ # spanner = Google::Cloud::Spanner.new
527
+ # db = spanner.client "my-instance", "my-database"
528
+ #
529
+ # row_count = db.execute_partition_update \
530
+ # "UPDATE users SET friends = NULL WHERE active = false",
531
+ # query_options: { optimizer_version: "1" }
532
+ def execute_partition_update sql, params: nil, types: nil,
533
+ query_options: nil
485
534
  ensure_service!
486
535
 
487
536
  params, types = Convert.to_input_params_and_types params, types
488
-
489
537
  results = nil
490
538
  @pool.with_session do |session|
491
539
  results = session.execute_query \
492
540
  sql, params: params, types: types,
493
- transaction: pdml_transaction(session)
541
+ transaction: pdml_transaction(session),
542
+ query_options: query_options
494
543
  end
495
544
  # Stream all PartialResultSet to get ResultSetStats
496
545
  results.rows.to_a
@@ -1291,7 +1340,7 @@ module Google
1291
1340
  project_id, instance_id, database_id
1292
1341
  ),
1293
1342
  labels: @session_labels
1294
- Session.from_grpc grpc, @project.service
1343
+ Session.from_grpc grpc, @project.service, query_options: @query_options
1295
1344
  end
1296
1345
 
1297
1346
  ##
@@ -1320,7 +1369,7 @@ module Google
1320
1369
  ),
1321
1370
  session_count,
1322
1371
  labels: @session_labels
1323
- resp.session.map { |grpc| Session.from_grpc grpc, @project.service }
1372
+ resp.session.map { |grpc| Session.from_grpc grpc, @project.service, query_options: @query_options }
1324
1373
  end
1325
1374
 
1326
1375
  # @private
@@ -68,12 +68,13 @@ module Google
68
68
  class Project
69
69
  ##
70
70
  # @private The Service object.
71
- attr_accessor :service
71
+ attr_accessor :service, :query_options
72
72
 
73
73
  ##
74
74
  # @private Creates a new Spanner Project instance.
75
- def initialize service
75
+ def initialize service, query_options: nil
76
76
  @service = service
77
+ @query_options = query_options
77
78
  end
78
79
 
79
80
  ##
@@ -452,6 +453,13 @@ module Google
452
453
  # * Label values must be between 0 and 63 characters long and must
453
454
  # conform to the regular expression `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
454
455
  # * No more than 64 labels can be associated with a given resource.
456
+ # @param [Hash] query_options A hash of values to specify the custom
457
+ # query options for executing SQL query. Query options are optional.
458
+ # The following settings can be provided:
459
+ #
460
+ # * `:optimizer_version` (String) The version of optimizer to use.
461
+ # Empty to use database default. "latest" to use the latest
462
+ # available optimizer version.
455
463
  #
456
464
  # @return [Client] The newly created client.
457
465
  #
@@ -470,12 +478,20 @@ module Google
470
478
  # end
471
479
  # end
472
480
  #
473
- def client instance_id, database_id, pool: {}, labels: nil
481
+ def client instance_id, database_id, pool: {}, labels: nil,
482
+ query_options: nil
474
483
  # Convert from possible Google::Protobuf::Map
475
484
  labels = Hash[labels.map { |k, v| [String(k), String(v)] }] if labels
485
+ # Configs set by environment variables take over client-level configs.
486
+ if query_options.nil?
487
+ query_options = @query_options
488
+ else
489
+ query_options = query_options.merge @query_options unless @query_options.nil?
490
+ end
476
491
  Client.new self, instance_id, database_id,
477
492
  session_labels: labels,
478
- pool_opts: valid_session_pool_options(pool)
493
+ pool_opts: valid_session_pool_options(pool),
494
+ query_options: query_options
479
495
  end
480
496
 
481
497
  ##
@@ -501,6 +517,13 @@ module Google
501
517
  # * Label values must be between 0 and 63 characters long and must
502
518
  # conform to the regular expression `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
503
519
  # * No more than 64 labels can be associated with a given resource.
520
+ # @param [Hash] query_options A hash of values to specify the custom
521
+ # query options for executing SQL query. Query options are optional.
522
+ # The following settings can be provided:
523
+ #
524
+ # * `:optimizer_version` (String) The version of optimizer to use.
525
+ # Empty to use database default. "latest" to use the latest
526
+ # available optimizer version.
504
527
  #
505
528
  # @return [Client] The newly created client.
506
529
  #
@@ -529,10 +552,12 @@ module Google
529
552
  # results = new_batch_snapshot.execute_partition \
530
553
  # new_partition
531
554
  #
532
- def batch_client instance_id, database_id, labels: nil
555
+ def batch_client instance_id, database_id, labels: nil,
556
+ query_options: nil
533
557
  # Convert from possible Google::Protobuf::Map
534
558
  labels = Hash[labels.map { |k, v| [String(k), String(v)] }] if labels
535
- BatchClient.new self, instance_id, database_id, session_labels: labels
559
+ BatchClient.new self, instance_id, database_id, session_labels: labels,
560
+ query_options: query_options
536
561
  end
537
562
 
538
563
  protected
@@ -242,10 +242,12 @@ module Google
242
242
 
243
243
  def self.execute_query service, session_path, sql, params: nil,
244
244
  types: nil, transaction: nil,
245
- partition_token: nil, seqno: nil
245
+ partition_token: nil, seqno: nil,
246
+ query_options: nil
246
247
  execute_query_options = {
247
248
  transaction: transaction, params: params, types: types,
248
- partition_token: partition_token, seqno: seqno
249
+ partition_token: partition_token, seqno: seqno,
250
+ query_options: query_options
249
251
  }
250
252
  enum = service.execute_streaming_sql session_path, sql,
251
253
  execute_query_options
@@ -303,7 +303,8 @@ module Google
303
303
 
304
304
  def execute_streaming_sql session_name, sql, transaction: nil,
305
305
  params: nil, types: nil, resume_token: nil,
306
- partition_token: nil, seqno: nil
306
+ partition_token: nil, seqno: nil,
307
+ query_options: nil
307
308
  opts = default_options_from_session session_name
308
309
  execute do
309
310
  service.execute_streaming_sql \
@@ -313,6 +314,7 @@ module Google
313
314
  resume_token: resume_token,
314
315
  partition_token: partition_token,
315
316
  seqno: seqno,
317
+ query_options: query_options,
316
318
  options: opts
317
319
  end
318
320
  end
@@ -49,10 +49,15 @@ module Google
49
49
  # @private The gRPC Service object.
50
50
  attr_accessor :service
51
51
 
52
+ ##
53
+ # @private The hash of query options.
54
+ attr_accessor :query_options
55
+
52
56
  # @private Creates a new Session instance.
53
- def initialize grpc, service
57
+ def initialize grpc, service, query_options: nil
54
58
  @grpc = grpc
55
59
  @service = service
60
+ @query_options = query_options
56
61
  end
57
62
 
58
63
  # The unique identifier for the project.
@@ -150,6 +155,13 @@ module Google
150
155
  # transactions.
151
156
  # @param [Integer] seqno A per-transaction sequence number used to
152
157
  # identify this request.
158
+ # @param [Hash] query_options A hash of values to specify the custom
159
+ # query options for executing SQL query. Query options are optional.
160
+ # The following settings can be provided:
161
+ #
162
+ # * `:optimizer_version` (String) The version of optimizer to use.
163
+ # Empty to use database default. "latest" to use the latest
164
+ # available optimizer version.
153
165
  #
154
166
  # @return [Google::Cloud::Spanner::Results] The results of the query
155
167
  # execution.
@@ -249,16 +261,35 @@ module Google
249
261
  # puts "User #{row[:id]} is #{row[:name]}"
250
262
  # end
251
263
  #
264
+ # @example Query using query options:
265
+ # require "google/cloud/spanner"
266
+ #
267
+ # spanner = Google::Cloud::Spanner.new
268
+ #
269
+ # db = spanner.client "my-instance", "my-database"
270
+ #
271
+ # results = db.execute_query \
272
+ # "SELECT * FROM users", query_options: { optimizer_version: "1" }
273
+ #
274
+ # results.rows.each do |row|
275
+ # puts "User #{row[:id]} is #{row[:name]}"
276
+ # end
277
+ #
252
278
  def execute_query sql, params: nil, types: nil, transaction: nil,
253
- partition_token: nil, seqno: nil
279
+ partition_token: nil, seqno: nil, query_options: nil
254
280
  ensure_service!
255
-
281
+ if query_options.nil?
282
+ query_options = @query_options
283
+ else
284
+ query_options = @query_options.merge query_options unless @query_options.nil?
285
+ end
256
286
  results = Results.execute_query service, path, sql,
257
287
  params: params,
258
288
  types: types,
259
289
  transaction: transaction,
260
290
  partition_token: partition_token,
261
- seqno: seqno
291
+ seqno: seqno,
292
+ query_options: query_options
262
293
  @last_updated_at = Time.now
263
294
  results
264
295
  end
@@ -688,8 +719,8 @@ module Google
688
719
  ##
689
720
  # @private Creates a new Session instance from a
690
721
  # Google::Spanner::V1::Session.
691
- def self.from_grpc grpc, service
692
- new grpc, service
722
+ def self.from_grpc grpc, service, query_options: nil
723
+ new grpc, service, query_options: query_options
693
724
  end
694
725
 
695
726
  ##