Skip to content

Commit

Permalink
feat(cubesql): Support float*interval and interval/float
Browse files Browse the repository at this point in the history
  • Loading branch information
mcheshkov committed Jul 22, 2024
1 parent 149eb01 commit 7ad8ef9
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 29 deletions.
16 changes: 8 additions & 8 deletions packages/cubejs-backend-native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions rust/cubesql/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/cubesql/cubesql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ homepage = "https://cube.dev"

[dependencies]
arc-swap = "1"
datafusion = { git = 'https://github.com/cube-js/arrow-datafusion.git', rev = "f99263552906b5b9fb22d679cb466876057d95e9", default-features = false, features = ["regex_expressions", "unicode_expressions"] }
datafusion = { git = 'https://github.com/cube-js/arrow-datafusion.git', rev = "bba28d60b4a048afb18944cc0ded1141437bd7c3", default-features = false, features = ["regex_expressions", "unicode_expressions"] }
anyhow = "1.0"
thiserror = "1.0.50"
cubeclient = { path = "../cubeclient" }
Expand Down
38 changes: 37 additions & 1 deletion rust/cubesql/cubesql/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10153,7 +10153,7 @@ ORDER BY
async fn test_interval_mul() -> Result<(), CubeError> {
let base_timestamp = "TO_TIMESTAMP('2020-01-01 00:00:00', 'yyyy-MM-dd HH24:mi:ss')";
let units = vec!["year", "month", "week", "day", "hour", "minute", "second"];
let multiplicands = vec![1, 5, -10];
let multiplicands = vec!["1", "5", "-10", "1.5"];

let selects = units
.iter()
Expand Down Expand Up @@ -10185,6 +10185,42 @@ ORDER BY
Ok(())
}

#[tokio::test]
async fn test_interval_div() -> Result<(), CubeError> {
let base_timestamp = "TO_TIMESTAMP('2020-01-01 00:00:00', 'yyyy-MM-dd HH24:mi:ss')";
let units = vec!["year", "month", "week", "day", "hour", "minute", "second"];
let divisors = vec!["1.5"];

let selects = units
.iter()
.enumerate()
.map(|(i, unit)| {
let columns = divisors
.iter()
.map(|divisor| {
// Brackets around interval are necessary due to bug in sqlparser
// See https://github.com/sqlparser-rs/sqlparser-rs/issues/1345
// TODO remove brackets once fixed
format!(
"{base_timestamp} + (interval '1 {unit}') / {divisor} AS \"i/{divisor}\""
)
})
.collect::<Vec<_>>();
format!(
"SELECT {i} AS id, '{unit}' AS unit, {}",
columns.join(", ")
)
})
.collect::<Vec<_>>();
let query = format!("{} ORDER BY id ASC", selects.join(" UNION ALL "));
insta::assert_snapshot!(
"interval_div",
execute_query(query, DatabaseProtocol::PostgreSQL).await?
);

Ok(())
}

#[tokio::test]
async fn test_interval_sum() -> Result<(), CubeError> {
init_logger();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: cubesql/src/compile/mod.rs
expression: "execute_query(query, DatabaseProtocol::PostgreSQL).await?"
---
+----+--------+-------------------------+
| id | unit | i/1.5 |
+----+--------+-------------------------+
| 0 | year | 2020-09-01T00:00:00.000 |
| 1 | month | 2020-01-21T00:00:00.000 |
| 2 | week | 2020-01-05T16:00:00.000 |
| 3 | day | 2020-01-01T16:00:00.000 |
| 4 | hour | 2020-01-01T00:40:00.000 |
| 5 | minute | 2020-01-01T00:00:40.000 |
| 6 | second | 2020-01-01T00:00:00.666 |
+----+--------+-------------------------+
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
source: cubesql/src/compile/mod.rs
expression: "execute_query(query, DatabaseProtocol::PostgreSQL).await?"
---
+----+--------+-------------------------+-------------------------+-------------------------+
| id | unit | i*1 | i*5 | i*-10 |
+----+--------+-------------------------+-------------------------+-------------------------+
| 0 | year | 2021-01-01T00:00:00.000 | 2025-01-01T00:00:00.000 | 2010-01-01T00:00:00.000 |
| 1 | month | 2020-02-01T00:00:00.000 | 2020-06-01T00:00:00.000 | 2019-03-01T00:00:00.000 |
| 2 | week | 2020-01-08T00:00:00.000 | 2020-02-05T00:00:00.000 | 2019-10-23T00:00:00.000 |
| 3 | day | 2020-01-02T00:00:00.000 | 2020-01-06T00:00:00.000 | 2019-12-22T00:00:00.000 |
| 4 | hour | 2020-01-01T01:00:00.000 | 2020-01-01T05:00:00.000 | 2019-12-31T14:00:00.000 |
| 5 | minute | 2020-01-01T00:01:00.000 | 2020-01-01T00:05:00.000 | 2019-12-31T23:50:00.000 |
| 6 | second | 2020-01-01T00:00:01.000 | 2020-01-01T00:00:05.000 | 2019-12-31T23:59:50.000 |
+----+--------+-------------------------+-------------------------+-------------------------+
+----+--------+-------------------------+-------------------------+-------------------------+-------------------------+
| id | unit | i*1 | i*5 | i*-10 | i*1.5 |
+----+--------+-------------------------+-------------------------+-------------------------+-------------------------+
| 0 | year | 2021-01-01T00:00:00.000 | 2025-01-01T00:00:00.000 | 2010-01-01T00:00:00.000 | 2021-07-01T00:00:00.000 |
| 1 | month | 2020-02-01T00:00:00.000 | 2020-06-01T00:00:00.000 | 2019-03-01T00:00:00.000 | 2020-02-16T00:00:00.000 |
| 2 | week | 2020-01-08T00:00:00.000 | 2020-02-05T00:00:00.000 | 2019-10-23T00:00:00.000 | 2020-01-11T12:00:00.000 |
| 3 | day | 2020-01-02T00:00:00.000 | 2020-01-06T00:00:00.000 | 2019-12-22T00:00:00.000 | 2020-01-02T12:00:00.000 |
| 4 | hour | 2020-01-01T01:00:00.000 | 2020-01-01T05:00:00.000 | 2019-12-31T14:00:00.000 | 2020-01-01T01:30:00.000 |
| 5 | minute | 2020-01-01T00:01:00.000 | 2020-01-01T00:05:00.000 | 2019-12-31T23:50:00.000 | 2020-01-01T00:01:30.000 |
| 6 | second | 2020-01-01T00:00:01.000 | 2020-01-01T00:00:05.000 | 2019-12-31T23:59:50.000 | 2020-01-01T00:00:01.500 |
+----+--------+-------------------------+-------------------------+-------------------------+-------------------------+

0 comments on commit 7ad8ef9

Please sign in to comment.