google-cloud-datastore 2.12.0 → 2.13.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8eddd9d4fceb6e041d7500b67922f0501aac4fef8bf151739a76cd5b7d244976
4
- data.tar.gz: e16e145cca01e0cb0dd2eef3c48243592444bade8f9d7da6cb2836a7ef161957
3
+ metadata.gz: 00f4895cedca98c329cdcccad6094e454ba6884afc5f18b2b765660c5df92da6
4
+ data.tar.gz: 500ab2a00f401e8a69da2e4edb838072da922a149340fb56ecd148bf5aabfa1a
5
5
  SHA512:
6
- metadata.gz: b93ac90c3b8f2b2abcd70ae70be0b9a2ed2b162c425cbac57ab125a1569c9b67bd9d6f045c9a59cf8bf483fe39f565721d210193ee4963217107ede2ae712c69
7
- data.tar.gz: 575f1f885078facd39cf006a5a218402f68add5e602bca839bd6cde28c8c4a068b46501a2371c1b89a3b2e0653d4ad522a56f5b23731087a06cd8179974c40a0
6
+ metadata.gz: 43730b11a7bbd83b69f2b495140fa752d8fee8deb6a729435fc99a7ff9e7d88f81628f209f7a852985d91b77cb3b9ca689edd09595ac9f71a49661bd57e0caef
7
+ data.tar.gz: 5a6d3307d349f0fcfeb115d73ead84a76f053f65b64b4398e943ee2d641a2a6e6dcb58c715bbee563fa48bb2f20e83b6fe71a78125019eac57c017eb5795ae8a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Release History
2
2
 
