Skip to content

Commit

Permalink
Add support for interval + operator
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Jan 14, 2025
1 parent 737ff3b commit 5c4445f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Amr Hesham
Copyright (c) 2023 - 2025 Amr Hesham

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ SELECT path, count() AS changes_count, SUM(insertions) AS additions, SUM(removal
```
MIT License
Copyright (c) 2023 Amr Hesham
Copyright (c) 2023 - 2025 Amr Hesham
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 13 additions & 0 deletions crates/gitql-ast/src/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ pub struct Interval {
pub seconds: f64,
}

impl Interval {
pub fn add(&self, interval: &Interval) -> Interval {
let mut result = self.clone();
result.years += interval.years;
result.months += interval.months;
result.days += interval.days;
result.hours += interval.hours;
result.minutes += interval.minutes;
result.seconds += interval.seconds;
result
}
}

impl fmt::Display for Interval {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut parts = Vec::new();
Expand Down
10 changes: 9 additions & 1 deletion crates/gitql-ast/src/types/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ impl DataType for IntervalType {
}

fn equals(&self, other: &Box<dyn DataType>) -> bool {
other.is_any() || other.is_int() || other.is_variant_with(|t| t.is_interval())
other.is_any() || other.is_interval() || other.is_variant_with(|t| t.is_interval())
}

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

fn can_perform_add_op_with(&self) -> Vec<Box<dyn DataType>> {
vec![Box::new(IntervalType)]
}

fn add_op_result_type(&self, _other: &Box<dyn DataType>) -> Box<dyn DataType> {
Box::new(IntervalType)
}
}
8 changes: 8 additions & 0 deletions crates/gitql-core/src/values/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ impl Value for IntervalValue {
fn as_any(&self) -> &dyn Any {
self
}

fn add_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_interval) = other.as_any().downcast_ref::<IntervalValue>() {
let result = self.interval.add(&other_interval.interval);
return Ok(Box::new(IntervalValue::new(result)));
}
Err("Unexpected type to perform `+` with".to_string())
}
}
2 changes: 1 addition & 1 deletion crates/gitql-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3201,7 +3201,7 @@ fn parse_term_expression(

// Return error if this operator can't be performed even with implicit cast
return Err(Diagnostic::error(&format!(
"Operator `-` can't be performed between types `{}` and `{}`",
"Operator `+` can't be performed between types `{}` and `{}`",
lhs_type, rhs_type
))
.add_help(
Expand Down

0 comments on commit 5c4445f

Please sign in to comment.