typedb_driver/analyze/
pipeline.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, fmt};
21
22use crate::analyze::conjunction::{Conjunction, ConjunctionID, Variable};
23
24/// A representation of a query pipeline.
25#[derive(Debug, Clone)]
26pub struct Pipeline {
27    /// A flattened list of conjunctions in the pipeline.
28    /// The actual logical structure can be reconstructed from the <code>Constraint</code>s within the conjunction.
29    pub conjunctions: Vec<Conjunction>,
30    /// The stages making up the pipeline
31    pub stages: Vec<PipelineStage>,
32    /// General information about a variable, such as its name.
33    pub variable_info: HashMap<Variable, VariableInfo>,
34    /// The variables available at the end of a pipeline
35    pub outputs: Vec<Variable>,
36}
37
38impl Pipeline {
39    /// Retrieves the name of a variable, if it has one.
40    pub fn variable_name(&self, variable: &Variable) -> Option<&str> {
41        self.variable_info.get(variable).map(|v| v.name.as_str())
42    }
43}
44
45/// Holds information about variables in a <code>Pipeline</code>.
46#[derive(Debug, Clone)]
47pub struct VariableInfo {
48    /// The name of the variable, if any.
49    pub name: String,
50}
51
52/// Representation of a stage in a <code>Pipeline</code>.
53#[derive(Debug, Clone)]
54pub enum PipelineStage {
55    /// match <block>
56    /// e.g. `match $f isa friendship, links (friend: $x, friend: $y)`
57    Match {
58        /// The index into <code>Pipeline.conjunctions</code>
59        block: ConjunctionID,
60    },
61    /// insert <block>
62    /// e.g. `insert $f isa friendship, links (friend: $x, friend: $y)`
63    Insert {
64        /// The index into <code>Pipeline.conjunctions</code>
65        block: ConjunctionID,
66    },
67    /// put <block>
68    /// e.g. `put $f isa friendship, links (friend: $x, friend: $y)`
69    Put {
70        /// The index into <code>Pipeline.conjunctions</code>
71        block: ConjunctionID,
72    },
73    /// update <block>
74    /// e.g. `update $owner has name "John"`
75    Update {
76        /// The index into <code>Pipeline.conjunctions</code>
77        block: ConjunctionID,
78    },
79    /// delete
80    ///     <block>;
81    ///     <deleted_variables>;
82    /// e.g.
83    /// ```typeql
84    ///  delete
85    ///     has $attribute of $owner; links ($player) of $relation;
86    ///     $deleted-instance;
87    /// ```
88    Delete {
89        /// The index into <code>Pipeline.conjunctions</code>
90        block: ConjunctionID,
91        /// The variables for which the unified concepts are to be deleted.
92        deleted_variables: Vec<Variable>,
93    },
94    /// select <variables>
95    /// e.g. `select $x, $y`
96    Select { variables: Vec<Variable> },
97    /// sort <variables-and-order>
98    /// e.g. `sort $x asc, $y desc`
99    Sort { variables: Vec<SortVariable> },
100    /// require <variables>
101    /// e.g. `require $x, $y`
102    Require { variables: Vec<Variable> },
103    /// offset <offset>
104    /// e.g. `offset 3`
105    Offset { offset: u64 },
106    /// limit <limit>
107    /// e.g. `limit 5`
108    Limit { limit: u64 },
109    /// distinct
110    Distinct,
111    /// reduce <reducers> groupby <groupby>
112    /// e.g.
113    /// ```typeql
114    /// reduce $sum = sum($v), $count = count groupby $x, $y;
115    /// ```
116    Reduce { reducers: Vec<ReduceAssignment>, groupby: Vec<Variable> },
117}
118
119/// Representation of an assignment from a reduction in a <code>PipelineStage::Reduce</code>,
120/// such as <code>reduce $c = sum($x);</code>
121#[derive(Debug, Clone)]
122pub struct ReduceAssignment {
123    pub assigned: Variable,
124    pub reducer: Reducer,
125}
126
127/// Representation of a reducer used either in a <code>PipelineStage::Reduce</code>
128/// or in a function's <code>ReturnOperation</code>.
129#[derive(Debug, Clone)]
130pub struct Reducer {
131    /// The arguments to the reducer.
132    pub arguments: Vec<Variable>,
133    /// The reduce operation applied
134    pub reducer: String,
135}
136
137/// The variable being sorted on and the ordering of the sort, as used in a <code>PipelineStage::Sort</code>,
138///  e.g. <code>sort $v desc</code>
139#[derive(Debug, Clone)]
140pub struct SortVariable {
141    pub variable: Variable,
142    pub order: SortOrder,
143}
144
145/// The order of a variable being sorted on in a <code>PipelineStage::Sort</code>
146#[repr(C)]
147#[derive(Debug, Clone)]
148pub enum SortOrder {
149    Ascending,
150    Descending,
151}