distinctOneColTb.py 14.0 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
###################################################################
#           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 *


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

    def tb_all_query(self, num, sql="tb_all", where=""):
        tdSql.query(
28
            f"select distinct ts2 from {sql} {where}")
29 30
        tdSql.checkRows(num)
        tdSql.query(
31
            f"select distinct cint from {sql} {where}")
32 33
        tdSql.checkRows(num)
        tdSql.query(
34
            f"select distinct cbigint from {sql} {where}")
35 36
        tdSql.checkRows(num)
        tdSql.query(
37
            f"select distinct csmallint from {sql} {where}")
38 39
        tdSql.checkRows(num)
        tdSql.query(
40
            f"select distinct ctinyint from {sql} {where}")
41 42
        tdSql.checkRows(num)
        tdSql.query(
43
            f"select distinct cfloat from {sql} {where}")
44 45
        tdSql.checkRows(num)
        tdSql.query(
46
            f"select distinct cdouble from {sql} {where}")
47 48
        tdSql.checkRows(num)
        tdSql.query(
49
            f"select distinct cbool from {sql} {where}")
C
cpwu 已提交
50
        if num < 3:
51 52
            tdSql.checkRows(num)
        else:
C
cpwu 已提交
53
            tdSql.checkRows(3)
54
        tdSql.query(
55
            f"select distinct cbinary from {sql} {where}")
56 57
        tdSql.checkRows(num)
        tdSql.query(
58
            f"select distinct cnchar from {sql} {where}")
59 60 61
        tdSql.checkRows(num)

        tdSql.query(
62
            f"select distinct ts2 as a from {sql} {where}")
63 64
        tdSql.checkRows(num)
        tdSql.query(
65
            f"select distinct cint as a from {sql} {where}")
66 67
        tdSql.checkRows(num)
        tdSql.query(
68
            f"select distinct cbigint as a from {sql} {where}")
69 70
        tdSql.checkRows(num)
        tdSql.query(
71
            f"select distinct csmallint as a from {sql} {where}")
72 73
        tdSql.checkRows(num)
        tdSql.query(
74
            f"select distinct ctinyint as a from {sql} {where}")
75 76
        tdSql.checkRows(num)
        tdSql.query(
77
            f"select distinct cfloat as a from {sql} {where}")
78 79
        tdSql.checkRows(num)
        tdSql.query(
80
            f"select distinct cdouble  as a from {sql} {where}")
81 82
        tdSql.checkRows(num)
        tdSql.query(
83
            f"select distinct cbool as a from {sql} {where}")
C
cpwu 已提交
84
        if num < 3:
85 86
            tdSql.checkRows(num)
        else:
C
cpwu 已提交
87
            tdSql.checkRows(3)
88
        tdSql.query(
89
            f"select distinct cbinary as a from {sql} {where}")
90 91
        tdSql.checkRows(num)
        tdSql.query(
92
            f"select distinct cnchar as a from {sql} {where}")
93 94 95 96 97
        tdSql.checkRows(num)


    def tb_all_query_sub(self, num, sql="tb_all", where="",colName = "c1"):
        tdSql.query(
98
            f"select distinct {colName} from {sql} {where}")
99 100 101 102
        tdSql.checkRows(num)
    
    def errorCheck(self, sql='tb_all'):
        tdSql.error(f"select distinct from {sql}")
