Skip to content

Commit

Permalink
ref: Move the filter operator in new file
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Jun 28, 2024
1 parent 44a1793 commit baba96c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ SELECT @arr[1], @arr[2], @arr[3], ARRAY_LENGTH(@arr);
SELECT @arr[1:2], @arr[2:], @arr[:2];

SELECT DISTINCT title AS tt FROM commits
SELECT name, COUNT(name) AS commit_num FROM commits GROUP BY name, author_email ORDER BY commit_num DESC LIMIT 10
SELECT author_name, COUNT(author_name) AS commit_num FROM commits GROUP BY author_name, author_email ORDER BY commit_num DESC LIMIT 10
SELECT commit_count FROM branches WHERE commit_count BETWEEN 0 .. 10

SELECT * FROM refs WHERE type = "branch"
Expand Down
54 changes: 10 additions & 44 deletions crates/gitql-engine/src/engine_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ use gitql_ast::statement::WhereStatement;
use gitql_core::environment::Environment;
use gitql_core::object::GitQLObject;
use gitql_core::object::Group;
use gitql_core::object::Row;
use gitql_core::value::Value;

use crate::data_provider::DataProvider;
use crate::engine_evaluator::evaluate_expression;
use crate::engine_filter::filter_rows_by_condition;

#[allow(clippy::borrowed_box)]
pub fn execute_statement(
Expand Down Expand Up @@ -169,31 +169,12 @@ fn execute_where_statement(
statement: &WhereStatement,
gitql_object: &mut GitQLObject,
) -> Result<(), String> {
if gitql_object.is_empty() {
return Ok(());
}

// Perform where command only on the first group
// because group by command not executed yet
let mut filtered_group: Group = Group { rows: vec![] };
let first_group = gitql_object.groups.first().unwrap().rows.iter();
for object in first_group {
let eval_result = evaluate_expression(
env,
&statement.condition,
&gitql_object.titles,
&object.values,
);
if eval_result.is_err() {
return Err(eval_result.err().unwrap());
}

if eval_result.ok().unwrap().as_bool() {
filtered_group.rows.push(Row {
values: object.values.clone(),
});
}
}
let condition = &statement.condition;
let main_group = &gitql_object.groups.first().unwrap().rows;
let rows = filter_rows_by_condition(env, condition, &gitql_object.titles, main_group)?;
let filtered_group: Group = Group { rows };

// Update the main group with the filtered data
gitql_object.groups.remove(0);
Expand All @@ -216,26 +197,11 @@ fn execute_having_statement(
}

// Perform where command only on the first group
// because groups are already merged
let mut filtered_group: Group = Group { rows: vec![] };
let first_group = gitql_object.groups.first().unwrap().rows.iter();
for object in first_group {
let eval_result = evaluate_expression(
env,
&statement.condition,
&gitql_object.titles,
&object.values,
);
if eval_result.is_err() {
return Err(eval_result.err().unwrap());
}

if eval_result.ok().unwrap().as_bool() {
filtered_group.rows.push(Row {
values: object.values.clone(),
});
}
}
// because group by command not executed yet
let condition = &statement.condition;
let main_group = &gitql_object.groups.first().unwrap().rows;
let rows = filter_rows_by_condition(env, condition, &gitql_object.titles, main_group)?;
let filtered_group: Group = Group { rows };

// Update the main group with the filtered data
gitql_object.groups.remove(0);
Expand Down
26 changes: 26 additions & 0 deletions crates/gitql-engine/src/engine_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use gitql_ast::expression::Expression;
use gitql_core::environment::Environment;
use gitql_core::object::Row;

use crate::engine_evaluator::evaluate_expression;

#[inline(always)]
#[allow(clippy::borrowed_box)]
pub fn filter_rows_by_condition(
env: &mut Environment,
condition: &Box<dyn Expression>,
titles: &[String],
objects: &Vec<Row>,
) -> Result<Vec<Row>, String> {
let mut rows: Vec<Row> = vec![];

for object in objects {
if evaluate_expression(env, condition, titles, &object.values)?.as_bool() {
rows.push(Row {
values: object.values.clone(),
});
}
}

Ok(rows)
}
1 change: 1 addition & 0 deletions crates/gitql-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod engine;
pub mod engine_distinct;
pub mod engine_evaluator;
pub mod engine_executor;
pub mod engine_filter;

0 comments on commit baba96c

Please sign in to comment.