Skip to content

Commit 079aac9

Browse files
committed
Implement the parser and eval for Group Comparison expression
1 parent a98dcb7 commit 079aac9

33 files changed

+489
-141
lines changed

crates/gitql-ast/src/expression.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ use std::any::Any;
33
use dyn_clone::DynClone;
44

55
use super::types::array::ArrayType;
6-
use super::types::base::DataType;
76
use super::types::boolean::BoolType;
87
use super::types::integer::IntType;
98
use super::types::null::NullType;
109
use super::types::text::TextType;
10+
use super::types::DataType;
1111

1212
use crate::interval::Interval;
1313
use crate::operator::ArithmeticOperator;
1414
use crate::operator::BinaryBitwiseOperator;
1515
use crate::operator::BinaryLogicalOperator;
1616
use crate::operator::ComparisonOperator;
17+
use crate::operator::GroupComparisonOperator;
1718
use crate::operator::PrefixUnaryOperator;
1819
use crate::types::float::FloatType;
1920
use crate::types::interval::IntervalType;
@@ -33,6 +34,7 @@ pub enum ExprKind {
3334
Slice,
3435
Arithmetic,
3536
Comparison,
37+
GroupComparison,
3638
Contains,
3739
ContainedBy,
3840
Like,
@@ -362,6 +364,32 @@ impl Expr for ComparisonExpr {
362364
}
363365
}
364366