103
        tdSql.error(f"distinct ts2 from {sql}")     
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
        tdSql.error(f"distinct c1 from")
        tdSql.error(f"select distinct ts2, avg(cint) from {sql}")

        ## the following line is going to core dump 
        #tdSql.error(f"select avg(cint), distinct ts2  from {sql}")

        tdSql.error(f"select distinct ts2 from {sql} group by cint")
        tdSql.error(f"select distinct ts2 from {sql} interval(1s)")
        tdSql.error(f"select distinct ts2 from {sql} slimit 1 soffset 1")
        tdSql.error(f"select distinct ts2 from {sql} slimit 1")

        ##order by is not supported but not being prohibited
        #tdSql.error(f"select distinct ts2 from {sql} order by desc")
        #tdSql.error(f"select distinct ts2 from {sql} order by asc")

        ##distinct should not use on first ts, but it can be applied
        #tdSql.error(f"select distinct ts from {sql}")

        ##distinct should not be used in inner query, but error did not occur
        # tdSql.error(f"select distinct ts2 from (select distinct ts2 from {sql})")


    def query_all_tb(self, whereNum=0,inNum=0,maxNum=0,tbName="tb_all"):
        self.tb_all_query(maxNum,sql=tbName)

        self.tb_all_query(num=whereNum,where="where ts2 = '2021-08-06 18:01:50.550'",sql=tbName)
        self.tb_all_query(num=whereNum,where="where cint = 1073741820",sql=tbName)
        self.tb_all_query(num=whereNum,where="where cbigint = 1125899906842620",sql=tbName)
        self.tb_all_query(num=whereNum,where="where csmallint = 150",sql=tbName)
        self.tb_all_query(num=whereNum,where="where ctinyint = 10",sql=tbName)
        self.tb_all_query(num=whereNum,where="where cfloat = 0.10",sql=tbName)
        self.tb_all_query(num=whereNum,where="where cdouble = 0.130",sql=tbName)
        self.tb_all_query(num=whereNum,where="where cbool = true",sql=tbName)
        self.tb_all_query(num=whereNum,where="where cbinary = 'adc1'",sql=tbName)
        self.tb_all_query(num=whereNum,where="where cnchar = '双精度浮'",sql=tbName)
        self.tb_all_query(num=inNum,where="where ts2 in ( '2021-08-06 18:01:50.550' , '2021-08-06 18:01:50.551' )",sql=tbName)
        self.tb_all_query(num=inNum,where="where cint in (1073741820,1073741821)",sql=tbName)
        self.tb_all_query(num=inNum,where="where cbigint in (1125899906842620,1125899906842621)",sql=tbName)
        self.tb_all_query(num=inNum,where="where csmallint in (150,151)",sql=tbName)
        self.tb_all_query(num=inNum,where="where ctinyint in (10,11)",sql=tbName)
        self.tb_all_query(num=inNum,where="where cfloat in (0.10,0.11)",sql=tbName)
        self.tb_all_query(num=inNum,where="where cdouble in (0.130,0.131)",sql=tbName)
        self.tb_all_query(num=inNum,where="where cbinary in ('adc0','adc1')",sql=tbName)
        self.tb_all_query(num=inNum,where="where cnchar in ('双','双精度浮')",sql=tbName)
        self.tb_all_query(num=inNum,where="where cbool in (0, 1)",sql=tbName)
        self.tb_all_query(num=whereNum,where="where cnchar like '双__浮'",sql=tbName)
