Skip to content

Commit

Permalink
Implement the parser and eval for Group Comparison expression
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Feb 15, 2025
1 parent a98dcb7 commit 079aac9
Show file tree
Hide file tree
Showing 33 changed files with 489 additions and 141 deletions.
30 changes: 29 additions & 1 deletion crates/gitql-ast/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ use std::any::Any;
use dyn_clone::DynClone;

use super::types::array::ArrayType;
use super::types::base::DataType;
use super::types::boolean::BoolType;
use super::types::integer::IntType;
use super::types::null::NullType;
use super::types::text::TextType;
use super::types::DataType;

use crate::interval::Interval;
use crate::operator::ArithmeticOperator;
use crate::operator::BinaryBitwiseOperator;
use crate::operator::BinaryLogicalOperator;
use crate::operator::ComparisonOperator;
use crate::operator::GroupComparisonOperator;
use crate::operator::PrefixUnaryOperator;
use crate::types::float::FloatType;
use crate::types::interval::IntervalType;
Expand All @@ -33,6 +34,7 @@ pub enum ExprKind {
Slice,
Arithmetic,
Comparison,
GroupComparison,
Contains,
ContainedBy,
Like,
Expand Down Expand Up @@ -362,6 +364,32 @@ impl Expr for ComparisonExpr {
}
}

#[derive(Clone)]
pub struct GroupComparisonExpr {
pub left: Box<dyn Expr>,
pub comparison_operator: ComparisonOperator,
pub group_operator: GroupComparisonOperator,
pub right: Box<dyn Expr>,
}

impl Expr for GroupComparisonExpr {
fn kind(&self) -> ExprKind {
ExprKind::GroupComparison
}

fn expr_type(&self) -> Box<dyn DataType> {
if self.comparison_operator == ComparisonOperator::NullSafeEqual {
Box::new(IntType)
} else {
Box::new(BoolType)
}
}

fn as_any(&self) -> &dyn Any {
self
}
}

