between.py 10.0 KB
Newer Older
C
cpwu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import taos
import sys

from util.log import *
from util.sql import *
from util.cases import *



class TDTestCase:

    def init(self, conn, logSql):
        tdLog.debug(f"start to excute {__file__}")
        tdSql.init(conn.cursor())

C
cpwu 已提交
16 17 18 19 20
    def run(self):
        dbname = "db"
        stb = f"{dbname}.stb1"
        rows = 10

C
cpwu 已提交
21 22 23 24
        tdSql.prepare()

        tdLog.printNoPrefix("==========step1:create table")
        tdSql.execute(
C
cpwu 已提交
25
            f'''create table if not exists {stb}
C
cpwu 已提交
26 27 28
            (ts timestamp, c1 int, c2 float, c3 bigint, c4 double, c5 smallint, c6 tinyint)
            tags(location binary(64), type int, isused bool , family nchar(64))'''
        )
C
cpwu 已提交
29 30
        tdSql.execute(f"create table {dbname}.t1 using {stb} tags('beijing', 1, 1, 'nchar1')")
        tdSql.execute(f"create table {dbname}.t2 using {stb} tags('shanghai', 2, 0, 'nchar2')")
C
cpwu 已提交
31 32

        tdLog.printNoPrefix("==========step2:insert data")
C
cpwu 已提交
33
        for i in range(rows):
C
cpwu 已提交
34
            tdSql.execute(
C
cpwu 已提交
35
                f"insert into {dbname}.t1 values (now()+{i}m, {32767+i}, {20.0+i/10}, {2**31+i}, {3.4*10**38+i/10}, {127+i}, {i})"
C
cpwu 已提交
36 37
            )
            tdSql.execute(
C
cpwu 已提交
38
                f"insert into {dbname}.t2 values (now()-{i}m, {-32767-i}, {20.0-i/10}, {-i-2**31}, {-i/10-3.4*10**38}, {-127-i}, {-i})"
C
cpwu 已提交
39 40
            )
        tdSql.execute(
C
cpwu 已提交
41
            f"insert into {dbname}.t1 values (now()+11m, {2**31-1}, {pow(10,37)*34}, {pow(2,63)-1}, {1.7*10**308}, 32767, 127)"
C
cpwu 已提交
42 43
        )
        tdSql.execute(
C
cpwu 已提交
44
            f"insert into {dbname}.t2 values (now()-11m, {1-2**31}, {-3.4*10**38}, {1-2**63}, {-1.7*10**308}, -32767, -127)"
C
cpwu 已提交
45 46
        )
        tdSql.execute(
C
cpwu 已提交
47
            f"insert into {dbname}.t2 values (now()-12m, null , {-3.4*10**38}, null , {-1.7*10**308}, null , null)"
C
cpwu 已提交
48 49 50 51
        )

        tdLog.printNoPrefix("==========step3:query timestamp type")

C
cpwu 已提交
52 53 54
        tdSql.query(f"select * from {dbname}.t1 where ts between now()-1m and now()+10m")
        tdSql.checkRows(rows)
        tdSql.query(f"select * from {dbname}.t1 where ts between '2021-01-01 00:00:00.000' and '2121-01-01 00:00:00.000'")
C
cpwu 已提交
55
        # tdSql.checkRows(11)
C
cpwu 已提交
56
        tdSql.query(f"select * from {dbname}.t1 where ts between '1969-01-01 00:00:00.000' and '1969-12-31 23:59:59.999'")
C
cpwu 已提交
57
        # tdSql.checkRows(0)
C
cpwu 已提交
58
        tdSql.query(f"select * from {dbname}.t1 where ts between -2793600 and 31507199")
C
cpwu 已提交
59
        tdSql.checkRows(0)
C
cpwu 已提交
60 61
        tdSql.query(f"select * from {dbname}.t1 where ts between 1609430400000 and 4765104000000")
        tdSql.checkRows(rows+1)
C
cpwu 已提交
62 63 64

        tdLog.printNoPrefix("==========step4:query int type")

