google-cloud-datastore 2.7.1 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 646ed90d461784d6870d425a6f98271a249338a72696200b3b2707d3e4725098
4
- data.tar.gz: 963afff082dd55de2dce0f58715bd76c17497e118b0db92b7a542b6dd813b00d
3
+ metadata.gz: 6c062edd7866b2d8f56365618cdbfca915d1df66440e9bdfca54d4e85bbc0133
4
+ data.tar.gz: c4085c6ba18cd98c6ec7f38b6487dfbab56dd00030eb3dcad70afa184772056e
5
5
  SHA512:
6
- metadata.gz: 832baf39842dd1261ee5394284a0cc8448bd65fbf08162cc49a95f16654676f4dae76d4cb80be58a2692f7035f613b3b1668bf6e5f7d73f175a3f3ad7d6e3eb0
7
- data.tar.gz: dd2b0d16cf4727a342e305a6c3dee82add4ce10f5faac8c691216e9e4a88d356d89cb49684cc71b92bfe162bc9aa099abca54c260698ca96722400a729a952e7
6
+ metadata.gz: 9621f21518ea67ebedcd0230f16b240976152d99133b2e0e4ecb7a770e92079e47a44868925cb7ac4aaeef8030a6138ef2842836749b4274c9a5ada151eeae28
7
+ data.tar.gz: 0dbf0d457be8a4b94e6be54a7eaae13ea31113d818025ed59905fc3c7bbd64c3c10bff2fbb36519f3a94cd17f0b901bfb9029d837e5a4c36f73282ef39012997
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Release History
2
2
 
