queryNormal.py 6.1 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 *
from util.cases import *
from util.sql import *
19
from util.dnodes import *
L
liuyq-617 已提交
20
import platform
21 22 23 24 25 26 27 28 29 30 31

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

    def run(self):
        tdSql.prepare()

        print("==============step1")

32 33 34 35 36 37 38
        tdSql.execute(
            "create table stb1 (ts timestamp, c1 int, c2 float) tags(t1 int, t2 binary(10), t3 nchar(10))")
        tdSql.execute(
            "insert into tb1 using stb1 tags(1,'tb1', '表1') values ('2020-04-18 15:00:00.000', 1, 0.1), ('2020-04-18 15:00:01.000', 2, 0.1)")
        tdSql.execute(
            "insert into tb2 using stb1 tags(2,'tb2', '表2') values ('2020-04-18 15:00:02.000', 3, 2.1), ('2020-04-18 15:00:03.000', 4, 2.2)")

39 40
        tdSql.error("select * from tb 1")
        
P
Ping Xiao 已提交
41 42
        tdSql.query("select * from tb1 a, tb2 b where a.ts = b.ts")
        tdSql.checkRows(0)
43 44

        # join 3 tables -- bug exists
45
        tdSql.error("select stb_t.ts, stb_t.dscrption, stb_t.temperature, stb_p.id, stb_p.dscrption, stb_p.pressure,stb_v.velocity from stb_p, stb_t, stb_v where stb_p.ts=stb_t.ts and stb_p.ts=stb_v.ts and stb_p.id = stb_t.id")
46

47 48
        tdSql.error("select * from stb1 whern c1 > 'test' limit 100")

P
Ping Xiao 已提交
49 50 51 52 53
        # query show stable
        tdSql.query("show stables")
        tdSql.checkRows(1)

        # query show tables
54
        tdSql.query("show tables")
P
Ping Xiao 已提交
55 56
        tdSql.checkRows(2)

57 58 59 60
        # query count
        tdSql.query("select count(*) from stb1")
        tdSql.checkData(0, 0, 4)

61
        # query first
62 63
        tdSql.query("select first(*) from stb1")
        tdSql.checkData(0, 1, 1)
64 65

        # query last
66 67 68
        tdSql.query("select last(*) from stb1")
        tdSql.checkData(0, 1, 4)

69
        # query last_row
P
Ping Xiao 已提交
70 71 72
        tdSql.query("select last_row(*) from stb1")
        tdSql.checkData(0, 1, 4)

73 74 75 76 77
        # query as
        tdSql.query("select t2 as number from stb1")
        tdSql.checkRows(2)

        # query first ... as
78
        tdSql.error("select first(*) as begin from stb1")        
79

80
        # query last ... as
81
        tdSql.error("select last(*) as end from stb1")
82

P
Ping Xiao 已提交
83
        # query last_row ... as
84
        tdSql.error("select last_row(*) as end from stb1")        
P
Ping Xiao 已提交
85

86 87 88 89 90 91 92 93 94 95 96 97
        # query group .. by
        tdSql.query("select sum(c1), t2 from stb1 group by t2")
        tdSql.checkRows(2)

        # query ... limit
        tdSql.query("select * from stb1 limit 2")
        tdSql.checkRows(2)

        # query ... limit offset
        tdSql.query("select * from stb1 limit 2 offset 3")
        tdSql.checkRows(1)

H
Haojun Liao 已提交
98 99 100
        # query ... alias for table
        tdSql.query("select t.ts from tb1 t")
        tdSql.checkRows(2)
P
Ping Xiao 已提交
101 102 103 104 105 106 107

        # query ... tbname
        tdSql.query("select tbname from stb1")
        tdSql.checkRows(2)

        # query ... tbname count  ---- bug
        tdSql.query("select count(tbname) from stb1")
108
        tdSql.checkData(0, 0, 2)
P
Ping Xiao 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

        # query ... select database ---- bug
        tdSql.query("SELECT database()")
        tdSql.checkRows(1)

        # query ... select client_version ---- bug
        tdSql.query("SELECT client_version()")
        tdSql.checkRows(1)

        # query ... select server_version ---- bug
        tdSql.query("SELECT server_version()")
        tdSql.checkRows(1)

        # query ... select server_status ---- bug
        tdSql.query("SELECT server_status()")
        tdSql.checkRows(1)

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
         # https://jira.taosdata.com:18080/browse/TD-3800
        tdSql.execute("create table m1(ts timestamp, k int) tags(a int)")
        tdSql.execute("create table tm0 using m1 tags(1)")
        tdSql.execute("create table tm1 using m1 tags(2)")
        tdSql.execute("insert into tm0 values('2020-3-1 1:1:1', 112)")
        tdSql.execute("insert into tm1 values('2020-1-1 1:1:1', 1)('2020-3-1 0:1:1', 421)")

        tdSql.query("select last(*) from m1 group by tbname")
        tdSql.checkData(0, 0, "2020-03-01 01:01:01")
        tdSql.checkData(0, 1, 112)
        tdSql.checkData(0, 2, "tm0")
        tdSql.checkData(1, 0, "2020-03-01 00:01:01")
        tdSql.checkData(1, 1, 421)
        tdSql.checkData(1, 2, "tm1")
        
L
liuyq-617 已提交
141 142 143
        if platform.system() == "Linux": 
            tdDnodes.stop(1)
            tdDnodes.start(1)
144 145 146 147 148 149 150 151

        tdSql.query("select last(*) from m1 group by tbname")
        tdSql.checkData(0, 0, "2020-03-01 01:01:01")
        tdSql.checkData(0, 1, 112)
        tdSql.checkData(0, 2, "tm0")
        tdSql.checkData(1, 0, "2020-03-01 00:01:01")
        tdSql.checkData(1, 1, 421)
        tdSql.checkData(1, 2, "tm1")
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
        
        # TD-12980
        if platform.system() == "Linux": 	
            types = ["tinyint unsigned", "smallint unsigned", "int unsigned", "bigint unsigned"]
            ts = 1640000000000
            
            for type in types:
                tdSql.execute("drop table if exists csvtest")
                tdSql.execute("create table csvtest(ts timestamp, c1 %s)" % type)
                for i in range(10):
                    tdSql.execute("insert into csvtest values(%d, %d)" % (ts + i, i))
                
                os.system("taos -s 'select c1 from db.csvtest >> a.csv'")
                
                tdSql.query("select c1 from csvtest")
                for i in range(10):
                    r = os.popen("sed -n %dp a.csv" % (i + 2))
                    data = r.read()              
                    tdSql.checkData(i, 0, int(data))
                
                os.system("rm -rf a.csv")
173

174 175 176 177 178 179 180
    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)


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