parser.py 9.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
###################################################################
#           Copyright (c) 2016 by TAOS Technologies, Inc.
#                     All rights reserved.
#
#  This file is proprietary and confidential to TAOS Technologies.
#  No part of this file may be reproduced, stored, transmitted,
#  disclosed or used in any form or by any means other than as
#  expressly provided by the written permission from Jianhui Tao
#
###################################################################

# -*- coding: utf-8 -*-

import sys
import time
import taos
from util.log import tdLog
from util.cases import tdCases
from util.sql import tdSql


class TDTestCase:
    def sleep(self, duration):
        tdLog.info("sleeping %f seconds" % duration)
        time.sleep(duration)

    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor(), logSql)

B
Bomin Zhang 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    '''
    def bug2222(self):
        tdSql.prepare()
        tdSql.execute("create table superreal(ts timestamp, addr binary(5), val float) tags (deviceNo binary(20))")
        tdSql.execute("create table real_001 using superreal tags('001')")
        tdSql.execute("create table tj_001 as select sum(val) from real_001 interval(1m)")

        t = datetime.datetime.now()
        for i in range(60):
            ts = t.strftime("%Y-%m-%d %H:%M")
            t += datetime.timedelta(minutes=1)
            sql = "insert into real_001 values('%s:0%d', '1', %d)" % (ts, 0, i)
            for j in range(4):
                sql += ",('%s:0%d', '%d', %d)" % (ts, j + 1, j + 1, i)
            tdSql.execute(sql)
            time.sleep(60 + random.random() * 60 - 30)
    '''

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    def tbase300(self):
        tdLog.debug("begin tbase300")

        tdSql.prepare()
        tdSql.execute("create table mt(ts timestamp, c1 int, c2 int) tags(t1 int)")
        tdSql.execute("create table tb1 using mt tags(1)");
        tdSql.execute("create table tb2 using mt tags(2)");
        tdSql.execute("create table strm as select count(*), avg(c1), sum(c2), max(c1), min(c2),first(c1), last(c2) from mt interval(4s) sliding(2s)")
        #tdSql.execute("create table strm as select count(*), avg(c1), sum(c2), max(c1), min(c2), first(c1) from mt interval(4s) sliding(2s)")
        self.sleep(10)
        tdSql.execute("insert into tb2 values(now, 1, 1)");
        tdSql.execute("insert into tb1 values(now, 1, 1)");
        self.sleep(4)
        tdSql.query("select * from mt")
        tdSql.query("select * from strm")
        tdSql.execute("drop table tb1")

        tdLog.info("retrieve data from strm")
        for i in range(100):
            time.sleep(1)
            tdSql.query("select * from strm")
            if tdSql.queryRows > 0:
                break
        if tdSql.queryRows < 1 or tdSql.queryRows > 2:
            tdLog.exit("rows should be 1 or 2")

        tdSql.execute("drop table tb2")
        tdSql.execute("drop table mt")
        tdSql.execute("drop table strm")

    def tbase304(self):
        tdLog.debug("begin tbase304")
        # we cannot reset query cache in server side, as a workaround,
        # set super table name to mt304, need to change back to mt later
        tdSql.execute("create table mt304 (ts timestamp, c1 int) tags(t1 int, t2 int)")
        tdSql.execute("create table tb1 using mt304 tags(1, 1)")
        tdSql.execute("create table tb2 using mt304 tags(1, -1)")
        time.sleep(0.1)
        tdSql.execute("create table strm as select count(*), avg(c1) from mt304 where t2 >= 0 interval(4s) sliding(2s)")
        tdSql.execute("insert into tb1 values (now,1)")
        tdSql.execute("insert into tb2 values (now,2)")

        tdLog.info("retrieve data from strm")
        for i in range(100):
            time.sleep(1)
            tdSql.query("select * from strm")
            if tdSql.queryRows > 0:
                break
        if tdSql.queryRows < 1 or tdSql.queryRows > 2:
            tdLog.exit("rows should be 1 or 2")

        tdSql.checkData(0, 1, 1)
        tdSql.checkData(0, 2, 1.000000000)
        tdSql.execute("alter table mt304 drop tag t2")
        tdSql.execute("insert into tb2 values (now,2)")
        tdSql.execute("insert into tb1 values (now,1)")
        tdSql.query("select * from strm")
        tdSql.execute("alter table mt304 add tag t2 int")
        self.sleep(1)
        tdSql.query("select * from strm")

    def wildcardFilterOnTags(self):
        tdLog.debug("begin wildcardFilterOnTag")
        tdSql.prepare()
        tdSql.execute("create table stb (ts timestamp, c1 int, c2 binary(10)) tags(t1 binary(10))")
        tdSql.execute("create table tb1 using stb tags('a1')")
        tdSql.execute("create table tb2 using stb tags('b2')")
        tdSql.execute("create table tb3 using stb tags('a3')")
        tdSql.execute("create table strm as select count(*), avg(c1), first(c2) from stb where t1 like 'a%' interval(4s) sliding(2s)")
        tdSql.query("describe strm")
        tdSql.checkRows(4)

        self.sleep(1)
        tdSql.execute("insert into tb1 values (now, 0, 'tb1')")
        self.sleep(4)
        tdSql.execute("insert into tb2 values (now, 2, 'tb2')")
        self.sleep(4)
        tdSql.execute("insert into tb3 values (now, 0, 'tb3')")

        tdLog.info("retrieve data from strm")
        for i in range(60):
            time.sleep(1)
            tdSql.query("select * from strm")
            if tdSql.queryRows == 4:
                break

        tdSql.checkRows(4)
        tdSql.checkData(0, 2, 0.000000000)
        if tdSql.getData(0, 3) == 'tb2':
            tdLog.exit("unexpected value of data03")
        if tdSql.getData(1, 3) == 'tb2':
            tdLog.exit("unexpected value of data13")
        if tdSql.getData(2, 3) == 'tb2':
            tdLog.exit("unexpected value of data23")
        if tdSql.getData(3, 3) == 'tb2':
            tdLog.exit("unexpected value of data33")

        tdLog.info("add table tb4 to see if stream still works correctly")
        # The vnode client needs to refresh metadata cache to allow strm calculate tb4's data.
        # But the current refreshing frequency is every 10 min
        # commented out the case below to save running time
        tdSql.execute("create table tb4 using stb tags('a4')")
        tdSql.execute("insert into tb4 values(now, 4, 'tb4')")
        tdLog.info("retrieve data from strm order by ts desc")
        for i in range(60):
            time.sleep(1)
            tdSql.query("select * from strm order by ts desc")
            if tdSql.queryRows == 6:
                break
        tdSql.checkRows(6)
        tdSql.checkData(0, 2, 4)
        tdSql.checkData(0, 3, "tb4")

        tdLog.info("change tag values to see if stream still works correctly")
        tdSql.execute("alter table tb4 set tag t1='b4'")
        self.sleep(3)
        tdSql.execute("insert into tb1 values (now, 1, 'tb1_a1')")
        self.sleep(4)
        tdSql.execute("insert into tb4 values (now, -4, 'tb4_b4')")
        tdLog.info("retrieve data from strm order by ts desc")
        for i in range(100):
            time.sleep(1)
            tdSql.query("select * from strm order by ts desc")
            if tdSql.queryRows == 8:
                break
        tdSql.checkRows(8)
        tdSql.checkData(0, 2, 1)
        tdSql.checkData(0, 3, "tb1_a1")

    def datatypes(self):
        tdLog.debug("begin data types")
        tdSql.prepare()
        tdSql.execute("create table stb (ts timestamp, c1 int, c2 bigint, c3 float, c4 double, c5 binary(15), c6 nchar(15), c7 bool) tags(t1 int, t2 binary(15))")
        tdSql.execute("create table tb0 using stb tags(0, 'tb0')")
        tdSql.execute("create table tb1 using stb tags(1, 'tb1')")
        tdSql.execute("create table tb2 using stb tags(2, 'tb2')")
        tdSql.execute("create table tb3 using stb tags(3, 'tb3')")
        tdSql.execute("create table tb4 using stb tags(4, 'tb4')")

        tdSql.execute("create table strm0 as select count(ts), count(c1), max(c2), min(c4), first(c5), last(c6) from stb where ts < now + 30s interval(4s) sliding(2s)")
        #tdSql.execute("create table strm0 as select count(ts), count(c1), max(c2), min(c4), first(c5) from stb where ts < now + 30s interval(4s) sliding(2s)")
        self.sleep(1)
        tdSql.execute("insert into tb0 values (now, 0, 0, 0, 0, 'binary0', '涛思0', true) tb1 values (now, 1, 1, 1, 1, 'binary1', '涛思1', false) tb2 values (now, 2, 2, 2, 2, 'binary2', '涛思2', true) tb3 values (now, 3, 3, 3, 3, 'binary3', '涛思3', false) tb4 values (now, 4, 4, 4, 4, 'binary4', '涛思4', true) ")

        tdLog.info("retrieve data from strm0")
        for i in range(60):
            time.sleep(1)
            tdSql.query("select * from strm0 order by ts desc")
            if tdSql.queryRows == 2:
                break
        tdSql.checkRows(2)

        tdSql.execute("insert into tb0 values (now, 10, 10, 10, 10, 'binary0', '涛思0', true) tb1 values (now, 11, 11, 11, 11, 'binary1', '涛思1', false) tb2 values (now, 12, 12, 12, 12, 'binary2', '涛思2', true) tb3 values (now, 13, 13, 13, 13, 'binary3', '涛思3', false) tb4 values (now, 14, 14, 14, 14, 'binary4', '涛思4', true) ")
        tdLog.info("retrieve data from strm0 again")
        for i in range(60):
            time.sleep(1)
            tdSql.query("select * from strm0 order by ts desc")
            if tdSql.queryRows == 4:
                break
        tdSql.checkRows(4)

    def run(self):
        self.tbase300()
        self.tbase304()
B
Bomin Zhang 已提交
213
        self.wildcardFilterOnTags()
214 215 216 217 218 219 220 221 222
        self.datatypes()

    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)


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