typedb_driver/analyze/mod.rs
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20use std::collections::HashMap;
21
22use crate::{
23 analyze::{
24 conjunction::Variable,
25 pipeline::{Pipeline, Reducer},
26 },
27 concept::{ValueType, type_::Type},
28};
29
30pub mod conjunction;
31pub mod pipeline;
32
33/// An <code>AnalyzedQuery</code> contains the server's representation of the query and preamble functions;
34/// as well as the result of types inferred for each variable by type-inference.
35#[derive(Debug, Clone)]
36pub struct AnalyzedQuery {
37 /// The original TypeQL query string
38 pub source: String,
39 /// A representation of the <code>Function</code>s in the preamble of the query
40 pub preamble: Vec<Function>,
41 /// A representation of the <code>Given</code> stage of the query, if it has one
42 pub given: Option<Given>,
43 /// A representation of the query as a <code>Pipeline</code>
44 pub query: Pipeline,
45 /// A representation of the <code>Fetch</code> stage of the query, if it has one
46 pub fetch: Option<Fetch>,
47}
48
49/// Holds a representation of the function, and the result of type-inference for each variable.
50#[derive(Debug, Clone)]
51pub struct Function {
52 /// The <code>Variable</code>s which are the arguments of the function.
53 pub argument_variables: Vec<Variable>,
54 /// A representation of the <code>ReturnOperation</code> of the function.
55 pub return_operation: ReturnOperation,
56 /// A representation of the <code>Pipeline</code> which forms the body of the function.
57 pub body: Pipeline,
58 /// The inferred type for each argument of the function.
59 pub argument_annotations: Vec<VariableAnnotations>,
60 /// The inferred type for each concept returned by the function.
61 pub return_annotations: Vec<VariableAnnotations>,
62}
63
64/// A representation of the return operation of the function
65#[derive(Debug, Clone)]
66pub enum ReturnOperation {
67 /// Indicates the function returns a stream of concepts.
68 /// e.g. <code>return { $x, $y };</code>
69 Stream {
70 /// The <code>Variables</code> in the returned row.
71 variables: Vec<Variable>,
72 },
73 /// Indicates the function returns a single row of the specified <code>Variables</code>.
74 /// e.g. <code>return first $x, $y;</code>
75 Single {
76 /// The selector used to determine which row to select.
77 selector: String,
78 /// The <code>Variables</code> in the returned row.
79 variables: Vec<Variable>,
80 },
81 /// Indicates the function returns a boolean - true if the body had answers, false otherwise.
82 /// e.g. <code>return check;</code>
83 Check {},
84 /// Indicates the function returns an aggregation over the rows in the body.
85 Reduce {
86 /// The <code>Reducers</code>s used to compute the aggregations.
87 /// e.g. <code>return count($x), sum($y);</code>
88 reducers: Vec<Reducer>,
89 },
90}
91
92/// A representation of the 'given' stage of a query
93#[derive(Debug, Clone)]
94pub struct Given {
95 /// The <code>Variables</code> in the given stage.
96 pub variables: Vec<Variable>,
97 /// The inferred annotations of each variable in the given stage.
98 pub variable_annotations: HashMap<Variable, VariableAnnotations>,
99}
100
101/// A representation of the 'fetch' stage of a query
102#[derive(Debug, Clone)]
103pub enum Fetch {
104 /// Indicates the value is a list of <code>Fetch</code> documents.
105 List(Box<Fetch>),
106 /// Indicates the value is a raw value.
107 Leaf(FetchLeaf),
108 /// Indicates the value is a mapping of string keys to <code>Fetch</code> documents.
109 Object(HashMap<String, Fetch>),
110}
111
112/// Holds typing information about a leaf value in a <code>Fetch</code> document.
113#[derive(Debug, Clone)]
114pub struct FetchLeaf {
115 /// The <code>ValueType</code> this value can be.
116 pub annotations: Vec<ValueType>,
117}
118
119#[derive(Debug, Clone)]
120pub struct VariableAnnotations {
121 #[allow(dead_code, reason = "TODO: Make pub when we know this is correct")]
122 pub(crate) is_optional: bool,
123 /// The <code>TypeAnnotations</code> of this variable.
124 pub types: TypeAnnotations,
125}
126
127/// The category of a variable, and the possible types determined by type-inference.
128#[derive(Debug, Clone)]
129pub enum TypeAnnotations {
130 /// Indicates the variable holds instances of any of the specified <code>Type</code>s.
131 Instance(Vec<Type>),
132 /// Indicates the variable holds types of any of the specified <code>Type</code>s.
133 Type(Vec<Type>),
134 /// Indicates the variable holds values of any of the specified <code>ValueType</code>s.
135 Value(ValueType),
136}