twa.py 5.7 KB
Newer Older
W
wenzhouwww@live.cn 已提交
1 2 3 4 5 6 7 8 9
from util.log import *
from util.cases import *
from util.sql import *
import numpy as np
import random ,os ,sys
import platform
import math

class TDTestCase:
C
cpwu 已提交
10
    updatecfgDict = {"maxTablesPerVnode":2 ,"minTablesPerVnode":2,"tableIncStepPerVnode":2 }
W
wenzhouwww@live.cn 已提交
11 12 13 14 15 16 17 18 19 20

    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor())
        self.vnode_disbutes = None
        self.ts = 1537146000000
        self.tb_nums = 20
        self.row_nums = 100
        self.time_step = 1000

C
cpwu 已提交
21
    def prepare_datas_of_distribute(self, dbname="testdb"):
G
Ganlin Zhao 已提交
22

W
wenzhouwww@live.cn 已提交
23
        # prepate datas for  20 tables distributed at different vgroups
C
cpwu 已提交
24
        tdSql.execute(f"create database if not exists {dbname} keep 3650 duration 1000 vgroups 5")
W
wenzhouwww@live.cn 已提交
25
        tdSql.execute(
C
cpwu 已提交
26
            f'''create table {dbname}.stb1
W
wenzhouwww@live.cn 已提交
27 28 29 30
            (ts timestamp, c1 int, c2 bigint, c3 smallint, c4 tinyint, c5 float, c6 double, c7 bool, c8 binary(16),c9 nchar(32), c10 timestamp)
            tags (t0 timestamp, t1 int, t2 bigint, t3 smallint, t4 tinyint, t5 float, t6 double, t7 bool, t8 binary(16),t9 nchar(32))
            '''
        )
G
Ganlin Zhao 已提交
31

W
wenzhouwww@live.cn 已提交
32
        for i in range(self.tb_nums):
C
cpwu 已提交
33
            tdSql.execute(f'create table {dbname}.ct{i+1} using {dbname}.stb1 tags ( now(), {1*i}, {11111*i}, {111*i}, {1*i}, {1.11*i}, {11.11*i}, {i%2}, "binary{i}", "nchar{i}" )')
W
wenzhouwww@live.cn 已提交
34 35
            ts = self.ts
            for j in range(self.row_nums):
G
Ganlin Zhao 已提交
36
                ts+=j*self.time_step
W
wenzhouwww@live.cn 已提交
37
                tdSql.execute(
C
cpwu 已提交
38
                    f"insert into {dbname}.ct{i+1} values({ts}, 1, 11111, 111, 1, 1.11, 11.11, 2, 'binary{j}', 'nchar{j}', now()+{1*j}a )"
W
wenzhouwww@live.cn 已提交
39
                )
G
Ganlin Zhao 已提交
40

C
cpwu 已提交
41 42 43
        tdSql.execute(f"insert into {dbname}.ct1 values (now()-810d, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ")
        tdSql.execute(f"insert into {dbname}.ct1 values (now()-400d, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ")
        tdSql.execute(f"insert into {dbname}.ct1 values (now()+90d, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL  ) ")
W
wenzhouwww@live.cn 已提交
44 45 46

        tdLog.info(" prepare data for distributed_aggregate done! ")

C
cpwu 已提交
47 48
    def twa_support_types(self, dbname="testdb"):
        tdSql.query(f"desc {dbname}.stb1 ")
W
update  
wenzhouwww@live.cn 已提交
49 50 51
        schema_list = tdSql.queryResult
        for col_type in schema_list:
            if col_type[1] in ["TINYINT" ,"SMALLINT","BIGINT" ,"INT","FLOAT","DOUBLE"]:
C
cpwu 已提交
52
                tdSql.query(f"select twa({col_type[0]}) from {dbname}.stb1 partition by tbname ")
W
update  
wenzhouwww@live.cn 已提交
53
            else:
C
cpwu 已提交
54
                tdSql.error(f"select twa({col_type[0]}) from {dbname}.stb1 partition by tbname ")
W
update  
wenzhouwww@live.cn 已提交
55 56


C
cpwu 已提交
57
    def check_distribute_datas(self, dbname="testdb"):
W
wenzhouwww@live.cn 已提交
58
        # get vgroup_ids of all
C
cpwu 已提交
59
        tdSql.query(f"show {dbname}.vgroups ")
W
wenzhouwww@live.cn 已提交
60 61 62
        vgroups = tdSql.queryResult

        vnode_tables={}
G
Ganlin Zhao 已提交
63

W
wenzhouwww@live.cn 已提交
64 65 66 67
        for vgroup_id in vgroups:
            vnode_tables[vgroup_id[0]]=[]

        # check sub_table of per vnode ,make sure sub_table has been distributed
C
cpwu 已提交
68
        tdSql.query(f"select * from information_schema.ins_tables where db_name = '{dbname}' and table_name like 'ct%'")