C
cpwu 已提交
150 151 152 153 154
        # self.tb_all_query(num=maxNum,where="where cnchar like '双%'",sql=tbName)
        # self.tb_all_query(num=maxNum,where="where cbinary like 'adc_'",sql=tbName)
        # self.tb_all_query(num=maxNum,where="where cbinary like 'a%'",sql=tbName)
        # self.tb_all_query(num=whereNum,where="limit 1",sql=tbName)
        # self.tb_all_query(num=whereNum,where="limit 1 offset 1",sql=tbName)
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

        #subquery
        self.tb_all_query(num=maxNum,sql=f'(select * from {tbName})')

        #subquery with where in outer query
        self.tb_all_query(num=whereNum,where="where ts2 = '2021-08-06 18:01:50.550'",sql=f'(select * from {tbName})')
        self.tb_all_query(num=whereNum,where="where cint = 1073741820",sql=f'(select * from {tbName})')
        self.tb_all_query(num=whereNum,where="where cbigint = 1125899906842620",sql=f'(select * from {tbName})')
        self.tb_all_query(num=whereNum,where="where csmallint = 150",sql=f'(select * from {tbName})')
        self.tb_all_query(num=whereNum,where="where ctinyint = 10",sql=f'(select * from {tbName})')
        self.tb_all_query(num=whereNum,where="where cfloat = 0.10",sql=f'(select * from {tbName})')
        self.tb_all_query(num=whereNum,where="where cdouble = 0.130",sql=f'(select * from {tbName})')
        self.tb_all_query(num=whereNum,where="where cbool = true",sql=f'(select * from {tbName})')
        self.tb_all_query(num=whereNum,where="where cbinary = 'adc1'",sql=f'(select * from {tbName})')
        self.tb_all_query(num=whereNum,where="where cnchar = '双精度浮'",sql=f'(select * from {tbName})')
        self.tb_all_query(num=inNum,where="where ts2 in ( '2021-08-06 18:01:50.550' , '2021-08-06 18:01:50.551' )",sql=f'(select * from {tbName})')
        self.tb_all_query(num=inNum,where="where cint in (1073741820,1073741821)",sql=f'(select * from {tbName})')
        self.tb_all_query(num=inNum,where="where cbigint in (1125899906842620,1125899906842621)",sql=f'(select * from {tbName})')
        self.tb_all_query(num=inNum,where="where csmallint in (150,151)",sql=f'(select * from {tbName})')
        self.tb_all_query(num=inNum,where="where ctinyint in (10,11)",sql=f'(select * from {tbName})')
        self.tb_all_query(num=inNum,where="where cfloat in (0.10,0.11)",sql=f'(select * from {tbName})')
        self.tb_all_query(num=inNum,where="where cdouble in (0.130,0.131)",sql=f'(select * from {tbName})')
        self.tb_all_query(num=inNum,where="where cbinary in ('adc0','adc1')",sql=f'(select * from {tbName})')
        self.tb_all_query(num=inNum,where="where cnchar in ('双','双精度浮')",sql=f'(select * from {tbName})')
        self.tb_all_query(num=inNum,where="where cbool in (0, 1)",sql=f'(select * from {tbName})')
        self.tb_all_query(num=whereNum,where="where cnchar like '双__浮'",sql=f'(select * from {tbName})')
C
cpwu 已提交
181 182 183 184 185
        # self.tb_all_query(num=maxNum,where="where cnchar like '双%'",sql=f'(select * from {tbName})')
        # self.tb_all_query(num=maxNum,where="where cbinary like 'adc_'",sql=f'(select * from {tbName})')
        # self.tb_all_query(num=maxNum,where="where cbinary like 'a%'",sql=f'(select * from {tbName})')
        # self.tb_all_query(num=whereNum,where="limit 1",sql=f'(select * from {tbName})')
        # self.tb_all_query(num=whereNum,where="limit 1 offset 1",sql=f'(select * from {tbName})')
186 187 188 189 190 191 192 193 194 195
        #table query with inner query has error
        tdSql.error('select distinct ts2 from (select )')
        #table query with error option
        self.errorCheck()


    def run(self):
        tdSql.prepare()

        tdLog.notice(
196
            "==============phase1 distinct col1 with no values==========")
197
        tdSql.execute("create stable if not exists stb_all (ts timestamp, ts2 timestamp, cint int, cbigint bigint, csmallint smallint, ctinyint tinyint,cfloat float, cdouble double, cbool bool, cbinary binary(32), cnchar nchar(32)) tags(tint int)")    
C
cpwu 已提交
198
        tdSql.execute("create table if not exists tb_all using stb_all tags(1)")
199 200 201 202 203 204
        self.query_all_tb()

        tdLog.notice(
            "==============phase1 finished ==========\n\n\n")

        tdLog.notice(
205
            "==============phase2 distinct col1 all values are null==========")
