queryInterval.py 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
###################################################################
#           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 tdLog
from util.cases import tdCases
from util.sql import tdSql
19
from util.dnodes import tdDnodes
20 21 22 23 24 25 26


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

27
        self.ts = 1593548685000                  
28 29 30 31 32

    def run(self):
        tdSql.prepare()

        tdSql.execute("create table st (ts timestamp, voltage int) tags (loc nchar(30))")
R
root 已提交
33 34 35
        tdSql.execute("insert into t0 using st tags('beijing') values(%d, 220) (%d, 221) (%d, 225) (%d, 228) (%d, 222)" 
                        % (self.ts, self.ts + 1000000000, self.ts + 2000000000, self.ts + 3000000000, self.ts + 6000000000))
        tdSql.execute("insert into t1 using st tags('shanghai') values(%d, 220) (%d, 221) (%d, 225) (%d, 228) (%d, 222)" 
36 37
                        % (self.ts, self.ts + 2000000000, self.ts + 4000000000, self.ts + 5000000000, self.ts + 7000000000))             
                
38 39

        tdSql.query("select avg(voltage) from st interval(1n)")
40 41 42 43 44 45 46
        tdSql.checkRows(3)        
        tdSql.checkData(0, 0, "2020-07-01 00:00:00")
        tdSql.checkData(0, 1, 221.4)        
        tdSql.checkData(1, 0, "2020-08-01 00:00:00")
        tdSql.checkData(1, 1, 227.0)        
        tdSql.checkData(2, 0, "2020-09-01 00:00:00")
        tdSql.checkData(2, 1, 222.0)
47 48
        
        tdSql.query("select avg(voltage) from st interval(1n, 15d)")
R
root 已提交
49
        tdSql.checkRows(4)
50
        tdSql.checkData(0, 0, "2020-06-16 00:00:00")
R
root 已提交
51
        tdSql.checkData(0, 1, 220.333333)
52
        tdSql.checkData(1, 0, "2020-07-16 00:00:00")
R
root 已提交
53
        tdSql.checkData(1, 1, 224.666666)
54
        tdSql.checkData(2, 0, "2020-08-16 00:00:00")
R
root 已提交
55
        tdSql.checkData(2, 1, 225.0)
56
        tdSql.checkData(3, 0, "2020-09-16 00:00:00")
R
root 已提交
57
        tdSql.checkData(3, 1, 222.0)
58 59

        tdSql.query("select avg(voltage) from st interval(1n, 15d) group by loc")
R
root 已提交
60
        tdSql.checkRows(7)
61
        tdSql.checkData(0, 0, "2020-06-16 00:00:00")
R
root 已提交
62
        tdSql.checkData(0, 1, 220.5)
63
        tdSql.checkData(1, 0, "2020-07-16 00:00:00")
R
root 已提交
64
        tdSql.checkData(1, 1, 226.5)
65
        tdSql.checkData(2, 0, "2020-08-16 00:00:00")
R
root 已提交
66
        tdSql.checkData(2, 1, 222.0)
67
        tdSql.checkData(3, 0, "2020-06-16 00:00:00")
R
root 已提交
68
        tdSql.checkData(3, 1, 220.0)
69
        tdSql.checkData(4, 0, "2020-07-16 00:00:00")
R
root 已提交
70
        tdSql.checkData(4, 1, 221.0)
71
        tdSql.checkData(5, 0, "2020-08-16 00:00:00")
R
root 已提交
72
        tdSql.checkData(5, 1, 226.5)
73
        tdSql.checkData(6, 0, "2020-09-16 00:00:00")
R
root 已提交
74
        tdSql.checkData(6, 1, 222.0)
75

76 77 78 79 80 81 82 83 84 85 86
        # test case for https://jira.taosdata.com:18080/browse/TD-2298
        tdSql.execute("create database test keep 36500")
        tdSql.execute("use test")
        tdSql.execute("create table t (ts timestamp, voltage int)")
        for i in range(10000):
            tdSql.execute("insert into t values(%d, 0)" % (1000000 + i * 6000))
        
        tdDnodes.stop(1)
        tdDnodes.start(1)
        tdSql.query("select last(*) from t interval(1s)")
        tdSql.checkRows(10000)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

        # test case for https://jira.taosdata.com:18080/browse/TD-2601
        newTs = 1601481600000

        tdSql.execute("create database test2")
        tdSql.execute("use test2")
        tdSql.execute("create table t (ts timestamp, voltage int)")
        for i in range(100):
            tdSql.execute("insert into t values(%d, %d)" % (newTs + i * 10000000, i))
        
        tdSql.query("select sum(voltage) from t where ts >='2020-10-01 00:00:00' and ts <='2020-12-01 00:00:00' interval(1n) fill(NULL)")
        tdSql.checkRows(3)
        tdSql.checkData(0, 1, 4950)
        tdSql.checkData(1, 1, None)
        tdSql.checkData(2, 1, None)

103 104
        

105 106 107 108 109 110 111
    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)


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