W
wenzhouwww@live.cn 已提交
69 70 71
        table_names = tdSql.queryResult
        tablenames = []
        for table_name in table_names:
G
Ganlin Zhao 已提交
72
            vnode_tables[table_name[6]].append(table_name[0])
W
wenzhouwww@live.cn 已提交
73 74 75 76 77 78 79 80 81
        self.vnode_disbutes = vnode_tables

        count = 0
        for k ,v in vnode_tables.items():
            if len(v)>=2:
                count+=1
        if count < 2:
            tdLog.exit(" the datas of all not satisfy sub_table has been distributed ")

C
cpwu 已提交
82
    def distribute_twa_query(self, dbname="testdb"):
W
wenzhouwww@live.cn 已提交
83
        # basic filter
C
cpwu 已提交
84
        tdSql.query(f"select twa(c1) from {dbname}.ct1  ")
W
wenzhouwww@live.cn 已提交
85 86
        tdSql.checkData(0,0,1.000000000)

C
cpwu 已提交
87
        tdSql.query(f"select twa(c1) from {dbname}.stb1 partition by tbname  ")
W
wenzhouwww@live.cn 已提交
88 89 90
        tdSql.checkRows(self.tb_nums)
        tdSql.checkData(0,0,1.000000000)

C
cpwu 已提交
91
        tdSql.query(f"select twa(c2) from {dbname}.stb1 group by tbname ")
W
wenzhouwww@live.cn 已提交
92 93 94
        tdSql.checkRows(self.tb_nums)
        tdSql.checkData(0,0,11111.000000000)

C
cpwu 已提交
95
        tdSql.query(f"select twa(c1+c2) from {dbname}.stb1 partition by tbname ")
W
wenzhouwww@live.cn 已提交
96 97
        tdSql.checkData(0,0,11112.000000000)

C
cpwu 已提交
98
        tdSql.query(f"select twa(c1) from {dbname}.stb1 partition by t1")
W
wenzhouwww@live.cn 已提交
99 100 101
        tdSql.checkRows(self.tb_nums)
        tdSql.checkData(0,0,1.000000000)

G
Ganlin Zhao 已提交
102
        # union all
C
cpwu 已提交
103
        tdSql.query(f"select twa(c1) from {dbname}.stb1 partition by tbname union all select twa(c1) from {dbname}.stb1 partition by tbname ")
W
wenzhouwww@live.cn 已提交
104 105 106
        tdSql.checkRows(40)
        tdSql.checkData(0,0,1.000000000)

G
Ganlin Zhao 已提交
107
        # join
W
wenzhouwww@live.cn 已提交
108 109 110

        tdSql.execute(" create database if not exists db ")
        tdSql.execute(" use db ")
C
cpwu 已提交
111 112 113
        tdSql.execute(" create stable db.st (ts timestamp , c1 int ,c2 float) tags(t1 int) ")
        tdSql.execute(" create table db.tb1 using db.st tags(1) ")
        tdSql.execute(" create table db.tb2 using db.st tags(2) ")
W
wenzhouwww@live.cn 已提交
114

G
Ganlin Zhao 已提交
115

W
wenzhouwww@live.cn 已提交
116 117
        for i in range(10):
            ts = i*10 + self.ts
C
cpwu 已提交
118 119
            tdSql.execute(f" insert into db.tb1 values({ts},{i},{i}.0)")
            tdSql.execute(f" insert into db.tb2 values({ts},{i},{i}.0)")
W
wenzhouwww@live.cn 已提交
120

C
cpwu 已提交
121
        tdSql.query(f"select twa(tb1.c1), twa(tb2.c2) from db.tb1 tb1, db.tb2 tb2 where tb1.ts=tb2.ts ")
W
wenzhouwww@live.cn 已提交
122 123 124 125 126
        tdSql.checkRows(1)
        tdSql.checkData(0,0,4.500000000)
        tdSql.checkData(0,1,4.500000000)

        # mixup with other functions
H
Haojun Liao 已提交
127
        tdSql.query(f"select twa(c1),twa(c2),max(c1),elapsed(ts) from {dbname}.ct1 ")
W
wenzhouwww@live.cn 已提交
128 129 130 131 132 133 134
        tdSql.checkData(0,0,1.000000000)
        tdSql.checkData(0,1,11111.000000000)
        tdSql.checkData(0,2,1)

    def run(self):
        self.prepare_datas_of_distribute()
        self.check_distribute_datas()
W
update  
wenzhouwww@live.cn 已提交
135
        self.twa_support_types()
W
wenzhouwww@live.cn 已提交
136
        self.distribute_twa_query()
G
Ganlin Zhao 已提交
137

W
wenzhouwww@live.cn 已提交
138 139 140 141 142 143
    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)

tdCases.addWindows(__file__, TDTestCase())
tdCases.addLinux(__file__, TDTestCase())