#[derive(Clone)]
pub struct ContainsExpr {
pub left: Box<dyn Expr>,
Expand Down
7 changes: 7 additions & 0 deletions crates/gitql-ast/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ pub enum ComparisonOperator {
NullSafeEqual,
}

#[derive(Clone, PartialEq)]
pub enum GroupComparisonOperator {
All,
Any,
Some,
}

#[derive(Clone, PartialEq)]
pub enum BinaryLogicalOperator {
Or,
Expand Down
87 changes: 86 additions & 1 deletion crates/gitql-ast/src/types/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fmt;
use dyn_clone::DynClone;

use crate::expression::Expr;
use crate::operator::GroupComparisonOperator;

use super::any::AnyType;
use super::array::ArrayType;
Expand Down Expand Up @@ -288,6 +289,18 @@ pub trait DataType: DynClone {
vec![]
}

/// Return a list of types that it's possible to perform `= [ALL|ANY|SOME]' operator with
/// between current DataType and any one of them
///
/// No need to define the result type, it always BoolType
#[allow(unused_variables)]
fn can_perform_group_eq_op_with(
&self,
group_op: &GroupComparisonOperator,
) -> Vec<Box<dyn DataType>> {
vec![]
}

/// Return a list of types that it's possible to perform `!=' or `<>` operator with
/// between current DataType and any one of them
///
Expand All @@ -296,14 +309,38 @@ pub trait DataType: DynClone {
vec![]
}

/// Return a list of types that it's possible to perform `<=>' operator with
/// Return a list of types that it's possible to perform `!= [ALL|ANY|SOME]' operator with
/// between current DataType and any one of them
///
/// No need to define the result type, it always BoolType
#[allow(unused_variables)]
fn can_perform_group_bang_eq_op_with(
&self,
group_op: &GroupComparisonOperator,
) -> Vec<Box<dyn DataType>> {
vec![]
}

/// Return a list of types that it's possible to perform `<=>' operator with
/// between current DataType and any one of them
///
/// No need to define the result type, it always IntType
fn can_perform_null_safe_eq_op_with(&self) -> Vec<Box<dyn DataType>> {
vec![]
}

/// Return a list of types that it's possible to perform `<=> [ALL|ANY|SOME]' operator with
/// between current DataType and any one of them
///
/// No need to define the result type, it always IntType
#[allow(unused_variables)]
fn can_perform_group_null_safe_eq_op_with(
&self,
group_op: &GroupComparisonOperator,
) -> Vec<Box<dyn DataType>> {
vec![]
}

/// Return a list of types that it's possible to perform `>' operator with
/// between current DataType and any one of them
///
Expand All @@ -312,6 +349,18 @@ pub trait DataType: DynClone {
vec![]
}

/// Return a list of types that it's possible to perform `> [ALL|ANY|SOME]' operator with
/// between current DataType and any one of them
///
/// No need to define the result type, it always BoolType
#[allow(unused_variables)]
fn can_perform_group_gt_op_with(
&self,
group_op: &GroupComparisonOperator,
) -> Vec<Box<dyn DataType>> {
vec![]
}

/// Return a list of types that it's possible to perform `>=' operator with
/// between current DataType and any one of them
///
Expand All @@ -320,6 +369,18 @@ pub trait DataType: DynClone {
vec![]
}

/// Return a list of types that it's possible to perform `>= [ALL|ANY|SOME]' operator with
/// between current DataType and any one of them
///
/// No need to define the result type, it always BoolType
#[allow(unused_variables)]
fn can_perform_group_gte_op_with(
&self,
group_op: &GroupComparisonOperator,
) -> Vec<Box<dyn DataType>> {
vec![]
}

/// Return a list of types that it's possible to perform `<' operator with
/// between current DataType and any one of them
///
Expand All @@ -328,6 +389,18 @@ pub trait DataType: DynClone {
vec![]
}

/// Return a list of types that it's possible to perform `< [ALL|ANY|SOME]' operator with
/// between current DataType and any one of them
///
/// No need to define the result type, it always BoolType
#[allow(unused_variables)]
fn can_perform_group_lt_op_with(
&self,
group_op: &GroupComparisonOperator,
) -> Vec<Box<dyn DataType>> {
vec![]
}

/// Return a list of types that it's possible to perform `=<' operator with
/// between current DataType and any one of them
///
Expand All @@ -336,6 +409,18 @@ pub trait DataType: DynClone {
vec![]
}

/// Return a list of types that it's possible to perform `<= [ALL|ANY|SOME]' operator with
/// between current DataType and any one of them
///
/// No need to define the result type, it always BoolType
#[allow(unused_variables)]
fn can_perform_group_lte_op_with(
&self,
group_op: &GroupComparisonOperator,
) -> Vec<Box<dyn DataType>> {
vec![]
}

/// Return a list of types that it's possible to perform unary `NOT' operator with
/// between current DataType and any one of them
fn can_perform_not_op(&self) -> bool {
Expand Down
4 changes: 3 additions & 1 deletion crates/gitql-ast/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod any;
pub mod array;
pub mod base;
pub mod boolean;
pub mod composite;
pub mod date;
Expand All @@ -17,3 +16,6 @@ pub mod time;
pub mod undefined;
pub mod varargs;
pub mod variant;

mod base;
pub use base::DataType;
2 changes: 1 addition & 1 deletion crates/gitql-core/src/environment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use gitql_ast::types::base::DataType;
use gitql_ast::types::DataType;

use crate::schema::Schema;
use crate::signature::AggregationFunction;
Expand Down
2 changes: 1 addition & 1 deletion crates/gitql-core/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use gitql_ast::types::base::DataType;
use gitql_ast::types::DataType;

/// A Representation of the Data Schema that constructed the following
///
Expand Down
2 changes: 1 addition & 1 deletion crates/gitql-core/src/signature.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::values::base::Value;

use gitql_ast::types::base::DataType;
use gitql_ast::types::DataType;

/// Standard function accept array of values and return single [`Value`]
pub type StandardFunction = fn(&[Box<dyn Value>]) -> Box<dyn Value>;
Expand Down
2 changes: 1 addition & 1 deletion crates/gitql-core/src/types_table.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::collections::HashMap;

use gitql_ast::types::base::DataType;
use gitql_ast::types::boolean::BoolType;
use gitql_ast::types::date::DateType;
use gitql_ast::types::datetime::DateTimeType;
use gitql_ast::types::float::FloatType;
use gitql_ast::types::integer::IntType;
use gitql_ast::types::text::TextType;
use gitql_ast::types::time::TimeType;
use gitql_ast::types::DataType;

/// Map of Types and Names to be used in type parser
pub struct TypesTable {
Expand Down
2 changes: 1 addition & 1 deletion crates/gitql-core/src/values/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::any::Any;
use std::cmp::Ordering;

use gitql_ast::types::array::ArrayType;
use gitql_ast::types::base::DataType;
use gitql_ast::types::DataType;

use super::base::Value;
use super::boolean::BoolValue;
Expand Down
Loading

0 comments on commit 079aac9

Please sign in to comment.