query.proto 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // Copyright 2018 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. //
  15. syntax = "proto3";
  16. package google.firestore.v1;
  17. import "google/firestore/v1/document.proto";
  18. import "google/protobuf/wrappers.proto";
  19. option csharp_namespace = "Google.Cloud.Firestore.V1Beta1";
  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\\V1beta1";
  26. // A Firestore query.
  27. message StructuredQuery {
  28. // A selection of a collection, such as `messages as m1`.
  29. message CollectionSelector {
  30. // The collection ID.
  31. // When set, selects only collections with this ID.
  32. string collection_id = 2;
  33. // When false, selects only collections that are immediate children of
  34. // the `parent` specified in the containing `RunQueryRequest`.
  35. // When true, selects all descendant collections.
  36. bool all_descendants = 3;
  37. }
  38. // A filter.
  39. message Filter {
  40. // The type of filter.
  41. oneof filter_type {
  42. // A composite filter.
  43. CompositeFilter composite_filter = 1;
  44. // A filter on a document field.
  45. FieldFilter field_filter = 2;
  46. // A filter that takes exactly one argument.
  47. UnaryFilter unary_filter = 3;
  48. }
  49. }
  50. // A filter that merges multiple other filters using the given operator.
  51. message CompositeFilter {
  52. // A composite filter operator.
  53. enum Operator {
  54. // Unspecified. This value must not be used.
  55. OPERATOR_UNSPECIFIED = 0;
  56. // Documents are required to satisfy all of the combined filters.
  57. AND = 1;
  58. // Documents are required to satisfy at least one of the combined filters.
  59. OR = 2;
  60. }
  61. // The operator for combining multiple filters.
  62. Operator op = 1;
  63. // The list of filters to combine.
  64. // Must contain at least one filter.
  65. repeated Filter filters = 2;
  66. }
  67. // A filter on a specific field.
  68. message FieldFilter {
  69. // A field filter operator.
  70. enum Operator {
  71. // Unspecified. This value must not be used.
  72. OPERATOR_UNSPECIFIED = 0;
  73. // The given `field` is less than the given `value`.
  74. //
  75. // Requires:
  76. //
  77. // * That `field` come first in `order_by`.
  78. LESS_THAN = 1;
  79. // The given `field` is less than or equal to the given `value`.
  80. //
  81. // Requires:
  82. //
  83. // * That `field` come first in `order_by`.
  84. LESS_THAN_OR_EQUAL = 2;
  85. // The given `field` is greater than the given `value`.
  86. //
  87. // Requires:
  88. //
  89. // * That `field` come first in `order_by`.
  90. GREATER_THAN = 3;
  91. // The given `field` is greater than or equal to the given `value`.
  92. //
  93. // Requires:
  94. //
  95. // * That `field` come first in `order_by`.
  96. GREATER_THAN_OR_EQUAL = 4;
  97. // The given `field` is equal to the given `value`.
  98. EQUAL = 5;
  99. // The given `field` is not equal to the given `value`.
  100. //
  101. // Requires:
  102. //
  103. // * No other `NOT_EQUAL`, `NOT_IN`, `IS_NOT_NULL`, or `IS_NOT_NAN`.
  104. // * That `field` comes first in the `order_by`.
  105. NOT_EQUAL = 6;
  106. // The given `field` is an array that contains the given `value`.
  107. ARRAY_CONTAINS = 7;
  108. // The given `field` is equal to at least one value in the given array.
  109. //
  110. // Requires:
  111. //
  112. // * That `value` is a non-empty `ArrayValue` with at most 10 values.
  113. // * No other `IN` or `ARRAY_CONTAINS_ANY` or `NOT_IN`.
  114. IN = 8;
  115. // The given `field` is an array that contains any of the values in the
  116. // given array.
  117. //
  118. // Requires:
  119. //
  120. // * That `value` is a non-empty `ArrayValue` with at most 10 values.
  121. // * No other `IN` or `ARRAY_CONTAINS_ANY` or `NOT_IN`.
  122. ARRAY_CONTAINS_ANY = 9;
  123. // The value of the `field` is not in the given array.
  124. //
  125. // Requires:
  126. //
  127. // * That `value` is a non-empty `ArrayValue` with at most 10 values.
  128. // * No other `IN`, `ARRAY_CONTAINS_ANY`, `NOT_IN`, `NOT_EQUAL`,
  129. // `IS_NOT_NULL`, or `IS_NOT_NAN`.
  130. // * That `field` comes first in the `order_by`.
  131. NOT_IN = 10;
  132. }
  133. // The field to filter by.
  134. FieldReference field = 1;
  135. // The operator to filter by.
  136. Operator op = 2;
  137. // The value to compare to.
  138. Value value = 3;
  139. }
  140. // A filter with a single operand.
  141. message UnaryFilter {
  142. // A unary operator.
  143. enum Operator {
  144. // Unspecified. This value must not be used.
  145. OPERATOR_UNSPECIFIED = 0;
  146. // The given `field` is equal to `NaN`.
  147. IS_NAN = 2;
  148. // The given `field` is equal to `NULL`.
  149. IS_NULL = 3;
  150. // The given `field` is not equal to `NaN`.
  151. //
  152. // Requires:
  153. //
  154. // * No other `NOT_EQUAL`, `NOT_IN`, `IS_NOT_NULL`, or `IS_NOT_NAN`.
  155. // * That `field` comes first in the `order_by`.
  156. IS_NOT_NAN = 4;
  157. // The given `field` is not equal to `NULL`.
  158. //
  159. // Requires:
  160. //
  161. // * A single `NOT_EQUAL`, `NOT_IN`, `IS_NOT_NULL`, or `IS_NOT_NAN`.
  162. // * That `field` comes first in the `order_by`.
  163. IS_NOT_NULL = 5;
  164. }
  165. // The unary operator to apply.
  166. Operator op = 1;
  167. // The argument to the filter.
  168. oneof operand_type {
  169. // The field to which to apply the operator.
  170. FieldReference field = 2;
  171. }
  172. }
  173. // An order on a field.
  174. message Order {
  175. // The field to order by.
  176. FieldReference field = 1;
  177. // The direction to order by. Defaults to `ASCENDING`.
  178. Direction direction = 2;
  179. }
  180. // A reference to a field, such as `max(messages.time) as max_time`.
  181. message FieldReference {
  182. string field_path = 2;
  183. }
  184. // The projection of document's fields to return.
  185. message Projection {
  186. // The fields to return.
  187. //
  188. // If empty, all fields are returned. To only return the name
  189. // of the document, use `['__name__']`.
  190. repeated FieldReference fields = 2;
  191. }
  192. // A sort direction.
  193. enum Direction {
  194. // Unspecified.
  195. DIRECTION_UNSPECIFIED = 0;
  196. // Ascending.
  197. ASCENDING = 1;
  198. // Descending.
  199. DESCENDING = 2;
  200. }
  201. // The projection to return.
  202. Projection select = 1;
  203. // The collections to query.
  204. repeated CollectionSelector from = 2;
  205. // The filter to apply.
  206. Filter where = 3;
  207. // The order to apply to the query results.
  208. //
  209. // Firestore guarantees a stable ordering through the following rules:
  210. //
  211. // * Any field required to appear in `order_by`, that is not already
  212. // specified in `order_by`, is appended to the order in field name order
  213. // by default.
  214. // * If an order on `__name__` is not specified, it is appended by default.
  215. //
  216. // Fields are appended with the same sort direction as the last order
  217. // specified, or 'ASCENDING' if no order was specified. For example:
  218. //
  219. // * `SELECT * FROM Foo ORDER BY A` becomes
  220. // `SELECT * FROM Foo ORDER BY A, __name__`
  221. // * `SELECT * FROM Foo ORDER BY A DESC` becomes
  222. // `SELECT * FROM Foo ORDER BY A DESC, __name__ DESC`
  223. // * `SELECT * FROM Foo WHERE A > 1` becomes
  224. // `SELECT * FROM Foo WHERE A > 1 ORDER BY A, __name__`
  225. repeated Order order_by = 4;
  226. // A starting point for the query results.
  227. Cursor start_at = 7;
  228. // A end point for the query results.
  229. Cursor end_at = 8;
  230. // The number of results to skip.
  231. //
  232. // Applies before limit, but after all other constraints. Must be >= 0 if
  233. // specified.
  234. int32 offset = 6;
  235. // The maximum number of results to return.
  236. //
  237. // Applies after all other constraints.
  238. // Must be >= 0 if specified.
  239. google.protobuf.Int32Value limit = 5;
  240. }
  241. message StructuredAggregationQuery {
  242. // Defines a aggregation that produces a single result.
  243. message Aggregation {
  244. // Count of documents that match the query.
  245. //
  246. // The `COUNT(*)` aggregation function operates on the entire document
  247. // so it does not require a field reference.
  248. message Count {
  249. // Optional. Optional constraint on the maximum number of documents to count.
  250. //
  251. // This provides a way to set an upper bound on the number of documents
  252. // to scan, limiting latency and cost.
  253. //
  254. // High-Level Example:
  255. //
  256. // ```
  257. // SELECT COUNT_UP_TO(1000) FROM ( SELECT * FROM k );
  258. // ```
  259. //
  260. // Requires:
  261. //
  262. // * Must be greater than zero when present.
  263. google.protobuf.Int64Value up_to = 1;
  264. }
  265. // Sum of the values of the requested field.
  266. //
  267. // * Only numeric values will be aggregated. All non-numeric values
  268. // including `NULL` are skipped.
  269. //
  270. // * If the aggregated values contain `NaN`, returns `NaN`.
  271. //
  272. // * If the aggregated value set is empty, returns 0.
  273. //
  274. // * Returns a 64-bit integer if the sum result is an integer value and does
  275. // not overflow. Otherwise, the result is returned as a double. Note that
  276. // even if all the aggregated values are integers, the result is returned
  277. // as a double if it cannot fit within a 64-bit signed integer. When this
  278. // occurs, the returned value will lose precision.
  279. //
  280. // * When underflow occurs, floating-point aggregation is non-deterministic.
  281. // This means that running the same query repeatedly without any changes to
  282. // the underlying values could produce slightly different results each
  283. // time. In those cases, values should be stored as integers over
  284. // floating-point numbers.
  285. message Sum {
  286. // The field to aggregate on.
  287. StructuredQuery.FieldReference field = 1;
  288. }
  289. // Average of the values of the requested field.
  290. //
  291. // * Only numeric values will be aggregated. All non-numeric values
  292. // including `NULL` are skipped.
  293. //
  294. // * If the aggregated values contain `NaN`, returns `NaN`.
  295. //
  296. // * If the aggregated value set is empty, returns `NULL`.
  297. //
  298. // * Always returns the result as a double.
  299. message Avg {
  300. // The field to aggregate on.
  301. StructuredQuery.FieldReference field = 1;
  302. }
  303. // The type of aggregation to perform, required.
  304. oneof operator {
  305. // Count aggregator.
  306. Count count = 1;
  307. // Sum aggregator.
  308. Sum sum = 2;
  309. // Average aggregator.
  310. Avg avg = 3;
  311. }
  312. // Required. The name of the field to store the result of the aggregation into.
  313. //
  314. // Requires:
  315. //
  316. // * Must be present.
  317. // * Must be unique across all aggregation aliases.
  318. // * Conform to existing [document field name][google.firestore.v1.Document.fields] limitations.
  319. string alias = 7;
  320. }
  321. // The base query to aggregate over.
  322. oneof query_type {
  323. // Nested structured query.
  324. StructuredQuery structured_query = 1;
  325. }
  326. // Optional. Series of aggregations to apply on top of the `structured_query`.
  327. repeated Aggregation aggregations = 3;
  328. }
  329. // A position in a query result set.
  330. message Cursor {
  331. // The values that represent a position, in the order they appear in
  332. // the order by clause of a query.
  333. //
  334. // Can contain fewer values than specified in the order by clause.
  335. repeated Value values = 1;
  336. // If the position is just before or just after the given values, relative
  337. // to the sort order defined by the query.
  338. bool before = 2;
  339. }