367+
#[derive(Clone)]
368+
pub struct GroupComparisonExpr {
369+
pub left: Box<dyn Expr>,
370+
pub comparison_operator: ComparisonOperator,
371+
pub group_operator: GroupComparisonOperator,
372+
pub right: Box<dyn Expr>,
373+
}
374+
375+
impl Expr for GroupComparisonExpr {
376+
fn kind(&self) -> ExprKind {
377+
ExprKind::GroupComparison
378+
}
379+
380+
fn expr_type(&self) -> Box<dyn DataType> {
381+
if self.comparison_operator == ComparisonOperator::NullSafeEqual {
382+
Box::new(IntType)
383+
} else {
384+
Box::new(BoolType)
385+
}
386+
}
387+
388+
fn as_any(&self) -> &dyn Any {
389+
self
390+
}
391+
}
392+
365393
#[derive(Clone)]
366394
pub struct ContainsExpr {
367395
pub left: Box<dyn Expr>,

crates/gitql-ast/src/operator.rs

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ pub enum ComparisonOperator {
2626
NullSafeEqual,
2727
}
2828

29+
#[derive(Clone, PartialEq)]
30+
pub enum GroupComparisonOperator {
31+
All,
32+
Any,
33+
Some,
34+
}
35+
2936
#[derive(Clone, PartialEq)]
3037
pub enum BinaryLogicalOperator {
3138
Or,

crates/gitql-ast/src/types/base.rs

+86-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::fmt;
44
use dyn_clone::DynClone;
55

66
use crate::expression::Expr;
7+
use crate::operator::GroupComparisonOperator;
78

89
use super::any::AnyType;
910
use super::array::ArrayType;
@@ -288,6 +289,18 @@ pub trait DataType: DynClone {
288289
vec![]
289290
}
290291

292+
/// Return a list of types that it's possible to perform `= [ALL|ANY|SOME]' operator with
293+
/// between current DataType and any one of them
294+
///
295+
/// No need to define the result type, it always BoolType
296+
#[allow(unused_variables)]
297+
fn can_perform_group_eq_op_with(
298+
&self,
299+
group_op: &GroupComparisonOperator,
300+
) -> Vec<Box<dyn DataType>> {
301+
vec![]
302+
}
303+
291304
/// Return a list of types that it's possible to perform `!=' or `<>` operator with
292305
/// between current DataType and any one of them
293306
///
@@ -296,14 +309,38 @@ pub trait DataType: DynClone {
296309
vec![]
297310
}
298311

299-
/// Return a list of types that it's possible to perform `<=>' operator with
312+
/// Return a list of types that it's possible to perform `!= [ALL|ANY|SOME]' operator with
300313
/// between current DataType and any one of them
301314
///
302315
/// No need to define the result type, it always BoolType
316+
#[allow(unused_variables)]
317+
fn can_perform_group_bang_eq_op_with(
318+
&self,
319+
group_op: &GroupComparisonOperator,
320+
) -> Vec<Box<dyn DataType>> {
321+
vec![]
322+
}
323+
324+
/// Return a list of types that it's possible to perform `<=>' operator with
325+
/// between current DataType and any one of them
326+
///
327+
/// No need to define the result type, it always IntType
303328
fn can_perform_null_safe_eq_op_with(&self) -> Vec<Box<dyn DataType>> {
304329
vec![]
305330
}
306331

332+
/// Return a list of types that it's possible to perform `<=> [ALL|ANY|SOME]' operator with
333+
/// between current DataType and any one of them
334+
///
335+
/// No need to define the result type, it always IntType
336+
#[allow(unused_variables)]
337+
fn can_perform_group_null_safe_eq_op_with(
338+
&self,
339+
group_op: &GroupComparisonOperator,
340+
) -> Vec<Box<dyn DataType>> {
341+
vec![]
342+
}
343+
307344
/// Return a list of types that it's possible to perform `>' operator with
308345
/// between current DataType and any one of them
309346
///
@@ -312,6 +349,18 @@ pub trait DataType: DynClone {
312349
vec![]
313350
}
314351

352+
/// Return a list of types that it's possible to perform `> [ALL|ANY|SOME]' operator with
353+
/// between current DataType and any one of them
354+
///
355+
/// No need to define the result type, it always BoolType
356+
#[allow(unused_variables)]
357+
fn can_perform_group_gt_op_with(
358+
&self,
359+
group_op: &GroupComparisonOperator,
360+
) -> Vec<Box<dyn DataType>> {
361+
vec![]
362+
}
363+
315364
/// Return a list of types that it's possible to perform `>=' operator with
316365
/// between current DataType and any one of them
317366
///
@@ -320,6 +369,18 @@ pub trait DataType: DynClone {
320369
vec![]
321370
}
322371

372+
/// Return a list of types that it's possible to perform `>= [ALL|ANY|SOME]' operator with
373+
/// between current DataType and any one of them
374+
///
375+
/// No need to define the result type, it always BoolType
376+
#[allow(unused_variables)]
377+
fn can_perform_group_gte_op_with(
378+
&self,
379+
group_op: &GroupComparisonOperator,
380+
) -> Vec<Box<dyn DataType>> {
381+
vec![]
382+
}
383+
323384
/// Return a list of types that it's possible to perform `<' operator with
324385
/// between current DataType and any one of them
325386
///
@@ -328,6 +389,18 @@ pub trait DataType: DynClone {
328389
vec![]
329390
}
330391

392+
/// Return a list of types that it's possible to perform `< [ALL|ANY|SOME]' operator with
393+
/// between current DataType and any one of them
394+
///
395+
/// No need to define the result type, it always BoolType
396+
#[allow(unused_variables)]
397+
fn can_perform_group_lt_op_with(
398+
&self,
399+
group_op: &GroupComparisonOperator,
400+
) -> Vec<Box<dyn DataType>> {
401+
vec![]
402+
}
403+
331404
/// Return a list of types that it's possible to perform `=<' operator with
332405
/// between current DataType and any one of them
333406
///
@@ -336,6 +409,18 @@ pub trait DataType: DynClone {
336409
vec![]
337410
}
338411

412+
/// Return a list of types that it's possible to perform `<= [ALL|ANY|SOME]' operator with
413+
/// between current DataType and any one of them
414+
///
415+
/// No need to define the result type, it always BoolType
416+
#[allow(unused_variables)]
417+
fn can_perform_group_lte_op_with(
418+
&self,
419+
group_op: &GroupComparisonOperator,
420+
) -> Vec<Box<dyn DataType>> {
421+
vec![]
422+
}
423+
339424
/// Return a list of types that it's possible to perform unary `NOT' operator with
340425
/// between current DataType and any one of them
341426
fn can_perform_not_op(&self) -> bool {

crates/gitql-ast/src/types/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
pub mod any;
22
pub mod array;
3-
pub mod base;
43
pub mod boolean;
54
pub mod composite;
65
pub mod date;
@@ -17,3 +16,6 @@ pub mod time;
1716
pub mod undefined;
1817
pub mod varargs;
1918
pub mod variant;
19+
20+
mod base;
21+
pub use base::DataType;

crates/gitql-core/src/environment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22

3-
use gitql_ast::types::base::DataType;
3+
use gitql_ast::types::DataType;
44

55
use crate::schema::Schema;
66
use crate::signature::AggregationFunction;

crates/gitql-core/src/schema.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22

3-
use gitql_ast::types::base::DataType;
3+
use gitql_ast::types::DataType;
44

55
/// A Representation of the Data Schema that constructed the following
66
///

crates/gitql-core/src/signature.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::values::base::Value;
22

3-
use gitql_ast::types::base::DataType;
3+
use gitql_ast::types::DataType;
44

55
/// Standard function accept array of values and return single [`Value`]
66
pub type StandardFunction = fn(&[Box<dyn Value>]) -> Box<dyn Value>;

crates/gitql-core/src/types_table.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::collections::HashMap;
22

3-
use gitql_ast::types::base::DataType;
43
use gitql_ast::types::boolean::BoolType;
54
use gitql_ast::types::date::DateType;
65
use gitql_ast::types::datetime::DateTimeType;
76
use gitql_ast::types::float::FloatType;
87
use gitql_ast::types::integer::IntType;
98
use gitql_ast::types::text::TextType;
109
use gitql_ast::types::time::TimeType;
10+
use gitql_ast::types::DataType;
1111

1212
/// Map of Types and Names to be used in type parser
1313
pub struct TypesTable {

crates/gitql-core/src/values/array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::any::Any;
22
use std::cmp::Ordering;
33

44
use gitql_ast::types::array::ArrayType;
5-
use gitql_ast::types::base::DataType;
5+
use gitql_ast::types::DataType;
66

77
use super::base::Value;
88
use super::boolean::BoolValue;

0 commit comments

Comments
 (0)