3
+ ### 2.13.0 (2025-07-24)
4
+
5
+ #### Features
6
+
7
+ * add explain query features to aggregate query in Cloud Datastore ([#30704](https://github.com/googleapis/google-cloud-ruby/issues/30704))
8
+
3
9
  ### 2.12.0 (2025-07-15)
4
10
 
5
11
  #### Features
@@ -57,9 +57,24 @@ module Google
57
57
  # @return [Google::Protobuf::Timestamp]
58
58
  attr_reader :read_time
59
59
 
60
+ # Query explain metrics. This is only present when the `explain_options`
61
+ # are provided to {Google::Cloud::Datastore::Dataset#run_aggregation}.
62
+ # It is sent only once with the response.
63
+ #
64
+ # @return [Google::Cloud::Datastore::V1::ExplainMetrics, nil]
65
+ attr_reader :explain_metrics
66
+
67
+ ##
68
+ # The options for query explanation.
69
+ #
70
+ # This is a copy of the input parameter supplied to the {Dataset#run_aggregation} function.
71
+ #
72
+ # @return [Google::Cloud::Datastore::V1::ExplainOptions, nil]
73
+ attr_reader :explain_options
74
+
60
75
  ##
61
76
  # @private
62
- attr_writer :aggregate_fields, :read_time
77
+ attr_writer :aggregate_fields, :read_time, :explain_metrics, :explain_options
63
78
 
64
79
  ##
65
80
  # Retrieves the aggregate data.
@@ -113,22 +128,33 @@ module Google
113
128
  ##
114
129
  # @private New AggregateQueryResults from a
115
130
  # Google::Cloud::Datastore::V1::RunAggregationQueryResponse object.
116
- def self.from_grpc aggregate_query_response
117
- aggregate_fields = aggregate_query_response
118
- .batch
119
- .aggregation_results[0]
120
- .aggregate_properties
121
- .map do |aggregate_alias, value|
122
- if value.has_integer_value?
123
- [aggregate_alias, value.integer_value]
124
- else
125
- [aggregate_alias, value.double_value]
126
- end
127
- end
131
+ def self.from_grpc aggregate_query_response, explain_options
132
+ # If the aggregate query is run with explain_options and analyze = false,
133
+ # the RunAggregationQueryResponse will not have batch results
134
+ # only explain metrics.
135
+ aggregate_fields = {}
136
+ read_time = nil
137
+
138
+ if aggregate_query_response.batch
139
+ aggregate_fields = aggregate_query_response
140
+ .batch
141
+ .aggregation_results[0]
142
+ .aggregate_properties
143
+ .map do |aggregate_alias, value|
144
+ if value.has_integer_value?
145
+ [aggregate_alias, value.integer_value]
146
+ else
147
+ [aggregate_alias, value.double_value]
148
+ end
149
+ end
150
+ read_time = aggregate_query_response.batch.read_time
151
+ end
128
152
 
129
153
  new.tap do |aq_result|
154
+ aq_result.explain_options = explain_options
155
+ aq_result.explain_metrics = aggregate_query_response.explain_metrics
130
156
  aq_result.aggregate_fields = aggregate_fields.to_h
131
- aq_result.read_time = aggregate_query_response.batch.read_time
157
+ aq_result.read_time = read_time
132
158
  end
133
159
  end
134
160
  end
@@ -549,6 +549,10 @@ module Google
549
549
  # [Eventual Consistency in Google Cloud
550
550
  # Datastore](https://cloud.google.com/datastore/docs/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore/#h.tf76fya5nqk8)
551
551
  # for more information.
552
+ # @param [Hash, Google::Cloud::Datastore::V1::ExplainOptions] explain_options The options for query explanation.
553
+ # Provide this argument to enable explain metrics. If this argument is left unset,
554
+ # the results will not include explain metrics.
555
+ # See {Google::Cloud::Datastore::V1::ExplainOptions} for details. Optional.
552
556
  #
553
557
  # @return [Google::Cloud::Datastore::Dataset::AggregateQueryResults]
554
558
  #
@@ -607,15 +611,32 @@ module Google
607
611
  # done: false
608
612
  # res = datastore.run_aggregation gql_query, namespace: "example-ns"
609
613
  #
610
- def run_aggregation aggregate_query, namespace: nil, consistency: nil, read_time: nil
614
+ # @example Run the aggregate query with explain options to get query plan and execution statistics.
615
+ # require "google/cloud/datastore"
616
+ #
617
+ # datastore = Google::Cloud::Datastore.new
618
+ #
619
+ # query = datastore.query("Task")
620
+ # aggregate_query = query.aggregate_query.add_count aggregate_alias: "total"
621
+ # results = datastore.run_aggregation aggregate_query, explain_options: { analyze: true }
622
+ #
623
+ # if results.explain_metrics
624
+ # stats = results.explain_metrics.execution_stats
625
+ # puts "Read operations: #{stats.read_operations}"
626
+ # end
627
+ #
628
+ def run_aggregation aggregate_query, namespace: nil, consistency: nil, read_time: nil, explain_options: nil
611
629
  ensure_service!
612
630
  unless aggregate_query.is_a?(AggregateQuery) || aggregate_query.is_a?(GqlQuery)
613
631
  raise ArgumentError, "Cannot run a #{aggregate_query.class} object."
614
632
  end
615
633
  check_consistency! consistency
616
634
  aggregate_query_res = service.run_aggregation_query aggregate_query.to_grpc, namespace,
617
- consistency: consistency, read_time: read_time
618
- AggregateQueryResults.from_grpc aggregate_query_res
635
+ consistency: consistency,
636
+ read_time: read_time,
637
+ explain_options: explain_options
638
+
639
+ AggregateQueryResults.from_grpc aggregate_query_res, explain_options
619
640
  end
620
641
 
621
642
  ##
@@ -236,14 +236,34 @@ module Google
236
236
  # .add_count
237
237
  # res = tx.run_aggregation aggregate_query
238
238
  # end
239
+ # @example Run the aggregate query with explain options:
240
+ # require "google/cloud/datastore"
241
+ #
242
+ # datastore = Google::Cloud::Datastore.new
243
+ #
244
+ # datastore.read_only_transaction do |tx|
245
+ # query = tx.query("Task")
246
+ # aggregate_query = query.aggregate_query.add_count aggregate_alias: "total"
247
+ # results = tx.run_aggregation aggregate_query, explain_options: { analyze: true }
248
+ #
249
+ # if results.explain_metrics
250
+ # stats = results.explain_metrics.execution_stats
251
+ # puts "Read operations: #{stats.read_operations}"
252
+ # end
253
+ # end
239
254
  #
240
- def run_aggregation aggregate_query, namespace: nil
255
+ def run_aggregation aggregate_query, namespace: nil, explain_options: nil
241
256
  ensure_service!
242
257
  unless aggregate_query.is_a?(AggregateQuery) || aggregate_query.is_a?(GqlQuery)
243
258
  raise ArgumentError, "Cannot run a #{aggregate_query.class} object."
244
259
  end
245
- aggregate_query_results = service.run_aggregation_query aggregate_query.to_grpc, namespace, transaction: @id
246
- Dataset::AggregateQueryResults.from_grpc aggregate_query_results
260
+
261
+ aggregate_query_results = service.run_aggregation_query aggregate_query.to_grpc,
262
+ namespace,
263
+ transaction: @id,
264
+ explain_options: explain_options
265
+
266
+ Dataset::AggregateQueryResults.from_grpc aggregate_query_results, explain_options
247
267
  end
248
268
 
249
269
  ##
@@ -98,7 +98,15 @@ module Google
98
98
  end
99
99
 
100
100
  ## Query for aggregates
101
- def run_aggregation_query query, namespace = nil, consistency: nil, transaction: nil, read_time: nil
101
+ def run_aggregation_query query, namespace = nil, consistency: nil, transaction: nil, read_time: nil,
102
+ explain_options: nil
103
+ if explain_options
104
+ explain_options = ::Gapic::Protobuf.coerce(
105
+ explain_options,
106
+ to: ::Google::Cloud::Datastore::V1::ExplainOptions
107
+ )
108
+ end
109
+
102
110
  gql_query = nil
103
111
  if query.is_a? Google::Cloud::Datastore::V1::GqlQuery
104
112
  gql_query = query
@@ -115,7 +123,8 @@ module Google
115
123
  partition_id: partition_id,
116
124
  read_options: read_options,
117
125
  aggregation_query: query,
118
- gql_query: gql_query
126
+ gql_query: gql_query,
127
+ explain_options: explain_options
119
128
  end
120
129
 
121
130
  ##
@@ -310,6 +310,59 @@ module Google
310
310
  end
311
311
  alias run_query run
312
312
 
313
+ ##
314
+ # Retrieve aggregate query results specified by an AggregateQuery. The query is run within the
315
+ # transaction.
316
+ #
317
+ # @param [AggregateQuery, GqlQuery] aggregate_query The Query object
318
+ # with the search criteria.
319
+ # @param [String] namespace The namespace the query is to run within.
320
+ # @param [Hash, Google::Cloud::Datastore::V1::ExplainOptions] explain_options
321
+ # The options for query explanation. See {Google::Cloud::Datastore::V1::ExplainOptions}
322
+ # for details. Optional.
323
+ #
324
+ # @return [Google::Cloud::Datastore::Dataset::AggregateQueryResults]
325
+ #
326
+ # @example
327
+ # require "google/cloud/datastore"
328
+ #
329
+ # datastore = Google::Cloud::Datastore.new
330
+ #
331
+ # datastore.transaction do |tx|
332
+ # query = tx.query("Task")
333
+ # .where("done", "=", false)
334
+ # aggregate_query = query.aggregate_query
335
+ # .add_count
336
+ # res = tx.run_aggregation aggregate_query
337
+ # end
338
+ #
339
+ # @example Run the aggregate query with explain options:
340
+ # require "google/cloud/datastore"
341
+ #
342
+ # datastore = Google::Cloud::Datastore.new
343
+ #
344
+ # datastore.transaction do |tx|
345
+ # query = tx.query("Task")
346
+ # aggregate_query = query.aggregate_query.add_count aggregate_alias: "total"
347
+ # results = tx.run_aggregation aggregate_query, explain_options: { analyze: true }
348
+ #
349
+ # if results.explain_metrics
350
+ # stats = results.explain_metrics.execution_stats
351
+ # puts "Read operations: #{stats.read_operations}"
352
+ # end
353
+ # end
354
+ #
355
+ def run_aggregation aggregate_query, namespace: nil, explain_options: nil
356
+ ensure_service!
357
+ unless aggregate_query.is_a?(AggregateQuery) || aggregate_query.is_a?(GqlQuery)
358
+ raise ArgumentError, "Cannot run a #{aggregate_query.class} object."
359
+ end
360
+ aggregate_query_results = service.run_aggregation_query aggregate_query.to_grpc, namespace,
361
+ transaction: @id,
362
+ explain_options: explain_options
363
+ Dataset::AggregateQueryResults.from_grpc aggregate_query_results, explain_options
364
+ end
365
+
313
366
  ##
314
367
  # Begins a transaction.
315
368
  # This method is run when a new Transaction is created.
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Datastore
19
- VERSION = "2.12.0".freeze
19
+ VERSION = "2.13.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-datastore
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.12.0
4
+ version: 2.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore