diff --git a/src/query/src/range_select/plan_rewrite.rs b/src/query/src/range_select/plan_rewrite.rs index ca7689af20b7..023b61ad095e 100644 --- a/src/query/src/range_select/plan_rewrite.rs +++ b/src/query/src/range_select/plan_rewrite.rs @@ -342,6 +342,11 @@ impl RangePlanRewriter { .row_key_column_names() .map(|key| Expr::Column(Column::new(Some(table_ref.clone()), key))) .collect(); + // If the user does not specify a primary key when creating a table, + // then by default all data will be aggregated into one time series, which is equivalent to using `by(1)` in SQL + if default_by.is_empty() { + default_by = vec![Expr::Literal(ScalarValue::Int64(Some(1)))]; + } time_index_expr = Expr::Column(Column::new( Some(table_ref.clone()), time_index_column.name.clone(), diff --git a/tests/cases/standalone/common/range/by.result b/tests/cases/standalone/common/range/by.result index d3f1d7c77810..3c1d83d0bc37 100644 --- a/tests/cases/standalone/common/range/by.result +++ b/tests/cases/standalone/common/range/by.result @@ -69,3 +69,39 @@ DROP TABLE host; Affected Rows: 0 +-- Test no primary key and by keyword +CREATE TABLE host ( + ts timestamp(3) time index, + host STRING, + val BIGINT, +); + +Affected Rows: 0 + +INSERT INTO TABLE host VALUES + (0, 'host1', 0), + (5000, 'host1', null), + (10000, 'host1', 1), + (15000, 'host1', null), + (20000, 'host1', 2), + (0, 'host2', 3), + (5000, 'host2', null), + (10000, 'host2', 4), + (15000, 'host2', null), + (20000, 'host2', 5); + +Affected Rows: 10 + +SELECT ts, max(val) RANGE '5s' FROM host ALIGN '20s' ORDER BY ts; + ++---------------------+----------------------------------+ +| ts | MAX(host.val) RANGE 5s FILL NULL | ++---------------------+----------------------------------+ +| 1970-01-01T00:00:00 | 3 | +| 1970-01-01T00:00:20 | 5 | ++---------------------+----------------------------------+ + +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 1ee99924cc69..0edb95534ea4 100644 --- a/tests/cases/standalone/common/range/by.sql +++ b/tests/cases/standalone/common/range/by.sql @@ -34,3 +34,27 @@ SELECT ts, CAST(length(host) as INT64) + 2, max(val) RANGE '5s' FROM host ALIGN SELECT ts, host, max(val) RANGE '5s' FROM host ALIGN '20s' BY () ORDER BY ts; DROP TABLE host; + +-- Test no primary key and by keyword + +CREATE TABLE host ( + ts timestamp(3) time index, + host STRING, + val BIGINT, +); + +INSERT INTO TABLE host VALUES + (0, 'host1', 0), + (5000, 'host1', null), + (10000, 'host1', 1), + (15000, 'host1', null), + (20000, 'host1', 2), + (0, 'host2', 3), + (5000, 'host2', null), + (10000, 'host2', 4), + (15000, 'host2', null), + (20000, 'host2', 5); + +SELECT ts, max(val) RANGE '5s' FROM host ALIGN '20s' ORDER BY ts; + +DROP TABLE host; \ No newline at end of file diff --git a/tests/cases/standalone/common/range/error.result b/tests/cases/standalone/common/range/error.result index cf720ee9f1e3..eeead0c8b2d8 100644 --- a/tests/cases/standalone/common/range/error.result +++ b/tests/cases/standalone/common/range/error.result @@ -35,6 +35,10 @@ SELECT min(val) FROM host ALIGN '5s'; Error: 2000(InvalidSyntax), sql parser error: Illegal Range select, no RANGE keyword found in any SelectItem +SELECT 1 FROM host ALIGN '5s'; + +Error: 2000(InvalidSyntax), sql parser error: Illegal Range select, no RANGE keyword found in any SelectItem + SELECT min(val) RANGE '10s', max(val) FROM host ALIGN '5s'; Error: 3001(EngineExecuteQuery), No field named "MAX(host.val)". Valid fields are "MIN(host.val) RANGE 10s FILL NULL", host.ts, host.host. diff --git a/tests/cases/standalone/common/range/error.sql b/tests/cases/standalone/common/range/error.sql index 19cf55d2285e..86ceda4ea1ab 100644 --- a/tests/cases/standalone/common/range/error.sql +++ b/tests/cases/standalone/common/range/error.sql @@ -28,6 +28,8 @@ SELECT min(val) RANGE '5s' FROM host ALIGN 'not_time'; SELECT min(val) FROM host ALIGN '5s'; +SELECT 1 FROM host ALIGN '5s'; + SELECT min(val) RANGE '10s', max(val) FROM host ALIGN '5s'; SELECT min(val) * 2 RANGE '10s' FROM host ALIGN '5s';