C
cpwu 已提交
65 66 67 68 69
        tdSql.query(f"select * from {dbname}.t1 where c1 between 32767 and 32776")
        tdSql.checkRows(rows)
        tdSql.query(f"select * from {dbname}.t1 where c1 between 32766.9 and 32776.1")
        tdSql.checkRows(rows)
        tdSql.query(f"select * from {dbname}.t1 where c1 between 32776 and 32767")
C
cpwu 已提交
70
        tdSql.checkRows(0)
C
cpwu 已提交
71
        tdSql.query(f"select * from {dbname}.t1 where c1 between 'a' and 'e'")
C
cpwu 已提交
72
        tdSql.checkRows(0)
C
cpwu 已提交
73
        # tdSql.query("select * from {dbname}.t1 where c1 between 0x64 and 0x69")
C
cpwu 已提交
74
        # tdSql.checkRows(6)
C
cpwu 已提交
75 76 77
        tdSql.query(f"select * from {dbname}.t1 where c1 not between 100 and 106")
        tdSql.checkRows(rows+1)
        tdSql.query(f"select * from {dbname}.t1 where c1 between {2**31-2} and {2**31+1}")
C
cpwu 已提交
78
        tdSql.checkRows(1)
C
cpwu 已提交
79
        tdSql.query(f"select * from {dbname}.t2 where c1 between null and {1-2**31}")
C
cpwu 已提交
80
        # tdSql.checkRows(3)
C
cpwu 已提交
81
        tdSql.query(f"select * from {dbname}.t2 where c1 between {-2**31} and {1-2**31}")
C
cpwu 已提交
82 83 84 85
        tdSql.checkRows(1)

        tdLog.printNoPrefix("==========step5:query float type")

C
cpwu 已提交
86
        tdSql.query(f"select * from {dbname}.t1 where c2 between 20.0 and 21.0")
C
cpwu 已提交
87
        tdSql.checkRows(10)
C
cpwu 已提交
88 89 90
        tdSql.query(f"select * from {dbname}.t1 where c2 between {-3.4*10**38-1} and {3.4*10**38+1}")
        tdSql.checkRows(rows+1)
        tdSql.query(f"select * from {dbname}.t1 where c2 between 21.0 and 20.0")
C
cpwu 已提交
91
        tdSql.checkRows(0)
C
cpwu 已提交
92
        tdSql.query(f"select * from {dbname}.t1 where c2 between 'DC3' and 'SYN'")
C
cpwu 已提交
93
        tdSql.checkRows(0)
C
cpwu 已提交
94 95 96
        tdSql.query(f"select * from {dbname}.t1 where c2 not between 0.1 and 0.2")
        tdSql.checkRows(rows+1)
        tdSql.query(f"select * from {dbname}.t1 where c2 between {pow(10,38)*3.4} and {pow(10,38)*3.4+1}")
C
cpwu 已提交
97
        # tdSql.checkRows(1)
C
cpwu 已提交
98
        tdSql.query(f"select * from {dbname}.t2 where c2 between {-3.4*10**38-1} and {-3.4*10**38}")
C
cpwu 已提交
99
        # tdSql.checkRows(2)
C
cpwu 已提交
100
        tdSql.query(f"select * from {dbname}.t2 where c2 between null and {-3.4*10**38}")
C
cpwu 已提交
101 102 103 104
        # tdSql.checkRows(3)

        tdLog.printNoPrefix("==========step6:query bigint type")

C
cpwu 已提交
105 106 107 108 109
        tdSql.query(f"select * from {dbname}.t1 where c3 between {2**31} and {2**31+10}")
        tdSql.checkRows(rows)
        tdSql.query(f"select * from {dbname}.t1 where c3 between {-2**63} and {2**63}")
        tdSql.checkRows(rows+1)
        tdSql.query(f"select * from {dbname}.t1 where c3 between {2**31+10} and {2**31}")
C
cpwu 已提交
110
        tdSql.checkRows(0)
