gitql_engine/
engine.rs

1use std::collections::HashMap;
2use std::vec;
3
4use gitql_ast::query::DescribeQuery;
5use gitql_ast::query::DoQuery;
6use gitql_ast::query::GlobalVariableDeclQuery;
7use gitql_ast::query::Query;
8use gitql_ast::query::SelectQuery;
9use gitql_ast::statement::Distinct;
10use gitql_ast::statement::Statement;
11use gitql_core::environment::Environment;
12use gitql_core::object::GitQLObject;
13use gitql_core::object::Group;
14use gitql_core::object::Row;
15use gitql_core::values::text::TextValue;
16use gitql_core::values::Value;
17
18use crate::data_provider::DataProvider;
19use crate::engine_distinct::apply_distinct_operator;
20use crate::engine_evaluator::evaluate_expression;
21use crate::engine_executor::execute_statement;
22
23/// Static Logical Plan, later must be replaced by a Plan from the Logical query Planner
24const FIXED_LOGICAL_PLAN_LEN: usize = 10;
25const FIXED_LOGICAL_PLAN: [&str; FIXED_LOGICAL_PLAN_LEN] = [
26    "select",
27    "where",
28    "group",
29    "aggregation",
30    "having",
31    "window_functions",
32    "qualify",
33    "order",
34    "offset",
35    "limit",
36];
37
38pub enum EvaluationResult {
39    Do,
40    SelectedGroups(GitQLObject),
41    SelectedInfo,
42    SetGlobalVariable,
43}
44
45#[allow(clippy::borrowed_box)]
46pub fn evaluate(
47    env: &mut Environment,
48    data_provider: &Box<dyn DataProvider>,
49    queries: Vec<Query>,
50) -> Result<Vec<EvaluationResult>, String> {
51    let mut evaluations_results: Vec<EvaluationResult> = Vec::with_capacity(queries.len());
52    for query in queries {
53        let evaluation_result = match query {
54            Query::Do(do_query) => evaluate_do_query(env, &do_query),
55            Query::Select(select_query) => evaluate_select_query(env, data_provider, select_query),
56            Query::GlobalVariableDecl(global) => evaluate_global_declaration_query(env, &global),
57            Query::DescribeTable(describe_query) => evaluate_describe_query(env, describe_query),
58            Query::ShowTables => evaluate_show_tables_query(env),
59        }?;
60        evaluations_results.push(evaluation_result);
61    }
62    Ok(evaluations_results)
63}
64
65fn evaluate_do_query(
66    env: &mut Environment,
67    do_query: &DoQuery,
68) -> Result<EvaluationResult, String> {
69    for expr in do_query.exprs.iter() {
70        evaluate_expression(env, expr, &[], &vec![])?;
71    }
72    Ok(EvaluationResult::Do)
73}
74
75#[allow(clippy::borrowed_box)]
76fn evaluate_select_query(
77    env: &mut Environment,
78    data_provider: &Box<dyn DataProvider>,
79    select_query: SelectQuery,
80) -> Result<EvaluationResult, String> {
81    let mut gitql_object = GitQLObject::default();
82    let mut alias_table: HashMap<String, String> = select_query.alias_table;
83
84    let hidden_selections_map = select_query.hidden_selections;
85    let hidden_selections: Vec<String> =
86        hidden_selections_map.values().flatten().cloned().collect();
87    let mut statements_map = select_query.statements;
88    let has_group_by_statement = statements_map.contains_key("group");
89
90    let mut distinct: Option<Distinct> = None;
91    for logical_node_name in FIXED_LOGICAL_PLAN {
92        if let Some(statement) = statements_map.get_mut(logical_node_name) {
93            execute_statement(
94                env,
95                statement,
96                data_provider,
97                &mut gitql_object,
98                &mut alias_table,
99                &hidden_selections_map,
100                has_group_by_statement,
101            )?;
102
103            if let Statement::Select(select_statement) = statement {
104                // If the main group is empty, no need to perform other statements
105                if gitql_object.is_empty() || gitql_object.groups[0].is_empty() {
106                    return Ok(EvaluationResult::SelectedGroups(gitql_object));
107                }
108
109                distinct = Some(select_statement.distinct.to_owned());
110            }
111        }
112    }
113
114    // Apply the distinct operation after executing statements
115    if let Some(distinct) = distinct {
116        apply_distinct_operator(&distinct, &mut gitql_object, &hidden_selections);
117    }
118
119    // Remove Hidden Selection from the rows after executing the query plan
120    remove_hidden_selected_from_groups(
121        &mut gitql_object.titles,
122        &mut gitql_object.groups,
123        &hidden_selections,
124    );
125
126    let number_of_groups = gitql_object.groups.len();
127    let main_group: &mut Group = &mut gitql_object.groups[0];
128
129    // If there are many groups that mean group by is executed before.
130    // must merge each group into only one element
131    if number_of_groups > 1 {
132        for group in gitql_object.groups.iter_mut() {
133            if group.len() > 1 {
134                group.rows.drain(1..);
135            }
136        }
137        gitql_object.flat();
138    }
139    // If it a single group but it select only aggregations function,
140    // should return only first element in the group
141    else if number_of_groups == 1
142        && !select_query.has_group_by_statement
143        && select_query.has_aggregation_function
144        && main_group.len() > 1
145    {
146        main_group.rows.drain(1..);
147    }
148
149    // Into statement must be executed last after flatted and remove hidden selections
150    if let Some(into_statement) = statements_map.get_mut("into") {
151        execute_statement(
152            env,
153            into_statement,
154            data_provider,
155            &mut gitql_object,
156            &mut alias_table,
157            &hidden_selections_map,
158            has_group_by_statement,
159        )?;
160
161        return Ok(EvaluationResult::SelectedInfo);
162    }
163
164    Ok(EvaluationResult::SelectedGroups(gitql_object))
165}
166
167fn evaluate_global_declaration_query(
168    env: &mut Environment,
169    global_decl_query: &GlobalVariableDeclQuery,
170) -> Result<EvaluationResult, String> {
171    let value = evaluate_expression(env, &global_decl_query.value, &[], &vec![])?;
172    env.globals
173        .insert(global_decl_query.name.to_string(), value);
174    Ok(EvaluationResult::SetGlobalVariable)
175}
176
177fn evaluate_describe_query(
178    env: &mut Environment,
179    describe_query: DescribeQuery,
180) -> Result<EvaluationResult, String> {
181    let table_fields = env
182        .schema
183        .tables_fields_names
184        .get(&describe_query.table_name.as_str())
185        .unwrap();
186
187    let mut gitql_object = GitQLObject::default();
188    gitql_object.titles.push("field".to_owned());
189    gitql_object.titles.push("type".to_owned());
190
191    let mut rows: Vec<Row> = Vec::with_capacity(table_fields.len());
192    for field in table_fields {
193        let value = env.schema.tables_fields_types.get(field).unwrap();
194        rows.push(Row {
195            values: vec![
196                Box::new(TextValue::new(field.to_owned().to_owned())),
197                Box::new(TextValue::new(value.literal())),
198            ],
199        })
200    }
201
202    gitql_object.groups.push(Group { rows });
203    Ok(EvaluationResult::SelectedGroups(gitql_object))
204}
205
206fn evaluate_show_tables_query(env: &mut Environment) -> Result<EvaluationResult, String> {
207    let tables = env.schema.tables_fields_names.keys();
208
209    let mut rows: Vec<Row> = Vec::with_capacity(tables.len());
210    for table in env.schema.tables_fields_names.keys() {
211        let values: Vec<Box<dyn Value>> =
212            vec![Box::new(TextValue::new(table.to_owned().to_owned()))];
213        rows.push(Row { values });
214    }
215
216    let mut gitql_object = GitQLObject::default();
217    gitql_object.titles.push("Tables".to_owned());
218    gitql_object.groups.push(Group { rows });
219
220    Ok(EvaluationResult::SelectedGroups(gitql_object))
221}
222
223fn remove_hidden_selected_from_groups(
224    titles: &mut Vec<String>,
225    groups: &mut [Group],
226    hidden_selections: &[String],
227) {
228    let titles_count = titles.len();
229    let mut index_list: Vec<usize> = vec![];
230    for i in (0..titles_count).rev() {
231        if hidden_selections.contains(&titles[i]) {
232            titles.remove(i);
233            index_list.push(i);
234        }
235    }
236
237    for group in groups.iter_mut() {
238        for index_to_delete in index_list.iter() {
239            for row in group.rows.iter_mut() {
240                row.values.remove(*index_to_delete);
241            }
242        }
243    }
244}