function_stddev.py 7.3 KB
Newer Older
P
Ping Xiao 已提交
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
###################################################################
#           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


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

        self.rowNum = 10
        self.ts = 1537146000000
29 30
        self.stb_prefix = 's'
        self.subtb_prefix = 't'
P
Ping Xiao 已提交
31 32 33 34 35 36 37 38
        
    def run(self):
        tdSql.prepare()

        intData = []        
        floatData = []

        tdSql.execute('''create table test(ts timestamp, col1 tinyint, col2 smallint, col3 int, col4 bigint, col5 float, col6 double, 
L
modify  
liuyq-617 已提交
39
                    col7 bool, col8 binary(20), col9 nchar(20), col11 tinyint unsigned, col12 smallint unsigned, col13 int unsigned, col14 bigint unsigned) tags(loc nchar(20))''')
P
Ping Xiao 已提交
40 41
        tdSql.execute("create table test1 using test tags('beijing')")
        for i in range(self.rowNum):
L
modify  
liuyq-617 已提交
42 43
            tdSql.execute("insert into test1 values(%d, %d, %d, %d, %d, %f, %f, %d, 'taosdata%d', '涛思数据%d', %d, %d, %d, %d)" 
                        % (self.ts + i, i + 1, i + 1, i + 1, i + 1, i + 0.1, i + 0.1, i % 2, i + 1, i + 1, i + 1, i + 1, i + 1, i + 1))
P
Ping Xiao 已提交
44 45 46 47 48
            intData.append(i + 1)            
            floatData.append(i + 0.1)                        

        # stddev verifacation 
        tdSql.error("select stddev(ts) from test1")
H
Haojun Liao 已提交
49 50 51 52 53 54 55 56

        # stddev support super table now
        # tdSql.error("select stddev(col1) from test")
        # tdSql.error("select stddev(col2) from test")
        # tdSql.error("select stddev(col3) from test")
        # tdSql.error("select stddev(col4) from test")
        # tdSql.error("select stddev(col5) from test")
        # tdSql.error("select stddev(col6) from test")
P
Ping Xiao 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        tdSql.error("select stddev(col7) from test1")
        tdSql.error("select stddev(col8) from test1")
        tdSql.error("select stddev(col9) from test1")

        tdSql.query("select stddev(col1) from test1")
        tdSql.checkData(0, 0, np.std(intData))

        tdSql.query("select stddev(col2) from test1")
        tdSql.checkData(0, 0, np.std(intData))

        tdSql.query("select stddev(col3) from test1")
        tdSql.checkData(0, 0, np.std(intData))

        tdSql.query("select stddev(col4) from test1")
        tdSql.checkData(0, 0, np.std(intData))

73 74 75 76 77 78 79 80 81 82 83 84
        tdSql.query("select stddev(col11) from test1")
        tdSql.checkData(0, 0, np.std(intData))

        tdSql.query("select stddev(col12) from test1")
        tdSql.checkData(0, 0, np.std(intData))

        tdSql.query("select stddev(col13) from test1")
        tdSql.checkData(0, 0, np.std(intData))

        tdSql.query("select stddev(col14) from test1")
        tdSql.checkData(0, 0, np.std(intData))

