Skip to content

Commit

Permalink
fix: empty by in range query (#2770)
Browse files Browse the repository at this point in the history
* fix: empty by in range query

* Apply suggestions from code review

---------

Co-authored-by: Ruihang Xia <[email protected]>
  • Loading branch information
Taylor-lagrange and waynexia authored Nov 17, 2023
1 parent 500e299 commit 91820a8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/query/src/range_select/plan_rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ 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(),
Expand Down
36 changes: 36 additions & 0 deletions tests/cases/standalone/common/range/by.result
Original file line number Diff line number Diff line change
Expand Up @@ -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

24 changes: 24 additions & 0 deletions tests/cases/standalone/common/range/by.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 4 additions & 0 deletions tests/cases/standalone/common/range/error.result
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/standalone/common/range/error.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 91820a8

Please sign in to comment.