Skip to content

Commit

Permalink
minor: port date_bin tests to sqllogictests (#5115)
Browse files Browse the repository at this point in the history
* minor: port date_bin tests to sqllogictests

* Add change to trigger CI

* Update datafusion/core/tests/sqllogictests/test_files/timestamps.slt

Co-authored-by: xudong.w <wxd963996380@gmail.com>

---------

Co-authored-by: xudong.w <wxd963996380@gmail.com>
  • Loading branch information
alamb and xudong963 authored Feb 1, 2023
1 parent bd64527 commit 11e8906
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 111 deletions.
111 changes: 0 additions & 111 deletions datafusion/core/tests/sql/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1243,117 +1243,6 @@ async fn to_timestamp_seconds_i32() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn date_bin() {
let ctx = SessionContext::new();

let sql = "SELECT DATE_BIN(INTERVAL '15 minutes', TIMESTAMP '2022-08-03 14:38:50Z', TIMESTAMP '1970-01-01T00:00:00Z') AS res";
let results = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+---------------------+",
"| res |",
"+---------------------+",
"| 2022-08-03T14:30:00 |",
"+---------------------+",
];
assert_batches_eq!(expected, &results);

// Shift forward by 5 minutes
let sql = "SELECT DATE_BIN(INTERVAL '15 minutes', TIMESTAMP '2022-08-03 14:38:50Z', TIMESTAMP '1970-01-01T00:05:00Z') AS res";
let results = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+---------------------+",
"| res |",
"+---------------------+",
"| 2022-08-03T14:35:00 |",
"+---------------------+",
];
assert_batches_eq!(expected, &results);

// Shift backward by 5 minutes
let sql = "SELECT DATE_BIN(INTERVAL '15 minutes', TIMESTAMP '2022-08-03 14:38:50Z', TIMESTAMP '1970-01-01T23:55:00Z') AS res";
let results = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+---------------------+",
"| res |",
"+---------------------+",
"| 2022-08-03T14:25:00 |",
"+---------------------+",
];
assert_batches_eq!(expected, &results);

// origin after source, timestamp in previous bucket
let sql = "SELECT DATE_BIN(INTERVAL '15 minutes', TIMESTAMP '2022-08-03 14:38:50Z', TIMESTAMP '2022-08-03 14:40:00Z') AS res";
let results = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+---------------------+",
"| res |",
"+---------------------+",
"| 2022-08-03T14:25:00 |",
"+---------------------+",
];
assert_batches_eq!(expected, &results);

// stride by 7 days
let sql = "SELECT DATE_BIN(INTERVAL '7 days', TIMESTAMP '2022-08-03 14:38:50Z', TIMESTAMP '1970-01-01 00:00:00Z') AS res";
let results = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+---------------------+",
"| res |",
"+---------------------+",
"| 2022-07-28T00:00:00 |",
"+---------------------+",
];
assert_batches_eq!(expected, &results);

// origin shifts bins forward 1 day
let sql = "SELECT DATE_BIN(INTERVAL '7 days', TIMESTAMP '2022-08-03 14:38:50Z', TIMESTAMP '1970-01-02 00:00:00Z') AS res";
let results = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+---------------------+",
"| res |",
"+---------------------+",
"| 2022-07-29T00:00:00 |",
"+---------------------+",
];
assert_batches_eq!(expected, &results);

// following test demonstrates array values for the source argument
let sql = "SELECT
DATE_BIN(INTERVAL '15' minute, time, TIMESTAMP '2001-01-01T00:00:00Z') AS time,
val
FROM (
VALUES
(TIMESTAMP '2021-06-10 17:05:00Z', 0.5),
(TIMESTAMP '2021-06-10 17:19:10Z', 0.3)
) as t (time, val)";
let results = execute_to_batches(&ctx, sql).await;
let expected = vec![
"+---------------------+-----+",
"| time | val |",
"+---------------------+-----+",
"| 2021-06-10T17:00:00 | 0.5 |",
"| 2021-06-10T17:15:00 | 0.3 |",
"+---------------------+-----+",
];
assert_batches_eq!(expected, &results);