C
cpwu 已提交
111
        tdSql.query(f"select * from {dbname}.t1 where c3 between 'a' and 'z'")
C
cpwu 已提交
112
        tdSql.checkRows(0)
C
cpwu 已提交
113
        tdSql.query(f"select * from {dbname}.t1 where c3 not between 1 and 2")
C
cpwu 已提交
114
        # tdSql.checkRows(0)
C
cpwu 已提交
115
        tdSql.query(f"select * from {dbname}.t1 where c3 between {2**63-2} and {2**63-1}")
C
cpwu 已提交
116
        tdSql.checkRows(1)
C
cpwu 已提交
117
        tdSql.query(f"select * from {dbname}.t2 where c3 between {-2**63} and {1-2**63}")
C
cpwu 已提交
118
        # tdSql.checkRows(3)
C
cpwu 已提交
119
        tdSql.query(f"select * from {dbname}.t2 where c3 between null and {1-2**63}")
C
cpwu 已提交
120 121 122 123
        # tdSql.checkRows(2)

        tdLog.printNoPrefix("==========step7:query double type")

C
cpwu 已提交
124 125 126
        tdSql.query(f"select * from {dbname}.t1 where c4 between {3.4*10**38} and {3.4*10**38+10}")
        tdSql.checkRows(rows)
        tdSql.query(f"select * from {dbname}.t1 where c4 between {1.7*10**308+1} and {1.7*10**308+2}")
C
cpwu 已提交
127 128
        # 因为精度原因,在超出bigint边界后,数值不能进行准确的判断
        # tdSql.checkRows(0)
C
cpwu 已提交
129
        tdSql.query(f"select * from {dbname}.t1 where c4 between {3.4*10**38+10} and {3.4*10**38}")
C
cpwu 已提交
130
        # tdSql.checkRows(0)
C
cpwu 已提交
131
        tdSql.query(f"select * from {dbname}.t1 where c4 between 'a' and 'z'")
C
cpwu 已提交
132
        tdSql.checkRows(0)
C
cpwu 已提交
133
        tdSql.query(f"select * from {dbname}.t1 where c4 not between 1 and 2")
C
cpwu 已提交
134
        # tdSql.checkRows(0)
C
cpwu 已提交
135
        tdSql.query(f"select * from {dbname}.t1 where c4 between {1.7*10**308} and {1.7*10**308+1}")
C
cpwu 已提交
136
        tdSql.checkRows(1)
C
cpwu 已提交
137
        tdSql.query(f"select * from {dbname}.t2 where c4 between {-1.7*10**308-1} and {-1.7*10**308}")
C
cpwu 已提交
138
        # tdSql.checkRows(3)
C
cpwu 已提交
139
        tdSql.query(f"select * from {dbname}.t2 where c4 between null and {-1.7*10**308}")
C
cpwu 已提交
140 141 142 143
        # tdSql.checkRows(3)

        tdLog.printNoPrefix("==========step8:query smallint type")

C
cpwu 已提交
144 145 146 147 148
        tdSql.query(f"select * from {dbname}.t1 where c5 between 127 and 136")
        tdSql.checkRows(rows)
        tdSql.query(f"select * from {dbname}.t1 where c5 between 126.9 and 135.9")
        tdSql.checkRows(rows-1)
        tdSql.query(f"select * from {dbname}.t1 where c5 between 136 and 127")
C
cpwu 已提交
149
        tdSql.checkRows(0)
C
cpwu 已提交
150
        tdSql.query(f"select * from {dbname}.t1 where c5 between '~' and '^'")
C
cpwu 已提交
151
        tdSql.checkRows(0)
C
cpwu 已提交
152
        tdSql.query(f"select * from {dbname}.t1 where c5 not between 1 and 2")
C
cpwu 已提交
153
        # tdSql.checkRows(0)
C
cpwu 已提交
154
        tdSql.query(f"select * from {dbname}.t1 where c5 between 32767 and 32768")
C
cpwu 已提交
155
        tdSql.checkRows(1)
