Files
greptimedb/tests/cases/standalone/common/range/error.result
T
dayan02devanddennis zhuang f1b4dc695e fix(query): stop reporting CTEs as missing tables in range queries (#10397)
* test(query): cover range queries over derived inputs without a time index

A range query over a CTE or an aliased subquery whose columns carry no
time index fails with `Table not found: greptime.public.<alias>` (#5839),
while the same input without an alias reports `Cannot find time index
column`. Plan all three shapes in a planner test and expect the time
index error, and add the CTE case to the `range/error` sqlness case.

Signed-off-by: dayan02dev <pawarudayan92@gmail.com>

* fix(query): report missing time index for range queries over CTEs

`RangePlanRewriter::get_index_by` resolves each field qualifier of the
range input as a table. A CTE or subquery alias is not a table, and the
resulting `TableNotExist` was skipped only when some field still carried
time index metadata. Without that metadata it was returned as is, so a
range query over such a CTE failed with `Table not found:
greptime.public.<cte>` (#5839).

Skip qualifiers that are not tables in every case. The input then falls
through to the existing time index metadata fallback or to
`TimeIndexNotFound`, which the same input already gets without an alias.

Signed-off-by: dayan02dev <pawarudayan92@gmail.com>

* test(query): drop derived-input time index unit test covered by sqlness

Signed-off-by: dayan02dev <pawarudayan92@gmail.com>

---------

Signed-off-by: dayan02dev <pawarudayan92@gmail.com>
Co-authored-by: dennis zhuang <killme2008@gmail.com>
2026-11-23 20:20:51 +00:00

128 lines
4.7 KiB
Plaintext

CREATE TABLE host (
ts timestamp(3) time index,
host STRING PRIMARY KEY,
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
-- Test Invalid cases
-- 1. error timestamp
SELECT min(val) RANGE 'not_time' FROM host ALIGN '5s';
Error: 2000(InvalidSyntax), Invalid SQL syntax: sql parser error: not a valid duration string: not_time
SELECT min(val) RANGE '5s' FROM host ALIGN 'not_time';
Error: 2000(InvalidSyntax), Invalid SQL syntax: sql parser error: not a valid duration string: not_time
-- 2.1 no range param
SELECT min(val) FROM host ALIGN '5s';
Error: 2000(InvalidSyntax), Invalid SQL syntax: sql parser error: Illegal range select: no RANGE keyword found in any SELECT item
SELECT 1 FROM host ALIGN '5s';
Error: 2000(InvalidSyntax), Invalid SQL syntax: sql parser error: Illegal range select: no RANGE keyword found in any SELECT item
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", host.ts, host.host.
SELECT min(val) * 2 RANGE '10s' FROM host ALIGN '5s';
Error: 2000(InvalidSyntax), Invalid SQL syntax: sql parser error: Can't use RANGE in expression 2 without a function
SELECT 1 RANGE '10s' FILL NULL FROM host ALIGN '1h' FILL NULL;
Error: 2000(InvalidSyntax), Invalid SQL syntax: sql parser error: Can't use RANGE in expression 1 without a function
-- 2.2 no align param
SELECT min(val) RANGE '5s' FROM host;
Error: 2000(InvalidSyntax), Invalid SQL syntax: sql parser error: ALIGN argument cannot be omitted in the range select query
SELECT min(val) RANGE '5s' FILL PREV FROM host;
Error: 2000(InvalidSyntax), Invalid SQL syntax: sql parser error: ALIGN argument cannot be omitted in the range select query
SELECT tmp.ts, tmp.host, min(tmp.val) RANGE '5s'
FROM (SELECT ts, host, val FROM host) AS tmp
ALIGN '5s';
Error: 2000(InvalidSyntax), Range Query: Cannot infer default BY columns from derived range query input
-- 2.3 type mismatch
SELECT covar(ceil(val), floor(val)) RANGE '20s' FROM host ALIGN '10s';
+------------------------------------------------------+
| covar_samp(ceil(host.val),floor(host.val)) RANGE 20s |
+------------------------------------------------------+
| |
| 0.5 |
| 0.5 |
| |
| |
| 0.5 |
| 0.5 |
| |
+------------------------------------------------------+
-- 2.4 nest query
SELECT min(max(val) RANGE '20s') RANGE '20s' FROM host ALIGN '10s';
Error: 3000(PlanQuery), Failed to plan SQL: Error during planning: Aggregate function calls cannot be nested: 'max(host.val)' is nested inside 'min(range_fn(max(host.val), Utf8("20s"), Utf8("")))'
-- 2.5 wrong Aggregate
SELECT rank() OVER (PARTITION BY host ORDER BY ts DESC) RANGE '10s' FROM host ALIGN '5s';
Error: 2000(InvalidSyntax), Range Query: Window functions is not allowed in Range Query
-- 2.6 invalid fill
SELECT min(val) RANGE '5s' FROM host ALIGN '5s' FILL 3.0;
Error: 3000(PlanQuery), Error during planning: 3.0 is not a valid fill option, fail to convert to a const value. { Arrow error: Cast error: Cannot cast string '3.0' to value of Int64 type }
-- 2.7 zero align/range
SELECT min(val) RANGE '5s' FROM host ALIGN '0s';
Error: 3000(PlanQuery), Error during planning: duration must be greater than 0
SELECT min(val) RANGE '0s' FROM host ALIGN '5s';
Error: 3000(PlanQuery), Error during planning: duration must be greater than 0
SELECT min(val) RANGE '5s' FROM host ALIGN (INTERVAL '0' day);
Error: 3000(PlanQuery), Error during planning: Illegal argument `IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 0, nanoseconds: 0 }")` in range select query
SELECT min(val) RANGE (INTERVAL '0' day) FROM host ALIGN '5s';
Error: 3000(PlanQuery), Error during planning: Illegal argument `IntervalMonthDayNano("IntervalMonthDayNano { months: 0, days: 0, nanoseconds: 0 }")` in range select query
-- 2.8 derived input without time index
WITH tmp AS (SELECT now() AS ts, 1 AS val)
SELECT ts, min(val) RANGE '5s' FROM tmp ALIGN '5s' BY (1);
Error: 1004(InvalidArguments), Cannot find time index column in table fields:[tmp.ts, tmp.val], metadata:{}
DROP TABLE host;
Affected Rows: 0