3
+ ### 2.8.0 (2023-09-12)
4
+
5
+ #### Features
6
+
7
+ * Support sum & avg aggregate functions ([#22884](https://github.com/googleapis/google-cloud-ruby/issues/22884))
8
+
3
9
  ### 2.7.1 (2023-05-18)
4
10
 
5
11
  #### Documentation
@@ -56,6 +56,15 @@ module Google
56
56
  # puts aggregate_query_results.get('total')
57
57
  #
58
58
  class AggregateQuery
59
+ # @private
60
+ DEFAULT_COUNT_AGGREGATE_ALIAS = "count".freeze
61
+
62
+ # @private
63
+ DEFAULT_SUM_AGGREGATE_ALIAS = "sum".freeze
64
+
65
+ # @private
66
+ DEFAULT_AVG_AGGREGATE_ALIAS = "avg".freeze
67
+
59
68
  ##
60
69
  # @private The Google::Cloud::Datastore::V1::AggregationQuery object.
61
70
  attr_reader :grpc
@@ -113,7 +122,7 @@ module Google
113
122
  # puts aggregate_query_results.get('total')
114
123
  #
115
124
  def add_count aggregate_alias: nil
116
- aggregate_alias ||= ALIASES[:count]
125
+ aggregate_alias ||= DEFAULT_COUNT_AGGREGATE_ALIAS
117
126
  @grpc.aggregations << Google::Cloud::Datastore::V1::AggregationQuery::Aggregation.new(
118
127
  count: Google::Cloud::Datastore::V1::AggregationQuery::Aggregation::Count.new,
119
128
  alias: aggregate_alias
@@ -122,16 +131,118 @@ module Google
122
131
  self
123
132
  end
124
133
 
125
- # @private
126
- def to_grpc
127
- @grpc
134
+ ##
135
+ # Adds a sum aggregate.
136
+ #
137
+ # @param name [String] The property to sum by.
138
+ # @param aggregate_alias [String] Alias to refer to the aggregate. Optional
139
+ #
140
+ # @return [AggregateQuery] The modified aggregate query object with the added SUM aggregate.
141
+ #
142
+ # @example
143
+ # require "google/cloud/datastore"
144
+ #
145
+ # datastore = Google::Cloud::Datastore.new
146
+ #
147
+ # query = Google::Cloud::Datastore::Query.new
148
+ # query.kind("Task")
149
+ # .where("done", "=", false)
150
+ #
151
+ # Create an aggregate query
152
+ # aggregate_query = query.aggregate_query
153
+ # .add_sum("score")
154
+ #
155
+ # aggregate_query_results = dataset.run_aggregation aggregate_query
156
+ # puts aggregate_query_results.get
157
+ #
158
+ # @example Alias an aggregate SUM query
159
+ # require "google/cloud/datastore"
160
+ #
161
+ # datastore = Google::Cloud::Datastore.new
162
+ #
163
+ # query = Google::Cloud::Datastore::Query.new
164
+ # query.kind("Task")
165
+ # .where("done", "=", false)
166
+ #
167
+ # # Create an aggregate query
168
+ # aggregate_query = query.aggregate_query
169
+ # .add_sum("score", aggregate_alias: 'total_score')
170
+ #
171
+ # aggregate_query_results = dataset.run_aggregation aggregate_query
172
+ # puts aggregate_query_results.get('total_score')
173
+ #
174
+ def add_sum name, aggregate_alias: nil
175
+ aggregate_alias ||= DEFAULT_SUM_AGGREGATE_ALIAS
176
+ @grpc.aggregations << Google::Cloud::Datastore::V1::AggregationQuery::Aggregation.new(
177
+ sum: Google::Cloud::Datastore::V1::AggregationQuery::Aggregation::Sum.new(
178
+ property: Google::Cloud::Datastore::V1::PropertyReference.new(
179
+ name: name
180
+ )
181
+ ),
182
+ alias: aggregate_alias
183
+ )
184
+
185
+ self
128
186
  end
129
187
 
130
188
  ##
189
+ # Adds an average aggregate.
190
+ #
191
+ # @param name [String] The property to apply average on.
192
+ # @param aggregate_alias [String] Alias to refer to the aggregate. Optional
193
+ #
194
+ # @return [AggregateQuery] The modified aggregate query object with the added AVG aggregate.
195
+ #
196
+ # @example
197
+ # require "google/cloud/datastore"
198
+ #
199
+ # datastore = Google::Cloud::Datastore.new
200
+ #
201
+ # query = Google::Cloud::Datastore::Query.new
202
+ # query.kind("Task")
203
+ # .where("done", "=", false)
204
+ #
205
+ # Create an aggregate query
206
+ # aggregate_query = query.aggregate_query
207
+ # .add_avg("score")
208
+ #
209
+ # aggregate_query_results = dataset.run_aggregation aggregate_query
210
+ # puts aggregate_query_results.get
211
+ #
212
+ # @example Alias an aggregate AVG query
213
+ # require "google/cloud/datastore"
214
+ #
215
+ # datastore = Google::Cloud::Datastore.new
216
+ #
217
+ # query = Google::Cloud::Datastore::Query.new
218
+ # query.kind("Task")
219
+ # .where("done", "=", false)
220
+ #
221
+ # # Create an aggregate query
222
+ # aggregate_query = query.aggregate_query
223
+ # .add_avg("score", aggregate_alias: 'avg_score')
224
+ #
225
+ # aggregate_query_results = dataset.run_aggregation aggregate_query
226
+ # puts aggregate_query_results.get('avg_score')
227
+ #
228
+ def add_avg name, aggregate_alias: nil
229
+ aggregate_alias ||= DEFAULT_AVG_AGGREGATE_ALIAS
230
+ @grpc.aggregations << Google::Cloud::Datastore::V1::AggregationQuery::Aggregation.new(
231
+ avg: Google::Cloud::Datastore::V1::AggregationQuery::Aggregation::Avg.new(
232
+ property: Google::Cloud::Datastore::V1::PropertyReference.new(
233
+ name: name
234
+ )
235
+ ),
236
+ alias: aggregate_alias
237
+ )
238
+
239
+ self
240
+ end
241
+
131
242
  # @private
132
- ALIASES = {
133
- count: "count"
134
- }.freeze
243
+ def to_grpc
244
+ @grpc
245
+ end
135
246
  end
136
247
  end
137
248
  end
@@ -39,6 +39,17 @@ module Google
39
39
  # puts aggregate_query_results.get
40
40
  #
41
41
  class AggregateQueryResults
42
+ ##
43
+ # @private Object of type [Hash{String => Object}].
44
+ #
45
+ # String can have the following values:
46
+ # - an aggregate literal "sum", "avg", or "count"
47
+ # - a custom aggregate alias
48
+ # Object can have the following types:
49
+ # - Integer
50
+ # - Float
51
+ attr_reader :aggregate_fields
52
+
42
53
  ##
43
54
  # Read timestamp the query was done on the database at.
44
55
  #
@@ -52,7 +63,8 @@ module Google
52
63
  # the aggregate value. For an AggregateQuery with a
53
64
  # single aggregate field, this parameter can be omitted.
54
65
  #
55
- # @return [Integer] The aggregate value.
66
+ # @return [Integer, Float, nil] The aggregate value. Returns `nil`
67
+ # if the aggregate_alias does not exist.
56
68
  #
57
69
  # @example
58
70
  # require "google/cloud/datastore"
@@ -101,8 +113,15 @@ module Google
101
113
  .batch
102
114
  .aggregation_results[0]
103
115
  .aggregate_properties
116
+ .map do |aggregate_alias, value|
117
+ if value.has_integer_value?
118
+ [aggregate_alias, value.integer_value]
119
+ else
120
+ [aggregate_alias, value.double_value]
121
+ end
122
+ end
104
123
  .to_h
105
- .transform_values { |v| v[:integer_value] }
124
+
106
125
  new.tap do |s|
107
126
  s.instance_variable_set :@aggregate_fields, aggregate_fields
108
127
  s.instance_variable_set :@read_time, aggregate_query_response.batch.read_time
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Datastore
19
- VERSION = "2.7.1".freeze
19
+ VERSION = "2.8.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.7.1
4
+ version: 2.8.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: 2023-05-19 00:00:00.000000000 Z
12
+ date: 2023-09-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: google-cloud-core
@@ -238,7 +238,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
238
238
  - !ruby/object:Gem::Version
239
239
  version: '0'
240
240
  requirements: []
241
- rubygems_version: 3.4.2
241
+ rubygems_version: 3.4.19
242
242
  signing_key:
243
243
  specification_version: 4
244
244
  summary: API Client library for Google Cloud Datastore