206 207 208 209 210 211 212 213
        tdSql.execute(
            "insert into tb_all values(now,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)")
        tdSql.execute(
            "insert into tb_all values(now,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)")
        tdSql.execute(
            "insert into tb_all values(now,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)")

        # normal query
C
cpwu 已提交
214
        self.query_all_tb(maxNum=1, inNum=0, whereNum=0)
215

C
cpwu 已提交
216
        tdLog.notice("==============phase2 finished ==========\n\n\n")
217

C
cpwu 已提交
218 219 220
        tdSql.prepare()
        tdSql.execute("create stable if not exists stb_all (ts timestamp, ts2 timestamp, cint int, cbigint bigint, csmallint smallint, ctinyint tinyint,cfloat float, cdouble double, cbool bool, cbinary binary(32), cnchar nchar(32)) tags(tint int)")
        tdSql.execute("create table if not exists tb_all using stb_all tags(1)")
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

        tdLog.notice(
            "==============phase3 distinct with distinct values ==========\n\n\n")
        tdSql.execute(
            "insert into tb_all values(now+1s,'2021-08-06 18:01:50.550',1073741820,1125899906842620,150,10,0.10,0.130, true,'adc0','双')")
        tdSql.execute(
            "insert into tb_all values(now+2s,'2021-08-06 18:01:50.551',1073741821,1125899906842621,151,11,0.11,0.131,false,'adc1','双精度浮')")
        tdSql.execute(
            "insert into tb_all values(now+3s,'2021-08-06 18:01:50.552',1073741822,1125899906842622,152,12,0.12,0.132,NULL,'adc2','双精度')")
        tdSql.execute(
            "insert into tb_all values(now+4s,'2021-08-06 18:01:50.553',1073741823,1125899906842623,153,13,0.13,0.133,NULL,'adc3','双精')")
        tdSql.execute(
            "insert into tb_all values(now+5s,'2021-08-06 18:01:50.554',1073741824,1125899906842624,154,14,0.14,0.134,NULL,'adc4','双精度浮点型')")

        # normal query
        self.query_all_tb(maxNum=5,inNum=2,whereNum=1)

C
cpwu 已提交
238 239 240 241 242
        tdLog.notice("==============phase3 finishes ==========\n\n\n")

        tdSql.prepare()
        tdSql.execute("create stable if not exists stb_all (ts timestamp, ts2 timestamp, cint int, cbigint bigint, csmallint smallint, ctinyint tinyint,cfloat float, cdouble double, cbool bool, cbinary binary(32), cnchar nchar(32)) tags(tint int)")
        tdSql.execute("create table if not exists tb_all using stb_all tags(1)")
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

        tdLog.notice(
            "==============phase4 distinct with some values the same values ==========\n\n\n")
        tdSql.execute(
            "insert into tb_all values(now+10s,'2021-08-06 18:01:50.550',1073741820,1125899906842620,150,10,0.10,0.130, true,'adc0','双')")
        tdSql.execute(
            "insert into tb_all values(now+20s,'2021-08-06 18:01:50.551',1073741821,1125899906842621,151,11,0.11,0.131,false,'adc1','双精度浮')")
        tdSql.execute(
            "insert into tb_all values(now+30s,'2021-08-06 18:01:50.552',1073741822,1125899906842622,152,12,0.12,0.132,NULL,'adc2','双精度')")
        tdSql.execute(
            "insert into tb_all values(now+40s,'2021-08-06 18:01:50.553',1073741823,1125899906842623,153,13,0.13,0.133,NULL,'adc3','双精')")
        tdSql.execute(
            "insert into tb_all values(now+50s,'2021-08-06 18:01:50.554',1073741824,1125899906842624,154,14,0.14,0.134,NULL,'adc4','双精度浮点型')")
        # normal query
        self.query_all_tb(maxNum=5,inNum=2,whereNum=1)

        tdLog.notice(
            "==============phase4 finishes ==========\n\n\n")


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


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