xref: /aosp_15_r20/external/googleapis/google/datastore/v1/query.proto (revision d5c09012810ac0c9f33fe448fb6da8260d444cc9)
1// Copyright 2023 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15syntax = "proto3";
16
17package google.datastore.v1;
18
19import "google/api/field_behavior.proto";
20import "google/datastore/v1/entity.proto";
21import "google/protobuf/timestamp.proto";
22import "google/protobuf/wrappers.proto";
23
24option csharp_namespace = "Google.Cloud.Datastore.V1";
25option go_package = "google.golang.org/genproto/googleapis/datastore/v1;datastore";
26option java_multiple_files = true;
27option java_outer_classname = "QueryProto";
28option java_package = "com.google.datastore.v1";
29option php_namespace = "Google\\Cloud\\Datastore\\V1";
30option ruby_package = "Google::Cloud::Datastore::V1";
31
32// The result of fetching an entity from Datastore.
33message EntityResult {
34  // Specifies what data the 'entity' field contains.
35  // A `ResultType` is either implied (for example, in `LookupResponse.missing`
36  // from `datastore.proto`, it is always `KEY_ONLY`) or specified by context
37  // (for example, in message `QueryResultBatch`, field `entity_result_type`
38  // specifies a `ResultType` for all the values in field `entity_results`).
39  enum ResultType {
40    // Unspecified. This value is never used.
41    RESULT_TYPE_UNSPECIFIED = 0;
42
43    // The key and properties.
44    FULL = 1;
45
46    // A projected subset of properties. The entity may have no key.
47    PROJECTION = 2;
48
49    // Only the key.
50    KEY_ONLY = 3;
51  }
52
53  // The resulting entity.
54  Entity entity = 1;
55
56  // The version of the entity, a strictly positive number that monotonically
57  // increases with changes to the entity.
58  //
59  // This field is set for
60  // [`FULL`][google.datastore.v1.EntityResult.ResultType.FULL] entity results.
61  //
62  // For [missing][google.datastore.v1.LookupResponse.missing] entities in
63  // `LookupResponse`, this is the version of the snapshot that was used to look
64  // up the entity, and it is always set except for eventually consistent reads.
65  int64 version = 4;
66
67  // The time at which the entity was created.
68  // This field is set for
69  // [`FULL`][google.datastore.v1.EntityResult.ResultType.FULL] entity results.
70  // If this entity is missing, this field will not be set.
71  google.protobuf.Timestamp create_time = 6;
72
73  // The time at which the entity was last changed.
74  // This field is set for
75  // [`FULL`][google.datastore.v1.EntityResult.ResultType.FULL] entity results.
76  // If this entity is missing, this field will not be set.
77  google.protobuf.Timestamp update_time = 5;
78
79  // A cursor that points to the position after the result entity.
80  // Set only when the `EntityResult` is part of a `QueryResultBatch` message.
81  bytes cursor = 3;
82}
83
84// A query for entities.
85message Query {
86  // The projection to return. Defaults to returning all properties.
87  repeated Projection projection = 2;
88
89  // The kinds to query (if empty, returns entities of all kinds).
90  // Currently at most 1 kind may be specified.
91  repeated KindExpression kind = 3;
92
93  // The filter to apply.
94  Filter filter = 4;
95
96  // The order to apply to the query results (if empty, order is unspecified).
97  repeated PropertyOrder order = 5;
98
99  // The properties to make distinct. The query results will contain the first
100  // result for each distinct combination of values for the given properties
101  // (if empty, all results are returned).
102  //
103  // Requires:
104  //
105  // * If `order` is specified, the set of distinct on properties must appear
106  // before the non-distinct on properties in `order`.
107  repeated PropertyReference distinct_on = 6;
108
109  // A starting point for the query results. Query cursors are
110  // returned in query result batches and
111  // [can only be used to continue the same
112  // query](https://cloud.google.com/datastore/docs/concepts/queries#cursors_limits_and_offsets).
113  bytes start_cursor = 7;
114
115  // An ending point for the query results. Query cursors are
116  // returned in query result batches and
117  // [can only be used to limit the same
118  // query](https://cloud.google.com/datastore/docs/concepts/queries#cursors_limits_and_offsets).
119  bytes end_cursor = 8;
120
121  // The number of results to skip. Applies before limit, but after all other
122  // constraints. Optional. Must be >= 0 if specified.
123  int32 offset = 10;
124
125  // The maximum number of results to return. Applies after all other
126  // constraints. Optional.
127  // Unspecified is interpreted as no limit.
128  // Must be >= 0 if specified.
129  google.protobuf.Int32Value limit = 12;
130}
131
132// Datastore query for running an aggregation over a
133// [Query][google.datastore.v1.Query].
134message AggregationQuery {
135  // Defines an aggregation that produces a single result.
136  message Aggregation {
137    // Count of entities that match the query.
138    //
139    // The `COUNT(*)` aggregation function operates on the entire entity
140    // so it does not require a field reference.
141    message Count {
142      // Optional. Optional constraint on the maximum number of entities to
143      // count.
144      //
145      // This provides a way to set an upper bound on the number of entities
146      // to scan, limiting latency, and cost.
147      //
148      // Unspecified is interpreted as no bound.
149      //
150      // If a zero value is provided, a count result of zero should always be
151      // expected.
152      //
153      // High-Level Example:
154      //
155      // ```
156      // AGGREGATE COUNT_UP_TO(1000) OVER ( SELECT * FROM k );
157      // ```
158      //
159      // Requires:
160      //
161      // * Must be non-negative when present.
162      google.protobuf.Int64Value up_to = 1
163          [(google.api.field_behavior) = OPTIONAL];
164    }
165
166    // Sum of the values of the requested property.
167    //
168    // * Only numeric values will be aggregated. All non-numeric values
169    // including `NULL` are skipped.
170    //
171    // * If the aggregated values contain `NaN`, returns `NaN`. Infinity math
172    // follows IEEE-754 standards.
173    //
174    // * If the aggregated value set is empty, returns 0.
175    //
176    // * Returns a 64-bit integer if all aggregated numbers are integers and the
177    // sum result does not overflow. Otherwise, the result is returned as a
178    // double. Note that even if all the aggregated values are integers, the
179    // result is returned as a double if it cannot fit within a 64-bit signed
180    // integer. When this occurs, the returned value will lose precision.
181    //
182    // * When underflow occurs, floating-point aggregation is non-deterministic.
183    // This means that running the same query repeatedly without any changes to
184    // the underlying values could produce slightly different results each
185    // time. In those cases, values should be stored as integers over
186    // floating-point numbers.
187    message Sum {
188      // The property to aggregate on.
189      PropertyReference property = 1;
190    }
191
192    // Average of the values of the requested property.
193    //
194    // * Only numeric values will be aggregated. All non-numeric values
195    // including `NULL` are skipped.
196    //
197    // * If the aggregated values contain `NaN`, returns `NaN`. Infinity math
198    // follows IEEE-754 standards.
199    //
200    // * If the aggregated value set is empty, returns `NULL`.
201    //
202    // * Always returns the result as a double.
203    message Avg {
204      // The property to aggregate on.
205      PropertyReference property = 1;
206    }
207
208    // The type of aggregation to perform, required.
209    oneof operator {
210      // Count aggregator.
211      Count count = 1;
212
213      // Sum aggregator.
214      Sum sum = 2;
215
216      // Average aggregator.
217      Avg avg = 3;
218    }
219
220    // Optional. Optional name of the property to store the result of the
221    // aggregation.
222    //
223    // If not provided, Datastore will pick a default name following the format
224    // `property_<incremental_id++>`. For example:
225    //
226    // ```
227    // AGGREGATE
228    //   COUNT_UP_TO(1) AS count_up_to_1,
229    //   COUNT_UP_TO(2),
230    //   COUNT_UP_TO(3) AS count_up_to_3,
231    //   COUNT(*)
232    // OVER (
233    //   ...
234    // );
235    // ```
236    //
237    // becomes:
238    //
239    // ```
240    // AGGREGATE
241    //   COUNT_UP_TO(1) AS count_up_to_1,
242    //   COUNT_UP_TO(2) AS property_1,
243    //   COUNT_UP_TO(3) AS count_up_to_3,
244    //   COUNT(*) AS property_2
245    // OVER (
246    //   ...
247    // );
248    // ```
249    //
250    // Requires:
251    //
252    // * Must be unique across all aggregation aliases.
253    // * Conform to [entity property
254    // name][google.datastore.v1.Entity.properties] limitations.
255    string alias = 7 [(google.api.field_behavior) = OPTIONAL];
256  }
257
258  // The base query to aggregate over.
259  oneof query_type {
260    // Nested query for aggregation
261    Query nested_query = 1;
262  }
263
264  // Optional. Series of aggregations to apply over the results of the
265  // `nested_query`.
266  //
267  // Requires:
268  //
269  // * A minimum of one and maximum of five aggregations per query.
270  repeated Aggregation aggregations = 3
271      [(google.api.field_behavior) = OPTIONAL];
272}
273
274// A representation of a kind.
275message KindExpression {
276  // The name of the kind.
277  string name = 1;
278}
279
280// A reference to a property relative to the kind expressions.
281message PropertyReference {
282  // A reference to a property.
283  //
284  // Requires:
285  //
286  // * MUST be a dot-delimited (`.`) string of segments, where each segment
287  // conforms to [entity property name][google.datastore.v1.Entity.properties]
288  // limitations.
289  string name = 2;
290}
291
292// A representation of a property in a projection.
293message Projection {
294  // The property to project.
295  PropertyReference property = 1;
296}
297
298// The desired order for a specific property.
299message PropertyOrder {
300  // The sort direction.
301  enum Direction {
302    // Unspecified. This value must not be used.
303    DIRECTION_UNSPECIFIED = 0;
304
305    // Ascending.
306    ASCENDING = 1;
307
308    // Descending.
309    DESCENDING = 2;
310  }
311
312  // The property to order by.
313  PropertyReference property = 1;
314
315  // The direction to order by. Defaults to `ASCENDING`.
316  Direction direction = 2;
317}
318
319// A holder for any type of filter.
320message Filter {
321  // The type of filter.
322  oneof filter_type {
323    // A composite filter.
324    CompositeFilter composite_filter = 1;
325
326    // A filter on a property.
327    PropertyFilter property_filter = 2;
328  }
329}
330
331// A filter that merges multiple other filters using the given operator.
332message CompositeFilter {
333  // A composite filter operator.
334  enum Operator {
335    // Unspecified. This value must not be used.
336    OPERATOR_UNSPECIFIED = 0;
337
338    // The results are required to satisfy each of the combined filters.
339    AND = 1;
340
341    // Documents are required to satisfy at least one of the combined filters.
342    OR = 2;
343  }
344
345  // The operator for combining multiple filters.
346  Operator op = 1;
347
348  // The list of filters to combine.
349  //
350  // Requires:
351  //
352  // * At least one filter is present.
353  repeated Filter filters = 2;
354}
355
356// A filter on a specific property.
357message PropertyFilter {
358  // A property filter operator.
359  enum Operator {
360    // Unspecified. This value must not be used.
361    OPERATOR_UNSPECIFIED = 0;
362
363    // The given `property` is less than the given `value`.
364    //
365    // Requires:
366    //
367    // * That `property` comes first in `order_by`.
368    LESS_THAN = 1;
369
370    // The given `property` is less than or equal to the given `value`.
371    //
372    // Requires:
373    //
374    // * That `property` comes first in `order_by`.
375    LESS_THAN_OR_EQUAL = 2;
376
377    // The given `property` is greater than the given `value`.
378    //
379    // Requires:
380    //
381    // * That `property` comes first in `order_by`.
382    GREATER_THAN = 3;
383
384    // The given `property` is greater than or equal to the given `value`.
385    //
386    // Requires:
387    //
388    // * That `property` comes first in `order_by`.
389    GREATER_THAN_OR_EQUAL = 4;
390
391    // The given `property` is equal to the given `value`.
392    EQUAL = 5;
393
394    // The given `property` is equal to at least one value in the given array.
395    //
396    // Requires:
397    //
398    // * That `value` is a non-empty `ArrayValue`, subject to disjunction
399    //   limits.
400    // * No `NOT_IN` is in the same query.
401    IN = 6;
402
403    // The given `property` is not equal to the given `value`.
404    //
405    // Requires:
406    //
407    // * No other `NOT_EQUAL` or `NOT_IN` is in the same query.
408    // * That `property` comes first in the `order_by`.
409    NOT_EQUAL = 9;
410
411    // Limit the result set to the given entity and its descendants.
412    //
413    // Requires:
414    //
415    // * That `value` is an entity key.
416    // * All evaluated disjunctions must have the same `HAS_ANCESTOR` filter.
417    HAS_ANCESTOR = 11;
418
419    // The value of the `property` is not in the given array.
420    //
421    // Requires:
422    //
423    // * That `value` is a non-empty `ArrayValue` with at most 10 values.
424    // * No other `OR`, `IN`, `NOT_IN`, `NOT_EQUAL` is in the same query.
425    // * That `field` comes first in the `order_by`.
426    NOT_IN = 13;
427  }
428
429  // The property to filter by.
430  PropertyReference property = 1;
431
432  // The operator to filter by.
433  Operator op = 2;
434
435  // The value to compare the property to.
436  Value value = 3;
437}
438
439// A [GQL
440// query](https://cloud.google.com/datastore/docs/apis/gql/gql_reference).
441message GqlQuery {
442  // A string of the format described
443  // [here](https://cloud.google.com/datastore/docs/apis/gql/gql_reference).
444  string query_string = 1;
445
446  // When false, the query string must not contain any literals and instead must
447  // bind all values. For example,
448  // `SELECT * FROM Kind WHERE a = 'string literal'` is not allowed, while
449  // `SELECT * FROM Kind WHERE a = @value` is.
450  bool allow_literals = 2;
451
452  // For each non-reserved named binding site in the query string, there must be
453  // a named parameter with that name, but not necessarily the inverse.
454  //
455  // Key must match regex `[A-Za-z_$][A-Za-z_$0-9]*`, must not match regex
456  // `__.*__`, and must not be `""`.
457  map<string, GqlQueryParameter> named_bindings = 5;
458
459  // Numbered binding site @1 references the first numbered parameter,
460  // effectively using 1-based indexing, rather than the usual 0.
461  //
462  // For each binding site numbered i in `query_string`, there must be an i-th
463  // numbered parameter. The inverse must also be true.
464  repeated GqlQueryParameter positional_bindings = 4;
465}
466
467// A binding parameter for a GQL query.
468message GqlQueryParameter {
469  // The type of parameter.
470  oneof parameter_type {
471    // A value parameter.
472    Value value = 2;
473
474    // A query cursor. Query cursors are returned in query
475    // result batches.
476    bytes cursor = 3;
477  }
478}
479
480// A batch of results produced by a query.
481message QueryResultBatch {
482  // The possible values for the `more_results` field.
483  enum MoreResultsType {
484    // Unspecified. This value is never used.
485    MORE_RESULTS_TYPE_UNSPECIFIED = 0;
486
487    // There may be additional batches to fetch from this query.
488    NOT_FINISHED = 1;
489
490    // The query is finished, but there may be more results after the limit.
491    MORE_RESULTS_AFTER_LIMIT = 2;
492
493    // The query is finished, but there may be more results after the end
494    // cursor.
495    MORE_RESULTS_AFTER_CURSOR = 4;
496
497    // The query is finished, and there are no more results.
498    NO_MORE_RESULTS = 3;
499  }
500
501  // The number of results skipped, typically because of an offset.
502  int32 skipped_results = 6;
503
504  // A cursor that points to the position after the last skipped result.
505  // Will be set when `skipped_results` != 0.
506  bytes skipped_cursor = 3;
507
508  // The result type for every entity in `entity_results`.
509  EntityResult.ResultType entity_result_type = 1;
510
511  // The results for this batch.
512  repeated EntityResult entity_results = 2;
513
514  // A cursor that points to the position after the last result in the batch.
515  bytes end_cursor = 4;
516
517  // The state of the query after the current batch.
518  MoreResultsType more_results = 5;
519
520  // The version number of the snapshot this batch was returned from.
521  // This applies to the range of results from the query's `start_cursor` (or
522  // the beginning of the query if no cursor was given) to this batch's
523  // `end_cursor` (not the query's `end_cursor`).
524  //
525  // In a single transaction, subsequent query result batches for the same query
526  // can have a greater snapshot version number. Each batch's snapshot version
527  // is valid for all preceding batches.
528  // The value will be zero for eventually consistent queries.
529  int64 snapshot_version = 7;
530
531  // Read timestamp this batch was returned from.
532  // This applies to the range of results from the query's `start_cursor` (or
533  // the beginning of the query if no cursor was given) to this batch's
534  // `end_cursor` (not the query's `end_cursor`).
535  //
536  // In a single transaction, subsequent query result batches for the same query
537  // can have a greater timestamp. Each batch's read timestamp
538  // is valid for all preceding batches.
539  // This value will not be set for eventually consistent queries in Cloud
540  // Datastore.
541  google.protobuf.Timestamp read_time = 8;
542}
543