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 371e123
Show file tree
Hide file tree
Showing 3 changed files with 1,077 additions and 40 deletions.
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 371e123

Please sign in to comment.