query.proto 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // Copyright 2022 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. syntax = "proto3";
  15. package google.firestore.v1;
  16. import "google/api/field_behavior.proto";
  17. import "google/firestore/v1/document.proto";
  18. import "google/protobuf/wrappers.proto";
  19. option csharp_namespace = "Google.Cloud.Firestore.V1";
  20. option go_package = "google.golang.org/genproto/googleapis/firestore/v1;firestore";
  21. option java_multiple_files = true;
  22. option java_outer_classname = "QueryProto";
  23. option java_package = "com.google.firestore.v1";
  24. option objc_class_prefix = "GCFS";
  25. option php_namespace = "Google\\Cloud\\Firestore\\V1";
  26. option ruby_package = "Google::Cloud::Firestore::V1";
  27. // A Firestore query.
  28. message StructuredQuery {
  29. // A selection of a collection, such as `messages as m1`.
  30. message CollectionSelector {
  31. // The collection ID.
  32. // When set, selects only collections with this ID.
  33. string collection_id = 2;
  34. // When false, selects only collections that are immediate children of
  35. // the `parent` specified in the containing `RunQueryRequest`.
  36. // When true, selects all descendant collections.
  37. bool all_descendants = 3;
  38. }
  39. // A filter.
  40. message Filter {
  41. // The type of filter.
  42. oneof filter_type {
  43. // A composite filter.
  44. CompositeFilter composite_filter = 1;
  45. // A filter on a document field.
  46. FieldFilter field_filter = 2;
  47. // A filter that takes exactly one argument.
  48. UnaryFilter unary_filter = 3;
  49. }
  50. }
  51. // A filter that merges multiple other filters using the given operator.
  52. message CompositeFilter {
  53. // A composite filter operator.
  54. enum Operator {
  55. // Unspecified. This value must not be used.
  56. OPERATOR_UNSPECIFIED = 0;
  57. // Documents are required to satisfy all of the combined filters.
  58. AND = 1;
  59. }
  60. // The operator for combining multiple filters.
  61. Operator op = 1;
  62. // The list of filters to combine.
  63. //
  64. // Requires:
  65. //
  66. // * At least one filter is present.
  67. repeated Filter filters = 2;
  68. }
  69. // A filter on a specific field.
  70. message FieldFilter {
  71. // A field filter operator.
  72. enum Operator {
  73. // Unspecified. This value must not be used.
  74. OPERATOR_UNSPECIFIED = 0;
  75. // The given `field` is less than the given `value`.
  76. //
  77. // Requires:
  78. //
  79. // * That `field` come first in `order_by`.
  80. LESS_THAN = 1;
  81. // The given `field` is less than or equal to the given `value`.
  82. //
  83. // Requires:
  84. //
  85. // * That `field` come first in `order_by`.
  86. LESS_THAN_OR_EQUAL = 2;
  87. // The given `field` is greater than the given `value`.
  88. //
  89. // Requires:
  90. //
  91. // * That `field` come first in `order_by`.
  92. GREATER_THAN = 3;
  93. // The given `field` is greater than or equal to the given `value`.
  94. //
  95. // Requires:
  96. //
  97. // * That `field` come first in `order_by`.
  98. GREATER_THAN_OR_EQUAL = 4;
  99. // The given `field` is equal to the given `value`.
  100. EQUAL = 5;
  101. // The given `field` is not equal to the given `value`.
  102. //
  103. // Requires:
  104. //
  105. // * No other `NOT_EQUAL`, `NOT_IN`, `IS_NOT_NULL`, or `IS_NOT_NAN`.
  106. // * That `field` comes first in the `order_by`.
  107. NOT_EQUAL = 6;
  108. // The given `field` is an array that contains the given `value`.
  109. ARRAY_CONTAINS = 7;
  110. // The given `field` is equal to at least one value in the given array.
  111. //
  112. // Requires:
  113. //
  114. // * That `value` is a non-empty `ArrayValue` with at most 10 values.
  115. // * No other `IN` or `ARRAY_CONTAINS_ANY` or `NOT_IN`.
  116. IN = 8;
  117. // The given `field` is an array that contains any of the values in the
  118. // given array.
  119. //
  120. // Requires:
  121. //
  122. // * That `value` is a non-empty `ArrayValue` with at most 10 values.
  123. // * No other `IN` or `ARRAY_CONTAINS_ANY` or `NOT_IN`.
  124. ARRAY_CONTAINS_ANY = 9;
  125. // The value of the `field` is not in the given array.
  126. //
  127. // Requires:
  128. //
  129. // * That `value` is a non-empty `ArrayValue` with at most 10 values.
  130. // * No other `IN`, `ARRAY_CONTAINS_ANY`, `NOT_IN`, `NOT_EQUAL`,
  131. // `IS_NOT_NULL`, or `IS_NOT_NAN`.
  132. // * That `field` comes first in the `order_by`.
  133. NOT_IN = 10;
  134. }
  135. // The field to filter by.
  136. FieldReference field = 1;
  137. // The operator to filter by.
  138. Operator op = 2;
  139. // The value to compare to.
  140. Value value = 3;
  141. }
  142. // A filter with a single operand.
  143. message UnaryFilter {
  144. // A unary operator.
  145. enum Operator {
  146. // Unspecified. This value must not be used.
  147. OPERATOR_UNSPECIFIED = 0;
  148. // The given `field` is equal to `NaN`.
  149. IS_NAN = 2;
  150. // The given `field` is equal to `NULL`.
  151. IS_NULL = 3;
  152. // The given `field` is not equal to `NaN`.
  153. //
  154. // Requires:
  155. //
  156. // * No other `NOT_EQUAL`, `NOT_IN`, `IS_NOT_NULL`, or `IS_NOT_NAN`.
  157. // * That `field` comes first in the `order_by`.
  158. IS_NOT_NAN = 4;
  159. // The given `field` is not equal to `NULL`.
  160. //
  161. // Requires:
  162. //
  163. // * A single `NOT_EQUAL`, `NOT_IN`, `IS_NOT_NULL`, or `IS_NOT_NAN`.
  164. // * That `field` comes first in the `order_by`.
  165. IS_NOT_NULL = 5;
  166. }
  167. // The unary operator to apply.
  168. Operator op = 1;
  169. // The argument to the filter.
  170. oneof operand_type {
  171. // The field to which to apply the operator.
  172. FieldReference field = 2;
  173. }
  174. }
  175. // An order on a field.
  176. message Order {
  177. // The field to order by.
  178. FieldReference field = 1;
  179. // The direction to order by. Defaults to `ASCENDING`.
  180. Direction direction = 2;
  181. }
  182. // A sort direction.
  183. enum Direction {
  184. // Unspecified.
  185. DIRECTION_UNSPECIFIED = 0;
  186. // Ascending.
  187. ASCENDING = 1;
  188. // Descending.
  189. DESCENDING = 2;
  190. }
  191. // A reference to a field in a document, ex: `stats.operations`.
  192. message FieldReference {
  193. // The relative path of the document being referenced.
  194. //
  195. // Requires:
  196. //
  197. // * Conform to [document field name][google.firestore.v1.Document.fields] limitations.
  198. string field_path = 2;
  199. }
  200. // The projection of document's fields to return.
  201. message Projection {
  202. // The fields to return.
  203. //
  204. // If empty, all fields are returned. To only return the name
  205. // of the document, use `['__name__']`.
  206. repeated FieldReference fields = 2;
  207. }
  208. // The projection to return.
  209. Projection select = 1;
  210. // The collections to query.
  211. repeated CollectionSelector from = 2;
  212. // The filter to apply.
  213. Filter where = 3;
  214. // The order to apply to the query results.
  215. //
  216. // Firestore allows callers to provide a full ordering, a partial ordering, or
  217. // no ordering at all. In all cases, Firestore guarantees a stable ordering
  218. // through the following rules:
  219. //
  220. // * The `order_by` is required to reference all fields used with an
  221. // inequality filter.
  222. // * All fields that are required to be in the `order_by` but are not already
  223. // present are appended in lexicographical ordering of the field name.
  224. // * If an order on `__name__` is not specified, it is appended by default.
  225. //
  226. // Fields are appended with the same sort direction as the last order
  227. // specified, or 'ASCENDING' if no order was specified. For example:
  228. //
  229. // * `ORDER BY a` becomes `ORDER BY a ASC, __name__ ASC`
  230. // * `ORDER BY a DESC` becomes `ORDER BY a DESC, __name__ DESC`
  231. // * `WHERE a > 1` becomes `WHERE a > 1 ORDER BY a ASC, __name__ ASC`
  232. // * `WHERE __name__ > ... AND a > 1` becomes
  233. // `WHERE __name__ > ... AND a > 1 ORDER BY a ASC, __name__ ASC`
  234. repeated Order order_by = 4;
  235. // A potential prefix of a position in the result set to start the query at.
  236. //
  237. // The ordering of the result set is based on the `ORDER BY` clause of the
  238. // original query.
  239. //
  240. // ```
  241. // SELECT * FROM k WHERE a = 1 AND b > 2 ORDER BY b ASC, __name__ ASC;
  242. // ```
  243. //
  244. // This query's results are ordered by `(b ASC, __name__ ASC)`.
  245. //
  246. // Cursors can reference either the full ordering or a prefix of the location,
  247. // though it cannot reference more fields than what are in the provided
  248. // `ORDER BY`.
  249. //
  250. // Continuing off the example above, attaching the following start cursors
  251. // will have varying impact:
  252. //
  253. // - `START BEFORE (2, /k/123)`: start the query right before `a = 1 AND
  254. // b > 2 AND __name__ > /k/123`.
  255. // - `START AFTER (10)`: start the query right after `a = 1 AND b > 10`.
  256. //
  257. // Unlike `OFFSET` which requires scanning over the first N results to skip,
  258. // a start cursor allows the query to begin at a logical position. This
  259. // position is not required to match an actual result, it will scan forward
  260. // from this position to find the next document.
  261. //
  262. // Requires:
  263. //
  264. // * The number of values cannot be greater than the number of fields
  265. // specified in the `ORDER BY` clause.
  266. Cursor start_at = 7;
  267. // A potential prefix of a position in the result set to end the query at.
  268. //
  269. // This is similar to `START_AT` but with it controlling the end position
  270. // rather than the start position.
  271. //
  272. // Requires:
  273. //
  274. // * The number of values cannot be greater than the number of fields
  275. // specified in the `ORDER BY` clause.
  276. Cursor end_at = 8;
  277. // The number of documents to skip before returning the first result.
  278. //
  279. // This applies after the constraints specified by the `WHERE`, `START AT`, &
  280. // `END AT` but before the `LIMIT` clause.
  281. //
  282. // Requires:
  283. //
  284. // * The value must be greater than or equal to zero if specified.
  285. int32 offset = 6;
  286. // The maximum number of results to return.
  287. //
  288. // Applies after all other constraints.
  289. //
  290. // Requires:
  291. //
  292. // * The value must be greater than or equal to zero if specified.
  293. google.protobuf.Int32Value limit = 5;
  294. }
  295. // Firestore query for running an aggregation over a [StructuredQuery][google.firestore.v1.StructuredQuery].
  296. message StructuredAggregationQuery {
  297. // Defines a aggregation that produces a single result.
  298. message Aggregation {
  299. // Count of documents that match the query.
  300. //
  301. // The `COUNT(*)` aggregation function operates on the entire document
  302. // so it does not require a field reference.
  303. message Count {
  304. // Optional. Optional constraint on the maximum number of documents to count.
  305. //
  306. // This provides a way to set an upper bound on the number of documents
  307. // to scan, limiting latency and cost.
  308. //
  309. // Unspecified is interpreted as no bound.
  310. //
  311. // High-Level Example:
  312. //
  313. // ```
  314. // AGGREGATE COUNT_UP_TO(1000) OVER ( SELECT * FROM k );
  315. // ```
  316. //
  317. // Requires:
  318. //
  319. // * Must be greater than zero when present.
  320. google.protobuf.Int64Value up_to = 1 [(google.api.field_behavior) = OPTIONAL];
  321. }
  322. // The type of aggregation to perform, required.
  323. oneof operator {
  324. // Count aggregator.
  325. Count count = 1;
  326. }
  327. // Optional. Optional name of the field to store the result of the aggregation into.
  328. //
  329. // If not provided, Firestore will pick a default name following the format
  330. // `field_<incremental_id++>`. For example:
  331. //
  332. // ```
  333. // AGGREGATE
  334. // COUNT_UP_TO(1) AS count_up_to_1,
  335. // COUNT_UP_TO(2),
  336. // COUNT_UP_TO(3) AS count_up_to_3,
  337. // COUNT_UP_TO(4)
  338. // OVER (
  339. // ...
  340. // );
  341. // ```
  342. //
  343. // becomes:
  344. //
  345. // ```
  346. // AGGREGATE
  347. // COUNT_UP_TO(1) AS count_up_to_1,
  348. // COUNT_UP_TO(2) AS field_1,
  349. // COUNT_UP_TO(3) AS count_up_to_3,
  350. // COUNT_UP_TO(4) AS field_2
  351. // OVER (
  352. // ...
  353. // );
  354. // ```
  355. //
  356. // Requires:
  357. //
  358. // * Must be unique across all aggregation aliases.
  359. // * Conform to [document field name][google.firestore.v1.Document.fields] limitations.
  360. string alias = 7 [(google.api.field_behavior) = OPTIONAL];
  361. }
  362. // The base query to aggregate over.
  363. oneof query_type {
  364. // Nested structured query.
  365. StructuredQuery structured_query = 1;
  366. }
  367. // Optional. Series of aggregations to apply over the results of the `structured_query`.
  368. //
  369. // Requires:
  370. //
  371. // * A minimum of one and maximum of five aggregations per query.
  372. repeated Aggregation aggregations = 3 [(google.api.field_behavior) = OPTIONAL];
  373. }
  374. // A position in a query result set.
  375. message Cursor {
  376. // The values that represent a position, in the order they appear in
  377. // the order by clause of a query.
  378. //
  379. // Can contain fewer values than specified in the order by clause.
  380. repeated Value values = 1;
  381. // If the position is just before or just after the given values, relative
  382. // to the sort order defined by the query.
  383. bool before = 2;
  384. }