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::{type_::Type, ValueType},
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 query as a <code>Pipeline</code>
40    pub query: Pipeline,
41    /// A representation of the <code>Function</code>s in the preamble of the query
42    pub preamble: Vec<Function>,
43    /// A representation of the <code>Fetch</code> stage of the query, if it has one
44    pub fetch: Option<Fetch>,
45}
46
47/// Holds a representation of the function, and the result of type-inference for each variable.
48#[derive(Debug, Clone)]
49pub struct Function {
50    /// The <code>Variable</code>s which are the arguments of the function.
51    pub argument_variables: Vec<Variable>,
52    /// A representation of the <code>ReturnOperation</code> of the function.
53    pub return_operation: ReturnOperation,
54    /// A representation of the <code>Pipeline</code> which forms the body of the function.
55    pub body: Pipeline,
56    /// The inferred type for each argument of the function.
57    pub argument_annotations: Vec<VariableAnnotations>,
58    /// The inferred type for each concept returned by the function.
59    pub return_annotations: Vec<VariableAnnotations>,
60}
61
62/// A representation of the return operation of the function
63#[derive(Debug, Clone)]
64pub enum ReturnOperation {
65    /// Indicates the function returns a stream of concepts.
66    /// e.g. <code>return { $x, $y };</code>
67    Stream {
68        /// The <code>Variables</code> in the returned row.
69        variables: Vec<Variable>,
70    },
71    /// Indicates the function returns a single row of the specified <code>Variables</code>.
72    /// e.g. <code>return first $x, $y;</code>
73    Single {
74        /// The selector used to determine which row to select.
75        selector: String,
76        /// The <code>Variables</code> in the returned row.
77        variables: Vec<Variable>,
78    },
79    /// Indicates the function returns a boolean - true if the body had answers, false otherwise.
80    /// e.g. <code>return check;</code>
81    Check {},
82    /// Indicates the function returns an aggregation over the rows in the body.
83    Reduce {
84        /// The <code>Reducers</code>s used to compute the aggregations.
85        /// e.g. <code>return count($x), sum($y);</code>
86        reducers: Vec<Reducer>,
87    },
88}
89
90/// A representation of the 'fetch' stage of a query
91#[derive(Debug, Clone)]
92pub enum Fetch {
93    /// Indicates the value is a list of <code>Fetch</code> documents.
94    List(Box<Fetch>),
95    /// Indicates the value is a raw value.
96    Leaf(FetchLeaf),
97    /// Indicates the value is a mapping of string keys to <code>Fetch</code> documents.
98    Object(HashMap<String, Fetch>),
99}
100
101/// Holds typing information about a leaf value in a <code>Fetch</code> document.
102#[derive(Debug, Clone)]
103pub struct FetchLeaf {
104    /// The <code>ValueType</code> this value can be.
105    pub annotations: Vec<ValueType>,
106}
107
108#[derive(Debug, Clone)]
109pub struct VariableAnnotations {
110    pub(crate) is_optional: bool, // TODO: Make pub when we know this is correct
111    /// The <code>TypeAnnotations</code> of this variable.
112    pub types: TypeAnnotations,
113}
114
115/// The category of a variable, and the possible types determined by type-inference.
116#[derive(Debug, Clone)]
117pub enum TypeAnnotations {
118    /// Indicates the variable holds instances of any of the specified <code>Type</code>s.
119    Instance(Vec<Type>),
120    /// Indicates the variable holds types of any of the specified <code>Type</code>s.
121    Type(Vec<Type>),
122    /// Indicates the variable holds values of any of the specified <code>ValueType</code>s.
123    Value(ValueType),
124}