Skip to content

Commit

Permalink
Add interval arithmetic for date and timestamp.
Browse files Browse the repository at this point in the history
  • Loading branch information
RinChanNOWWW committed Oct 14, 2022
1 parent 32d36ca commit 32a251c
Show file tree
Hide file tree
Showing 4 changed files with 1,078 additions and 42 deletions.
3 changes: 1 addition & 2 deletions src/query/expression/tests/it/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn random_type() -> DataTypeImpl {
let mut rng = rand::thread_rng();

loop {
let datatype = match rng.gen_range(0..22) {
let datatype = match rng.gen_range(0..21) {
0 => DataTypeImpl::Null(NullType {}),
1 => NullableType::new_impl(u64::to_data_type()),
2 => DataTypeImpl::Boolean(BooleanType {}),
Expand All @@ -50,7 +50,6 @@ fn random_type() -> DataTypeImpl {
17 => ArrayType::new_impl(f32::to_data_type()),
18 => DataTypeImpl::VariantArray(VariantArrayType {}),
19 => DataTypeImpl::VariantObject(VariantObjectType {}),
20 => DataTypeImpl::Interval(IntervalType::new(IntervalKind::Day)),
_ => DataTypeImpl::Variant(VariantType {}),
};
if can_convert(&datatype) {
Expand Down
265 changes: 265 additions & 0 deletions src/query/functions-v2/tests/it/scalars/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ fn test_datetime() {
// date_{add | sub}({year | quarter | month | week | day | hour | minute | second}, date, number)
test_date_date_add_sub(file);
test_timestamp_date_add_sub(file);
// date {+ | -} interval number {year | quarter | month | week | day | hour | minute | second}
test_date_arith(file);
test_timestamp_arith(file);
}

fn test_to_timestamp(file: &mut impl Write) {
Expand Down Expand Up @@ -630,3 +633,265 @@ fn test_timestamp_date_add_sub(file: &mut impl Write) {
),
]);
}

fn test_date_arith(file: &mut impl Write) {
run_ast(file, "to_date(0) + interval 10000 year", &[]); // failed
run_ast(file, "to_date(0) + interval 100 year", &[]);
run_ast(file, "to_date(0) + interval 100 quarter", &[]);
run_ast(file, "to_date(0) + interval 100 month", &[]);
run_ast(file, "to_date(0) + interval 100 day", &[]);
run_ast(file, "to_date(0) - interval 100 year", &[]);
run_ast(file, "to_date(0) - interval 100 quarter", &[]);
run_ast(file, "to_date(0) - interval 100 month", &[]);
run_ast(file, "to_date(0) - interval 100 day", &[]);
run_ast(file, "a + interval b year", &[
("a", DataType::Date, from_date_data(vec![-100, 0, 100])),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a + interval b quarter", &[
("a", DataType::Date, from_date_data(vec![-100, 0, 100])),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a + interval b month", &[
("a", DataType::Date, from_date_data(vec![-100, 0, 100])),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a + interval b day", &[
("a", DataType::Date, from_date_data(vec![-100, 0, 100])),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a - interval b year", &[
("a", DataType::Date, from_date_data(vec![-100, 0, 100])),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a - interval b quarter", &[
("a", DataType::Date, from_date_data(vec![-100, 0, 100])),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a - interval b month", &[
("a", DataType::Date, from_date_data(vec![-100, 0, 100])),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a - interval b day", &[
("a", DataType::Date, from_date_data(vec![-100, 0, 100])),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
}

fn test_timestamp_arith(file: &mut impl Write) {
run_ast(file, "to_timestamp(0) + interval 10000 year", &[]); // failed
run_ast(file, "to_timestamp(0) + interval 100 year", &[]);
run_ast(file, "to_timestamp(0) + interval 100 quarter", &[]);
run_ast(file, "to_timestamp(0) + interval 100 month", &[]);
run_ast(file, "to_timestamp(0) + interval 100 day", &[]);
run_ast(file, "to_timestamp(0) + interval 100 hour", &[]);
run_ast(file, "to_timestamp(0) + interval 100 minute", &[]);
run_ast(file, "to_timestamp(0) + interval 100 second", &[]);
run_ast(file, "to_timestamp(0) - interval 100 year", &[]);
run_ast(file, "to_timestamp(0) - interval 100 quarter", &[]);
run_ast(file, "to_timestamp(0) - interval 100 month", &[]);
run_ast(file, "to_timestamp(0) - interval 100 day", &[]);
run_ast(file, "to_timestamp(0) - interval 100 hour", &[]);
run_ast(file, "to_timestamp(0) - interval 100 minute", &[]);
run_ast(file, "to_timestamp(0) - interval 100 second", &[]);
run_ast(file, "a + interval b year", &[
(
"a",
DataType::Timestamp,
from_timestamp_data(vec![-100, 0, 100]),
),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a + interval b quarter", &[
(
"a",
DataType::Timestamp,
from_timestamp_data(vec![-100, 0, 100]),
),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a + interval b month", &[
(
"a",
DataType::Timestamp,
from_timestamp_data(vec![-100, 0, 100]),
),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a + interval b day", &[
(
"a",
DataType::Timestamp,
from_timestamp_data(vec![-100, 0, 100]),
),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a + interval b hour", &[
(
"a",
DataType::Timestamp,
from_timestamp_data(vec![-100, 0, 100]),
),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a + interval b minute", &[
(
"a",
DataType::Timestamp,
from_timestamp_data(vec![-100, 0, 100]),
),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a + interval b second", &[
(
"a",
DataType::Timestamp,
from_timestamp_data(vec![-100, 0, 100]),
),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a - interval b year", &[
(
"a",
DataType::Timestamp,
from_timestamp_data(vec![-100, 0, 100]),
),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a - interval b quarter", &[
(
"a",
DataType::Timestamp,
from_timestamp_data(vec![-100, 0, 100]),
),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a - interval b month", &[
(
"a",
DataType::Timestamp,
from_timestamp_data(vec![-100, 0, 100]),
),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a - interval b day", &[
(
"a",
DataType::Timestamp,
from_timestamp_data(vec![-100, 0, 100]),
),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a - interval b hour", &[
(
"a",
DataType::Timestamp,
from_timestamp_data(vec![-100, 0, 100]),
),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a - interval b minute", &[
(
"a",
DataType::Timestamp,
from_timestamp_data(vec![-100, 0, 100]),
),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
run_ast(file, "a - interval b second", &[
(
"a",
DataType::Timestamp,
from_timestamp_data(vec![-100, 0, 100]),
),
(
"b",
DataType::Number(NumberDataType::Int32),
Column::from_data(vec![1, 2, 3]),
),
]);
}
Loading

0 comments on commit 32a251c

Please sign in to comment.