From 4544d06ef411d887e28cce2b1e01f35cc4d232c6 Mon Sep 17 00:00:00 2001 From: charles Date: Thu, 18 Jan 2024 10:42:53 +0800 Subject: [PATCH 1/4] add test case for function 'elapsed' by charles --- .../query/function/test_fun_elapsed.py | 418 ++++++++++++++++++ tests/parallel_test/cases.task | 1 + 2 files changed, 419 insertions(+) create mode 100644 tests/army/community/query/function/test_fun_elapsed.py diff --git a/tests/army/community/query/function/test_fun_elapsed.py b/tests/army/community/query/function/test_fun_elapsed.py new file mode 100644 index 0000000000..66775c8e6c --- /dev/null +++ b/tests/army/community/query/function/test_fun_elapsed.py @@ -0,0 +1,418 @@ +from frame.log import * +from frame.cases import * +from frame.sql import * +from frame.caseBase import * +from frame import * +from frame.eos import * + + +class TDTestCase(TBase): + """Verify the elapsed function + """ + def init(self, conn, logSql, replicaVar=1): + self.replicaVar = int(replicaVar) + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor()) + self.dbname = 'db' + self.table_dic = { + "super_table": ["st1", "st2", "st_empty"], + "child_table": ["ct1_1", "ct1_2", "ct1_empty", "ct2_1", "ct2_2", "ct2_empty"], + "tags_value": [("2023-03-01 15:00:00", 1, 'bj'), ("2023-03-01 15:10:00", 2, 'sh'), ("2023-03-01 15:20:00", 3, 'sz'), ("2023-03-01 15:00:00", 4, 'gz'), ("2023-03-01 15:10:00", 5, 'cd'), ("2023-03-01 15:20:00", 6, 'hz')], + "common_table": ["t1", "t2", "t_empty"] + } + self.start_ts = 1677654000000 # 2023-03-01 15:00:00.000 + self.row_num = 100 + + def prepareData(self): + # db + tdSql.execute("create database {};".format(self.dbname)) + tdSql.execute("use {};".format(self.dbname)) + tdLog.debug("Create database %s" % self.dbname) + + # commont table + for common_table in self.table_dic["common_table"]: + tdSql.execute("create table {} (ts timestamp, c_ts timestamp, c_int int, c_bigint bigint, c_double double, c_nchar nchar(16));".format(common_table)) + tdLog.debug("Create common table %s" % common_table) + + # super table + for super_table in self.table_dic["super_table"]: + tdSql.execute("create stable {} (ts timestamp, c_ts timestamp, c_int int, c_bigint bigint, c_double double, c_nchar nchar(16)) tags (t1 timestamp, t2 int, t3 binary(16));".format(super_table)) + tdLog.debug("Create super table %s" % super_table) + + # child table + for i in range(len(self.table_dic["child_table"])): + if self.table_dic["child_table"][i].startswith("ct1"): + tdSql.execute("create table {} using {} tags('{}', {}, '{}');".format(self.table_dic["child_table"][i], "st1", self.table_dic["tags_value"][i][0], self.table_dic["tags_value"][i][1], self.table_dic["tags_value"][i][2])) + elif self.table_dic["child_table"][i].startswith("ct2"): + tdSql.execute("create table {} using {} tags('{}', {}, '{}');".format(self.table_dic["child_table"][i], "st2", self.table_dic["tags_value"][i][0], self.table_dic["tags_value"][i][1], self.table_dic["tags_value"][i][2])) + + # insert data + table_list = ["t1", "t2", "ct1_1", "ct1_2", "ct2_1", "ct2_2"] + for t in table_list: + sql = "insert into {} values".format(t) + for i in range(self.row_num): + sql += "({}, {}, {}, {}, {}, '{}'),".format(self.start_ts + i * 1000, self.start_ts + i * 1000, 32767+i, 65535+i, i, t + str(i)) + sql += ";" + tdSql.execute(sql) + tdLog.debug("Insert data into table %s" % t) + + def test_normal_query(self): + # only one timestamp + tdSql.query("select elapsed(ts) from t1 group by c_ts;") + assert(len(tdSql.queryResult) == self.row_num and tdSql.queryResult[0][0] == 0) + + tdSql.query("select elapsed(ts, 1m) from t1 group by c_ts;") + assert(len(tdSql.queryResult) == self.row_num and tdSql.queryResult[0][0] == 0) + + # child table with group by + tdSql.query("select elapsed(ts) from ct1_2 group by tbname;") + assert(len(tdSql.queryResult) == 1 and tdSql.queryResult[0][0] == 99000) + + # empty super table + tdSql.query("select elapsed(ts, 1s) from st_empty group by tbname;") + assert(len(tdSql.queryResult) == 0) + + # empty child table + tdSql.query("select elapsed(ts, 1s) from ct1_empty group by tbname;") + assert(len(tdSql.queryResult) == 0) + + # empty common table + tdSql.query("select elapsed(ts, 1s) from t_empty group by tbname;") + assert(len(tdSql.queryResult) == 0) + + # unit as second + tdSql.query("select elapsed(ts, 1s) from st2 group by tbname;") + assert(len(tdSql.queryResult) == 2 and tdSql.queryResult[0][0] == 99) + + # unit as minute + tdSql.query("select elapsed(ts, 1m) from st2 group by tbname;") + assert(len(tdSql.queryResult) == 2 and tdSql.queryResult[0][0] == 1.65) + + # unit as hour + tdSql.query("select elapsed(ts, 1h) from st2 group by tbname;") + assert(len(tdSql.queryResult) == 2 and tdSql.queryResult[0][0] == 0.0275) + + def test_query_with_filter(self): + end_ts = 1677654000000 + 1000 * 99 + query_list = [ + { + "sql": "select elapsed(ts, 1s) from st1 where ts >= 1677654000000 group by tbname;", + "res": [(99.0, ), (99.0, )] + }, + { + "sql": "select elapsed(ts, 1s) from st1 where ts >= 1677654000000 and c_ts >= 1677654000000 group by tbname;", + "res": [(99.0, ), (99.0, )] + }, + { + "sql": "select elapsed(ts, 1s) from st1 where ts >= 1677654000000 and c_ts >= 1677654000000 and t1='2023-03-01 15:10:00.000' group by tbname;", + "res": [(99.0, )] + }, + { + "sql": "select elapsed(ts, 1s) from st_empty where ts >= 1677654000000 and c_ts >= 1677654000000 and t1='2023-03-01 15:10:00.000' group by tbname;", + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from ct1_1 where ts >= 1677654000000 group by tbname;", + "res": [(99.0, )] + }, + { + "sql": "select elapsed(ts, 1s) from ct1_2 where ts >= 1677654000000 and c_ts >= 1677654000000 group by tbname;", + "res": [(99.0, )] + }, + { + "sql": "select elapsed(ts, 1s) from ct1_empty where ts >= 1677654000000 and c_ts >= 1677654000000 group by tbname;", + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from t1 where ts >= 1677654000000 group by tbname;", + "res": [(99.0, )] + }, + { + "sql": "select elapsed(ts, 1s) from t2 where ts >= 1677654000000 and c_ts >= 1677654000000 group by tbname;", + "res": [(99.0, )] + }, + { + "sql": "select elapsed(ts, 1s) from t_empty where ts >= 1677654000000 and c_ts >= 1677654000000 group by tbname;", + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_ts > {} group by tbname;".format(end_ts), + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_ts > {} and t1='2023-03-01 15:10:00' group by tbname;".format(end_ts), + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_int < 1 group by tbname;", + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_int >= 1 and t1='2023-03-01 15:10:00' group by tbname;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_int <> 1 and t1='2023-03-01 15:10:00' group by tbname;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_nchar like 'ct2_%' and t1='2023-03-01 15:10:00' group by tbname;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_nchar like 'ct1_%' and t1='2023-03-01 15:10:00' group by tbname;", + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_nchar match '^ct2_' and t1='2023-03-01 15:10:00' group by tbname;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_nchar nmatch '^ct1_' and t1='2023-03-01 15:10:00' group by tbname;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and t3 like 'g%' group by tbname;", + "res": [(99,)] + } + ] + for q in query_list: + tdSql.query(q["sql"]) + if len(q["res"]) == 0: + assert(len(tdSql.queryResult) == len(q["res"])) + else: + assert(len(tdSql.queryResult) == len(q["res"]) and tdSql.queryResult == q["res"]) + + def test_query_with_other_function(self): + query_list = [ + { + "sql": "select avg(c_int), count(*), elapsed(ts, 1s), leastsquares(c_int, 0, 1), spread(c_bigint), sum(c_int), hyperloglog(c_int) from st1;", + "res": [(32816.5, 200, 99.0, '{slop:0.499962, intercept:32766.753731}', 99.0, 6563300, 100)] + }, + { + "sql": "select twa(c_int) * elapsed(ts, 1s) from ct1_1;", + "res": [(3.248833500000000e+06,)] + } + ] + for q in query_list: + tdSql.query(q["sql"]) + assert(len(tdSql.queryResult) == len(q["res"]) and tdSql.queryResult == q["res"]) + + def test_query_with_join(self): + query_list = [ + { + "sql": "select elapsed(st1.ts, 1s) from st1, st2 where st1.ts = st2.ts;", + "res": [(99,)] + }, + { + "sql": "select elapsed(st1.ts, 1s) from st1, st_empty where st1.ts = st_empty.ts and st1.c_ts = st_empty.c_ts;", + "res": [] + }, + { + "sql": "select elapsed(st1.ts, 1s) from st1, ct1_1 where st1.ts = ct1_1.ts;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ct1.ts, 1s) from ct1_1 ct1, ct1_2 ct2 where ct1.ts = ct2.ts;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ct1.ts, 1s) from ct1_1 ct1, ct1_empty ct2 where ct1.ts = ct2.ts;", + "res": [] + }, + { + "sql": "select elapsed(st1.ts, 1s) from st1, ct1_empty where st1.ts = ct1_empty.ts;", + "res": [] + }, + { + "sql": "select elapsed(st1.ts, 1s) from st1, t1 where st1.ts = t1.ts;", + "res": [(99,)] + }, + { + "sql": "select elapsed(st1.ts, 1s) from st1, t_empty where st1.ts = t_empty.ts;", + "res": [] + }, + { + "sql": "select elapsed(ct1.ts, 1s) from ct1_1 ct1, t1 t2 where ct1.ts = t2.ts;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ct1.ts, 1s) from ct1_1 ct1, t_empty t2 where ct1.ts = t2.ts;", + "res": [] + }, + { + "sql": "select elapsed(st1.ts, 1s) from st1, st2, st_empty where st1.ts=st2.ts and st2.ts=st_empty.ts;", + "res": [] + } + ] + + for q in query_list: + tdSql.query(q["sql"]) + assert(len(tdSql.queryResult) == len(q["res"]) and tdSql.queryResult == q["res"]) + + def test_query_with_union(self): + query_list = [ + { + "sql": "select elapsed(ts, 1s) from st1 union select elapsed(ts, 1s) from st2;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st1 union all select elapsed(ts, 1s) from st2;", + "res": [(99,),(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st1 union all select elapsed(ts, 1s) from st_empty;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from ct1_1 union all select elapsed(ts, 1s) from ct1_2;", + "res": [(99,),(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from ct1_1 union select elapsed(ts, 1s) from ct1_2;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from ct1_1 union select elapsed(ts, 1s) from ct1_empty;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st1 where ts < '2023-03-01 15:05:00.000' union select elapsed(ts, 1s) from ct1_1 where ts >= '2023-03-01 15:01:00.000';", + "res": [(39,),(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from ct1_empty union select elapsed(ts, 1s) from t_empty;", + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from st1 group by tbname union select elapsed(ts, 1s) from st2 group by tbname;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st1 group by tbname union all select elapsed(ts, 1s) from st2 group by tbname;", + "res": [(99,),(99,),(99,),(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st_empty group by tbname union all select elapsed(ts, 1s) from st2 group by tbname;", + "res": [(39,),(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from t1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:40.000' interval(10s) fill(next) union select elapsed(ts, 1s) from st2 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:49.000' interval(5s) fill(prev);", + "res": [(9,), (), (4,), (5,),(10,)] + }, + { + "sql": "select elapsed(ts, 1s) from st1 group by tbname union select elapsed(ts, 1s) from st2 group by tbname union select elapsed(ts, 1s) from st_empty group by tbname;", + "res": [(99,)] + } + ] + for q in query_list: + tdSql.query(q["sql"]) + assert(len(tdSql.queryResult) == len(q["res"]) and tdSql.queryResult == q["res"]) + + def test_query_with_window(self): + query_list = [ + { + "sql": "select elapsed(ts, 1s) from st1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:00:20.000' interval(10s) fill(next);", + "res": [(10,),(10,)()] + }, + { + "sql": "select elapsed(ts, 1s) from (select * from st1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:20.000' and c_int > 100) where ts >= '2023-03-01 15:01:00.000' and ts < '2023-03-01 15:02:00.000' interval(10s) fill(prev);", + "res": [(10,)(10,)(),(),(),()] + }, + { + "sql": "select elapsed(ts, 1s) from st1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:00:20.000' session(ts, 2s);", + "res": [(20,)] + }, + { + "sql": "select elapsed(ts, 1s) from st_empty where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:00:20.000' session(ts, 2s);", + "res": [] + } + ] + for q in query_list: + tdSql.query(q["sql"]) + assert(len(tdSql.queryResult) == len(q["res"]) and tdSql.queryResult == q["res"]) + + def test_nested_query(self): + query_list = [ + { + "sql": "select elapsed(ts, 1s) from (select * from st1 where c_int > 10 and ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:40.000');", + "res": [(99,)] + }, + { + "sql": "select sum(v) from (select elapsed(ts, 1s) as v from st1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:00:20.000' interval(10s) fill(next));", + "res": [(20,)] + }, + { + "sql": "select avg(v) from (select elapsed(ts, 1s) as v from st2 group by tbname order by v);", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from (select * from st1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:40.000') where c_int > 10;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from (select * from st1 where c_int > 10 and ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:40.000') where c_int < 20;", + "res": [] + } + ] + for q in query_list: + tdSql.query(q["sql"]) + assert(len(tdSql.queryResult) == len(q["res"]) and tdSql.queryResult == q["res"]) + + def test_abnormal_query(self): + # incorrect parameter + table_list = self.table_dic["super_table"] + self.table_dic["child_table"] + self.table_dic["common_table"] + incorrect_parameter_list = ["()", "(null)", "(*)", "(c_ts)", "(c_ts, 1s)", "(c_int)", "(c_bigint)", "(c_double)", "(c_nchar)", "(ts, null)", + "(ts, *)", "(2024-01-09 17:00:00)", "(2024-01-09 17:00:00, 1s)", "(t1)", "(t1, 1s)", "(t2)", "(t3)"] + for table in table_list: + for param in incorrect_parameter_list: + if table.startswith("st"): + tdSql.error("select elapsed{} from {} group by tbname order by ts;".format(param, table)) + else: + tdSql.error("select elapsed{} from {};".format(param, table)) + tdSql.error("select elapsed{} from {} group by ".format(param, table)) + + # query with unsupported function, like leastsquares、diff、derivative、top、bottom、last_row、interp + tdSql.error(" select elapsed(leastsquares(c_int, 1, 2)) from st1 group by tbname;") + tdSql.error("select elapsed(diff(ts)) from st1;") + tdSql.error("select elapsed(derivative(ts, 1s, 1)) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(top(ts, 5)) from st1 group by tbname order by ts;") + tdSql.error("select top(elapsed(ts), 5) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(bottom(ts)) from st1 group by tbname order by ts;") + tdSql.error("select bottom(elapsed(ts)) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(last_row(ts)) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(interp(ts, 0)) from st1 group by tbname order by ts;") + + # nested aggregate function + tdSql.error("select avg(elapsed(ts, 1s)) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(avg(ts), 1s) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(sum(ts), 1s) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(count(ts), 1s) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(min(ts), 1s) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(max(ts), 1s) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(first(ts), 1s) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(last(ts), 1s) from st1 group by tbname order by ts;") + + # other error + tdSql.error("select elapsed(ts, 1s) from t1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:40.000' interval(10s) fill(next) union select elapsed(ts, 1s) from st2 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:49.000' interval(5s) fill(prev) group by tbname;") + tdSql.error("select elapsed(time ,1s) from (select elapsed(ts,1s) time from st1);") + tdSql.error("select elapsed(ts, 1s) from (select elapsed(ts, 1s) ts from st2);") + tdSql.error("select elapsed(time, 1s) from (select elapsed(ts, 1s) time from st1 group by tbname);") + tdSql.error("select elapsed(ts , 1s) from (select elapsed(ts, 1s) ts from st2 group by tbname);") + tdSql.error("select elapsed(ts, 1s) from (select * from st1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:40.000' interval(10s) fill(next)) where c_int > 10;") + + def run(self): + self.prepareData() + self.test_normal_query() + self.test_query_with_filter() + self.test_query_with_other_function() + self.test_query_with_join() + self.test_query_with_union() + self.test_abnormal_query() + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) diff --git a/tests/parallel_test/cases.task b/tests/parallel_test/cases.task index 694af127af..239ed7a25e 100644 --- a/tests/parallel_test/cases.task +++ b/tests/parallel_test/cases.task @@ -12,6 +12,7 @@ ,,y,army,./pytest.sh python3 ./test.py -f enterprise/s3/s3_basic.py -L 3 -D 1 ,,y,army,./pytest.sh python3 ./test.py -f community/cluster/snapshot.py -N 3 -L 3 -D 2 ,,n,army,python3 ./test.py -f community/cmdline/fullopt.py +,,y,army,python3 ./test.py -f community/query/function/test_fun_elapsed.py # From aa39a151600f46daa41c5a83e961dac38f5484ff Mon Sep 17 00:00:00 2001 From: charles Date: Thu, 18 Jan 2024 10:42:53 +0800 Subject: [PATCH 2/4] update test cases --- .../query/function/test_fun_elapsed.py | 420 ++++++++++++++++++ tests/parallel_test/cases.task | 1 + 2 files changed, 421 insertions(+) create mode 100644 tests/army/community/query/function/test_fun_elapsed.py diff --git a/tests/army/community/query/function/test_fun_elapsed.py b/tests/army/community/query/function/test_fun_elapsed.py new file mode 100644 index 0000000000..0279e92704 --- /dev/null +++ b/tests/army/community/query/function/test_fun_elapsed.py @@ -0,0 +1,420 @@ +from frame.log import * +from frame.cases import * +from frame.sql import * +from frame.caseBase import * +from frame import * +from frame.eos import * + + +class TDTestCase(TBase): + """Verify the elapsed function + """ + def init(self, conn, logSql, replicaVar=1): + self.replicaVar = int(replicaVar) + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor()) + self.dbname = 'db' + self.table_dic = { + "super_table": ["st1", "st2", "st_empty"], + "child_table": ["ct1_1", "ct1_2", "ct1_empty", "ct2_1", "ct2_2", "ct2_empty"], + "tags_value": [("2023-03-01 15:00:00", 1, 'bj'), ("2023-03-01 15:10:00", 2, 'sh'), ("2023-03-01 15:20:00", 3, 'sz'), ("2023-03-01 15:00:00", 4, 'gz'), ("2023-03-01 15:10:00", 5, 'cd'), ("2023-03-01 15:20:00", 6, 'hz')], + "common_table": ["t1", "t2", "t_empty"] + } + self.start_ts = 1677654000000 # 2023-03-01 15:00:00.000 + self.row_num = 100 + + def prepareData(self): + # db + tdSql.execute("create database {};".format(self.dbname)) + tdSql.execute("use {};".format(self.dbname)) + tdLog.debug("Create database %s" % self.dbname) + + # commont table + for common_table in self.table_dic["common_table"]: + tdSql.execute("create table {} (ts timestamp, c_ts timestamp, c_int int, c_bigint bigint, c_double double, c_nchar nchar(16));".format(common_table)) + tdLog.debug("Create common table %s" % common_table) + + # super table + for super_table in self.table_dic["super_table"]: + tdSql.execute("create stable {} (ts timestamp, c_ts timestamp, c_int int, c_bigint bigint, c_double double, c_nchar nchar(16)) tags (t1 timestamp, t2 int, t3 binary(16));".format(super_table)) + tdLog.debug("Create super table %s" % super_table) + + # child table + for i in range(len(self.table_dic["child_table"])): + if self.table_dic["child_table"][i].startswith("ct1"): + tdSql.execute("create table {} using {} tags('{}', {}, '{}');".format(self.table_dic["child_table"][i], "st1", self.table_dic["tags_value"][i][0], self.table_dic["tags_value"][i][1], self.table_dic["tags_value"][i][2])) + elif self.table_dic["child_table"][i].startswith("ct2"): + tdSql.execute("create table {} using {} tags('{}', {}, '{}');".format(self.table_dic["child_table"][i], "st2", self.table_dic["tags_value"][i][0], self.table_dic["tags_value"][i][1], self.table_dic["tags_value"][i][2])) + + # insert data + table_list = ["t1", "t2", "ct1_1", "ct1_2", "ct2_1", "ct2_2"] + for t in table_list: + sql = "insert into {} values".format(t) + for i in range(self.row_num): + sql += "({}, {}, {}, {}, {}, '{}'),".format(self.start_ts + i * 1000, self.start_ts + i * 1000, 32767+i, 65535+i, i, t + str(i)) + sql += ";" + tdSql.execute(sql) + tdLog.debug("Insert data into table %s" % t) + + def test_normal_query(self): + # only one timestamp + tdSql.query("select elapsed(ts) from t1 group by c_ts;") + assert(len(tdSql.queryResult) == self.row_num and tdSql.queryResult[0][0] == 0) + + tdSql.query("select elapsed(ts, 1m) from t1 group by c_ts;") + assert(len(tdSql.queryResult) == self.row_num and tdSql.queryResult[0][0] == 0) + + # child table with group by + tdSql.query("select elapsed(ts) from ct1_2 group by tbname;") + assert(len(tdSql.queryResult) == 1 and tdSql.queryResult[0][0] == 99000) + + # empty super table + tdSql.query("select elapsed(ts, 1s) from st_empty group by tbname;") + assert(len(tdSql.queryResult) == 0) + + # empty child table + tdSql.query("select elapsed(ts, 1s) from ct1_empty group by tbname;") + assert(len(tdSql.queryResult) == 0) + + # empty common table + tdSql.query("select elapsed(ts, 1s) from t_empty group by tbname;") + assert(len(tdSql.queryResult) == 0) + + # unit as second + tdSql.query("select elapsed(ts, 1s) from st2 group by tbname;") + assert(len(tdSql.queryResult) == 2 and tdSql.queryResult[0][0] == 99) + + # unit as minute + tdSql.query("select elapsed(ts, 1m) from st2 group by tbname;") + assert(len(tdSql.queryResult) == 2 and tdSql.queryResult[0][0] == 1.65) + + # unit as hour + tdSql.query("select elapsed(ts, 1h) from st2 group by tbname;") + assert(len(tdSql.queryResult) == 2 and tdSql.queryResult[0][0] == 0.0275) + + def test_query_with_filter(self): + end_ts = 1677654000000 + 1000 * 99 + query_list = [ + { + "sql": "select elapsed(ts, 1s) from st1 where ts >= 1677654000000 group by tbname;", + "res": [(99.0, ), (99.0, )] + }, + { + "sql": "select elapsed(ts, 1s) from st1 where ts >= 1677654000000 and c_ts >= 1677654000000 group by tbname;", + "res": [(99.0, ), (99.0, )] + }, + { + "sql": "select elapsed(ts, 1s) from st1 where ts >= 1677654000000 and c_ts >= 1677654000000 and t1='2023-03-01 15:10:00.000' group by tbname;", + "res": [(99.0, )] + }, + { + "sql": "select elapsed(ts, 1s) from st_empty where ts >= 1677654000000 and c_ts >= 1677654000000 and t1='2023-03-01 15:10:00.000' group by tbname;", + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from ct1_1 where ts >= 1677654000000 group by tbname;", + "res": [(99.0, )] + }, + { + "sql": "select elapsed(ts, 1s) from ct1_2 where ts >= 1677654000000 and c_ts >= 1677654000000 group by tbname;", + "res": [(99.0, )] + }, + { + "sql": "select elapsed(ts, 1s) from ct1_empty where ts >= 1677654000000 and c_ts >= 1677654000000 group by tbname;", + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from t1 where ts >= 1677654000000 group by tbname;", + "res": [(99.0, )] + }, + { + "sql": "select elapsed(ts, 1s) from t2 where ts >= 1677654000000 and c_ts >= 1677654000000 group by tbname;", + "res": [(99.0, )] + }, + { + "sql": "select elapsed(ts, 1s) from t_empty where ts >= 1677654000000 and c_ts >= 1677654000000 group by tbname;", + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_ts > {} group by tbname;".format(end_ts), + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_ts > {} and t1='2023-03-01 15:10:00' group by tbname;".format(end_ts), + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_int < 1 group by tbname;", + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_int >= 1 and t1='2023-03-01 15:10:00' group by tbname;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_int <> 1 and t1='2023-03-01 15:10:00' group by tbname;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_nchar like 'ct2_%' and t1='2023-03-01 15:10:00' group by tbname;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_nchar like 'ct1_%' and t1='2023-03-01 15:10:00' group by tbname;", + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_nchar match '^ct2_' and t1='2023-03-01 15:10:00' group by tbname;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and c_nchar nmatch '^ct1_' and t1='2023-03-01 15:10:00' group by tbname;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st2 where ts >= 1677654000000 and t3 like 'g%' group by tbname;", + "res": [(99,)] + } + ] + for q in query_list: + tdSql.query(q["sql"]) + if len(q["res"]) == 0: + assert(len(tdSql.queryResult) == len(q["res"])) + else: + assert(len(tdSql.queryResult) == len(q["res"]) and tdSql.queryResult == q["res"]) + + def test_query_with_other_function(self): + query_list = [ + { + "sql": "select avg(c_int), count(*), elapsed(ts, 1s), leastsquares(c_int, 0, 1), spread(c_bigint), sum(c_int), hyperloglog(c_int) from st1;", + "res": [(32816.5, 200, 99.0, '{slop:0.499962, intercept:32766.753731}', 99.0, 6563300, 100)] + }, + { + "sql": "select twa(c_int) * elapsed(ts, 1s) from ct1_1;", + "res": [(3.248833500000000e+06,)] + } + ] + for q in query_list: + tdSql.query(q["sql"]) + assert(len(tdSql.queryResult) == len(q["res"]) and tdSql.queryResult == q["res"]) + + def test_query_with_join(self): + query_list = [ + { + "sql": "select elapsed(st1.ts, 1s) from st1, st2 where st1.ts = st2.ts;", + "res": [(99,)] + }, + { + "sql": "select elapsed(st1.ts, 1s) from st1, st_empty where st1.ts = st_empty.ts and st1.c_ts = st_empty.c_ts;", + "res": [] + }, + { + "sql": "select elapsed(st1.ts, 1s) from st1, ct1_1 where st1.ts = ct1_1.ts;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ct1.ts, 1s) from ct1_1 ct1, ct1_2 ct2 where ct1.ts = ct2.ts;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ct1.ts, 1s) from ct1_1 ct1, ct1_empty ct2 where ct1.ts = ct2.ts;", + "res": [] + }, + { + "sql": "select elapsed(st1.ts, 1s) from st1, ct1_empty where st1.ts = ct1_empty.ts;", + "res": [] + }, + { + "sql": "select elapsed(st1.ts, 1s) from st1, t1 where st1.ts = t1.ts;", + "res": [(99,)] + }, + { + "sql": "select elapsed(st1.ts, 1s) from st1, t_empty where st1.ts = t_empty.ts;", + "res": [] + }, + { + "sql": "select elapsed(ct1.ts, 1s) from ct1_1 ct1, t1 t2 where ct1.ts = t2.ts;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ct1.ts, 1s) from ct1_1 ct1, t_empty t2 where ct1.ts = t2.ts;", + "res": [] + }, + { + "sql": "select elapsed(st1.ts, 1s) from st1, st2, st_empty where st1.ts=st2.ts and st2.ts=st_empty.ts;", + "res": [] + } + ] + + for q in query_list: + tdSql.query(q["sql"]) + assert(len(tdSql.queryResult) == len(q["res"]) and tdSql.queryResult == q["res"]) + + def test_query_with_union(self): + query_list = [ + { + "sql": "select elapsed(ts, 1s) from st1 union select elapsed(ts, 1s) from st2;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st1 union all select elapsed(ts, 1s) from st2;", + "res": [(99,),(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st1 union all select elapsed(ts, 1s) from st_empty;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from ct1_1 union all select elapsed(ts, 1s) from ct1_2;", + "res": [(99,),(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from ct1_1 union select elapsed(ts, 1s) from ct1_2;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from ct1_1 union select elapsed(ts, 1s) from ct1_empty;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st1 where ts < '2023-03-01 15:05:00.000' union select elapsed(ts, 1s) from ct1_1 where ts >= '2023-03-01 15:01:00.000';", + "res": [(39,),(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from ct1_empty union select elapsed(ts, 1s) from t_empty;", + "res": [] + }, + { + "sql": "select elapsed(ts, 1s) from st1 group by tbname union select elapsed(ts, 1s) from st2 group by tbname;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st1 group by tbname union all select elapsed(ts, 1s) from st2 group by tbname;", + "res": [(99,),(99,),(99,),(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from st_empty group by tbname union all select elapsed(ts, 1s) from st2 group by tbname;", + "res": [(99,),(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from t1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:40.000' interval(10s) fill(next) union select elapsed(ts, 1s) from st2 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:49.000' interval(5s) fill(prev);", + "res": [(9,), (None,), (4,), (5,),(10,)] + }, + { + "sql": "select elapsed(ts, 1s) from st1 group by tbname union select elapsed(ts, 1s) from st2 group by tbname union select elapsed(ts, 1s) from st_empty group by tbname;", + "res": [(99,)] + } + ] + for q in query_list: + tdSql.query(q["sql"]) + tdLog.debug(q["sql"] + " with res: " + str(tdSql.queryResult)) + assert(len(tdSql.queryResult) == len(q["res"]) and tdSql.queryResult == q["res"]) + + def test_query_with_window(self): + query_list = [ + { + "sql": "select elapsed(ts, 1s) from st1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:00:20.000' interval(10s) fill(next);", + "res": [(10,),(10,)()] + }, + { + "sql": "select elapsed(ts, 1s) from (select * from st1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:20.000' and c_int > 100) where ts >= '2023-03-01 15:01:00.000' and ts < '2023-03-01 15:02:00.000' interval(10s) fill(prev);", + "res": [(10,)(10,)(),(),(),()] + }, + { + "sql": "select elapsed(ts, 1s) from st1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:00:20.000' session(ts, 2s);", + "res": [(20,)] + }, + { + "sql": "select elapsed(ts, 1s) from st_empty where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:00:20.000' session(ts, 2s);", + "res": [] + } + ] + for q in query_list: + tdSql.query(q["sql"]) + assert(len(tdSql.queryResult) == len(q["res"]) and tdSql.queryResult == q["res"]) + + def test_nested_query(self): + query_list = [ + { + "sql": "select elapsed(ts, 1s) from (select * from st1 where c_int > 10 and ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:40.000');", + "res": [(99,)] + }, + { + "sql": "select sum(v) from (select elapsed(ts, 1s) as v from st1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:00:20.000' interval(10s) fill(next));", + "res": [(20,)] + }, + { + "sql": "select avg(v) from (select elapsed(ts, 1s) as v from st2 group by tbname order by v);", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from (select * from st1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:40.000') where c_int > 10;", + "res": [(99,)] + }, + { + "sql": "select elapsed(ts, 1s) from (select * from st1 where c_int > 10 and ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:40.000') where c_int < 20;", + "res": [] + } + ] + for q in query_list: + tdSql.query(q["sql"]) + tdLog.debug(q["sql"] + " with res: " + str(tdSql.queryResult)) + assert(len(tdSql.queryResult) == len(q["res"]) and tdSql.queryResult == q["res"]) + + def test_abnormal_query(self): + # incorrect parameter + table_list = self.table_dic["super_table"] + self.table_dic["child_table"] + self.table_dic["common_table"] + incorrect_parameter_list = ["()", "(null)", "(*)", "(c_ts)", "(c_ts, 1s)", "(c_int)", "(c_bigint)", "(c_double)", "(c_nchar)", "(ts, null)", + "(ts, *)", "(2024-01-09 17:00:00)", "(2024-01-09 17:00:00, 1s)", "(t1)", "(t1, 1s)", "(t2)", "(t3)"] + for table in table_list: + for param in incorrect_parameter_list: + if table.startswith("st"): + tdSql.error("select elapsed{} from {} group by tbname order by ts;".format(param, table)) + else: + tdSql.error("select elapsed{} from {};".format(param, table)) + tdSql.error("select elapsed{} from {} group by ".format(param, table)) + + # query with unsupported function, like leastsquares、diff、derivative、top、bottom、last_row、interp + tdSql.error(" select elapsed(leastsquares(c_int, 1, 2)) from st1 group by tbname;") + tdSql.error("select elapsed(diff(ts)) from st1;") + tdSql.error("select elapsed(derivative(ts, 1s, 1)) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(top(ts, 5)) from st1 group by tbname order by ts;") + tdSql.error("select top(elapsed(ts), 5) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(bottom(ts)) from st1 group by tbname order by ts;") + tdSql.error("select bottom(elapsed(ts)) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(last_row(ts)) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(interp(ts, 0)) from st1 group by tbname order by ts;") + + # nested aggregate function + tdSql.error("select avg(elapsed(ts, 1s)) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(avg(ts), 1s) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(sum(ts), 1s) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(count(ts), 1s) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(min(ts), 1s) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(max(ts), 1s) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(first(ts), 1s) from st1 group by tbname order by ts;") + tdSql.error("select elapsed(last(ts), 1s) from st1 group by tbname order by ts;") + + # other error + tdSql.error("select elapsed(ts, 1s) from t1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:40.000' interval(10s) fill(next) union select elapsed(ts, 1s) from st2 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:49.000' interval(5s) fill(prev) group by tbname;") + tdSql.error("select elapsed(time ,1s) from (select elapsed(ts,1s) time from st1);") + tdSql.error("select elapsed(ts, 1s) from (select elapsed(ts, 1s) ts from st2);") + tdSql.error("select elapsed(time, 1s) from (select elapsed(ts, 1s) time from st1 group by tbname);") + tdSql.error("select elapsed(ts , 1s) from (select elapsed(ts, 1s) ts from st2 group by tbname);") + tdSql.error("select elapsed(ts, 1s) from (select * from st1 where ts between '2023-03-01 15:00:00.000' and '2023-03-01 15:01:40.000' interval(10s) fill(next)) where c_int > 10;") + + def run(self): + self.prepareData() + self.test_normal_query() + self.test_query_with_filter() + self.test_query_with_other_function() + self.test_query_with_join() + self.test_query_with_union() + self.test_abnormal_query() + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) diff --git a/tests/parallel_test/cases.task b/tests/parallel_test/cases.task index 694af127af..239ed7a25e 100644 --- a/tests/parallel_test/cases.task +++ b/tests/parallel_test/cases.task @@ -12,6 +12,7 @@ ,,y,army,./pytest.sh python3 ./test.py -f enterprise/s3/s3_basic.py -L 3 -D 1 ,,y,army,./pytest.sh python3 ./test.py -f community/cluster/snapshot.py -N 3 -L 3 -D 2 ,,n,army,python3 ./test.py -f community/cmdline/fullopt.py +,,y,army,python3 ./test.py -f community/query/function/test_fun_elapsed.py # From ad170ecb761980bc8f2b0df9bd6eb0ba452ef50e Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 22 Jan 2024 13:56:07 +0800 Subject: [PATCH 3/4] Delete tests/pytest/arm64_ci_feishu_notice.py --- tests/pytest/arm64_ci_feishu_notice.py | 84 -------------------------- 1 file changed, 84 deletions(-) delete mode 100644 tests/pytest/arm64_ci_feishu_notice.py diff --git a/tests/pytest/arm64_ci_feishu_notice.py b/tests/pytest/arm64_ci_feishu_notice.py deleted file mode 100644 index 9964fb2633..0000000000 --- a/tests/pytest/arm64_ci_feishu_notice.py +++ /dev/null @@ -1,84 +0,0 @@ -import requests -import subprocess - -"""This file is used to send notice to feishu robot with daily arm64 ci test result -""" -group_url = 'https://open.feishu.cn/open-apis/bot/v2/hook/56c333b5-eae9-4c18-b0b6-7e4b7174f5c9' - -def get_msg(text): - return { - "msg_type": "post", - "content": { - "post": { - "zh_cn": { - "title": "ARM64 CI test report", - "content": [ - [{ - "tag": "text", - "text": text - } - ]] - } - } - } - } - -def send_msg(result, testScope, owner, hostname, logDir, others): - text = f'''result: {result} - test scope: {testScope} - owner: {owner} - hostname: {hostname} - log dir: {logDir} - others: {others}\n''' - - json = get_msg(text) - headers = { - 'Content-Type': 'application/json' - } - - req = requests.post(url=group_url, headers=headers, json=json) - inf = req.json() - if "StatusCode" in inf and inf["StatusCode"] == 0: - pass - else: - print(inf) - -def check_build_failed(): - path = "/home/arm64/log/" - res = False - try: - r = subprocess.Popen("ls | wc -l", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=path) - stdout, stderr = r.communicate() - if r.returncode != 0: - print("Failed to execute 'ls -l' for path '{}' with error: {}".format(path, stderr)) - else: - if int(stdout) == 0: - res = True - return res - except Exception as ex: - raise Exception("Failed to check build failed with error: {}".format(str(ex))) - -def check_run_case_status(): - path = "/home/arm64/log/*/stat.txt" - r = subprocess.Popen("cat {}".format(path), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = r.communicate() - if r.returncode != 0: - print("Failed to execute 'cat {}' with error: {}".format(path, stderr)) - else: - for line in stdout.decode().split("\n"): - if "Failed:" in line: - failed_case_num = int(line.split(":")[1].strip()) - if 0 == failed_case_num: - return "All cases passed" - else: - return stdout.decode() - -def send_notice(): - if check_build_failed(): - send_msg("Build failed", "arm64_ci_test", "charles", "ecs-3e78", "/home/arm64/log", "arm 64 ci build failed") - run_res = check_run_case_status() - if "All cases passed" != run_res: - send_msg(run_res, "arm64_ci_test", "charles", "ecs-3e78", "/home/arm64/log", "arm 64 ci run case failed") - -if __name__ == '__main__': - send_notice() From eb1f10764559596a008bf27d11db50456ce978c9 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 22 Jan 2024 13:56:35 +0800 Subject: [PATCH 4/4] Delete tests/system-test/2-query/test_ts4382.py --- tests/system-test/2-query/test_ts4382.py | 531 ----------------------- 1 file changed, 531 deletions(-) delete mode 100644 tests/system-test/2-query/test_ts4382.py diff --git a/tests/system-test/2-query/test_ts4382.py b/tests/system-test/2-query/test_ts4382.py deleted file mode 100644 index 144cd58ff6..0000000000 --- a/tests/system-test/2-query/test_ts4382.py +++ /dev/null @@ -1,531 +0,0 @@ -import random -import itertools -from util.log import * -from util.cases import * -from util.sql import * -from util.sqlset import * -from util import constant -from util.common import * - - -class TDTestCase: - """Verify the jira TS-4382 - """ - def init(self, conn, logSql, replicaVar=1): - self.replicaVar = int(replicaVar) - tdLog.debug("start to execute %s" % __file__) - tdSql.init(conn.cursor()) - # self.dbname = 'db' - # self.stbname = 'st' - # self.ctbname_list = ["ct1", "ct2"] - # self.tag_value_list = ['{"instance":"100"}', '{"instance":"200"}'] - - self.metadata_dic = { - "db_tag_json": { - "supertables": [ - { - "name": "st", - "child_table_num": 2, - "columns": [ - { - "name": "ts", - "type": "timestamp" - }, - { - "name": "col1", - "type": "int" - } - ], - "tags": [ - { - "name": "t1", - "type": "json" - } - ] - } - ] - }, - "db": { - "supertables": [ - { - "name": "st1", - "child_table_num": 2, - "columns": [ - { - "name": "ts", - "type": "timestamp" - }, - { - "name": "col1", - "type": "int" - }, - { - "name": "col2", - "type": "bigint" - }, - { - "name": "col3", - "type": "float" - }, - { - "name": "col4", - "type": "double" - }, - { - "name": "col5", - "type": "bool" - }, - { - "name": "col6", - "type": "binary(16)" - }, - { - "name": "col7", - "type": "nchar(16)" - }, - { - "name": "col8", - "type": "geometry(512)" - }, - { - "name": "col9", - "type": "varbinary(32)" - } - ], - "tags": [ - { - "name": "t1", - "type": "timestamp" - }, - { - "name": "t2", - "type": "int" - }, - { - "name": "t3", - "type": "bigint" - }, - { - "name": "t4", - "type": "float" - }, - { - "name": "t5", - "type": "double" - }, - { - "name": "t6", - "type": "bool" - }, - { - "name": "t7", - "type": "binary(16)" - }, - { - "name": "t8", - "type": "nchar(16)" - }, - { - "name": "t9", - "type": "geometry(512)" - }, - { - "name": "t10", - "type": "varbinary(32)" - } - ] - }, - { - "name": "st2", - "child_table_num": 2, - "columns": [ - { - "name": "ts", - "type": "timestamp" - }, - { - "name": "col1", - "type": "int" - }, - { - "name": "col2", - "type": "bigint" - }, - { - "name": "col3", - "type": "float" - }, - { - "name": "col4", - "type": "double" - }, - { - "name": "col5", - "type": "bool" - }, - { - "name": "col6", - "type": "binary(16)" - }, - { - "name": "col7", - "type": "nchar(16)" - }, - { - "name": "col8", - "type": "geometry(512)" - }, - { - "name": "col9", - "type": "varbinary(32)" - } - ], - "tags": [ - { - "name": "t1", - "type": "timestamp" - }, - { - "name": "t2", - "type": "int" - }, - { - "name": "t3", - "type": "bigint" - }, - { - "name": "t4", - "type": "float" - }, - { - "name": "t5", - "type": "double" - }, - { - "name": "t6", - "type": "bool" - }, - { - "name": "t7", - "type": "binary(16)" - }, - { - "name": "t8", - "type": "nchar(16)" - }, - { - "name": "t9", - "type": "geometry(512)" - }, - { - "name": "t10", - "type": "varbinary(32)" - } - ] - } - ] - } - } - - def prepareData(self): - for db in self.metadata_dic.keys(): - if db == "db_tag_json": - # db - tdSql.execute("create database {};".format(db)) - tdSql.execute("use {};".format(db)) - tdLog.debug("Create database %s" % db) - - # super table - for item in self.metadata_dic[db]["supertables"]: - sql = "create table {} (".format(item["name"]) - for column in item["columns"]: - sql += "{} {},".format(column["name"], column["type"]) - sql = sql[:-1] + ") tags (" - for tag in item["tags"]: - sql += "{} {},".format(tag["name"], tag["type"]) - sql = sql[:-1] + ");" - tdLog.debug(sql) - tdSql.execute(sql) - tdLog.debug("Create super table {}".format(item["name"])) - - # child table - tag_value_list = ['{"instance":"100"}', '{"instance":"200"}'] - for i in range(item["child_table_num"]): - tdSql.execute("create table {} using {} tags('{}');".format("ct" + str(i+1), item["name"], tag_value_list[i])) - tdLog.debug("Create child table {} successfully".format("ct" + str(i+1))) - - # insert data - if i == 0: - tdSql.execute("insert into {} values(now, 1)(now+1s, 2)".format("ct" + str(i+1))) - elif i == 1: - tdSql.execute("insert into {} values(now, null)(now+1s, null)".format("ct" + str(i+1))) - elif db == "db": - # create database db_empty - tdSql.execute("create database db_empty;") - tdSql.execute("use db_empty;") - tdLog.debug("Create database db_empty successfully") - - # super table - for item in self.metadata_dic[db]["supertables"]: - sql = "create table {} (".format(item["name"]) - for column in item["columns"]: - sql += "{} {},".format(column["name"], column["type"]) - sql = sql[:-1] + ") tags (" - for tag in item["tags"]: - sql += "{} {},".format(tag["name"], tag["type"]) - sql = sql[:-1] + ");" - tdLog.debug(sql) - tdSql.execute(sql) - tdLog.debug("Create super table {}".format(item["name"])) - - # child table - tag_value_list = [['2024-01-01 12:00:00.000', 1, 1111111111111, 1.11, 111111.1111, True, 'aaa', 'beijing', 'POINT (3.000000 6.000000)', '0x7661726331'],['2024-01-02 12:00:00.000', 2, 2222222222222, 2.22, 222222.2222, False, 'bbb', 'shanghai', 'LINESTRING (1.000000 1.000000, 2.000000 2.000000, 5.000000 5.000000)', '0x7f829000']] - for i in range(item["child_table_num"]): - sql = "create table {} using {} tags(".format("ct" + (str(i+1) if item["name"] == "st1" else str(i+3)), item["name"]) - for tag in tag_value_list[i]: - if type(tag) == str: - sql += "'{}',".format(tag) - else: - sql += "{},".format(tag) - sql = sql[:-1] + ");" - tdSql.execute(sql) - tdLog.debug("Create child table {} successfully".format("ct" + (str(i+1) if item["name"] == "st1" else str(i+3)))) - - # create database db_with_data - tdSql.execute("create database db_with_data;") - tdSql.execute("use db_with_data;") - tdLog.debug("Create database db_with_data successfully") - - # super table - for item in self.metadata_dic[db]["supertables"]: - sql = "create table {} (".format(item["name"]) - for column in item["columns"]: - sql += "{} {},".format(column["name"], column["type"]) - sql = sql[:-1] + ") tags (" - for tag in item["tags"]: - sql += "{} {},".format(tag["name"], tag["type"]) - sql = sql[:-1] + ");" - tdLog.debug(sql) - tdSql.execute(sql) - tdLog.debug("Create super table {}".format(item["name"])) - - # child table - tag_value_list = [['2024-01-01 12:00:00.000', 1, 1111111111111, 1.11, 111111.1111, True, 'aaa', 'beijing', 'POINT (3.000000 6.000000)', '0x7661726331'],['2024-01-02 12:00:00.000', 2, 2222222222222, 2.22, 222222.2222, False, 'bbb', 'shanghai', 'LINESTRING (1.000000 1.000000, 2.000000 2.000000, 5.000000 5.000000)', '0x7f829000']] - for i in range(item["child_table_num"]): - sql = "create table {} using {} tags(".format("ct" + (str(i+1) if item["name"] == "st1" else str(i+3)), item["name"]) - for tag in tag_value_list[i]: - if type(tag) == str: - sql += "'{}',".format(tag) - else: - sql += "{},".format(tag) - sql = sql[:-1] + ");" - tdSql.execute(sql) - tdLog.debug("Create child table {} successfully".format("ct" + (str(i+1) if item["name"] == "st1" else str(i+3)))) - - # insert into data - start_ts = 1677654000000 # 2023-03-01 15:00:00.000 - sql = "insert into {} values".format("ct" + (str(i+1) if item["name"] == "st1" else str(i+3))) - binary_vlist = ["ccc", "ddd", "eee", "fff"] - nchar_vlist = ["guangzhou", "tianjing", "shenzhen", "hangzhou"] - geometry_vlist = ["POINT (4.0 8.0)", "POINT (3.0 5.0)", "LINESTRING (1.000000 1.000000, 2.000000 2.000000, 5.000000 5.000000)", "POLYGON ((3.000000 6.000000, 5.000000 6.000000, 5.000000 8.000000, 3.000000 8.000000, 3.000000 6.000000))"] - varbinary_vlist = ["0x7661726332", "0x7661726333", "0x7661726334", "0x7661726335"] - st_index = i if item["name"] == "st1" else (i+2) - for i in range(100): - sql += "({}, {}, {}, {}, {}, {}, '{}', '{}', '{}', '{}')".format(start_ts + 1000 * i, str(i+1), str(i+1), str(i+1), str(i+1), True if i % 2 == 0 else False, binary_vlist[st_index % 4], nchar_vlist[st_index % 4], geometry_vlist[st_index % 4], varbinary_vlist[st_index % 4]) - tdSql.execute(sql) - tdLog.debug("Insert into data into child table {} successfully".format("ct" + (str(i+1) if item["name"] == "st1" else str(i+3)))) - - def test_tag_json(self): - tdSql.execute("use db_tag_json;") - sql_list = [ - # super table query with correct tag name of json type - { - "sql": "select to_char(ts, 'yyyy-mm-dd hh24:mi:ss') as time, irate(col1) from st group by to_char(ts, 'yyyy-mm-dd hh24:mi:ss'), t1->'instance' order by time;", - "result_check": "0.0" - }, - # child table query with incorrect tag name of json type - { - "sql": "select to_char(ts, 'yyyy-mm-dd hh24:mi:ss') as time, irate(col1) from ct1 group by to_char(ts, 'yyyy-mm-dd hh24:mi:ss'), t1->'name' order by time;", - "result_check": "None" - }, - # child table query with null value - { - "sql": "select ts, avg(col1) from ct2 group by ts, t1->'name' order by ts;", - "result_check": "None" - } - ] - for sql_dic in sql_list: - tdSql.query(sql_dic["sql"]) - tdLog.debug("execute sql: %s" % sql_dic["sql"]) - for item in [row[1] for row in tdSql.queryResult]: - if sql_dic["result_check"] in str(item): - tdLog.debug("Check query result of '{}' successfully".format(sql_dic["sql"])) - break - - def test_db_empty(self): - tdSql.execute("use db_empty;") - table_list = ["st1", "ct1"] - column_list = ["col1", "col2", "col3", "col4", "col5"] - tag_list = ["t2", "t3", "t4", "t5", "t6"] - operator_list = ["+", "-", "*", "/"] - fun_list = ["avg", "count", "sum", "spread"] - - # two columns with arithmetic operation - for table in table_list: - for columns in list(itertools.combinations(column_list + tag_list, 2)): - operator = random.choice(operator_list) - sql = "select ({} {} {}) as total from {};".format(columns[0], operator, columns[1], table) - tdSql.query(sql) - tdSql.checkRows(0) - - # aggregation function - for table in table_list: - for columns in list(itertools.combinations(column_list[:-1] + tag_list[:-1], 2)): - fun = random.sample(fun_list, 2) - sql = "select ({}({}) + {}({})) as total from {};".format(fun[0], columns[0], fun[1], columns[1], table) - tdSql.query(sql) - if "count" in fun: - # default config 'countAlwaysReturnValue' as 0 - tdSql.checkRows(1) - else: - tdSql.checkRows(0) - - # join - table_list = ["st1", "st2", "ct1", "ct2", "ct3", "ct4"] - column_list = ["col1", "col2", "col3", "col4", "col5"] - tag_list = ["t2", "t3", "t4", "t5", "t6"] - where_list = ["col1 > 100", "col2 < 237883294", "col3 >= 163.23", "col4 <= 674324.2374898237", "col5=true", "col6='aaa'", - "col7='beijing'", "col8!='POINT (3.000000 6.000000)'", "col9='0x7661726331'"] - for table in list(itertools.combinations(table_list,2)): - where = random.choice(where_list) - column = random.choice(column_list) - tag = random.choice(tag_list) - sql = "select ({} + {}) total from {} join {} on {} where {};".format(table[0] + "." + column, table[1] + "." + tag, table[0], table[1], table[0]+ ".ts=" + table[1] + ".ts", table[0] + "." + where) - tdSql.query(sql) - tdSql.checkRows(0) - - # group by - value_fun_list = ["sum(col1+col2)", "avg(col3+col4)", "count(col6+col7)", "stddev(col2+col4)", "spread(col2+col3)"] - group_by_list = ["tbname", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9", "t10"] - for table in table_list: - value_fun = random.choice(value_fun_list) - where = random.choice(where_list) - group_by = random.choice(group_by_list) - sql = "select {} from {} where {} group by {};".format(value_fun, table, where, group_by) - tdSql.query(sql) - # default config 'countAlwaysReturnValue' as 0 - if "count" in value_fun and "st" in table: - tdSql.checkRows(2) - elif "count" in value_fun and "ct" in table: - tdSql.checkRows(1) - else: - tdSql.checkRows(0) - - # window query - for table in table_list: - tag = random.choice(tag_list) - if "st" in table: - sql = "select _wstart, {}, avg(col3+col4) from {} where ts between '2024-03-01' and '2024-03-02' partition by {} interval(10s) sliding(5s) fill(linear);".format(tag, table, tag) - elif "ct" in table: - sql = "select _wstart, sum(col1+col2) from {} where ts between '2024-03-01' and '2024-03-02' partition by {} interval(10s) sliding(5s) fill(next);".format(table, tag) - tdSql.query(sql) - tdSql.checkRows(0) - - # nested query - for table in table_list: - sql_list = [ - "select (col1 + col2) from (select sum(col1) as col1, avg(col2) as col2 from {} where col1 > 100 and ts between '2024-03-01' and '2024-03-02' group by tbname);".format(table), - "select last(ts), avg(col2 - col3) from (select first(ts) as ts, sum(col2) as col2, last(col3) as col3 from {} where col9 != 'abc' partition by tbname interval(10s) sliding(5s));".format(table), - "select elapsed(ts, 1s), sum(c1 + c2) from (select * from (select ts, (col1+col2) as c1, (col3 * col4) as c2, tbname from {} where col1 > 100 and ts between '2024-03-01' and '2024-03-02')) group by tbname;".format(table) - ] - for sql in sql_list: - tdSql.query(sql) - tdSql.checkRows(0) - - # drop column/tag - del_column_tag_list = ["col1", "t1"] - error_sql_list = [ - "select first(t1), sum(col1) from st1 group by tbname;", - "select last(ts), avg(col1) from st1 group by tbname;", - "select count(col1) from (select * from st1 where ts between '2024-03-01' and '2024-03-02' and col1 > 100) group by tbname;", - ] - for item in del_column_tag_list: - if "col" in item: - sql = "alter table st1 drop column {};".format(item) - elif "t" in item: - sql = "alter table st1 drop tag {};".format(item) - tdSql.execute(sql) - tdLog.debug("Delete {} successfully".format(str(del_column_tag_list))) - - for table in table_list: - for sql in error_sql_list: - tdSql.error(sql) - - # modify column for common table - tdSql.execute("create table t1 (ts timestamp, col1 int, col2 bigint, col3 float, col4 double, col5 bool, col6 binary(16), col7 nchar(16), col8 geometry(512), col9 varbinary(32));") - tdSql.execute("insert into t1 values(now, 1, 1111111111111, 1.11, 111111.1111, True, 'aaa', 'beijing', 'POINT (3.000000 6.000000)', '0x7661726331');") - tdSql.execute("alter table t1 rename column col1 col11;") - tdSql.error("select col1 from t1 where ts <= now and col3=1.11;") - tdSql.query("select col11 from t1 where ts <= now and col3=1.11;") - assert(len(tdSql.queryResult) == 1 and tdSql.queryResult[0][0] == 1) - - def test_db_with_data(self): - tdSql.execute("use db_with_data;") - - sql_list = [ - "select pow(col1, null) from st1 where ts > now;", - "select pow(null, col1) from st1 where ts > now;", - "select log(null, col2) from st1 where col1 > 1000;", - "select log(col2, null) from st1 where col1 > 1000;", - "select avg(col1 + t2) from ct1 where ts between '2025-03-01' and '2025-03-02' and t2 < 0;", - "select char_length(col6) from st1 where ts > now;", - "select concat(col6, col7) from st1 where ts > now;", - "select char_length(concat(col6, col7)) from st1 where ts > now;", - "select rtrim(ltrim(concat(col6, col7))) from st1 where ts > now;", - "select lower(rtrim(ltrim(concat(col6, col7)))) from st1 where ts > now;", - "select upper(rtrim(ltrim(concat(col6, col7)))) from st1 where ts > now;", - "select substr(rtrim(ltrim(concat(col6, col7))), 1, 10) from st1 where ts > now;", - "select avg(col1 - col2) as v from st1 where ts between '2022-03-01' and '2022-03-02';", - "select avg(col1 * col3) as v from st1 where ts between '2022-03-01' and '2022-03-02' and col1 > 100 group by tbname;", - "select sum(col1 / col4) as cv, avg(t2 + t3) as tv from st1 where ts between '2022-03-01' and '2022-03-02' and col1 > 100 group by tbname;", - "select sum(v1+v2) from (select first(ts) as time, avg(col1+col2) as v1, max(col3) as v2 from st1 where ts > now group by (col1+col2) order by (col1+col2));", - "select first(ts), count(*), avg(col2 * t3) from (select ts, col1, col2, col3, t1, t2, t3, tbname from st1 where ts between '2022-03-01' and '2022-03-02' and col1 > 100) group by tbname;", - "select cast(t8 as nchar(32)), sum(col1), avg(col2) from st1 where ts > now group by cast(t8 as nchar(32));", - "select to_char(time, 'yyyy-mm-dd'), sum(v2 - v1) from (select first(ts) as time, avg(col2 + col3) as v1, max(col4) as v2 from st1 where ts < now group by (col2+col3) order by (col2+col3)) where time > now group by to_char(time, 'yyyy-mm-dd');", - "select count(time) * sum(v) from (select to_iso8601(ts, '+00:00') as time, abs(col1+col2) as v, tbname from st1 where ts between '2023-03-01' and '2023-03-02' and col1 > 100) group by tbname;", - "select avg(v) from (select apercentile(col1, 50) as v from st1 where ts between '2023-03-01' and '2023-03-02' group by tbname) where v > 50;", - ] - for sql in sql_list: - tdSql.query(sql) - tdSql.checkRows(0) - - sql_list_with_res = [ - { - "sql": "select total / v from (select elapsed(ts, 1s) as v, sum(col1) as total from st1 where ts between '2023-03-01' and '2023-03-02' interval(10s) fill(next));", - "res": [8641, (11,)] - }, - { - "sql": "select to_char(time, 'yyyy-mm-dd'), sum(v2 - v1) from (select first(ts) as time, avg(col2 + col3) as v1, max(col4) as v2 from st1 where ts < now group by (col2+col3) order by (col2+col3)) group by to_char(time, 'yyyy-mm-dd');", - "res": [1, ('2023-03-01', -5050,)] - }, - { - "sql": "select avg(v) from (select apercentile(col1, 50) as v from st1 where ts between '2023-03-01' and '2023-03-02' group by tbname) group by (50 -v);", - "res": [1, (50,)] - } - ] - for sql in sql_list_with_res: - tdSql.query(sql["sql"]) - assert(len(tdSql.queryResult) == sql["res"][0] and tdSql.queryResult[0][0] == sql["res"][1][0]) - - def run(self): - self.prepareData() - self.test_tag_json() - self.test_db_empty() - self.test_db_with_data() - - def stop(self): - tdSql.close() - tdLog.success("%s successfully executed" % __file__) - -tdCases.addWindows(__file__, TDTestCase()) -tdCases.addLinux(__file__, TDTestCase())