Skip to content

Commit 6b73cd9

Browse files
committed
Update the structure of Statements to use taged union
1 parent 1a22951 commit 6b73cd9

File tree

8 files changed

+91
-285
lines changed

8 files changed

+91
-285
lines changed

crates/gitql-ast/src/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub enum Query {
1212
}
1313

1414
pub struct SelectQuery {
15-
pub statements: HashMap<&'static str, Box<dyn Statement>>,
15+
pub statements: HashMap<&'static str, Statement>,
1616
pub alias_table: HashMap<String, String>,
1717
pub has_aggregation_function: bool,
1818
pub has_group_by_statement: bool,

crates/gitql-ast/src/statement.rs

Lines changed: 12 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
1-
use std::any::Any;
21
use std::collections::HashMap;
32

4-
use dyn_clone::DynClone;
5-
63
use crate::expression::Expr;
74

8-
pub enum StatementKind {
9-
Select,
10-
Where,
11-
Having,
12-
Limit,
13-
Offset,
14-
OrderBy,
15-
GroupBy,
16-
AggregateFunction,
17-
WindowFunction,
18-
Qualify,
19-
Into,
20-
}
21-
22-
dyn_clone::clone_trait_object!(Statement);
23-
24-
pub trait Statement: DynClone {
25-
fn kind(&self) -> StatementKind;
26-
fn as_any(&self) -> &dyn Any;
5+
pub enum Statement {
6+
Select(SelectStatement),
7+
Where(WhereStatement),
8+
Having(HavingStatement),
9+
Limit(LimitStatement),
10+
Offset(OffsetStatement),
11+
OrderBy(OrderByStatement),
12+
GroupBy(GroupByStatement),
13+
AggregateFunction(AggregationsStatement),
14+
WindowFunction(WindowFunctionsStatement),
15+
Qualify(QualifyStatement),
16+
Into(IntoStatement),
2717
}
2818

2919
#[derive(Clone)]
@@ -72,76 +62,26 @@ pub struct SelectStatement {
7262
pub distinct: Distinct,
7363
}
7464

75-
impl Statement for SelectStatement {
76-
fn as_any(&self) -> &dyn Any {
77-
self
78-
}
79-
80-
fn kind(&self) -> StatementKind {
81-
StatementKind::Select
82-
}
83-
}
84-
8565
#[derive(Clone)]
8666
pub struct WhereStatement {
8767
pub condition: Box<dyn Expr>,
8868
}
8969

90-
impl Statement for WhereStatement {
91-
fn as_any(&self) -> &dyn Any {
92-
self
93-
}
94-
95-
fn kind(&self) -> StatementKind {
96-
StatementKind::Where
97-
}
98-
}
99-
10070
#[derive(Clone)]
10171
pub struct HavingStatement {
10272
pub condition: Box<dyn Expr>,
10373
}
10474

105-
impl Statement for HavingStatement {
106-
fn as_any(&self) -> &dyn Any {
107-
self
108-
}
109-
110-
fn kind(&self) -> StatementKind {
111-
StatementKind::Having
112-
}
113-
}
114-
11575
#[derive(Clone)]
11676
pub struct LimitStatement {
11777
pub count: usize,
11878
}
11979

120-
impl Statement for LimitStatement {
121-
fn as_any(&self) -> &dyn Any {
122-
self
123-
}
124-
125-
fn kind(&self) -> StatementKind {
126-
StatementKind::Limit
127-
}
128-
}
129-
13080
#[derive(Clone)]
13181
pub struct OffsetStatement {
13282
pub start: Box<dyn Expr>,
13383
}
13484

135-
impl Statement for OffsetStatement {
136-
fn as_any(&self) -> &dyn Any {
137-
self
138-
}
139-
140-
fn kind(&self) -> StatementKind {
141-
StatementKind::Offset
142-
}
143-
}
144-
14585
#[derive(Clone, PartialEq)]
14686
pub enum SortingOrder {
14787
Ascending,
@@ -161,32 +101,12 @@ pub struct OrderByStatement {
161101
pub nulls_order_policies: Vec<NullsOrderPolicy>,
162102
}
163103

164-
impl Statement for OrderByStatement {
165-
fn as_any(&self) -> &dyn Any {
166-
self
167-
}
168-
169-
fn kind(&self) -> StatementKind {
170-
StatementKind::OrderBy
171-
}
172-
}
173-
174104
#[derive(Clone)]
175105
pub struct GroupByStatement {
176106
pub values: Vec<Box<dyn Expr>>,
177107
pub has_with_roll_up: bool,
178108
}
179109

180-
impl Statement for GroupByStatement {
181-
fn as_any(&self) -> &dyn Any {
182-
self
183-
}
184-
185-
fn kind(&self) -> StatementKind {
186-
StatementKind::GroupBy
187-
}
188-
}
189-
190110
#[derive(Clone)]
191111
pub struct WindowPartitioningClause {
192112
pub expr: Box<dyn Expr>,
@@ -229,31 +149,11 @@ pub struct WindowFunctionsStatement {
229149
pub window_values: HashMap<String, WindowValue>,
230150
}
231151

232-
impl Statement for WindowFunctionsStatement {
233-
fn as_any(&self) -> &dyn Any {
234-
self
235-
}
236-
237-
fn kind(&self) -> StatementKind {
238-
StatementKind::WindowFunction
239-
}
240-
}
241-
242152
#[derive(Clone)]
243153
pub struct QualifyStatement {
244154
pub condition: Box<dyn Expr>,
245155
}
246156

247-
impl Statement for QualifyStatement {
248-
fn as_any(&self) -> &dyn Any {
249-
self
250-
}
251-
252-
fn kind(&self) -> StatementKind {
253-
StatementKind::Qualify
254-
}
255-
}
256-
257157
#[derive(Clone)]
258158
pub enum AggregateValue {
259159
Expression(Box<dyn Expr>),
@@ -265,30 +165,10 @@ pub struct AggregationsStatement {
265165
pub aggregations: HashMap<String, AggregateValue>,
266166
}
267167

268-
impl Statement for AggregationsStatement {
269-
fn as_any(&self) -> &dyn Any {
270-
self
271-
}
272-
273-
fn kind(&self) -> StatementKind {
274-
StatementKind::AggregateFunction
275-
}
276-
}
277-
278168
#[derive(Clone)]
279169
pub struct IntoStatement {
280170
pub file_path: String,
281171
pub lines_terminated: String,
282172
pub fields_terminated: String,
283173
pub enclosed: String,
284174
}
285-
286-
impl Statement for IntoStatement {
287-
fn as_any(&self) -> &dyn Any {
288-
self
289-
}
290-
291-
fn kind(&self) -> StatementKind {
292-
StatementKind::Into
293-
}
294-
}

crates/gitql-engine/src/engine.rs

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use gitql_ast::query::GlobalVariableDeclQuery;
77
use gitql_ast::query::Query;
88
use gitql_ast::query::SelectQuery;
99
use gitql_ast::statement::Distinct;
10-
use gitql_ast::statement::SelectStatement;
10+
use gitql_ast::statement::Statement;
1111
use gitql_core::environment::Environment;
1212
use gitql_core::object::GitQLObject;
1313
use gitql_core::object::Group;
@@ -90,42 +90,23 @@ fn evaluate_select_query(
9090
let mut distinct: Option<Distinct> = None;
9191
for logical_node_name in FIXED_LOGICAL_PLAN {
9292
if let Some(statement) = statements_map.get_mut(logical_node_name) {
93-
match logical_node_name {
94-
"select" => {
95-
// Select statement should be performed on all repositories, can be executed in parallel
96-
let select_statement = statement
97-
.as_any()
98-
.downcast_ref::<SelectStatement>()
99-
.unwrap();
100-
101-
execute_statement(
102-
env,
103-
statement,
104-
data_provider,
105-
&mut gitql_object,
106-
&mut alias_table,
107-
&hidden_selections_map,
108-
has_group_by_statement,
109-
)?;
110-
111-
// If the main group is empty, no need to perform other statements
112-
if gitql_object.is_empty() || gitql_object.groups[0].is_empty() {
113-
return Ok(EvaluationResult::SelectedGroups(gitql_object));
114-
}
115-
116-
distinct = Some(select_statement.distinct.to_owned());
117-
}
118-
_ => {
119-
execute_statement(
120-
env,
121-
statement,
122-
data_provider,
123-
&mut gitql_object,
124-
&mut alias_table,
125-
&hidden_selections_map,
126-
has_group_by_statement,
127-
)?;
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));
128107
}
108+
109+
distinct = Some(select_statement.distinct.to_owned());
129110
}
130111
}
131112
}

0 commit comments

Comments
 (0)