From bbaae9223ac2a4d2821d06fed1dc78e3e0ee1d02 Mon Sep 17 00:00:00 2001 From: WU Jingdi Date: Mon, 6 Nov 2023 11:45:33 +0800 Subject: [PATCH] feat: enable empty range by (#2697) --- Cargo.lock | 14 +++++++------- Cargo.toml | 2 +- tests/cases/standalone/common/range/by.result | 17 +++++++++++++++++ tests/cases/standalone/common/range/by.sql | 9 +++++++++ 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b6bdd8082..89639ff117 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1883,7 +1883,7 @@ dependencies = [ "datatypes", "serde", "snafu", - "sqlparser 0.38.0 (git+https://github.com/GreptimeTeam/sqlparser-rs.git?rev=602d7878c9949e48512251c7f18695a50936e51c)", + "sqlparser 0.38.0 (git+https://github.com/GreptimeTeam/sqlparser-rs.git?rev=0fbae07d0c46dc18e3381c406d8b9b8abef6b1fd)", "sqlparser_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "statrs", "tokio", @@ -3322,7 +3322,7 @@ dependencies = [ "session", "snafu", "sql", - "sqlparser 0.38.0 (git+https://github.com/GreptimeTeam/sqlparser-rs.git?rev=602d7878c9949e48512251c7f18695a50936e51c)", + "sqlparser 0.38.0 (git+https://github.com/GreptimeTeam/sqlparser-rs.git?rev=0fbae07d0c46dc18e3381c406d8b9b8abef6b1fd)", "storage", "store-api", "strfmt", @@ -5529,7 +5529,7 @@ dependencies = [ "session", "snafu", "sql", - "sqlparser 0.38.0 (git+https://github.com/GreptimeTeam/sqlparser-rs.git?rev=602d7878c9949e48512251c7f18695a50936e51c)", + "sqlparser 0.38.0 (git+https://github.com/GreptimeTeam/sqlparser-rs.git?rev=0fbae07d0c46dc18e3381c406d8b9b8abef6b1fd)", "storage", "store-api", "substrait 0.4.2", @@ -8551,7 +8551,7 @@ dependencies = [ "once_cell", "regex", "snafu", - "sqlparser 0.38.0 (git+https://github.com/GreptimeTeam/sqlparser-rs.git?rev=602d7878c9949e48512251c7f18695a50936e51c)", + "sqlparser 0.38.0 (git+https://github.com/GreptimeTeam/sqlparser-rs.git?rev=0fbae07d0c46dc18e3381c406d8b9b8abef6b1fd)", "sqlparser_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "table", ] @@ -8614,13 +8614,13 @@ dependencies = [ [[package]] name = "sqlparser" version = "0.38.0" -source = "git+https://github.com/GreptimeTeam/sqlparser-rs.git?rev=602d7878c9949e48512251c7f18695a50936e51c#602d7878c9949e48512251c7f18695a50936e51c" +source = "git+https://github.com/GreptimeTeam/sqlparser-rs.git?rev=0fbae07d0c46dc18e3381c406d8b9b8abef6b1fd#0fbae07d0c46dc18e3381c406d8b9b8abef6b1fd" dependencies = [ "lazy_static", "log", "regex", "sqlparser 0.38.0 (registry+https://github.com/rust-lang/crates.io-index)", - "sqlparser_derive 0.1.1 (git+https://github.com/GreptimeTeam/sqlparser-rs.git?rev=602d7878c9949e48512251c7f18695a50936e51c)", + "sqlparser_derive 0.1.1 (git+https://github.com/GreptimeTeam/sqlparser-rs.git?rev=0fbae07d0c46dc18e3381c406d8b9b8abef6b1fd)", ] [[package]] @@ -8637,7 +8637,7 @@ dependencies = [ [[package]] name = "sqlparser_derive" version = "0.1.1" -source = "git+https://github.com/GreptimeTeam/sqlparser-rs.git?rev=602d7878c9949e48512251c7f18695a50936e51c#602d7878c9949e48512251c7f18695a50936e51c" +source = "git+https://github.com/GreptimeTeam/sqlparser-rs.git?rev=0fbae07d0c46dc18e3381c406d8b9b8abef6b1fd#0fbae07d0c46dc18e3381c406d8b9b8abef6b1fd" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 7944b098aa..9d4b42901e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -109,7 +109,7 @@ serde_json = "1.0" smallvec = "1" snafu = { version = "0.7", features = ["backtraces"] } # on branch v0.38.x -sqlparser = { git = "https://github.com/GreptimeTeam/sqlparser-rs.git", rev = "602d7878c9949e48512251c7f18695a50936e51c", features = [ +sqlparser = { git = "https://github.com/GreptimeTeam/sqlparser-rs.git", rev = "0fbae07d0c46dc18e3381c406d8b9b8abef6b1fd", features = [ "visitor", ] } strum = { version = "0.25", features = ["derive"] } diff --git a/tests/cases/standalone/common/range/by.result b/tests/cases/standalone/common/range/by.result index 251f3a67e4..d3f1d7c778 100644 --- a/tests/cases/standalone/common/range/by.result +++ b/tests/cases/standalone/common/range/by.result @@ -39,6 +39,17 @@ SELECT ts, max(val) RANGE '5s' FROM host ALIGN '20s' BY (2) ORDER BY ts; | 1970-01-01T00:00:20 | 5 | +---------------------+----------------------------------+ +-- The user explicitly specifies that the aggregation key is empty. In this case, there is no aggregation key. All data will be aggregated into a group. +-- Implement by rewrite `BY()` to `BY(1)` automatically through sqlparser. They are semantically equivalent. +SELECT ts, max(val) RANGE '5s' FROM host ALIGN '20s' BY () ORDER BY ts; + ++---------------------+----------------------------------+ +| ts | MAX(host.val) RANGE 5s FILL NULL | ++---------------------+----------------------------------+ +| 1970-01-01T00:00:00 | 3 | +| 1970-01-01T00:00:20 | 5 | ++---------------------+----------------------------------+ + SELECT ts, CAST(length(host) as INT64) + 2, max(val) RANGE '5s' FROM host ALIGN '20s' BY (CAST(length(host) as INT64) + 2) ORDER BY ts; +---------------------+----------------------------------------+----------------------------------+ @@ -48,6 +59,12 @@ SELECT ts, CAST(length(host) as INT64) + 2, max(val) RANGE '5s' FROM host ALIGN | 1970-01-01T00:00:20 | 7 | 5 | +---------------------+----------------------------------------+----------------------------------+ +-- Test error +-- project non-aggregation key +SELECT ts, host, max(val) RANGE '5s' FROM host ALIGN '20s' BY () ORDER BY ts; + +Error: 3001(EngineExecuteQuery), No field named host.host. Valid fields are "MAX(host.val) RANGE 5s FILL NULL", host.ts, "Int64(1)". + DROP TABLE host; Affected Rows: 0 diff --git a/tests/cases/standalone/common/range/by.sql b/tests/cases/standalone/common/range/by.sql index 0ffa2afc82..1ee99924cc 100644 --- a/tests/cases/standalone/common/range/by.sql +++ b/tests/cases/standalone/common/range/by.sql @@ -22,6 +22,15 @@ SELECT ts, length(host), max(val) RANGE '5s' FROM host ALIGN '20s' BY (length(ho SELECT ts, max(val) RANGE '5s' FROM host ALIGN '20s' BY (2) ORDER BY ts; +-- The user explicitly specifies that the aggregation key is empty. In this case, there is no aggregation key. All data will be aggregated into a group. +-- Implement by rewrite `BY()` to `BY(1)` automatically through sqlparser. They are semantically equivalent. +SELECT ts, max(val) RANGE '5s' FROM host ALIGN '20s' BY () ORDER BY ts; + SELECT ts, CAST(length(host) as INT64) + 2, max(val) RANGE '5s' FROM host ALIGN '20s' BY (CAST(length(host) as INT64) + 2) ORDER BY ts; +-- Test error + +-- project non-aggregation key +SELECT ts, host, max(val) RANGE '5s' FROM host ALIGN '20s' BY () ORDER BY ts; + DROP TABLE host;