P
Ping Xiao 已提交
85 86 87 88 89
        tdSql.query("select stddev(col5) from test1")
        tdSql.checkData(0, 0, np.std(floatData))

        tdSql.query("select stddev(col6) from test1")
        tdSql.checkData(0, 0, np.std(floatData))
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

        #add for td-3276
        sql="create table s (ts timestamp, c1 int, c2 float, c3 bigint, c4 smallint, c5 tinyint, c6 double, c7 bool,c8 binary(20),c9 nchar(20),c11 int unsigned,c12 smallint unsigned,c13 tinyint unsigned,c14 bigint unsigned) \
            tags(t1 int, t2 float, t3 bigint, t4 smallint, t5 tinyint, t6 double, t7 bool,t8 binary(20),t9 nchar(20), t10 int unsigned , t11 smallint unsigned , t12 tinyint unsigned , t13 bigint unsigned)"
        tdSql.execute(sql)
        for j in range(2):
            if j % 2 == 0:
                sql = "create table %s using %s tags(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)" % \
                        (self.subtb_prefix+str(j)+'_'+str(j),self.stb_prefix)
            else:
                sql = "create table %s using %s tags(%d,%d,%d,%d,%d,%d,%d,'%s','%s',%d,%d,%d,%d)" % \
                        (self.subtb_prefix+str(j)+'_'+str(j),self.stb_prefix,j,j/2.0,j%41,j%51,j%53,j*1.0,j%2,'taos'+str(j),'涛思'+str(j), j%43, j%23 , j%17 , j%3167)
            tdSql.execute(sql)
            for i in range(10):
                if i % 5 == 0 :
                    ret = tdSql.execute(
                    "insert into %s values (%d , NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)" %
                    (self.subtb_prefix+str(j)+'_'+str(j), self.ts+i))
                else:
                    ret = tdSql.execute(
                        "insert into %s values (%d , %d,%d,%d,%d,%d,%d,%d,'%s','%s',%d,%d,%d,%d)" %
                        (self.subtb_prefix+str(j)+'_'+str(j), self.ts+i, i%100, i/2.0, i%41, i%51, i%53, i*1.0, i%2,'taos'+str(i),'涛思'+str(i), i%43, i%23 , i%17 , i%3167))
        
        for i in range(13):
            tdSql.query('select stddev(c4) from s group by t%s' % str(i+1) )

116 117 118 119 120
        #add for td-3223
        for i in range(13):
            if i == 1 or i == 5 or i == 6 or i == 7 or i == 9 or i == 8 :continue
            tdSql.query('select stddev(c%d),stddev(c%d) from s group by c%d' %( i+1 , i+1 , i+1  ) )
        
L
liuyq-617 已提交
121 122 123 124 125
        #add for TD-3318
        tdSql.execute('create table t1(ts timestamp, k int, b binary(12));')
        tdSql.execute("insert into t1 values(now, 1, 'abc');")
        tdLog.info("select stddev(k) from t1  where b <> 'abc' interval(1s);")
        tdSql.query("select stddev(k) from t1  where b <> 'abc' interval(1s);")
126 127 128 129

        tdSql.execute("create table stdtable(ts timestamp, col1 int) tags(loc nchar(64))")
        tdSql.execute("create table std1 using stdtable tags('beijing')")
        tdSql.execute("create table std2 using stdtable tags('shanghai')")
wmmhello's avatar
wmmhello 已提交
130 131 132 133 134 135 136
        tdSql.execute("create table std3 using stdtable tags('河南')")
        tdSql.execute("insert into std1 values(now + 1s, 1)")
        tdSql.execute("insert into std1 values(now + 2s, 2);")
        tdSql.execute("insert into std2 values(now + 3s, 1);")
        tdSql.execute("insert into std2 values(now + 4s, 2);")
        tdSql.execute("insert into std3 values(now + 5s, 4);")
        tdSql.execute("insert into std3 values(now + 6s, 8);")
137
        tdSql.query("select stddev(col1) from stdtable group by loc;")
wmmhello's avatar
wmmhello 已提交
138
        tdSql.checkData(0, 0, 2.0)
139
        tdSql.checkData(1, 0, 0.5)
wmmhello's avatar
wmmhello 已提交
140
        tdSql.checkData(2, 0, 0.5)
141 142 143 144

        tdSql.execute("create table stdtableint(ts timestamp, col1 int) tags(num int)")
        tdSql.execute("create table stdint1 using stdtableint tags(1)")
        tdSql.execute("create table stdint2 using stdtableint tags(2)")
wmmhello's avatar
wmmhello 已提交
145 146 147 148
        tdSql.execute("insert into stdint1 values(now + 1s, 1)")
        tdSql.execute("insert into stdint1 values(now + 2s, 2);")
        tdSql.execute("insert into stdint2 values(now + 3s, 1);")
        tdSql.execute("insert into stdint2 values(now + 4s, 2);")
149 150 151
        tdSql.query("select stddev(col1) from stdtableint group by num")
        tdSql.checkData(0, 0, 0.5)
        tdSql.checkData(1, 0, 0.5)
152

P
Ping Xiao 已提交
153 154 155 156 157 158
    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)

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