cognite/dto/data_modeling/
query.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4use serde_with::skip_serializing_none;
5
6use crate::{
7    models::{instances::NodeOrEdge, PropertySort, TaggedViewReference},
8    AdvancedFilter, RawValue,
9};
10
11#[skip_serializing_none]
12#[derive(Serialize, Deserialize, Clone, Debug)]
13#[serde(rename_all = "camelCase")]
14/// Reference to a property on a view.
15pub struct ViewPropertyReference {
16    /// View reference
17    pub view: TaggedViewReference,
18    /// Property identifier.
19    pub identifier: String,
20}
21
22#[skip_serializing_none]
23#[derive(Serialize, Deserialize, Clone, Debug, Default)]
24#[serde(rename_all = "camelCase")]
25/// Query for nodes.
26pub struct NodesQuery {
27    /// Chain result starting from this query.
28    pub from: Option<String>,
29    /// Chain result through this direct relation property in the
30    /// query referenced by `from`.
31    pub through: Option<ViewPropertyReference>,
32    /// Filter on nodes to retrieve.
33    pub filter: Option<AdvancedFilter>,
34    /// The direction to use when traversing direct relations. Only
35    /// applicable when `through` is specified.
36    pub direction: Option<QueryDirection>,
37    /// Control which side of the edge to chain from. This option is only applicable if
38    /// the view referenced in the `from` field consists of edges.
39    pub chain_to: Option<QueryChainSide>,
40}
41
42#[skip_serializing_none]
43#[derive(Serialize, Deserialize, Clone, Debug, Default)]
44#[serde(rename_all = "camelCase")]
45/// Expression for querying nodes.
46pub struct QueryNodeTableExpression {
47    /// Sort result set by a list of properties. The order is significant.
48    pub sort: Option<Vec<PropertySort>>,
49    /// Maximum number of nodes to retrieve.
50    pub limit: Option<i32>,
51    /// Node query.
52    pub nodes: NodesQuery,
53}
54
55#[skip_serializing_none]
56#[derive(Serialize, Deserialize, Clone, Debug, Default)]
57#[serde(rename_all = "camelCase")]
58/// Direction to query edges or direct relations.
59pub enum QueryDirection {
60    #[default]
61    /// Query edges or direct relations outwards.
62    Outwards,
63    /// Query edges or direct relations inwards.
64    Inwards,
65}
66
67#[skip_serializing_none]
68#[derive(Serialize, Deserialize, Clone, Debug, Default)]
69#[serde(rename_all = "camelCase")]
70/// Which side of edge to chain from.
71pub enum QueryChainSide {
72    /// Chain to `start` if `direction = outwards`, or
73    /// `end` if `direction = inwards`
74    Source,
75    #[default]
76    /// Chain to `end` if `direction = outwards`, or
77    /// `start` if direction `inwards`.
78    Destination,
79}
80
81#[skip_serializing_none]
82#[derive(Serialize, Deserialize, Clone, Debug, Default)]
83#[serde(rename_all = "camelCase")]
84/// Query for edges
85pub struct EdgesQuery {
86    /// Chain result starting from this query.
87    pub from: Option<String>,
88    /// Maximum number of levels to traverse.
89    pub max_distance: Option<i32>,
90    /// The direction to use when traversing. Defaults to `outwards`.
91    pub direction: Option<QueryDirection>,
92    /// Filter on edges.
93    pub filter: Option<AdvancedFilter>,
94    /// Filter on nodes along the path.
95    pub node_filter: Option<AdvancedFilter>,
96    /// Termination filter, if this matches, the query won't go any deeper, but the
97    /// edge will be included.
98    pub termination_filter: Option<AdvancedFilter>,
99    /// Limit the number of returned edges for each of the source nodes in the result set.
100    /// The indicated limit applies to the result set from the referenced `from`.
101    /// `limit_each` only has meaning when you also specify `max_distance = 1` and `from`.
102    pub limit_each: Option<i32>,
103}
104
105#[skip_serializing_none]
106#[derive(Serialize, Deserialize, Clone, Debug, Default)]
107#[serde(rename_all = "camelCase")]
108/// Expression for querying edges.
109pub struct QueryEdgeTableExpression {
110    /// Sort result set by a list of properties. The order is significant.
111    pub sort: Option<Vec<PropertySort>>,
112    /// Sort result set when using recursive graph traversal. The order is significant.
113    pub post_sort: Option<Vec<PropertySort>>,
114    /// Maximum number of edges to retrieve.
115    pub limit: Option<i32>,
116    /// Query for edges.
117    pub edges: EdgesQuery,
118}
119
120#[skip_serializing_none]
121#[derive(Serialize, Deserialize, Clone, Debug)]
122#[serde(untagged, rename_all_fields = "camelCase")]
123/// Composite query performing operations on other result sets.
124pub enum QuerySetOperationTableExpression {
125    /// Return the union of the specified result sets. May return duplicate results.
126    UnionAll {
127        /// List of query sets or references to other queries.
128        union_all: Vec<QuerySetOrString>,
129        /// Exclude matching results.
130        except: Option<Vec<String>>,
131        /// Maximum number of results in result set.
132        limit: Option<i32>,
133    },
134    /// Return the union of the specified result sets, will not return duplicate results.
135    ///
136    /// Note: Using `UnionAll` is more efficient in general.
137    Union {
138        /// List of query sets or references to other queries.
139        union: Vec<QuerySetOrString>,
140        /// Exclude matching results.
141        except: Option<Vec<String>>,
142        /// Maximum number of results in result set.
143        limit: Option<i32>,
144    },
145    /// Find the common elements in the returned result set.
146    Intersection {
147        /// List of query sets or references to other queries.
148        intersection: Vec<QuerySetOrString>,
149        /// Exclude matching results.
150        except: Option<Vec<String>>,
151        /// Maximum number of results in result set.
152        limit: Option<i32>,
153    },
154}
155
156#[derive(Serialize, Deserialize, Clone, Debug)]
157#[serde(untagged)]
158/// Element of a query set operation. Either another query set, or a reference
159/// to a different query.
160pub enum QuerySetOrString {
161    /// Nested query set.
162    QuerySet(Box<QuerySetOperationTableExpression>),
163    /// Reference to a query.
164    String(String),
165}
166
167#[derive(Serialize, Deserialize, Clone, Debug)]
168#[serde(untagged)]
169/// Expression for querying instances.
170pub enum QueryTableExpression {
171    /// Query nodes.
172    Node(QueryNodeTableExpression),
173    /// Query edges.
174    Edge(QueryEdgeTableExpression),
175    /// Perform complex operations on other query results.
176    SetOperation(QuerySetOperationTableExpression),
177}
178
179#[skip_serializing_none]
180#[derive(Serialize, Deserialize, Clone, Debug)]
181#[serde(rename_all = "camelCase")]
182/// Select a set of properties from a view.
183pub struct SourceSelector {
184    /// View identifier.
185    pub source: TaggedViewReference,
186    /// List of property identifiers.
187    pub properties: Vec<String>,
188}
189
190#[skip_serializing_none]
191#[derive(Serialize, Deserialize, Clone, Debug)]
192#[serde(rename_all = "camelCase")]
193/// Expression for selecting properties from a list of views.
194pub struct SelectExpression {
195    /// List of views to retrieve from.
196    pub sources: Vec<SourceSelector>,
197    /// Optional sort on returned values.
198    pub sort: Option<Vec<PropertySort>>,
199    /// Maximum number of values to return.
200    pub limit: Option<i32>,
201}
202
203#[skip_serializing_none]
204#[derive(Serialize, Deserialize, Clone, Debug)]
205#[serde(rename_all = "camelCase")]
206/// Request for querying instances.
207pub struct QueryInstancesRequest {
208    /// Collection of queries, indexed by their ID.
209    pub with: HashMap<String, QueryTableExpression>,
210    /// Map of cursors. The keys here should match the expressions in
211    /// the `with` clause.
212    pub cursors: Option<HashMap<String, String>>,
213    /// Define which properties to return for each query.
214    pub select: HashMap<String, SelectExpression>,
215    /// Values in filters can be parameterised. Parameters are provided as part of the query object,
216    /// and referenced in the filter itself.
217    pub parameters: Option<HashMap<String, RawValue>>,
218}
219
220#[skip_serializing_none]
221#[derive(Serialize, Deserialize, Clone, Debug)]
222#[serde(rename_all = "camelCase")]
223/// Response for querying instances.
224pub struct QueryInstancesResponse<TProperties> {
225    /// Retrieved instances, grouped by query.
226    pub items: HashMap<String, Vec<NodeOrEdge<TProperties>>>,
227    /// Set of cursors for pagination.
228    pub next_cursor: Option<HashMap<String, String>>,
229}