// following test demonstrates array values for the origin argument are not currently supported
let sql = "SELECT
DATE_BIN(INTERVAL '15' minute, time, origin) AS time,
val
FROM (
VALUES
(TIMESTAMP '2021-06-10 17:05:00Z', TIMESTAMP '2001-01-01T00:00:00Z', 0.5),
(TIMESTAMP '2021-06-10 17:19:10Z', TIMESTAMP '2001-01-01T00:00:00Z', 0.3)
) as t (time, origin, val)";
let result = try_execute_to_batches(&ctx, sql).await;
assert_eq!(
result.err().unwrap().to_string(),
"This feature is not implemented: DATE_BIN only supports literal values for the origin argument, not arrays"
);
}

#[tokio::test]
async fn timestamp_add_interval_second() -> Result<()> {
let ctx = SessionContext::new();
Expand Down
66 changes: 66 additions & 0 deletions datafusion/core/tests/sqllogictests/test_files/timestamps.slt
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,69 @@ select * from foo where ts != '2000-02-01T00:00:00';

statement ok
drop table foo;

###
## test date_bin function
###
query T
SELECT DATE_BIN(INTERVAL '15 minutes', TIMESTAMP '2022-08-03 14:38:50Z', TIMESTAMP '1970-01-01T00:00:00Z') AS res
----
2022-08-03T14:30:00

# Shift forward by 5 minutes
query T
SELECT DATE_BIN(INTERVAL '15 minutes', TIMESTAMP '2022-08-03 14:38:50Z', TIMESTAMP '1970-01-01T00:05:00Z') AS res
----
2022-08-03T14:35:00


# Shift backward by 5 minutes
query T
SELECT DATE_BIN(INTERVAL '15 minutes', TIMESTAMP '2022-08-03 14:38:50Z', TIMESTAMP '1970-01-01T23:55:00Z') AS res
----
2022-08-03T14:25:00

# origin after source, timestamp in previous bucket
query T
SELECT DATE_BIN(INTERVAL '15 minutes', TIMESTAMP '2022-08-03 14:38:50Z', TIMESTAMP '2022-08-03 14:40:00Z') AS res
----
2022-08-03T14:25:00

# stride by 7 days
query T
SELECT DATE_BIN(INTERVAL '7 days', TIMESTAMP '2022-08-03 14:38:50Z', TIMESTAMP '1970-01-01 00:00:00Z') AS res
----
2022-07-28T00:00:00


# origin shifts bins forward 1 day
query T
SELECT DATE_BIN(INTERVAL '7 days', TIMESTAMP '2022-08-03 14:38:50Z', TIMESTAMP '1970-01-02 00:00:00Z') AS res
----
2022-07-29T00:00:00


# demonstrates array values (rather than scalar) for the source argument
query T rowsort
SELECT
DATE_BIN(INTERVAL '15' minute, time, TIMESTAMP '2001-01-01T00:00:00Z') AS time,
val
FROM (
VALUES
(TIMESTAMP '2021-06-10 17:05:00Z', 0.5),
(TIMESTAMP '2021-06-10 17:19:10Z', 0.3)
) as t (time, val)
----
2021-06-10T17:00:00 0.5
2021-06-10T17:15:00 0.3

# demonstrates array values for the origin argument are not currently supported
statement error This feature is not implemented: DATE_BIN only supports literal values for the origin argument, not arrays
SELECT
DATE_BIN(INTERVAL '15' minute, time, origin) AS time,
val
FROM (
VALUES
(TIMESTAMP '2021-06-10 17:05:00Z', TIMESTAMP '2001-01-01T00:00:00Z', 0.5),
(TIMESTAMP '2021-06-10 17:19:10Z', TIMESTAMP '2001-01-01T00:00:00Z', 0.3)
) as t (time, origin, val)

0 comments on commit 11e8906

Please sign in to comment.