queryStateWindow.py 6.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
###################################################################
#           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 taos
from util.log import *
from util.cases import *
from util.sql import *
import numpy as np


22 23
class TDTestCase:       
     
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor())
        self.rowNum = 100000
        self.ts = 1537146000000
        
    def run(self):
        tdSql.prepare()

        print("==============step1")
        tdSql.execute(
            "create table if not exists st (ts timestamp, t1 int, t2 timestamp, t3 bigint, t4 float, t5 double, t6 binary(10), t7 smallint, t8 tinyint, t9 bool, t10 nchar(10), t11 int unsigned, t12 bigint unsigned, t13 smallint unsigned, t14 tinyint unsigned ,t15 int) tags(dev nchar(50), tag2 binary(16))")
        tdSql.execute(
            'CREATE TABLE if not exists dev_001 using st tags("dev_01", "tag_01")')
        tdSql.execute(
            'CREATE TABLE if not exists dev_002 using st tags("dev_02", "tag_02")') 

        print("==============step2")

        tdSql.execute(
            "INSERT INTO dev_001 VALUES('2020-05-13 10:00:00.000', 1, '2020-05-13 10:00:00.000', 10, 3.1, 3.14, 'test', -10, -126, true, '测试', 15, 10, 65534, 254, 1)('2020-05-13 10:00:01.000', 1, '2020-05-13 10:00:01.000', 10, 3.1, 3.14, 'test', -10, -126, true, '测试', 15, 10, 65534, 253, 5)('2020-05-13 10:00:02.000', 10, '2020-05-13 10:00:00.000', 11, 3.1, 3.14, 'test', 10, -127, false, '测试', 15, 10, 65534, 253, 10)('2020-05-13 10:00:03.000', 1, '2020-05-13 10:00:00.000', 11, 3.1, 3.14, 'test', -10, -126, true, '测试', 14, 12, 65532, 254, 15)")

46 47 48 49 50 51
        
        for i in range(10):
            sql = "insert into dev_002 (ts,t1) values "            
            for j in range(int(self.rowNum / 1000)):
                sql += "(%d, %d,)" % (self.ts + i * 1000 + j, i * 1000 + j + 1)
                tdSql.execute(sql)            
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

        tdSql.query("select count(ts) from dev_001 state_window(t1)")
        tdSql.checkRows(3)
        tdSql.checkData(0, 0, 2)
        tdSql.query("select count(ts) from dev_001 state_window(t3)")
        tdSql.checkRows(2)
        tdSql.checkData(1, 0, 2)
        tdSql.query("select count(ts) from dev_001 state_window(t7)")
        tdSql.checkRows(3)
        tdSql.checkData(1, 0, 1)
        tdSql.query("select count(ts) from dev_001 state_window(t8)")
        tdSql.checkRows(3)
        tdSql.checkData(2, 0, 1)
        tdSql.query("select count(ts) from dev_001 state_window(t11)")
        tdSql.checkRows(2)
        tdSql.checkData(0, 0, 3)     
        tdSql.query("select count(ts) from dev_001 state_window(t12)")
        tdSql.checkRows(2)
        tdSql.checkData(1, 0, 1)     
        tdSql.query("select count(ts) from dev_001 state_window(t13)")
        tdSql.checkRows(2)
        tdSql.checkData(1, 0, 1)         
        tdSql.query("select count(ts) from dev_001 state_window(t14)")
        tdSql.checkRows(3)
        tdSql.checkData(1, 0, 2)
        tdSql.query("select count(ts) from dev_002 state_window(t1)")
78
        tdSql.checkRows(1000)
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
        
        # with all aggregate function
        tdSql.query("select count(*),sum(t1),avg(t1),twa(t1),stddev(t15),leastsquares(t15,1,1),first(t15),last(t15),spread(t15),percentile(t15,90),t9 from dev_001 state_window(t9);")
        tdSql.checkRows(3)
        tdSql.checkData(0, 0, 2)
        tdSql.checkData(1, 1, 10)
        tdSql.checkData(0, 2, 1)             
        # tdSql.checkData(0, 3, 1) 
        tdSql.checkData(0, 4, np.std([1,5])) 
        # tdSql.checkData(0, 5, 1) 
        tdSql.checkData(0, 6, 1)         
        tdSql.checkData(0, 7, 5)         
        tdSql.checkData(0, 8, 4)         
        tdSql.checkData(0, 9, 4.6)         
        tdSql.checkData(0, 10, 'True')         

        # with where
        tdSql.query("select avg(t15),t9 from dev_001 where  t9='true' state_window(t9);")
        tdSql.checkData(0, 0, 7)  
        tdSql.checkData(0, 1, 'True')  

        # error      
        tdSql.error("select count(*) from dev_001 state_window(t2)")
        tdSql.error("select count(*) from st state_window(t3)")
        tdSql.error("select count(*) from dev_001 state_window(t4)")
        tdSql.error("select count(*) from dev_001 state_window(t5)")
        tdSql.error("select count(*) from dev_001 state_window(t6)")
        tdSql.error("select count(*) from dev_001 state_window(t10)")
        tdSql.error("select count(*) from dev_001 state_window(tag2)")
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
        
        # TD-15164, TD-15226, TD-15227, TD-15186
        tdSql.execute("drop database if exists dd")
        tdSql.execute("create database dd keep 36500")
        tdSql.execute("use dd")
        tdSql.execute("create table table_1(ts timestamp , q_int int,q_bool bool)")
        tdSql.execute("insert into table_1 (ts , q_int,q_bool) values(1630000000000, 1,0)")
        tdSql.execute("insert into table_1 (ts , q_int,q_bool) values(1630000010000, 2,0)")
        tdSql.execute("insert into table_1 (ts , q_int,q_bool) values(1630000020000, 3,0)")
        tdSql.execute("insert into table_1 (ts , q_int,q_bool) values(1630000100000, 3,0)")
        tdSql.execute("insert into table_1 (ts , q_int,q_bool) values(1630000110000, 2,0)")
        tdSql.execute("insert into table_1 (ts , q_int,q_bool) values(1630000120000, 1,0)")
        
        tdSql.query("select STDDEV(q_int) from table_1 STATE_WINDOW(q_bool) order by ts ")
        tdSql.checkData(0, 0, 0.8164965)
        
        tdSql.query("select STDDEV(q_int) from table_1 STATE_WINDOW(q_bool) order by ts desc")
        tdSql.checkData(0, 0, 0.8164965)
        
        tdSql.query("select MAX(q_int) from table_1 STATE_WINDOW(q_bool) order by ts;")
        tdSql.checkData(0, 0, 3)
        
        tdSql.query("select MAX(q_int) from table_1 STATE_WINDOW(q_bool) order by ts desc;")
        tdSql.checkData(0, 0, 3)
        
        tdSql.query("select MAX(q_int) from table_1 where q_bool in ( true , false) STATE_WINDOW(q_bool) order by ts desc")
        tdSql.checkData(0, 0, 3)
        
        tdSql.query("select TOP(q_int,3) from table_1 STATE_WINDOW(q_bool) order by ts")
        tdSql.checkData(0, 1, 2)
        tdSql.checkData(1, 1, 3)
        tdSql.checkData(2, 1, 3)
        
        tdSql.query("select TOP(q_int,3) from table_1 STATE_WINDOW(q_bool) order by ts desc")
        tdSql.checkData(0, 1, 3)
        tdSql.checkData(1, 1, 3)
        tdSql.checkData(2, 1, 2)
145 146 147 148 149 150 151 152

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


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