From baba96cd1ab73e1f4fa1e8aab3968419a225860b Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Fri, 28 Jun 2024 12:30:59 +0200 Subject: [PATCH] ref: Move the filter operator in new file --- README.md | 2 +- crates/gitql-engine/src/engine_executor.rs | 54 ++++------------------ crates/gitql-engine/src/engine_filter.rs | 26 +++++++++++ crates/gitql-engine/src/lib.rs | 1 + 4 files changed, 38 insertions(+), 45 deletions(-) create mode 100644 crates/gitql-engine/src/engine_filter.rs diff --git a/README.md b/README.md index c4ac4645..4d6635d4 100644 --- a/README.md +++ b/README.md @@ -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" diff --git a/crates/gitql-engine/src/engine_executor.rs b/crates/gitql-engine/src/engine_executor.rs index bd30e3f5..d6c6ede3 100644 --- a/crates/gitql-engine/src/engine_executor.rs +++ b/crates/gitql-engine/src/engine_executor.rs @@ -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( @@ -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); @@ -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); diff --git a/crates/gitql-engine/src/engine_filter.rs b/crates/gitql-engine/src/engine_filter.rs new file mode 100644 index 00000000..0ade9ab1 --- /dev/null +++ b/crates/gitql-engine/src/engine_filter.rs @@ -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, + titles: &[String], + objects: &Vec, +) -> Result, String> { + let mut rows: Vec = vec![]; + + for object in objects { + if evaluate_expression(env, condition, titles, &object.values)?.as_bool() { + rows.push(Row { + values: object.values.clone(), + }); + } + } + + Ok(rows) +} diff --git a/crates/gitql-engine/src/lib.rs b/crates/gitql-engine/src/lib.rs index fefb3b30..e3f663e5 100644 --- a/crates/gitql-engine/src/lib.rs +++ b/crates/gitql-engine/src/lib.rs @@ -3,3 +3,4 @@ pub mod engine; pub mod engine_distinct; pub mod engine_evaluator; pub mod engine_executor; +pub mod engine_filter;