distinctOneColTb.py 14.3 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 78 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 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 150 151 152 153 154 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 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 270 271 272 273 274 275 276 277 278 279 280
###################################################################
#           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(
            f"select distinct(ts2) from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(cint) from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(cbigint) from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(csmallint) from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(ctinyint) from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(cfloat) from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(cdouble) from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(cbool) from {sql} {where}")
        if num < 2:
            tdSql.checkRows(num)
        else:
            tdSql.checkRows(2)
        tdSql.query(
            f"select distinct(cbinary) from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(cnchar) from {sql} {where}")
        tdSql.checkRows(num)

        tdSql.query(
            f"select distinct(ts2) as a from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(cint) as a from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(cbigint) as a from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(csmallint) as a from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(ctinyint) as a from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(cfloat) as a from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(cdouble) as a from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(cbool) as a from {sql} {where}")
        if num < 2:
            tdSql.checkRows(num)
        else:
            tdSql.checkRows(2)
        tdSql.query(
            f"select distinct(cbinary) as a from {sql} {where}")
        tdSql.checkRows(num)
        tdSql.query(
            f"select distinct(cnchar) as a from {sql} {where}")
        tdSql.checkRows(num)


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

        tdSql.error(f"select distinct(ts2), cint from {sql}")
        tdSql.error(f"select distinct ts2, cint from {sql}")
        tdSql.error(f"select distinct(ts2) from {sql} group by cint")
        tdSql.error(f"select distinct ts2 from {sql} group by cint")
        tdSql.error(f"select distinct(ts2) from {sql} intervasl(1s)")
        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 soffset 1")
        tdSql.error(f"select distinct(ts2) from {sql} slimit 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 desc")
        #tdSql.error(f"select distinct(ts2) from {sql} order by asc")
        #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}")
        #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})")
        # 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)
        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)

        #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})')
        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})')
        #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(
            "==============phase1 distinct(col1) with no values==========")
        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)")
        self.query_all_tb()

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

        tdLog.notice(
            "==============phase2 distinct(col1) all values are 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)")
        tdSql.execute(
            "insert into tb_all values(now,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)")

        # normal query
        self.query_all_tb()

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


        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)

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

        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())