C
cpwu 已提交
156
        tdSql.query(f"select * from {dbname}.t2 where c5 between -32768 and -32767")
C
cpwu 已提交
157
        tdSql.checkRows(1)
C
cpwu 已提交
158
        tdSql.query(f"select * from {dbname}.t2 where c5 between null and -32767")
C
cpwu 已提交
159 160 161 162
        # tdSql.checkRows(1)

        tdLog.printNoPrefix("==========step9:query tinyint type")

C
cpwu 已提交
163 164 165 166 167
        tdSql.query(f"select * from {dbname}.t1 where c6 between 0 and 9")
        tdSql.checkRows(rows)
        tdSql.query(f"select * from {dbname}.t1 where c6 between -1.1 and 8.9")
        tdSql.checkRows(rows-1)
        tdSql.query(f"select * from {dbname}.t1 where c6 between 9 and 0")
C
cpwu 已提交
168
        tdSql.checkRows(0)
C
cpwu 已提交
169
        tdSql.query(f"select * from {dbname}.t1 where c6 between 'NUL' and 'HT'")
C
cpwu 已提交
170
        tdSql.checkRows(1)
C
cpwu 已提交
171
        tdSql.query(f"select * from {dbname}.t1 where c6 not between 1 and 2")
C
cpwu 已提交
172
        # tdSql.checkRows(1)
C
cpwu 已提交
173
        tdSql.query(f"select * from {dbname}.t1 where c6 between 127 and 128")
C
cpwu 已提交
174
        tdSql.checkRows(1)
C
cpwu 已提交
175
        tdSql.query(f"select * from {dbname}.t2 where c6 between -128 and -127")
C
cpwu 已提交
176
        tdSql.checkRows(1)
C
cpwu 已提交
177
        tdSql.query(f"select * from {dbname}.t2 where c6 between null and -127")
C
cpwu 已提交
178 179 180 181
        # tdSql.checkRows(3)

        tdLog.printNoPrefix("==========step10:invalid query type")

C
cpwu 已提交
182
        # TODO tag is not finished
C
cpwu 已提交
183 184 185 186 187 188 189 190 191 192 193
        tdSql.query(f"select * from {stb} where location between 'beijing' and 'shanghai'")
        tdSql.checkRows(rows * 2 + 3)
        # 非0值均解析为1,因此"between 负值 and o"解析为"between 1 and 0"
        tdSql.query(f"select * from {stb} where isused between 0 and 1")
        tdSql.checkRows(rows * 2 + 3)
        tdSql.query(f"select * from {stb} where isused between -1 and 0")
        tdSql.checkRows(rows + 2)
        tdSql.query(f"select * from {stb} where isused between false and true")
        tdSql.checkRows(rows * 2 + 3)
        tdSql.query(f"select * from {stb} where family between '拖拉机' and '自行车'")
        tdSql.checkRows(0)
C
cpwu 已提交
194 195 196

        tdLog.printNoPrefix("==========step11:query HEX/OCT/BIN type")

C
cpwu 已提交
197 198 199 200 201
        tdSql.error(f"select * from {dbname}.t1 where c6 between 0x7f and 0x80")      # check filter HEX
        tdSql.error(f"select * from {dbname}.t1 where c6 between 0b1 and 0b11111")    # check filter BIN
        tdSql.error(f"select * from {dbname}.t1 where c6 between 0b1 and 0x80")
        tdSql.error(f"select * from {dbname}.t1 where c6=0b1")
        tdSql.error(f"select * from {dbname}.t1 where c6=0x1")
C
cpwu 已提交
202
        # 八进制数据会按照十进制数据进行判定
C
cpwu 已提交
203 204
        tdSql.query(f"select * from {dbname}.t1 where c6 between 01 and 0200")        # check filter OCT
        tdSql.checkRows(rows)
C
cpwu 已提交
205 206 207 208 209 210 211

    def stop(self):
        tdSql.close()
        tdLog.success(f"{__file__} successfully executed")

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