diff --git a/docs/en/14-reference/09-error-code.md b/docs/en/14-reference/09-error-code.md index 1d3ea3f9a1..7a171f47be 100644 --- a/docs/en/14-reference/09-error-code.md +++ b/docs/en/14-reference/09-error-code.md @@ -458,6 +458,10 @@ This document details the server error codes that may be encountered when using | 0x80002665 | The _TAGS pseudocolumn can only be used for subtable and supertable queries | Illegal tag column query | Check and correct the SQL statement | | 0x80002666 | Subquery does not output primary timestamp column | Check and correct the SQL statement | | | 0x80002667 | Invalid usage of expr: %s | Illegal expression | Check and correct the SQL statement | +| 0x80002687 | Invalid using cols function | Illegal using cols function | Check and correct the SQL statement | +| 0x80002688 | Cols function's first param must be a select function | The first parameter of the cols function should be a selection function | Check and correct the SQL statement | +| 0x80002689 | Invalid using cols function with multiple output columns | Illegal using the cols function for multiple column output | Check and correct the SQL statement | +| 0x80002690 | Invalid using alias for cols function | Illegal cols function alias | Check and correct the SQL statement | | 0x800026FF | Parser internal error | Internal error in parser | Preserve the scene and logs, report issue on GitHub | | 0x80002700 | Planner internal error | Internal error in planner | Preserve the scene and logs, report issue on GitHub | | 0x80002701 | Expect ts equal | JOIN condition validation failed | Preserve the scene and logs, report issue on GitHub | diff --git a/docs/zh/14-reference/09-error-code.md b/docs/zh/14-reference/09-error-code.md index 2bebe2406b..3022a7ec75 100644 --- a/docs/zh/14-reference/09-error-code.md +++ b/docs/zh/14-reference/09-error-code.md @@ -473,8 +473,12 @@ description: TDengine 服务端的错误码列表和详细说明 | 0x80002663 | Not unique table/alias | 表名(别名)冲突 | 检查并修正SQL语句 | | 0x80002664 | Join requires valid time series input | 不支持子查询不含主键时间戳列输出的JOIN查询 | 检查并修正SQL语句 | | 0x80002665 | The _TAGS pseudo column can only be used for subtable and supertable queries | 非法TAG列查询 | 检查并修正SQL语句 | -| 0x80002666 | 子查询不含主键时间戳列输出 | 检查并修正SQL语句 | +| 0x80002666 | 子查询不含主键时间戳列输出 | 检查并修正SQL语句 | | 0x80002667 | Invalid usage of expr: %s | 非法表达式 | 检查并修正SQL语句 | +| 0x80002687 | Invalid using cols function | cols函数使用错误 | 检查并修正SQL语句 | +| 0x80002688 | Cols function's first param must be a select function | cols函数第一个参数应该为选择函数 | 检查并修正SQL语句 | +| 0x80002689 | Invalid using cols function with multiple output columns | 多列输出的 cols 函数使用错误 | 检查并修正SQL语句 | +| 0x80002690 | Invalid using alias for cols function | cols 函数输出列重命名错误 | 检查并修正SQL语句 | | 0x800026FF | Parser internal error | 解析器内部错误 | 保留现场和日志,github上报issue | | 0x80002700 | Planner internal error | 计划期内部错误 | 保留现场和日志,github上报issue | | 0x80002701 | Expect ts equal | JOIN条件校验失败 | 保留现场和日志,github上报issue | diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 2410016485..58557dac1a 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -908,8 +908,8 @@ int32_t taosGetErrSize(); #define TSDB_CODE_PAR_INVALID_VGID_LIST TAOS_DEF_ERROR_CODE(0, 0x2686) #define TSDB_CODE_PAR_INVALID_COLS_FUNCTION TAOS_DEF_ERROR_CODE(0, 0x2687) #define TSDB_CODE_PAR_INVALID_COLS_SELECTFUNC TAOS_DEF_ERROR_CODE(0, 0x2688) -#define TSDB_CODE_INVALID_MULITI_COLS_FUNC TAOS_DEF_ERROR_CODE(0, 0x2688) -#define TSDB_CODE_INVALID_COLS_ALIAS TAOS_DEF_ERROR_CODE(0, 0x2689) +#define TSDB_CODE_INVALID_MULITI_COLS_FUNC TAOS_DEF_ERROR_CODE(0, 0x2689) +#define TSDB_CODE_INVALID_COLS_ALIAS TAOS_DEF_ERROR_CODE(0, 0x2690) #define TSDB_CODE_PAR_INTERNAL_ERROR TAOS_DEF_ERROR_CODE(0, 0x26FF) //planner diff --git a/tests/system-test/2-query/cols_function.py b/tests/system-test/2-query/cols_function.py index a9b0d7349e..fd41c3a1a1 100644 --- a/tests/system-test/2-query/cols_function.py +++ b/tests/system-test/2-query/cols_function.py @@ -14,6 +14,10 @@ class TDTestCase: tdSql.init(conn.cursor()) self.dbname = 'test' + + def condition_check(self, condition, row, col, expected_value): + if condition: + tdSql.checkData(row, col, expected_value) def create_test_data(self): tdSql.execute(f'create database if not exists {self.dbname};') @@ -27,12 +31,13 @@ class TDTestCase: tdSql.execute(f'create table {self.dbname}.meters (ts timestamp, c0 int, c1 float, c2 nchar(30), c3 bool) tags (t1 nchar(30))') tdSql.execute(f'create table {self.dbname}.d0 using {self.dbname}.meters tags("st1")') tdSql.execute(f'create table {self.dbname}.d1 using {self.dbname}.meters tags("st2")') - tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929000, 1, 1, "a1", true)') + tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929000, 1, 1, "c2", true)') tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929001, 2, 2, "bbbbbbbbb1", false)') - tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929002, 3, 3, "a2", true)') - tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929003, 4, 4, "bbbbbbbbb2", false)') + tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929002, 2, 2, "bbbbbbbbb1", false)') + tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929003, 3, 3, "a2", true)') + tdSql.execute(f'insert into {self.dbname}.d0 values(1734574929004, 4, 4, "bbbbbbbbb2", false)') - tdSql.execute(f'insert into {self.dbname}.d1 values(1734574929000, 1, 1, "a1", true)') + tdSql.execute(f'insert into {self.dbname}.d1 values(1734574929000, 1, 1, "c2", true)') tdSql.execute(f'use {self.dbname}') tdSql.execute(f'Create table {self.dbname}.normal_table (ts timestamp, c0 int, c1 float, c2 nchar(30), c3 bool)') @@ -51,142 +56,161 @@ class TDTestCase: tdSql.query(f'select cols(last(ts+1), ts+2 as t1) from {self.dbname}.meters') tdSql.query(f'select cols(last(ts+1), c0+10) from {self.dbname}.meters') + def one_cols_multi_output_with_group_test(self, from_table = 'test.meters', isTmpTable = False): + select_t1 = ["", ", t1", ", t1 as tag1"] + for t1 in select_t1: + tags_count = 0 if t1 == "" else 1 + tdLog.info("one_cols_1output_test_with_group") + tdSql.query(f'select cols(last(c1), ts) {t1} from {from_table} group by tbname') + tdSql.checkRows(2) + tdSql.checkCols(1 + tags_count) + tdSql.query(f'select cols(last(c1), ts) {t1} from {from_table} group by tbname order by tbname') + tdSql.checkCols(1 + tags_count) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(1, 0, 1734574929000) + tdSql.query(f'select cols(last(c1), ts) {t1} from {from_table} group by tbname order by ts') + tdSql.checkCols(1 + tags_count) + tdSql.checkData(0, 0, 1734574929000) + tdSql.checkData(1, 0, 1734574929004) + tdSql.query(f'select cols(last(c1), ts), tbname {t1} from {from_table} group by tbname order by tbname') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 'd0') + tdSql.checkData(1, 0, 1734574929000) + tdSql.checkData(1, 1, 'd1') + tdSql.query(f'select cols(last(c1), ts), tbname, t1 from {from_table} group by tbname order by tbname') + tdSql.checkRows(2) + tdSql.checkCols(3) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 'd0') + tdSql.checkData(0, 2, 'st1') + tdSql.checkData(1, 0, 1734574929000) + tdSql.checkData(1, 1, 'd1') + tdSql.checkData(1, 2, 'st2') + tdSql.query(f'select cols(last(c1), ts), tbname, t1 from {from_table} group by tbname order by t1') + tdSql.checkRows(2) + tdSql.checkCols(3) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 'd0') + tdSql.checkData(0, 2, 'st1') + tdSql.checkData(1, 0, 1734574929000) + tdSql.checkData(1, 1, 'd1') + tdSql.checkData(1, 2, 'st2') + tdSql.query(f'select cols(last(ts), ts, c0), count(1) {t1} from {from_table} group by t1 order by t1') + tdSql.checkRows(2) + tdSql.checkCols(3 + tags_count) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 5) + self.condition_check(t1!="", 0, 3, 'st1') + tdSql.checkData(1, 0, 1734574929000) + tdSql.checkData(1, 1, 1) + tdSql.checkData(1, 2, 1) + self.condition_check(t1!="", 1, 3, 'st2') + + tdSql.query(f'select cols(last(ts), ts, c0), sum(c0) {t1} from {from_table} group by t1 order by t1') + tdSql.checkRows(2) + tdSql.checkCols(3 + tags_count) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 12) + self.condition_check(t1!="", 0, 3, 'st1') + tdSql.checkData(1, 0, 1734574929000) + tdSql.checkData(1, 1, 1) + tdSql.checkData(1, 2, 1) + self.condition_check(t1!="", 1, 3, 'st2') - def one_cols_multi_output_test(self): - tdLog.info("one_cols_1output_test") - tdSql.query(f'select cols(last(ts), ts, c0) from {self.dbname}.meters') + tdSql.error(f'select cols(last(ts), ts, c0), count(1), t1 from {from_table} group by t1 order by tbname') + + if t1 != "" and isTmpTable: + # Not a GROUP BY expression + tdSql.error(f'select cols(last(ts), ts, c0), count(1) {t1} from {from_table} group by tbname order by tbname') + tdSql.error(f'select cols(last(ts), ts, c0), count(1), tbname {t1} from {from_table} group by tbname order by tbname') + tdSql.error(f'select cols(max(c0), ts, c0), count(1), tbname {t1} from {from_table} group by tbname order by tbname') + tdSql.error(f'select cols(last(c1), ts), count(1) {t1} from {from_table} group by tbname') + continue + tdSql.query(f'select cols(last(ts), ts, c0), count(1) {t1} from {from_table} group by tbname order by tbname') + tdSql.checkRows(2) + tdSql.checkCols(3 + tags_count) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 5) + tdSql.checkData(1, 0, 1734574929000) + tdSql.checkData(1, 1, 1) + tdSql.checkData(1, 2, 1) + tdSql.query(f'select cols(last(ts), ts, c0), count(1), tbname {t1} from {from_table} group by tbname order by tbname') + tdSql.checkRows(2) + tdSql.checkCols(4 + tags_count) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 5) + tdSql.checkData(0, 3, 'd0') + tdSql.checkData(1, 0, 1734574929000) + tdSql.checkData(1, 1, 1) + tdSql.checkData(1, 2, 1) + tdSql.checkData(1, 3, 'd1') + tdSql.query(f'select cols(max(c0), ts, c0), count(1), tbname {t1} from {from_table} group by tbname order by tbname') + tdSql.checkRows(2) + tdSql.checkCols(4 + tags_count) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 5) + tdSql.checkData(0, 3, 'd0') + self.condition_check(t1!="", 0, 4, 'st1') + tdSql.checkData(1, 0, 1734574929000) + tdSql.checkData(1, 1, 1) + tdSql.checkData(1, 2, 1) + tdSql.checkData(1, 3, 'd1') + self.condition_check(t1!="", 1, 4, 'st2') + + tdSql.query(f'select cols(last(c1), ts), count(1) {t1} from {from_table} group by tbname') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + + def one_cols_multi_output_test(self, from_table = 'test.meters'): + tdLog.info(f"one_cols_1output_test {from_table}") + tdSql.query(f'select cols(last(ts), ts, c0) from {from_table}') tdSql.checkRows(1) tdSql.checkCols(2) - tdSql.checkData(0, 0, 1734574929003) + tdSql.checkData(0, 0, 1734574929004) tdSql.checkData(0, 1, 4) - tdSql.query(f'select cols(last(ts), ts as time, c0 cc) from {self.dbname}.meters') + tdSql.query(f'select cols(last(ts), ts as time, c0 cc) from {from_table}') tdSql.checkRows(1) tdSql.checkCols(2) - tdSql.checkData(0, 0, 1734574929003) + tdSql.checkData(0, 0, 1734574929004) tdSql.checkData(0, 1, 4) - tdSql.query(f'select cols(last(ts), c0, c1, c2, c3) from {self.dbname}.meters') + tdSql.query(f'select cols(last(ts), c0, c1, c2, c3) from {from_table}') tdSql.checkRows(1) tdSql.checkCols(4) tdSql.checkData(0, 0, 4) tdSql.checkData(0, 1, 4) tdSql.checkData(0, 2, 'bbbbbbbbb2') tdSql.checkData(0, 3, False) - tdSql.query(f'select cols(last(ts), c0, t1) from {self.dbname}.meters') + tdSql.query(f'select cols(last(ts), c0, t1) from {from_table}') tdSql.checkRows(1) tdSql.checkData(0, 0, 4) tdSql.checkData(0, 1, 'st1') - tdSql.query(f'select cols(last(c1), ts) from {self.dbname}.meters group by tbname') - tdSql.checkRows(2) + tdSql.query(f'select cols(max(c0), ts) from {from_table}') tdSql.checkCols(1) - tdSql.query(f'select cols(last(c1), ts) from {self.dbname}.meters group by tbname order by tbname') - tdSql.checkCols(1) - tdSql.checkData(0, 0, 1734574929003) - tdSql.checkData(1, 0, 1734574929000) - tdSql.query(f'select cols(last(c1), ts) from {self.dbname}.meters group by tbname order by ts') - tdSql.checkCols(1) - tdSql.checkData(0, 0, 1734574929000) - tdSql.checkData(1, 0, 1734574929003) - tdSql.query(f'select cols(last(c1), ts), tbname from {self.dbname}.meters group by tbname order by tbname') - tdSql.checkRows(2) - tdSql.checkCols(2) - tdSql.checkData(0, 0, 1734574929003) - tdSql.checkData(0, 1, 'd0') - tdSql.checkData(1, 0, 1734574929000) - tdSql.checkData(1, 1, 'd1') - tdSql.query(f'select cols(last(c1), ts), tbname, t1 from {self.dbname}.meters group by tbname order by tbname') - tdSql.checkRows(2) - tdSql.checkCols(3) - tdSql.checkData(0, 0, 1734574929003) - tdSql.checkData(0, 1, 'd0') - tdSql.checkData(0, 2, 'st1') - tdSql.checkData(1, 0, 1734574929000) - tdSql.checkData(1, 1, 'd1') - tdSql.checkData(1, 2, 'st2') - tdSql.query(f'select cols(last(c1), ts), tbname, t1 from {self.dbname}.meters group by tbname order by t1') - tdSql.checkRows(2) - tdSql.checkCols(3) - tdSql.checkData(0, 0, 1734574929003) - tdSql.checkData(0, 1, 'd0') - tdSql.checkData(0, 2, 'st1') - tdSql.checkData(1, 0, 1734574929000) - tdSql.checkData(1, 1, 'd1') - tdSql.checkData(1, 2, 'st2') - tdSql.query(f'select cols(max(c0), ts) from {self.dbname}.meters') - tdSql.checkCols(1) - tdSql.checkData(0, 0, 1734574929003) - tdSql.query(f'select cols(min(c1), ts, c0) from {self.dbname}.meters') + tdSql.checkData(0, 0, 1734574929004) + tdSql.query(f'select cols(min(c1), ts, c0) from {from_table}') tdSql.checkCols(2) tdSql.checkData(0, 0, 1734574929000) tdSql.checkData(0, 1, 1) - tdSql.query(f'select cols(last(ts), ts, c0), count(1) from {self.dbname}.meters group by tbname order by tbname') - tdSql.checkRows(2) - tdSql.checkCols(3) - tdSql.checkData(0, 0, 1734574929003) - tdSql.checkData(0, 1, 4) - tdSql.checkData(0, 2, 4) - tdSql.checkData(1, 0, 1734574929000) - tdSql.checkData(1, 1, 1) - tdSql.checkData(1, 2, 1) - tdSql.query(f'select cols(last(ts), ts, c0), count(1), tbname from {self.dbname}.meters group by tbname order by tbname') - tdSql.checkRows(2) - tdSql.checkCols(4) - tdSql.checkData(0, 0, 1734574929003) - tdSql.checkData(0, 1, 4) - tdSql.checkData(0, 2, 4) - tdSql.checkData(0, 3, 'd0') - tdSql.checkData(1, 0, 1734574929000) - tdSql.checkData(1, 1, 1) - tdSql.checkData(1, 2, 1) - tdSql.checkData(1, 3, 'd1') - tdSql.query(f'select cols(last(ts), ts, c0), count(1), tbname, t1 from {self.dbname}.meters group by tbname order by tbname') - tdSql.checkRows(2) - tdSql.checkCols(5) - tdSql.checkData(0, 0, 1734574929003) - tdSql.checkData(0, 1, 4) - tdSql.checkData(0, 2, 4) - tdSql.checkData(0, 3, 'd0') - tdSql.checkData(0, 4, 'st1') - tdSql.checkData(1, 0, 1734574929000) - tdSql.checkData(1, 1, 1) - tdSql.checkData(1, 2, 1) - tdSql.checkData(1, 3, 'd1') - tdSql.checkData(1, 4, 'st2') - tdSql.query(f'select cols(last(ts), ts, c0), count(1), t1 from {self.dbname}.meters group by t1 order by t1') - tdSql.checkRows(2) - tdSql.checkCols(4) - tdSql.checkData(0, 0, 1734574929003) - tdSql.checkData(0, 1, 4) - tdSql.checkData(0, 2, 4) - tdSql.checkData(0, 3, 'st1') - tdSql.checkData(1, 0, 1734574929000) - tdSql.checkData(1, 1, 1) - tdSql.checkData(1, 2, 1) - tdSql.checkData(1, 3, 'st2') - tdSql.error(f'select cols(last(ts), ts, c0), count(1), t1 from {self.dbname}.meters group by t1 order by tbname') - tdSql.query(f'select cols(last(ts), ts, c0), sum(c0), t1 from {self.dbname}.meters group by t1 order by t1') - tdSql.checkRows(2) - tdSql.checkCols(4) - tdSql.checkData(0, 0, 1734574929003) - tdSql.checkData(0, 1, 4) - tdSql.checkData(0, 2, 10) - tdSql.checkData(0, 3, 'st1') - tdSql.checkData(1, 0, 1734574929000) - tdSql.checkData(1, 1, 1) - tdSql.checkData(1, 2, 1) - tdSql.checkData(1, 3, 'st2') - tdSql.query(f'select cols(max(c0), ts, c0), count(1) from {self.dbname}.meters') + tdSql.query(f'select cols(max(c0), ts, c0), count(1) from {from_table}') tdSql.checkRows(1) tdSql.checkCols(3) - tdSql.checkData(0, 0, 1734574929003) + tdSql.checkData(0, 0, 1734574929004) tdSql.checkData(0, 1, 4) - tdSql.checkData(0, 2, 5) + tdSql.checkData(0, 2, 6) tdSql.query(f'select cols(last(ts), ts, c0), count(1) from {self.dbname}.d0') tdSql.checkRows(1) tdSql.checkCols(3) - tdSql.checkData(0, 0, 1734574929003) + tdSql.checkData(0, 0, 1734574929004) tdSql.checkData(0, 1, 4) - tdSql.checkData(0, 2, 4) + tdSql.checkData(0, 2, 5) tdSql.query(f'select cols(last(ts), ts, c0), count(1) from {self.dbname}.d1') tdSql.checkRows(1) tdSql.checkCols(3) @@ -196,51 +220,205 @@ class TDTestCase: tdSql.query(f'select cols(last(ts), ts, c0), count(1) from {self.dbname}.normal_table') tdSql.checkRows(1) tdSql.checkCols(3) - tdSql.checkData(0, 0, 1734574929003) - tdSql.checkData(0, 1, 4) - tdSql.checkData(0, 2, 4) - - tdSql.query(f'select cols(last(ts), ts, c0), avg(c0) from {self.dbname}.meters') - tdSql.checkRows(1) - tdSql.checkCols(3) - tdSql.checkData(0, 0, 1734574929003) - tdSql.checkData(0, 1, 4) - tdSql.checkData(0, 2, 2.2) - - tdSql.query(f'select cols(last(ts), ts, c0), count(1) from {self.dbname}.meters') - tdSql.checkRows(1) - tdSql.checkCols(3) - tdSql.checkData(0, 0, 1734574929003) + tdSql.checkData(0, 0, 1734574929004) tdSql.checkData(0, 1, 4) tdSql.checkData(0, 2, 5) - - tdSql.query(f'select cols(last(ts), ts, c0), sum(c0) from {self.dbname}.meters') + tdSql.query(f'select cols(first(ts), ts, c0), count(1) from {self.dbname}.normal_table') tdSql.checkRows(1) tdSql.checkCols(3) - tdSql.checkData(0, 0, 1734574929003) + tdSql.checkData(0, 0, 1734574929000) + tdSql.checkData(0, 1, 1) + tdSql.checkData(0, 2, 5) + tdSql.query(f'select cols(min(c0), ts, c0), count(1) from {self.dbname}.normal_table') + tdSql.checkRows(1) + tdSql.checkCols(3) + tdSql.checkData(0, 0, 1734574929000) + tdSql.checkData(0, 1, 1) + tdSql.checkData(0, 2, 5) + + tdSql.query(f'select cols(last(ts), ts, c0), avg(c0) from {from_table}') + tdSql.checkRows(1) + tdSql.checkCols(3) + tdSql.checkData(0, 0, 1734574929004) tdSql.checkData(0, 1, 4) - tdSql.checkData(0, 2, 11) - - - tdSql.query(f'select count(1), cols(last(ts), ts, c0), min(c0) from {self.dbname}.meters') - tdSql.query(f'select cols(last(ts), ts, c0), count(1) from {self.dbname}.meters') - tdSql.query(f'select cols(last(ts), ts as time, c0 cc), count(1) from {self.dbname}.meters') - tdSql.query(f'select cols(last(ts), c0, c1, c2, c3), count(1) from {self.dbname}.meters') - tdSql.query(f'select cols(last(c1), ts), count(1) from {self.dbname}.meters group by tbname') + tdSql.checkData(0, 2, 2.1666666666666665) + + tdSql.query(f'select cols(last(ts), ts, c0), count(1) from {from_table}') + tdSql.checkRows(1) + tdSql.checkCols(3) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 6) + + tdSql.query(f'select cols(last(ts), ts, c0), sum(c0) from {from_table}') + tdSql.checkRows(1) + tdSql.checkCols(3) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 13) + + tdSql.query(f'select count(1), cols(last(ts), ts, c0), min(c0) from {from_table}') + tdSql.checkRows(1) + tdSql.checkCols(4) + tdSql.checkData(0, 0, 6) + tdSql.checkData(0, 1, 1734574929004) + tdSql.checkData(0, 2, 4) + tdSql.checkData(0, 3, 1) + + # there's some error on last_row func when using sub query. + tdSql.query(f'select count(1), cols(last_row(ts), ts, c0), min(c0) from test.meters') + tdSql.checkRows(1) + tdSql.checkCols(4) + tdSql.checkData(0, 0, 6) + tdSql.checkData(0, 1, 1734574929004) + tdSql.checkData(0, 2, 4) + tdSql.checkData(0, 3, 1) + + # there's some error on last_row func when using sub query. + tdSql.query(f'select count(1), cols(last_row(ts), ts, c0), last_row(c1), last_row(c3) from test.meters') + tdSql.checkRows(1) + tdSql.checkCols(5) + tdSql.checkData(0, 0, 6) + tdSql.checkData(0, 1, 1734574929004) + tdSql.checkData(0, 2, 4) + tdSql.checkData(0, 3, 4) + tdSql.checkData(0, 4, False) + + + tdSql.query(f'select cols(last(ts), ts, c0), count(1) from {from_table}') + tdSql.checkRows(1) + tdSql.checkCols(3) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 6) + tdSql.query(f'select cols(max(c0), ts, c0), count(1) from {from_table}') + tdSql.checkRows(1) + tdSql.checkCols(3) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 6) - tdSql.query(f'select cols(max(c0), ts), count(1) from {self.dbname}.meters') - tdSql.query(f'select cols(min(c1), ts, c0), count(1) from {self.dbname}.meters') - tdSql.query(f'select count(1), cols(max(c0), ts) from {self.dbname}.meters') - tdSql.query(f'select max(c0), cols(max(c0), ts) from {self.dbname}.meters') - tdSql.query(f'select max(c1), cols(max(c0), ts) from {self.dbname}.meters') + tdSql.query(f'select cols(last(ts), ts as time, c0 cc), count(1) from {from_table}') + tdSql.checkRows(1) + tdSql.checkCols(3) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 6) + tdSql.query(f'select cols(max(c1), ts as time, c0 cc), count(1) from {from_table}') + tdSql.checkRows(1) + tdSql.checkCols(3) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 6) + tdSql.query(f'select cols(last(ts), c0, c1, c2, c3), count(1) from {from_table}') + tdSql.checkRows(1) + tdSql.checkCols(5) + tdSql.checkData(0, 0, 4) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 'bbbbbbbbb2') + tdSql.checkData(0, 3, False) + tdSql.checkData(0, 4, 6) - def multi_cols_output_test(self): + tdSql.query(f'select cols(max(c0), ts), count(1) from {from_table}') + tdSql.query(f'select cols(min(c1), ts, c0), count(1) from {from_table}') + tdSql.query(f'select count(1), cols(max(c0), ts) from {from_table}') + tdSql.query(f'select max(c0), cols(max(c0), ts) from {from_table}') + tdSql.query(f'select max(c1), cols(max(c0), ts) from {from_table}') + + def multi_cols_output_test(self, from_table = 'test.meters', isTmpTable = False): tdLog.info("multi_cols_output_test") tdSql.query(f'select cols(last(c0), ts, c1), cols(first(c0), ts, c1), count(1) from {self.dbname}.meters') - tdSql.query(f'select cols(last(c0), ts as t1, c1 as c11), cols(first(c0), ts as c2, c1 c21), count(1) from {self.dbname}.meters') + tdSql.checkRows(1) + tdSql.checkCols(5) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 1734574929000) + tdSql.checkData(0, 3, 1) + tdSql.checkData(0, 4, 6) + tdSql.query(f'select cols(last(c0),ts lts, c1 lc1), cols(first(c0), ts fts, c1 as fc1), count(1) from test.meters') + tdSql.checkRows(1) + tdSql.checkCols(5) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 1734574929000) + tdSql.checkData(0, 3, 1) + tdSql.checkData(0, 4, 6) + tdSql.query(f'select cols(max(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21), count(1) from {self.dbname}.meters') + tdSql.checkRows(1) + tdSql.checkCols(5) + tdSql.checkData(0, 0, 1734574929004) + tdSql.checkData(0, 1, 4) + tdSql.checkData(0, 2, 1734574929000) + tdSql.checkData(0, 3, 1) + tdSql.checkData(0, 4, 6) + tdSql.query(f'select cols(max(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21), count(1) from {self.dbname}.meters where c0 < 4') + tdSql.checkRows(1) + tdSql.checkCols(5) + tdSql.checkData(0, 0, 1734574929003) + tdSql.checkData(0, 1, 3) + tdSql.checkData(0, 2, 1734574929000) + tdSql.checkData(0, 3, 1) + tdSql.checkData(0, 4, 5) + tdSql.query(f'select cols(max(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21), count(1) from test.meters where c0 < 4 group by tbname order by t1') + tdSql.checkRows(2) + tdSql.checkCols(5) + tdSql.checkData(0, 0, 1734574929000) + tdSql.checkData(0, 1, 1) + tdSql.checkData(0, 2, 1734574929000) + tdSql.checkData(0, 3, 1) + tdSql.checkData(0, 4, 1) + tdSql.checkData(1, 0, 1734574929003) + tdSql.checkData(1, 1, 3) + tdSql.checkData(1, 2, 1734574929000) + tdSql.checkData(1, 3, 1) + tdSql.checkData(1, 4, 4) + + tdSql.query(f'select cols(last_row(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21), count(1) from test.meters where c0 < 4 group by tbname order by t1') + tdSql.checkRows(2) + tdSql.checkCols(5) + tdSql.checkData(0, 0, 1734574929000) + tdSql.checkData(0, 1, 1) + tdSql.checkData(0, 2, 1734574929000) + tdSql.checkData(0, 3, 1) + tdSql.checkData(0, 4, 1) + tdSql.checkData(1, 0, 1734574929003) + tdSql.checkData(1, 1, 3) + tdSql.checkData(1, 2, 1734574929000) + tdSql.checkData(1, 3, 1) + tdSql.checkData(1, 4, 4) + + tdSql.query(f'select cols(last_row(c0), ts as t1, c1 as c11), cols(min(c0), ts as t2, c1 c21), count(1) from test.meters where c0 < 4 group by tbname order by t1') + tdSql.checkRows(2) + tdSql.checkCols(5) + tdSql.checkData(0, 0, 1734574929000) + tdSql.checkData(0, 1, 1) + tdSql.checkData(0, 2, 1734574929000) + tdSql.checkData(0, 3, 1) + tdSql.checkData(0, 4, 1) + tdSql.checkData(1, 0, 1734574929003) + tdSql.checkData(1, 1, 3) + tdSql.checkData(1, 2, 1734574929000) + tdSql.checkData(1, 3, 1) + tdSql.checkData(1, 4, 4) + + tdSql.query(f'select cols(last_row(c0), ts as t1, c1 as c11), cols(mode(c0), ts as t2, c1 c21), count(1) from test.meters where c0 < 4 group by tbname order by t1') + tdSql.checkRows(2) + tdSql.checkCols(5) + tdSql.checkData(0, 0, 1734574929000) + tdSql.checkData(0, 1, 1) + tdSql.checkData(0, 2, 1734574929000) + tdSql.checkData(0, 3, 1) + tdSql.checkData(0, 4, 1) + tdSql.checkData(1, 0, 1734574929003) + tdSql.checkData(1, 1, 3) + #tdSql.checkData(1, 2, 1734574929000) # mode(c0) is return a random ts of same c0 + tdSql.checkData(1, 3, 2) + tdSql.checkData(1, 4, 4) + + #select cols(last_row(c0), ts as t1, c1 as c11), cols(first(c0), ts as t2, c1 c21), first(c0) from test.meters where c0 < 4; def funcSupperTableTest(self): tdSql.execute('create database if not exists db;') @@ -250,20 +428,20 @@ class TDTestCase: tdSql.execute('create table db.st (ts timestamp, c0 int, c1 float, c2 nchar(30), c3 bool) tags (t1 nchar(30))') tdSql.execute('create table db.st_1 using db.st tags("st1")') tdSql.execute('create table db.st_2 using db.st tags("st1")') - tdSql.execute('insert into db.st_1 values(1734574929000, 1, 1, "a1", true)') + tdSql.execute('insert into db.st_1 values(1734574929000, 1, 1, "c2", true)') tdSql.execute('insert into db.st_1 values(1734574929001, 2, 2, "bbbbbbbbb1", false)') tdSql.execute('insert into db.st_1 values(1734574929002, 3, 3, "a2", true)') - tdSql.execute('insert into db.st_1 values(1734574929003, 4, 4, "bbbbbbbbb2", false)') + tdSql.execute('insert into db.st_1 values(1734574929004, 4, 4, "bbbbbbbbb2", false)') tdSql.query(f'select cols(last(c0), ts, c1, c2, c3), cols(first(c0), ts, c1, c2, c3) from db.st') tdSql.checkRows(1) - tdSql.checkData(0, 0, 1734574929003) + tdSql.checkData(0, 0, 1734574929004) tdSql.checkData(0, 1, '4.0') tdSql.checkData(0, 2, 'bbbbbbbbb2') tdSql.checkData(0, 3, False) tdSql.checkData(0, 4, 1734574929000) tdSql.checkData(0, 5, '1.0') - tdSql.checkData(0, 6, 'a1') + tdSql.checkData(0, 6, 'c2') tdSql.checkData(0, 7, True) #tdSql.execute(f'drop table if exists db.st') @@ -278,6 +456,8 @@ class TDTestCase: tdSql.execute('insert into db.d1 values(1734574929000, 1, 1.1, "a", true)') tdSql.execute('insert into db.d1 values(1734574930000, 2, 2.2, "bbbbbbbbb", false)') + groupby = ["", "group by tbname order ts", "group by tbname order t1", "group by tbname order ts"] + tdSql.query(f'select cols(last(c0), ts, c2), cols(first(c0), ts, c2) from db.d1') tdSql.checkRows(1) tdSql.checkCols(4) @@ -365,15 +545,167 @@ class TDTestCase: tdSql.checkData(0, 1, 'a') tdSql.checkData(0, 2, 1734574929000) tdSql.checkData(0, 3, 6) - - def subquery_test(self): - tdSql.query(f'select count(1), cols(last(c0),c0) from (select * from test.d0)') - tdSql.query(f'select count(1), cols(last(c0),c0) from (select *, tbname from test.meters) group by tbname') - - def window_test(self): - tdSql.query(f'select tbname, _wstart,_wend, max(c0), max(c1), cols( max(c0), c1) from test.meters partition \ - by tbname count_window(2) order by tbname') + def orderby_test(self, from_table = 'test.meters', isTmpTable = False): + select_t1 = ["", ", t1", ", t1 as tag1"] + for t1 in select_t1: + if t1 != "" and isTmpTable: + # Not a GROUP BY expression + tdSql.error(f'select count(1), cols(last(c0),c2) {t1} from {from_table} group by tbname order by cols(last(c0), c2)') + tdSql.error(f'select count(1), cols(last(c0),c2) {t1} from {from_table} group by tbname order by cols(last(c0), c2) desc') + tdSql.error(f'select count(1), cols(last(c0),c2) {t1} from {from_table} group by tbname order by cols(last(c0), c0)') + tdSql.error(f'select count(1), cols(last(c0),c2) {t1} from {from_table} group by tbname order by cols(last(c0), c0), cols(last(c0), c1)') + tdSql.error(f'select count(1), last(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c2)') + tdSql.error(f'select count(1), last(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c2) desc') + tdSql.error(f'select count(1), last(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c0)') + tdSql.error(f'select count(1), last(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c0), cols(last(c0), c1)') + tdSql.error(f'select count(1), max(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c2)') + tdSql.error(f'select count(1), max(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c2) desc') + tdSql.error(f'select count(1), max(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c0)') + tdSql.error(f'select count(1), max(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c0), cols(last(c0), c1)') + continue + tdSql.query(f'select count(1), cols(last(c0),c2) {t1} from {from_table} group by tbname order by cols(last(c0), c2)') + tdSql.checkRows(2) + tags_count = 0 if t1 == "" else 1 + tdLog.debug(f'tags_count: {tags_count}') + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 5) + tdSql.checkData(0, 1, 'bbbbbbbbb2') + self.condition_check(t1 != "", 0, 2, 'st1') + tdSql.checkData(1, 0, 1) + tdSql.checkData(1, 1, 'c2') + self.condition_check(t1 != "", 1, 2, 'st2') + + tdSql.query(f'select count(1), cols(last(c0),c2) {t1} from {from_table} group by tbname order by cols(last(c0), c2) desc') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 1) + tdSql.checkData(0, 1, 'c2') + self.condition_check(t1 != "", 0, 2, 'st2') + tdSql.checkData(1, 0, 5) + tdSql.checkData(1, 1, 'bbbbbbbbb2') + self.condition_check(t1 != "", 1, 2, 'st1') + + tdSql.query(f'select count(1), cols(last(c0),c2) {t1} from {from_table} group by tbname order by cols(last(c0), c0)') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 1) + tdSql.checkData(0, 1, 'c2') + self.condition_check(t1 != "", 0, 2, 'st2') + tdSql.checkData(1, 0, 5) + tdSql.checkData(1, 1, 'bbbbbbbbb2') + self.condition_check(t1 != "", 1, 2, 'st1') + + tdSql.query(f'select count(1), cols(last(c0),c2) {t1} from {from_table} group by tbname order by 1') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 1) + tdSql.checkData(0, 1, 'c2') + self.condition_check(t1 != "", 0, 2, 'st2') + tdSql.checkData(1, 0, 5) + tdSql.checkData(1, 1, 'bbbbbbbbb2') + self.condition_check(t1 != "", 1, 2, 'st1') + + tdSql.query(f'select count(1), cols(last(c0),c2) {t1} from {from_table} group by tbname order by 2') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 5) + tdSql.checkData(0, 1, 'bbbbbbbbb2') + self.condition_check(t1 != "", 0, 2, 'st1') + tdSql.checkData(1, 0, 1) + tdSql.checkData(1, 1, 'c2') + self.condition_check(t1 != "", 1, 2, 'st2') + + tdSql.query(f'select count(1), cols(last(c0),c2) {t1} from {from_table} group by tbname order by cols(last(c0), c0), cols(last(c0), c1)') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 1) + tdSql.checkData(0, 1, 'c2') + self.condition_check(t1 != "", 0, 2, 'st2') + tdSql.checkData(1, 0, 5) + tdSql.checkData(1, 1, 'bbbbbbbbb2') + self.condition_check(t1 != "", 1, 2, 'st1') + + tdSql.query(f'select count(1), last(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c2)') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 5) + tdSql.checkData(0, 1, 'bbbbbbbbb2') + self.condition_check(t1 != "", 0, 2, 'st1') + tdSql.checkData(1, 0, 1) + tdSql.checkData(1, 1, 'c2') + self.condition_check(t1 != "", 1, 2, 'st2') + + tdSql.query(f'select count(1), last(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c2) desc') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 1) + tdSql.checkData(0, 1, 'c2') + self.condition_check(t1 != "", 0, 2, 'st2') + tdSql.checkData(1, 0, 5) + tdSql.checkData(1, 1, 'bbbbbbbbb2') + self.condition_check(t1 != "", 1, 2, 'st1') + + tdSql.query(f'select count(1), last(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c0)') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 1) + tdSql.checkData(0, 1, 'c2') + self.condition_check(t1 != "", 0, 2, 'st2') + tdSql.checkData(1, 0, 5) + tdSql.checkData(1, 1, 'bbbbbbbbb2') + self.condition_check(t1 != "", 1, 2, 'st1') + + tdSql.query(f'select count(1), last(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c0), cols(last(c0), c1)') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 1) + tdSql.checkData(0, 1, 'c2') + self.condition_check(t1 != "", 0, 2, 'st2') + tdSql.checkData(1, 0, 5) + tdSql.checkData(1, 1, 'bbbbbbbbb2') + self.condition_check(t1 != "", 1, 2, 'st1') + + tdSql.query(f'select count(1), max(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c2)') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 5) + tdSql.checkData(0, 1, 'c2') + self.condition_check(t1 != "", 0, 2, 'st1') + tdSql.checkData(1, 0, 1) + tdSql.checkData(1, 1, 'c2') + self.condition_check(t1 != "", 1, 2, 'st2') + + tdSql.query(f'select count(1), max(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c2) desc') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 1) + tdSql.checkData(0, 1, 'c2') + self.condition_check(t1 != "", 0, 2, 'st2') + tdSql.checkData(1, 0, 5) + tdSql.checkData(1, 1, 'c2') + self.condition_check(t1 != "", 1, 2, 'st1') + + tdSql.query(f'select count(1), max(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c0)') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 1) + tdSql.checkData(0, 1, 'c2') + self.condition_check(t1 != "", 0, 2, 'st2') + tdSql.checkData(1, 0, 5) + tdSql.checkData(1, 1, 'c2') + self.condition_check(t1 != "", 1, 2, 'st1') + + tdSql.query(f'select count(1), max(c2) {t1} from {from_table} group by tbname order by cols(last(c0), c0), cols(last(c0), c1)') + tdSql.checkRows(2) + tdSql.checkCols(2 + tags_count) + tdSql.checkData(0, 0, 1) + tdSql.checkData(0, 1, 'c2') + self.condition_check(t1 != "", 0, 2, 'st2') + tdSql.checkData(1, 0, 5) + tdSql.checkData(1, 1, 'c2') + self.condition_check(t1 != "", 1, 2, 'st1') + def parse_test(self): tdLog.info("parse test") @@ -383,7 +715,6 @@ class TDTestCase: tdSql.error(f'select last(cols(ts)) from {self.dbname}.meters') tdSql.error(f'select last(cols(ts, ts)) from {self.dbname}.meters') tdSql.error(f'select last(cols(ts, ts), ts) from {self.dbname}.meters') - tdSql.error(f'd{self.dbname}.meters') tdSql.error(f'select cols(last(ts), ts as t1) as t1 from {self.dbname}.meters') tdSql.error(f'select cols(last(ts), ts, c0) t1 from {self.dbname}.meters') tdSql.error(f'select cols(last(ts), ts t1) tt from {self.dbname}.meters') @@ -406,7 +737,33 @@ class TDTestCase: tdSql.error(f'select cols(cols(last(ts), c0), c0) as cc from {self.dbname}.meters') tdSql.error(f'select cols(last(ts), cols(last(ts), c0), c0) as cc from {self.dbname}.meters') + + # Aggregate functions do not support nesting + tdSql.error(f'select count(1), cols(last_row(ts), ts, first(c0)), last_row(c1) from {self.dbname}.meters') + # Not a GROUP BY expression + tdSql.error(f'select count(1), cols(last(c0),c0) from test.meters group by tbname order by c3 desc') + + def subquery_test(self): + tdSql.query(f'select count(1), cols(last(c0),c0) from (select * from test.d0)') + tdSql.query(f'select count(1), cols(last(c0),c0) from (select *, tbname from test.meters) group by tbname') + + tdLog.info("subquery_test: orderby_test from meters") + self.orderby_test("test.meters", False) + tdLog.info("subquery_test: orderby_test from (select *, tbname from meters)") + self.orderby_test("(select *, tbname from test.meters)", True) + tdLog.info("subquery_test: one_cols_multi_output_with_group_test from meters") + self.one_cols_multi_output_with_group_test("test.meters", False) + tdLog.info("subquery_test: one_cols_multi_output_with_group_test from (select *, tbname from meters)") + self.one_cols_multi_output_with_group_test("(select *, tbname from test.meters)", True) + + self.one_cols_multi_output_test("test.meters") + self.one_cols_multi_output_test("(select *, tbname from test.meters)") + + + def window_test(self): + tdSql.query(f'select tbname, _wstart,_wend, max(c0), max(c1), cols( max(c0), c1) from test.meters partition \ + by tbname count_window(2) order by tbname') def run(self): self.funcNestTest() @@ -414,10 +771,9 @@ class TDTestCase: self.create_test_data() self.parse_test() self.one_cols_1output_test() - self.one_cols_multi_output_test() self.multi_cols_output_test() self.subquery_test() - + self.window_test() def stop(self): tdSql.close()