Skip to content

Commit

Permalink
feat: Support casting DateTime to Date and verse versa
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Nov 15, 2024
1 parent 14e868a commit 8cefc04
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions crates/gitql-ast/src/types/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::any::Any;
use crate::expression::Expr;
use crate::expression::StringExpr;
use crate::format_checker::is_valid_date_format;
use crate::types::datetime::DateTimeType;

use super::base::DataType;

Expand Down Expand Up @@ -52,4 +53,8 @@ impl DataType for DateType {
}
false
}

fn can_perform_explicit_cast_op_to(&self) -> Vec<Box<dyn DataType>> {
vec![Box::new(DateTimeType)]
}
}
5 changes: 5 additions & 0 deletions crates/gitql-ast/src/types/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::any::Any;
use crate::expression::Expr;
use crate::expression::StringExpr;
use crate::format_checker::is_valid_datetime_format;
use crate::types::date::DateType;

use super::base::DataType;

Expand Down Expand Up @@ -52,4 +53,8 @@ impl DataType for DateTimeType {
}
false
}

fn can_perform_explicit_cast_op_to(&self) -> Vec<Box<dyn DataType>> {
vec![Box::new(DateType)]
}
}
8 changes: 8 additions & 0 deletions crates/gitql-core/src/values/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::cmp::Ordering;

use super::base::Value;
use super::boolean::BoolValue;
use super::date::DateValue;

use chrono::DateTime;
use gitql_ast::types::base::DataType;
Expand Down Expand Up @@ -90,4 +91,11 @@ impl Value for DateTimeValue {
}
Err("Unexpected type to perform `<=` with".to_string())
}

fn cast_op(&self, target_type: &Box<dyn DataType>) -> Result<Box<dyn Value>, String> {
if target_type.is_date() {
return Ok(Box::new(DateValue { value: self.value }));
}
Err("Unexpected type to perform `Cast` with".to_string())
}
}
3 changes: 2 additions & 1 deletion crates/gitql-parser/src/parse_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ fn cast_expression_or_error(
}

Err(Diagnostic::error(&format!(
"Unsupported `CAST` operator to type {}",
"Unsupported `CAST` operator from type `{}` to type `{}`",
value_type.literal(),
target_type.literal(),
))
.with_location(location)
Expand Down

0 comments on commit 8cefc04

Please sign in to comment.