queryTestCases.py 76.2 KB
Newer Older
C
cpwu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
###################################################################
#           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 subprocess
C
cpwu 已提交
16 17
import random
import math
W
wu champion 已提交
18
import numpy as np
C
cpwu 已提交
19
import inspect
C
cpwu 已提交
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

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


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

    def getBuildPath(self) -> str:
        selfPath = os.path.dirname(os.path.realpath(__file__))

        if ("community" in selfPath):
            projPath = selfPath[:selfPath.find("community")]
        else:
            projPath = selfPath[:selfPath.find("tests")]

        for root, dirs, files in os.walk(projPath):
            if ("taosd" in files):
                rootRealPath = os.path.dirname(os.path.realpath(root))
                if ("packaging" not in rootRealPath):
                    buildPath = root[:len(root) - len("/debug/build/bin")]
                    break
        return buildPath

    def getCfgDir(self) -> str:
        selfPath = os.path.dirname(os.path.realpath(__file__))

        if ("community" in selfPath):
            cfgDir = self.getBuildPath() + "/community/sim/dnode1/cfg"
        else:
            cfgDir = self.getBuildPath() + "/sim/dnode1/cfg"
        return cfgDir

    def getCfgFile(self) -> str:
        return self.getCfgDir()+"/taos.cfg"

    def td3690(self):
        tdLog.printNoPrefix("==========TD-3690==========")
W
wu champion 已提交
62 63 64 65 66 67 68 69

        tdSql.prepare()

        tdSql.execute("show variables")
        res_off = tdSql.cursor.fetchall()
        resList = np.array(res_off)
        index = np.where(resList == "offlineThreshold")
        index_value = np.dstack((index[0])).squeeze()
C
cpwu 已提交
70
        tdSql.query("show variables")
W
wu champion 已提交
71
        tdSql.checkData(index_value, 1, 864000)
C
cpwu 已提交
72 73 74

    def td4082(self):
        tdLog.printNoPrefix("==========TD-4082==========")
W
wu champion 已提交
75 76
        tdSql.prepare()

C
cpwu 已提交
77 78 79
        cfgfile = self.getCfgFile()
        max_compressMsgSize = 100000000

W
wu champion 已提交
80 81 82 83 84 85
        tdSql.execute("show variables")
        res_com = tdSql.cursor.fetchall()
        rescomlist = np.array(res_com)
        cpms_index = np.where(rescomlist == "compressMsgSize")
        index_value = np.dstack((cpms_index[0])).squeeze()

C
cpwu 已提交
86
        tdSql.query("show variables")
W
wu champion 已提交
87
        tdSql.checkData(index_value, 1, -1)
C
cpwu 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100

        tdSql.query("show dnodes")
        index = tdSql.getData(0, 0)

        tdDnodes.stop(index)
        cmd = f"sed -i '$a compressMSgSize {max_compressMsgSize}' {cfgfile} "
        try:
            _ = subprocess.check_output(cmd, shell=True).decode("utf-8")
        except Exception as e:
            raise e

        tdDnodes.start(index)
        tdSql.query("show variables")
W
wu champion 已提交
101
        tdSql.checkData(index_value, 1, 100000000)
C
cpwu 已提交
102 103 104 105 106 107 108 109 110 111

        tdDnodes.stop(index)
        cmd = f"sed -i '$s/{max_compressMsgSize}/{max_compressMsgSize+10}/g' {cfgfile} "
        try:
            _ = subprocess.check_output(cmd, shell=True).decode("utf-8")
        except Exception as e:
            raise e

        tdDnodes.start(index)
        tdSql.query("show variables")
W
wu champion 已提交
112
        tdSql.checkData(index_value, 1, -1)
C
cpwu 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

        tdDnodes.stop(index)
        cmd = f"sed -i '$d' {cfgfile}"
        try:
            _ = subprocess.check_output(cmd, shell=True).decode("utf-8")
        except Exception as e:
            raise e

        tdDnodes.start(index)

    def td4097(self):
        tdLog.printNoPrefix("==========TD-4097==========")
        tdSql.execute("drop database if exists db")
        tdSql.execute("drop database if exists db1")
        tdSql.execute("create database  if not exists db keep 3650")
        tdSql.execute("create database  if not exists db1 keep 3650")
C
cpwu 已提交
129 130 131
        tdSql.execute("create database  if not exists new keep 3650")
        tdSql.execute("create database  if not exists private keep 3650")
        tdSql.execute("create database  if not exists db2 keep 3650")
C
cpwu 已提交
132 133 134 135 136 137 138 139 140 141

        tdSql.execute("create stable db.stb1 (ts timestamp, c1 int) tags(t1 int)")
        tdSql.execute("create stable db.stb2 (ts timestamp, c1 int) tags(t1 int)")
        tdSql.execute("create stable db1.stb3 (ts timestamp, c1 int) tags(t1 int)")

        tdSql.execute("create table db.t10 using db.stb1 tags(1)")
        tdSql.execute("create table db.t11 using db.stb1 tags(2)")
        tdSql.execute("create table db.t20 using db.stb2 tags(3)")
        tdSql.execute("create table db1.t30 using db1.stb3 tags(4)")

142
        # tdLog.printNoPrefix("==========TD-4097==========")
C
cpwu 已提交
143 144 145 146 147
        # 插入数据,然后进行show create 操作

        # p1 不进入指定数据库
        tdSql.query("show create database db")
        tdSql.checkRows(1)
C
cpwu 已提交
148 149 150 151 152 153 154 155
        tdSql.query("show create database db1")
        tdSql.checkRows(1)
        tdSql.query("show create database db2")
        tdSql.checkRows(1)
        tdSql.query("show create database new")
        tdSql.checkRows(1)
        tdSql.query("show create database private")
        tdSql.checkRows(1)
C
cpwu 已提交
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
        tdSql.error("show create database ")
        tdSql.error("show create databases db ")
        tdSql.error("show create database db.stb1")
        tdSql.error("show create database db0")
        tdSql.error("show create database db db1")
        tdSql.error("show create database db, db1")
        tdSql.error("show create database stb1")
        tdSql.error("show create database * ")

        tdSql.query("show create stable db.stb1")
        tdSql.checkRows(1)
        tdSql.error("show create stable db.t10")
        tdSql.error("show create stable db.stb0")
        tdSql.error("show create stable stb1")
        tdSql.error("show create stable ")
        tdSql.error("show create stable *")
        tdSql.error("show create stable db.stb1 db.stb2")
        tdSql.error("show create stable db.stb1, db.stb2")

        tdSql.query("show create table db.stb1")
        tdSql.checkRows(1)
        tdSql.query("show create table db.t10")
        tdSql.checkRows(1)
        tdSql.error("show create table db.stb0")
        tdSql.error("show create table stb1")
        tdSql.error("show create table ")
        tdSql.error("show create table *")
        tdSql.error("show create table db.stb1 db.stb2")
        tdSql.error("show create table db.stb1, db.stb2")

        # p2 进入指定数据库
        tdSql.execute("use db")

        tdSql.query("show create database db")
        tdSql.checkRows(1)
        tdSql.query("show create database db1")
        tdSql.checkRows(1)
        tdSql.error("show create database ")
        tdSql.error("show create databases db ")
        tdSql.error("show create database db.stb1")
        tdSql.error("show create database db0")
        tdSql.error("show create database db db1")
        tdSql.error("show create database db, db1")
        tdSql.error("show create database stb1")
        tdSql.error("show create database * ")

        tdSql.query("show create stable db.stb1")
        tdSql.checkRows(1)
        tdSql.query("show create stable stb1")
        tdSql.checkRows(1)
        tdSql.query("show create stable db1.stb3")
        tdSql.checkRows(1)
        tdSql.error("show create stable db.t10")
        tdSql.error("show create stable db")
        tdSql.error("show create stable t10")
        tdSql.error("show create stable db.stb0")
        tdSql.error("show create stables stb1")
        tdSql.error("show create stable ")
        tdSql.error("show create stable *")
        tdSql.error("show create stable db.stb1 db.stb2")
        tdSql.error("show create stable stb1 stb2")
        tdSql.error("show create stable db.stb1, db.stb2")
        tdSql.error("show create stable stb1, stb2")

        tdSql.query("show create table db.stb1")
        tdSql.checkRows(1)
        tdSql.query("show create table stb1")
        tdSql.checkRows(1)
        tdSql.query("show create table db.t10")
        tdSql.checkRows(1)
        tdSql.query("show create table t10")
        tdSql.checkRows(1)
        tdSql.query("show create table db1.t30")
        tdSql.checkRows(1)
        tdSql.error("show create table t30")
        tdSql.error("show create table db.stb0")
        tdSql.error("show create table db.t0")
        tdSql.error("show create table db")
        tdSql.error("show create tables stb1")
        tdSql.error("show create tables t10")
        tdSql.error("show create table ")
        tdSql.error("show create table *")
        tdSql.error("show create table db.stb1 db.stb2")
        tdSql.error("show create table db.t11 db.t10")
        tdSql.error("show create table db.stb1, db.stb2")
        tdSql.error("show create table db.t11, db.t10")
        tdSql.error("show create table stb1 stb2")
        tdSql.error("show create table t11 t10")
        tdSql.error("show create table stb1, stb2")
        tdSql.error("show create table t11, t10")

        # p3 删库删表后进行查询
        tdSql.execute("drop table if exists t11")

        tdSql.error("show create table t11")
        tdSql.error("show create table db.t11")
        tdSql.query("show create stable stb1")
        tdSql.checkRows(1)
        tdSql.query("show create table t10")
        tdSql.checkRows(1)

        tdSql.execute("drop stable if exists stb2")

        tdSql.error("show create table stb2")
        tdSql.error("show create table db.stb2")
        tdSql.error("show create stable stb2")
        tdSql.error("show create stable db.stb2")
        tdSql.error("show create stable db.t20")
        tdSql.query("show create database db")
        tdSql.checkRows(1)
        tdSql.query("show create stable db.stb1")
        tdSql.checkRows(1)

        tdSql.execute("drop database if exists db1")
        tdSql.error("show create database db1")
        tdSql.error("show create stable db1.t31")
        tdSql.error("show create stable db1.stb3")
        tdSql.query("show create database db")
        tdSql.checkRows(1)
        tdSql.query("show create stable db.stb1")
        tdSql.checkRows(1)

D
dapan1121 已提交
278 279 280 281 282 283
        tdSql.execute("drop database if exists db")
        tdSql.execute("drop database if exists db1")
        tdSql.execute("drop database if exists new")
        tdSql.execute("drop database if exists db2")
        tdSql.execute("drop database if exists private")

C
cpwu 已提交
284 285 286 287 288 289 290 291 292 293
    def td4153(self):
        tdLog.printNoPrefix("==========TD-4153==========")

        pass

    def td4288(self):
        tdLog.printNoPrefix("==========TD-4288==========")
        # keep ~ [days,365000]
        tdSql.execute("drop database if exists db")
        tdSql.execute("create database  if not exists db")
W
wu champion 已提交
294 295 296 297 298 299 300

        tdSql.execute("show variables")
        res_kp = tdSql.cursor.fetchall()
        resList = np.array(res_kp)
        keep_index = np.where(resList == "keep")
        index_value = np.dstack((keep_index[0])).squeeze()

C
cpwu 已提交
301
        tdSql.query("show variables")
W
wu champion 已提交
302 303
        tdSql.checkData(index_value, 1, 3650)

C
cpwu 已提交
304
        tdSql.query("show databases")
W
wu champion 已提交
305 306 307 308 309
        selfPath = os.path.dirname(os.path.realpath(__file__))
        if ("community" in selfPath):
            tdSql.checkData(0, 7, "3650,3650,3650")
        else:
            tdSql.checkData(0, 7, 3650)
C
cpwu 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

        days = tdSql.getData(0, 6)
        tdSql.error("alter database db keep 3650001")
        tdSql.error("alter database db keep 9")
        tdSql.error("alter database db keep 0b")
        tdSql.error("alter database db keep 3650,9,36500")
        tdSql.error("alter database db keep 3650,3650,365001")
        tdSql.error("alter database db keep 36500,a,36500")
        tdSql.error("alter database db keep (36500,3650,3650)")
        tdSql.error("alter database db keep [36500,3650,36500]")
        tdSql.error("alter database db keep 36500,0xff,3650")
        tdSql.error("alter database db keep 36500,0o365,3650")
        tdSql.error("alter database db keep 36500,0A3Ch,3650")
        tdSql.error("alter database db keep")
        tdSql.error("alter database db keep0 36500")

        tdSql.execute("alter database db keep 36500")
        tdSql.query("show databases")
W
wu champion 已提交
328 329 330 331 332
        if ("community" in selfPath):
            tdSql.checkData(0, 7, "36500,36500,36500")
        else:
            tdSql.checkData(0, 7, 36500)

C
cpwu 已提交
333 334 335
        tdSql.execute("drop database if exists db")
        tdSql.execute("create database  if not exists db1")
        tdSql.query("show databases")
W
wu champion 已提交
336 337 338 339 340
        if ("community" in selfPath):
            tdSql.checkData(0, 7, "3650,3650,3650")
        else:
            tdSql.checkData(0, 7, 3650)

C
cpwu 已提交
341
        tdSql.query("show variables")
W
wu champion 已提交
342
        tdSql.checkData(index_value, 1, 3650)
C
cpwu 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

        tdSql.execute("alter database db1 keep 365")
        tdSql.execute("drop database if exists db1")


        pass

    def td4724(self):
        tdLog.printNoPrefix("==========TD-4724==========")
        cfgfile = self.getCfgFile()
        minTablesPerVnode = 5
        maxTablesPerVnode = 10
        maxVgroupsPerDb = 100

        tdSql.query("show dnodes")
        index = tdSql.getData(0, 0)

        tdDnodes.stop(index)
        vnode_cmd = f"sed -i '$a maxVgroupsPerDb {maxVgroupsPerDb}' {cfgfile} "
        min_cmd = f"sed -i '$a minTablesPerVnode {minTablesPerVnode}' {cfgfile} "
        max_cmd = f"sed -i '$a maxTablesPerVnode {maxTablesPerVnode}' {cfgfile} "
        try:
            _ = subprocess.check_output(vnode_cmd, shell=True).decode("utf-8")
            _ = subprocess.check_output(min_cmd, shell=True).decode("utf-8")
            _ = subprocess.check_output(max_cmd, shell=True).decode("utf-8")
        except Exception as e:
            raise e

        tdDnodes.start(index)
        tdSql.execute("drop database if exists db")
        tdSql.execute("create database  if not exists db keep 3650")
        tdSql.execute("use db")
        tdSql.execute("create stable db.stb1 (ts timestamp, c1 int) tags(t1 int)")
        insert_sql = "insert into "
        for i in  range(100):
            tdSql.execute(f"create table db.t1{i} using db.stb1 tags({i})")
            insert_sql += f" t1{i} values({1604298064000 + i*1000}, {i})"
        tdSql.query("show dnodes")
        vnode_count = tdSql.getData(0, 2)
        if vnode_count <= 1:
            tdLog.exit("vnode is less than 2")

        tdSql.execute(insert_sql)
        tdDnodes.stop(index)
        cmd = f"sed -i '$d' {cfgfile}"
        try:
            _ = subprocess.check_output(cmd, shell=True).decode("utf-8")
            _ = subprocess.check_output(cmd, shell=True).decode("utf-8")
            _ = subprocess.check_output(cmd, shell=True).decode("utf-8")
        except Exception as e:
            raise e

        tdDnodes.start(index)

        pass

C
cpwu 已提交
399 400
    def td4889(self):
        tdLog.printNoPrefix("==========TD-4889==========")
D
dapan1121 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414
        cfg = {
            'minRowsPerFileBlock': '10',
            'maxRowsPerFileBlock': '200',
            'minRows': '10',
            'maxRows': '200',
            'maxVgroupsPerDb': '100',
            'maxTablesPerVnode': '1200',
        }
        tdSql.query("show dnodes")
        dnode_index = tdSql.getData(0,0)
        tdDnodes.stop(dnode_index)
        tdDnodes.deploy(dnode_index, cfg)
        tdDnodes.start(dnode_index)

C
cpwu 已提交
415
        tdSql.execute("drop database if exists db")
D
dapan1121 已提交
416
        tdSql.execute("create database  if not exists db keep 3650 blocks 3 minrows 10 maxrows 200")
C
cpwu 已提交
417 418 419 420

        tdSql.execute("use db")
        tdSql.execute("create stable db.stb1 (ts timestamp, c1 int) tags(t1 int)")

C
cpwu 已提交
421
        nowtime = int(round(time.time() * 1000))
C
cpwu 已提交
422 423
        for i in range(1000):
            tdSql.execute(f"create table db.t1{i} using db.stb1 tags({i})")
C
cpwu 已提交
424
            sql = f"insert into db.t1{i} values"
D
dapan1121 已提交
425
            for j in range(260):
C
cpwu 已提交
426 427 428
                sql += f"({nowtime-1000*i-j}, {i+j})"
                # tdSql.execute(f"insert into db.t1{i} values (now-100d, {i+j})")
            tdSql.execute(sql)
C
cpwu 已提交
429

D
dapan1121 已提交
430 431
        # tdDnodes.stop(dnode_index)
        # tdDnodes.start(dnode_index)
432

C
cpwu 已提交
433 434 435 436
        tdSql.query("show vgroups")
        index = tdSql.getData(0,0)
        tdSql.checkData(0, 6, 0)
        tdSql.execute(f"compact vnodes in({index})")
437 438
        start_time = time.time()
        while True:
C
cpwu 已提交
439
            tdSql.query("show vgroups")
440
            if tdSql.getData(0, 6) != 0:
C
cpwu 已提交
441 442
                tdLog.printNoPrefix("show vgroups row:0 col:6 data:1 == expect:1")
                break
443 444
            run_time = time.time()-start_time
            if run_time > 3:
C
cpwu 已提交
445
                tdLog.exit("compacting not occured")
D
dapan1121 已提交
446
            # time.sleep(0.1)
C
cpwu 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

        pass

    def td5168insert(self):
        tdSql.execute("drop database if exists db")
        tdSql.execute("create database  if not exists db keep 3650")

        tdSql.execute("use db")
        tdSql.execute("create stable db.stb1 (ts timestamp, c1 float, c2 float, c3 double, c4 double) tags(t1 int)")
        tdSql.execute("create table db.t1 using db.stb1 tags(1)")

        for i in range(5):
            c1 = 1001.11 + i*0.1
            c2 = 1001.11 + i*0.1 + 1*0.01
            c3 = 1001.11 + i*0.1 + 2*0.01
            c4 = 1001.11 + i*0.1 + 3*0.01
            tdSql.execute(f"insert into db.t1 values ('2021-07-01 08:00:0{i}.000', {c1}, {c2}, {c3}, {c4})")

        # tdSql.execute("insert into db.t1 values ('2021-07-01 08:00:00.000', 1001.11, 1001.12, 1001.13, 1001.14)")
        # tdSql.execute("insert into db.t1 values ('2021-07-01 08:00:01.000', 1001.21, 1001.22, 1001.23, 1001.24)")
        # tdSql.execute("insert into db.t1 values ('2021-07-01 08:00:02.000', 1001.31, 1001.32, 1001.33, 1001.34)")
        # tdSql.execute("insert into db.t1 values ('2021-07-01 08:00:03.000', 1001.41, 1001.42, 1001.43, 1001.44)")
        # tdSql.execute("insert into db.t1 values ('2021-07-01 08:00:04.000', 1001.51, 1001.52, 1001.53, 1001.54)")

        # for i in range(1000000):
C
cpwu 已提交
472
        for i in range(10000):
C
cpwu 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
            random1 = random.uniform(1000,1001)
            random2 = random.uniform(1000,1001)
            random3 = random.uniform(1000,1001)
            random4 = random.uniform(1000,1001)
            tdSql.execute(f"insert into db.t1 values (now+{i}a, {random1}, {random2},{random3}, {random4})")

        pass

    def td5168(self):
        tdLog.printNoPrefix("==========TD-5168==========")
        # 插入小范围内的随机数
        tdLog.printNoPrefix("=====step0: 默认情况下插入数据========")
        self.td5168insert()

        # 获取五个时间点的数据作为基准数值,未压缩情况下精准匹配
        for i in range(5):
            tdSql.query(f"select * from db.t1 where ts='2021-07-01 08:00:0{i}.000' ")
            # c1, c2, c3, c4 = tdSql.getData(0, 1), tdSql.getData(0, 2), tdSql.getData(0, 3), tdSql.getData(0, 4)
            for j in range(4):
                locals()["f" + str(j) + str(i)] = tdSql.getData(0, j+1)
                print(f"f{j}{i}:", locals()["f" + str(j) + str(i)])
                tdSql.checkData(0, j+1, locals()["f" + str(j) + str(i)])

        # tdSql.query("select * from db.t1 limit 100,1")
        # f10, f11, f12, f13 = tdSql.getData(0,1), tdSql.getData(0,2), tdSql.getData(0,3), tdSql.getData(0,4)
        #
        # tdSql.query("select * from db.t1 limit 1000,1")
        # f20, f21, f22, f23 = tdSql.getData(0,1), tdSql.getData(0,2), tdSql.getData(0,3), tdSql.getData(0,4)
        #
        # tdSql.query("select * from db.t1 limit 10000,1")
        # f30, f31, f32, f33 = tdSql.getData(0,1), tdSql.getData(0,2), tdSql.getData(0,3), tdSql.getData(0,4)
        #
        # tdSql.query("select * from db.t1 limit 100000,1")
        # f40, f41, f42, f43 = tdSql.getData(0,1), tdSql.getData(0,2), tdSql.getData(0,3), tdSql.getData(0,4)
        #
        # tdSql.query("select * from db.t1 limit 1000000,1")
        # f50, f51, f52, f53 = tdSql.getData(0,1), tdSql.getData(0,2), tdSql.getData(0,3), tdSql.getData(0,4)

        # 关闭服务并获取未开启压缩情况下的数据容量
        tdSql.query("show dnodes")
        index = tdSql.getData(0, 0)
        tdDnodes.stop(index)

        cfgdir = self.getCfgDir()
        cfgfile = self.getCfgFile()

        lossy_cfg_cmd=f"grep lossyColumns {cfgfile}|awk '{{print $2}}'"
        data_size_cmd = f"du -s {cfgdir}/../data/vnode/  | awk '{{print $1}}'"
        dsize_init = int(subprocess.check_output(data_size_cmd,shell=True).decode("utf-8"))
        lossy_args = subprocess.check_output(lossy_cfg_cmd, shell=True).decode("utf-8")
        tdLog.printNoPrefix(f"close the lossyColumns,data size is: {dsize_init};the lossyColumns line is: {lossy_args}")

        ###################################################
        float_lossy = "float"
        double_lossy = "double"
        float_double_lossy = "float|double"
        no_loosy = ""

        double_precision_cmd = f"sed -i '$a dPrecision 0.000001' {cfgfile}"
        _ = subprocess.check_output(double_precision_cmd, shell=True).decode("utf-8")

        lossy_float_cmd = f"sed -i '$a  lossyColumns {float_lossy}' {cfgfile} "
        lossy_double_cmd = f"sed -i '$d' {cfgfile} && sed -i '$a  lossyColumns {double_lossy}' {cfgfile} "
        lossy_float_double_cmd = f"sed -i '$d' {cfgfile} && sed -i '$a  lossyColumns {float_double_lossy}' {cfgfile} "
        lossy_no_cmd =  f"sed -i '$a  lossyColumns {no_loosy}' {cfgfile} "

        ###################################################

        # 开启有损压缩,参数float,并启动服务插入数据
        tdLog.printNoPrefix("=====step1: lossyColumns设置为float========")
        lossy_float =  subprocess.check_output(lossy_float_cmd, shell=True).decode("utf-8")
        tdDnodes.start(index)
        self.td5168insert()

        # 查询前面所述5个时间数据并与基准数值进行比较
        for i in range(5):
            tdSql.query(f"select * from db.t1 where ts='2021-07-01 08:00:0{i}.000' ")
            # c1, c2, c3, c4 = tdSql.getData(0, 1), tdSql.getData(0, 2), tdSql.getData(0, 3), tdSql.getData(0, 4)
            for j in range(4):
                # locals()["f" + str(j) + str(i)] = tdSql.getData(0, j+1)
                # print(f"f{j}{i}:", locals()["f" + str(j) + str(i)])
                tdSql.checkData(0, j+1, locals()["f" + str(j) + str(i)])

        # 关闭服务并获取压缩参数为float情况下的数据容量
        tdDnodes.stop(index)
        dsize_float = int(subprocess.check_output(data_size_cmd,shell=True).decode("utf-8"))
        lossy_args = subprocess.check_output(lossy_cfg_cmd, shell=True).decode("utf-8")
        tdLog.printNoPrefix(f"open the lossyColumns, data size is:{dsize_float};the lossyColumns line is: {lossy_args}")

        # 修改有损压缩,参数double,并启动服务
        tdLog.printNoPrefix("=====step2: lossyColumns设置为double========")
        lossy_double =  subprocess.check_output(lossy_double_cmd, shell=True).decode("utf-8")
        tdDnodes.start(index)
        self.td5168insert()

        # 查询前面所述5个时间数据并与基准数值进行比较
        for i in range(5):
            tdSql.query(f"select * from db.t1 where ts='2021-07-01 08:00:0{i}.000' ")
            for j in range(4):
                tdSql.checkData(0, j+1, locals()["f" + str(j) + str(i)])

        # 关闭服务并获取压缩参数为double情况下的数据容量
        tdDnodes.stop(index)
        dsize_double = int(subprocess.check_output(data_size_cmd, shell=True).decode("utf-8"))
        lossy_args = subprocess.check_output(lossy_cfg_cmd, shell=True).decode("utf-8")
        tdLog.printNoPrefix(f"open the lossyColumns, data size is:{dsize_double};the lossyColumns line is: {lossy_args}")

        # 修改有损压缩,参数 float&&double ,并启动服务
        tdLog.printNoPrefix("=====step3: lossyColumns设置为 float&&double ========")
        lossy_float_double =  subprocess.check_output(lossy_float_double_cmd, shell=True).decode("utf-8")
        tdDnodes.start(index)
        self.td5168insert()

        # 查询前面所述5个时间数据并与基准数值进行比较
        for i in range(5):
            tdSql.query(f"select * from db.t1 where ts='2021-07-01 08:00:0{i}.000' ")
            for j in range(4):
                tdSql.checkData(0, j+1, locals()["f" + str(j) + str(i)])

        # 关闭服务并获取压缩参数为 float&&double 情况下的数据容量
        tdDnodes.stop(index)
        dsize_float_double = int(subprocess.check_output(data_size_cmd, shell=True).decode("utf-8"))
        lossy_args = subprocess.check_output(lossy_cfg_cmd, shell=True).decode("utf-8")
        tdLog.printNoPrefix(f"open the lossyColumns, data size is:{dsize_float_double};the lossyColumns line is: {lossy_args}")

        if not ((dsize_float_double < dsize_init) and (dsize_double < dsize_init) and (dsize_float < dsize_init)) :
            tdLog.printNoPrefix(f"When lossyColumns value is float, data size is: {dsize_float}")
            tdLog.printNoPrefix(f"When lossyColumns value is double, data size is: {dsize_double}")
            tdLog.printNoPrefix(f"When lossyColumns value is float and double, data size is: {dsize_float_double}")
            tdLog.printNoPrefix(f"When lossyColumns is closed, data size is: {dsize_init}")
            tdLog.exit("压缩未生效")
        else:
            tdLog.printNoPrefix(f"When lossyColumns value is float, data size is: {dsize_float}")
            tdLog.printNoPrefix(f"When lossyColumns value is double, data size is: {dsize_double}")
            tdLog.printNoPrefix(f"When lossyColumns value is float and double, data size is: {dsize_float_double}")
            tdLog.printNoPrefix(f"When lossyColumns is closed, data size is: {dsize_init}")
            tdLog.printNoPrefix("压缩生效")

        pass

    def td5433(self):
        tdLog.printNoPrefix("==========TD-5433==========")
        tdSql.execute("drop database if exists db")
        tdSql.execute("create database  if not exists db keep 3650")

        tdSql.execute("use db")
        tdSql.execute("create stable db.stb1 (ts timestamp, c1 int) tags(t0 tinyint, t1 int)")
        tdSql.execute("create stable db.stb2 (ts timestamp, c1 int) tags(t0 binary(16), t1 binary(16))")
W
wu champion 已提交
621
        numtab=20000
C
cpwu 已提交
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
        for i in range(numtab):
            sql  = f"create table db.t{i} using db.stb1 tags({i%128}, {100+i})"
            tdSql.execute(sql)
            tdSql.execute(f"insert into db.t{i} values (now-10d, {i})")
            tdSql.execute(f"insert into db.t{i} values (now-9d, {i*2})")
            tdSql.execute(f"insert into db.t{i} values (now-8d, {i*3})")

        tdSql.execute("create table db.t01 using db.stb2 tags('1', '100')")
        tdSql.execute("create table db.t02 using db.stb2 tags('2', '200')")
        tdSql.execute("create table db.t03 using db.stb2 tags('3', '300')")
        tdSql.execute("create table db.t04 using db.stb2 tags('4', '400')")
        tdSql.execute("create table db.t05 using db.stb2 tags('5', '500')")

        tdSql.query("select distinct t1 from stb1 where t1 != '150'")
        tdSql.checkRows(numtab-1)
        tdSql.query("select distinct t1 from stb1 where t1 != 150")
        tdSql.checkRows(numtab-1)
        tdSql.query("select distinct t1 from stb1 where t1 = 150")
        tdSql.checkRows(1)
        tdSql.query("select distinct t1 from stb1 where t1 = '150'")
        tdSql.checkRows(1)
        tdSql.query("select distinct t1 from stb1")
        tdSql.checkRows(numtab)

        tdSql.query("select distinct t0 from stb1 where t0 != '2'")
        tdSql.checkRows(127)
        tdSql.query("select distinct t0 from stb1 where t0 != 2")
        tdSql.checkRows(127)
        tdSql.query("select distinct t0 from stb1 where t0 = 2")
        tdSql.checkRows(1)
        tdSql.query("select distinct t0 from stb1 where t0 = '2'")
        tdSql.checkRows(1)
        tdSql.query("select distinct t0 from stb1")
        tdSql.checkRows(128)

        tdSql.query("select distinct t1 from stb2 where t1 != '200'")
        tdSql.checkRows(4)
        tdSql.query("select distinct t1 from stb2 where t1 != 200")
        tdSql.checkRows(4)
        tdSql.query("select distinct t1 from stb2 where t1 = 200")
        tdSql.checkRows(1)
        tdSql.query("select distinct t1 from stb2 where t1 = '200'")
        tdSql.checkRows(1)
        tdSql.query("select distinct t1 from stb2")
        tdSql.checkRows(5)

        tdSql.query("select distinct t0 from stb2 where t0 != '2'")
        tdSql.checkRows(4)
        tdSql.query("select distinct t0 from stb2 where t0 != 2")
        tdSql.checkRows(4)
        tdSql.query("select distinct t0 from stb2 where t0 = 2")
        tdSql.checkRows(1)
        tdSql.query("select distinct t0 from stb2 where t0 = '2'")
        tdSql.checkRows(1)
        tdSql.query("select distinct t0 from stb2")
        tdSql.checkRows(5)

        pass

    def td5798(self):
C
cpwu 已提交
682
        tdLog.printNoPrefix("==========TD-5798 + TD-5810==========")
C
cpwu 已提交
683 684 685 686 687
        tdSql.execute("drop database if exists db")
        tdSql.execute("create database  if not exists db keep 3650")

        tdSql.execute("use db")
        tdSql.execute("create stable db.stb1 (ts timestamp, c1 int, c2 int) tags(t0 tinyint, t1 int, t2 int)")
C
cpwu 已提交
688 689 690 691 692
        tdSql.execute("create stable db.stb2 (ts timestamp, c2 int, c3 binary(16)) tags(t2 binary(16), t3 binary(16), t4 int)")
        maxRemainderNum=7
        tbnum=101
        for i in range(tbnum-1):
            sql  = f"create table db.t{i} using db.stb1 tags({i%maxRemainderNum}, {(i-1)%maxRemainderNum}, {i%2})"
C
cpwu 已提交
693 694
            tdSql.execute(sql)
            tdSql.execute(f"insert into db.t{i} values (now-10d, {i}, {i%3})")
C
cpwu 已提交
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
            tdSql.execute(f"insert into db.t{i} values (now-9d, {i}, {(i-1)%3})")
            tdSql.execute(f"insert into db.t{i} values (now-8d, {i}, {(i-2)%3})")
            tdSql.execute(f"insert into db.t{i} (ts )values (now-7d)")

            tdSql.execute(f"create table db.t0{i} using db.stb2 tags('{i%maxRemainderNum}', '{(i-1)%maxRemainderNum}', {i%3})")
            tdSql.execute(f"insert into db.t0{i} values (now-10d, {i}, '{(i+1)%3}')")
            tdSql.execute(f"insert into db.t0{i} values (now-9d, {i}, '{(i+2)%3}')")
            tdSql.execute(f"insert into db.t0{i} values (now-8d, {i}, '{(i)%3}')")
            tdSql.execute(f"insert into db.t0{i} (ts )values (now-7d)")
        tdSql.execute("create table db.t100num using db.stb1 tags(null, null, null)")
        tdSql.execute("create table db.t0100num using db.stb2 tags(null, null, null)")
        tdSql.execute(f"insert into db.t100num values (now-10d, {tbnum-1}, 1)")
        tdSql.execute(f"insert into db.t100num values (now-9d, {tbnum-1}, 0)")
        tdSql.execute(f"insert into db.t100num values (now-8d, {tbnum-1}, 2)")
        tdSql.execute(f"insert into db.t100num (ts )values (now-7d)")
        tdSql.execute(f"insert into db.t0100num values (now-10d, {tbnum-1}, 1)")
        tdSql.execute(f"insert into db.t0100num values (now-9d, {tbnum-1}, 0)")
        tdSql.execute(f"insert into db.t0100num values (now-8d, {tbnum-1}, 2)")
        tdSql.execute(f"insert into db.t0100num (ts )values (now-7d)")

        #========== TD-5810 suport distinct multi-data-coloumn ==========
        tdSql.query(f"select distinct  c1 from stb1 where c1 <{tbnum}")
        tdSql.checkRows(tbnum)
        tdSql.query(f"select distinct  c2 from stb1")
        tdSql.checkRows(4)
        tdSql.query(f"select distinct  c1,c2 from stb1 where c1 <{tbnum}")
        tdSql.checkRows(tbnum*3)
        tdSql.query(f"select distinct  c1,c1 from stb1 where c1 <{tbnum}")
        tdSql.checkRows(tbnum)
        tdSql.query(f"select distinct  c1,c2 from stb1 where c1 <{tbnum} limit 3")
        tdSql.checkRows(3)
        tdSql.query(f"select distinct  c1,c2 from stb1 where c1 <{tbnum} limit 3 offset {tbnum*3-2}")
        tdSql.checkRows(2)

        tdSql.query(f"select distinct  c1 from t1 where c1 <{tbnum}")
        tdSql.checkRows(1)
        tdSql.query(f"select distinct  c2 from t1")
        tdSql.checkRows(4)
        tdSql.query(f"select distinct  c1,c2 from t1 where c1 <{tbnum}")
        tdSql.checkRows(3)
        tdSql.query(f"select distinct  c1,c1 from t1 ")
        tdSql.checkRows(2)
        tdSql.query(f"select distinct  c1,c1 from t1 where c1 <{tbnum}")
        tdSql.checkRows(1)
        tdSql.query(f"select distinct  c1,c2 from t1 where c1 <{tbnum} limit 3")
        tdSql.checkRows(3)
        tdSql.query(f"select distinct  c1,c2 from t1 where c1 <{tbnum} limit 3 offset 2")
        tdSql.checkRows(1)

        tdSql.query(f"select distinct  c3 from stb2 where c2 <{tbnum} ")
        tdSql.checkRows(3)
        tdSql.query(f"select distinct  c3, c2 from stb2 where c2 <{tbnum} limit 2")
        tdSql.checkRows(2)

        tdSql.error("select distinct c5 from stb1")
        tdSql.error("select distinct c5 from t1")
        tdSql.error("select distinct c1 from db.*")
        tdSql.error("select c2, distinct c1 from stb1")
        tdSql.error("select c2, distinct c1 from t1")
        tdSql.error("select distinct c2 from ")
        tdSql.error("distinct c2 from stb1")
        tdSql.error("distinct c2 from t1")
        tdSql.error("select distinct  c1, c2, c3 from stb1")
        tdSql.error("select distinct  c1, c2, c3 from t1")
        tdSql.error("select distinct  stb1.c1, stb1.c2, stb2.c2, stb2.c3 from stb1")
        tdSql.error("select distinct  stb1.c1, stb1.c2, stb2.c2, stb2.c3 from t1")
        tdSql.error("select distinct  t1.c1, t1.c2, t2.c1, t2.c2 from t1")
        tdSql.query(f"select distinct  c1 c2, c2 c3 from stb1 where c1 <{tbnum}")
        tdSql.checkRows(tbnum*3)
        tdSql.query(f"select distinct  c1 c2, c2 c3 from t1 where c1 <{tbnum}")
        tdSql.checkRows(3)
dengyihao's avatar
dengyihao 已提交
766 767
        tdSql.error("select distinct  c1, c2 from stb1 order by ts")
        tdSql.error("select distinct  c1, c2 from t1 order by ts")
C
cpwu 已提交
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
        tdSql.error("select distinct  c1, ts from stb1 group by c2")
        tdSql.error("select distinct  c1, ts from t1 group by c2")
        tdSql.error("select distinct  c1, max(c2) from stb1 ")
        tdSql.error("select distinct  c1, max(c2) from t1 ")
        tdSql.error("select max(c2), distinct  c1 from stb1 ")
        tdSql.error("select max(c2), distinct  c1 from t1 ")
        tdSql.error("select distinct  c1, c2 from stb1 where c1 > 3 group by t0")
        tdSql.error("select distinct  c1, c2 from t1 where c1 > 3 group by t0")
        tdSql.error("select distinct  c1, c2 from stb1 where c1 > 3 interval(1d) ")
        tdSql.error("select distinct  c1, c2 from t1 where c1 > 3 interval(1d) ")
        tdSql.error("select distinct  c1, c2 from stb1 where c1 > 3 interval(1d) fill(next)")
        tdSql.error("select distinct  c1, c2 from t1 where c1 > 3 interval(1d) fill(next)")
        tdSql.error("select distinct  c1, c2 from stb1 where ts > now-10d and ts < now interval(1d) fill(next)")
        tdSql.error("select distinct  c1, c2 from t1 where ts > now-10d and ts < now interval(1d) fill(next)")
        tdSql.error("select distinct  c1, c2 from stb1 where c1 > 3 slimit 1")
        tdSql.error("select distinct  c1, c2 from t1 where c1 > 3 slimit 1")
        tdSql.query(f"select distinct  c1, c2 from stb1 where c1 between {tbnum-2} and {tbnum} ")
        tdSql.checkRows(6)
        tdSql.query("select distinct  c1, c2 from stb1 where c1 in (1,2,3,4,5)")
        tdSql.checkRows(15)
        tdSql.query("select distinct  c1, c2 from stb1 where c1 in (100,1000,10000)")
        tdSql.checkRows(3)

        tdSql.query(f"select distinct  c1,c2 from (select * from stb1 where c1 > {tbnum-2}) ")
        tdSql.checkRows(3)
        tdSql.query(f"select distinct  c1,c2 from (select * from t1 where c1 < {tbnum}) ")
        tdSql.checkRows(3)
        tdSql.query(f"select distinct  c1,c2 from (select * from stb1 where t2 !=0 and t2 != 1) ")
D
dapan1121 已提交
796
        tdSql.checkRows(0)
C
cpwu 已提交
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
        tdSql.error("select distinct  c1, c2 from (select distinct c1, c2 from stb1 where t0 > 2 and t1 < 3) ")
        tdSql.error("select  c1, c2 from (select distinct c1, c2 from stb1 where t0 > 2 and t1 < 3) ")
        tdSql.query("select distinct  c1, c2 from (select c2, c1 from stb1 where c1 > 2 ) where  c1 < 4")
        tdSql.checkRows(3)
        tdSql.error("select distinct  c1, c2 from (select c1 from stb1 where t0 > 2 ) where  t1 < 3")
        tdSql.error("select distinct  c1, c2 from (select c2, c1 from stb1 where c1 > 2 order by ts)")
        # tdSql.error("select distinct  c1, c2 from (select c2, c1 from t1 where c1 > 2 order by ts)")
        tdSql.error("select distinct  c1, c2 from (select c2, c1 from stb1 where c1 > 2 group by c1)")
        # tdSql.error("select distinct  c1, c2 from (select max(c1) c1, max(c2) c2 from stb1 group by c1)")
        # tdSql.error("select distinct  c1, c2 from (select max(c1) c1, max(c2) c2 from t1 group by c1)")
        tdSql.query("select distinct  c1, c2 from (select max(c1) c1, max(c2) c2 from stb1 )")
        tdSql.checkRows(1)
        tdSql.query("select distinct  c1, c2 from (select max(c1) c1, max(c2) c2 from t1 )")
        tdSql.checkRows(1)
        tdSql.error("select distinct  stb1.c1, stb1.c2 from stb1 , stb2 where stb1.ts=stb2.ts and stb1.t2=stb2.t4")
        tdSql.error("select distinct  t1.c1, t1.c2 from t1 , t2 where t1.ts=t2.ts ")

        # tdSql.error("select distinct  c1, c2 from (select count(c1) c1, count(c2) c2 from stb1 group by ts)")
        # tdSql.error("select distinct  c1, c2 from (select count(c1) c1, count(c2) c2 from t1 group by ts)")
C
cpwu 已提交
816 817


C
cpwu 已提交
818 819

        #========== TD-5798 suport distinct multi-tags-coloumn ==========
C
cpwu 已提交
820
        tdSql.query("select distinct  t1 from stb1")
C
cpwu 已提交
821
        tdSql.checkRows(maxRemainderNum+1)
C
cpwu 已提交
822
        tdSql.query("select distinct  t0, t1 from stb1")
C
cpwu 已提交
823
        tdSql.checkRows(maxRemainderNum+1)
C
cpwu 已提交
824
        tdSql.query("select distinct  t1, t0 from stb1")
C
cpwu 已提交
825
        tdSql.checkRows(maxRemainderNum+1)
C
cpwu 已提交
826
        tdSql.query("select distinct  t1, t2 from stb1")
C
cpwu 已提交
827
        tdSql.checkRows(maxRemainderNum*2+1)
C
cpwu 已提交
828
        tdSql.query("select distinct  t0, t1, t2 from stb1")
C
cpwu 已提交
829
        tdSql.checkRows(maxRemainderNum*2+1)
C
cpwu 已提交
830
        tdSql.query("select distinct  t0 t1, t1 t2 from stb1")
C
cpwu 已提交
831
        tdSql.checkRows(maxRemainderNum+1)
C
cpwu 已提交
832
        tdSql.query("select distinct  t0, t0, t0 from stb1")
C
cpwu 已提交
833
        tdSql.checkRows(maxRemainderNum+1)
C
cpwu 已提交
834 835
        tdSql.query("select distinct  t0, t1 from t1")
        tdSql.checkRows(1)
C
cpwu 已提交
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
        tdSql.query("select distinct  t0, t1 from t100num")
        tdSql.checkRows(1)

        tdSql.query("select distinct  t3 from stb2")
        tdSql.checkRows(maxRemainderNum+1)
        tdSql.query("select distinct  t2, t3 from stb2")
        tdSql.checkRows(maxRemainderNum+1)
        tdSql.query("select distinct  t3, t2 from stb2")
        tdSql.checkRows(maxRemainderNum+1)
        tdSql.query("select distinct  t4, t2 from stb2")
        tdSql.checkRows(maxRemainderNum*3+1)
        tdSql.query("select distinct  t2, t3, t4 from stb2")
        tdSql.checkRows(maxRemainderNum*3+1)
        tdSql.query("select distinct  t2 t1, t3 t2 from stb2")
        tdSql.checkRows(maxRemainderNum+1)
        tdSql.query("select distinct  t3, t3, t3 from stb2")
        tdSql.checkRows(maxRemainderNum+1)
        tdSql.query("select distinct  t2, t3 from t01")
        tdSql.checkRows(1)
        tdSql.query("select distinct  t3, t4 from t0100num")
        tdSql.checkRows(1)
C
cpwu 已提交
857 858 859 860


        ########## should be error #########
        tdSql.error("select distinct  from stb1")
C
cpwu 已提交
861 862
        tdSql.error("select distinct t3 from stb1")
        tdSql.error("select distinct t1 from db.*")
C
cpwu 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
        tdSql.error("select distinct t2 from ")
        tdSql.error("distinct t2 from stb1")
        tdSql.error("select distinct stb1")
        tdSql.error("select distinct  t0, t1, t2, t3 from stb1")
        tdSql.error("select distinct  stb1.t0, stb1.t1, stb2.t2, stb2.t3 from stb1")

        tdSql.error("select dist t0 from stb1")
        tdSql.error("select distinct  stb2.t2, stb2.t3 from stb1")
        tdSql.error("select distinct  stb2.t2 t1, stb2.t3 t2 from stb1")

        tdSql.error("select distinct  t0, t1 from t1 where t0 < 7")

        ########## add where condition ##########
        tdSql.query("select distinct  t0, t1 from stb1 where t1 > 3")
        tdSql.checkRows(3)
        tdSql.query("select distinct  t0, t1 from stb1 where t1 > 3 limit 2")
        tdSql.checkRows(2)
        tdSql.query("select distinct  t0, t1 from stb1 where t1 > 3 limit 2 offset 2")
        tdSql.checkRows(1)
        tdSql.query("select distinct  t0, t1 from stb1 where t1 > 3 slimit 2")
        tdSql.checkRows(3)
        tdSql.error("select distinct  t0, t1 from stb1 where c1 > 2")
        tdSql.query("select distinct  t0, t1 from stb1 where t1 > 3 and t1 < 5")
        tdSql.checkRows(1)
        tdSql.error("select distinct  stb1.t0, stb1.t1 from stb1, stb2 where stb1.t2=stb2.t4")
        tdSql.error("select distinct  t0, t1 from stb1 where stb2.t4 > 2")
C
cpwu 已提交
889 890 891 892 893 894 895
        tdSql.error("select distinct  t0, t1 from stb1 where t1 > 3 group by t0")
        tdSql.error("select distinct  t0, t1 from stb1 where t1 > 3 interval(1d) ")
        tdSql.error("select distinct  t0, t1 from stb1 where t1 > 3 interval(1d) fill(next)")
        tdSql.error("select distinct  t0, t1 from stb1 where ts > now-10d and ts < now interval(1d) fill(next)")

        tdSql.error("select max(c1), distinct t0 from stb1 where t0 > 2")
        tdSql.error("select distinct t0, max(c1) from stb1 where t0 > 2")
C
cpwu 已提交
896 897 898 899 900
        tdSql.error("select distinct  t0  from stb1 where t0 in (select t0 from stb1 where t0 > 2)")
        tdSql.query("select distinct  t0, t1  from stb1 where t0 in (1,2,3,4,5)")
        tdSql.checkRows(5)
        tdSql.query("select distinct  t1 from (select t0, t1 from stb1 where t0 > 2) ")
        tdSql.checkRows(4)
C
cpwu 已提交
901 902
        tdSql.error("select distinct  t1 from (select distinct t0, t1 from stb1 where t0 > 2 and t1 < 3) ")
        tdSql.error("select distinct  t1 from (select distinct t0, t1 from stb1 where t0 > 2 ) where  t1 < 3")
C
cpwu 已提交
903 904 905
        tdSql.query("select distinct  t1 from (select t0, t1 from stb1 where t0 > 2 ) where  t1 < 3")
        tdSql.checkRows(1)
        tdSql.error("select distinct  t1, t0 from (select t1 from stb1 where t0 > 2 ) where  t1 < 3")
C
cpwu 已提交
906 907
        tdSql.error("select distinct  t1, t0 from (select max(t1) t1, max(t0) t0 from stb1 group by t1)")
        tdSql.error("select distinct  t1, t0 from (select max(t1) t1, max(t0) t0 from stb1)")
C
cpwu 已提交
908 909
        tdSql.query("select distinct  t1, t0 from (select t1,t0 from stb1 where t0 > 2 ) where  t1 < 3")
        tdSql.checkRows(1)
C
cpwu 已提交
910 911 912 913 914
        tdSql.error(" select distinct  t1, t0 from (select t1,t0 from stb1 where t0 > 2 order by ts) where  t1 < 3")
        tdSql.error("select t1, t0 from (select distinct t1,t0 from stb1 where t0 > 2 ) where  t1 < 3")
        tdSql.error(" select distinct  t1, t0 from (select t1,t0 from stb1 where t0 > 2  group by ts) where  t1 < 3")
        tdSql.error("select distinct  stb1.t1, stb1.t2 from stb1 , stb2 where stb1.ts=stb2.ts and stb1.t2=stb2.t4")
        tdSql.error("select distinct  t1.t1, t1.t2 from t1 , t2 where t1.ts=t2.ts ")
C
cpwu 已提交
915 916 917 918

        pass

    def td5935(self):
C
cpwu 已提交
919
        tdLog.printNoPrefix("==========TD-5935==========")
C
cpwu 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932
        tdSql.execute("drop database if exists db")
        tdSql.execute("create database  if not exists db keep 3650")

        tdSql.execute("use db")
        tdSql.execute("create stable db.stb1 (ts timestamp, c1 int, c2 float) tags(t1 int, t2 int)")
        nowtime=int(round((time.time()*1000)))
        for i in range(100):
            sql = f"create table db.t{i} using db.stb1 tags({i % 7}, {i % 2})"
            tdSql.execute(sql)
            for j in range(1000):
                tdSql.execute(f"insert into db.t{i} values ({nowtime-j*10}, {1000-j}, {round(random.random()*j,3)})")
            tdSql.execute(f"insert into db.t{i} (ts) values ({nowtime-10000}) ")

C
cpwu 已提交
933
        ########### TD-5933  verify the bug of "function stddev with interval return 0 rows" is fixed ##########
W
wu champion 已提交
934
        stddevAndIntervalSql=f"select last(*) from t0 where ts>={nowtime-10000}  interval(10a) limit 10"
C
cpwu 已提交
935 936 937 938
        tdSql.query(stddevAndIntervalSql)
        tdSql.checkRows(10)

        ########## TD-5978 verify the bug of "when start row is null, result by fill(next) is 0 " is fixed ##########
C
cpwu 已提交
939 940 941 942 943 944 945 946 947 948
        fillsql=f"select last(*) from t0 where ts>={nowtime-10000} and ts<{nowtime}  interval(10a) fill(next) limit 10"
        tdSql.query(fillsql)
        fillResult=False
        if (tdSql.getData(0,2) != 0) and (tdSql.getData(0, 2)  is not None):
            fillResult=True
        if fillResult:
            tdLog.success(f"sql is :{fillsql}, fill(next) is correct")
        else:
            tdLog.exit("fill(next) is wrong")

C
cpwu 已提交
949
        pass
C
cpwu 已提交
950

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
    def td6068(self):
        tdLog.printNoPrefix("==========TD-6068==========")
        tdSql.execute("drop database if exists db")
        tdSql.execute("create database  if not exists db keep 3650")
        tdSql.execute("use db")

        tdSql.execute("create stable db.stb1 (ts timestamp, c1 int, c2 float, c3 timestamp, c4 binary(16), c5 double, c6 bool) tags(t1 int)")

        for i in range(100):
            sql  = f"create table db.t{i} using db.stb1 tags({i})"
            tdSql.execute(sql)
            tdSql.execute(f"insert into db.t{i} values (now-10h, {i}, {i+random.random()}, now-10h, 'a_{i}', '{i-random.random()}', True)")
            tdSql.execute(f"insert into db.t{i} values (now-9h, {i+random.randint(1,10)}, {i+random.random()}, now-9h, 'a_{i}', '{i-random.random()}', FALSE )")
            tdSql.execute(f"insert into db.t{i} values (now-8h, {i+random.randint(1,10)}, {i+random.random()}, now-8h, 'b_{i}', '{i-random.random()}', True)")
            tdSql.execute(f"insert into db.t{i} values (now-7h, {i+random.randint(1,10)}, {i+random.random()}, now-7h, 'b_{i}', '{i-random.random()}', FALSE )")
            tdSql.execute(f"insert into db.t{i} values (now-6h, {i+random.randint(1,10)}, {i+random.random()}, now-6h, 'c_{i}', '{i-random.random()}', True)")
            tdSql.execute(f"insert into db.t{i} values (now-5h, {i+random.randint(1,10)}, {i+random.random()}, now-5h, 'c_{i}', '{i-random.random()}', FALSE )")
            tdSql.execute(f"insert into db.t{i} (ts)values (now-4h)")
            tdSql.execute(f"insert into db.t{i} (ts)values (now-11h)")
            tdSql.execute(f"insert into db.t{i} (ts)values (now-450m)")

        tdSql.query("select ts as t,derivative(c1, 10m, 0) from t1")
        tdSql.checkRows(5)
        tdSql.checkCols(3)
        for i in range(5):
            data=tdSql.getData(i, 0)
            tdSql.checkData(i, 1, data)
        tdSql.query("select ts as t, derivative(c1, 1h, 0) from stb1 group by tbname")
        tdSql.checkRows(500)
        tdSql.checkCols(4)
        tdSql.query("select ts as t, derivative(c1, 1s, 0) from t1")
        tdSql.query("select ts as t, derivative(c1, 1d, 0) from t1")
        tdSql.error("select ts as t, derivative(c1, 1h, 0) from stb1")
        tdSql.query("select ts as t, derivative(c2, 1h, 0) from t1")
        tdSql.checkRows(5)
        tdSql.error("select ts as t, derivative(c3, 1h, 0) from t1")
        tdSql.error("select ts as t, derivative(c4, 1h, 0) from t1")
        tdSql.query("select ts as t, derivative(c5, 1h, 0) from t1")
        tdSql.checkRows(5)
        tdSql.error("select ts as t, derivative(c6, 1h, 0) from t1")
        tdSql.error("select ts as t, derivative(t1, 1h, 0) from t1")

        tdSql.query("select ts as t, diff(c1) from t1")
        tdSql.checkRows(5)
        tdSql.checkCols(3)
        for i in range(5):
            data=tdSql.getData(i, 0)
            tdSql.checkData(i, 1, data)
        tdSql.query("select ts as t, diff(c1) from stb1 group by tbname")
        tdSql.checkRows(500)
        tdSql.checkCols(4)
        tdSql.query("select ts as t, diff(c1) from t1")
        tdSql.query("select ts as t, diff(c1) from t1")
        tdSql.error("select ts as t, diff(c1) from stb1")
        tdSql.query("select ts as t, diff(c2) from t1")
        tdSql.checkRows(5)
        tdSql.error("select ts as t, diff(c3) from t1")
        tdSql.error("select ts as t, diff(c4) from t1")
        tdSql.query("select ts as t, diff(c5) from t1")
        tdSql.checkRows(5)
        tdSql.error("select ts as t, diff(c6) from t1")
        tdSql.error("select ts as t, diff(t1) from t1")
        tdSql.error("select ts as t, diff(c1, c2) from t1")

        tdSql.error("select ts as t, bottom(c1, 0) from t1")
        tdSql.query("select ts as t, bottom(c1, 5) from t1")
        tdSql.checkRows(5)
        tdSql.checkCols(3)
        for i in range(5):
            data=tdSql.getData(i, 0)
            tdSql.checkData(i, 1, data)
        tdSql.query("select ts as t, bottom(c1, 5) from stb1")
        tdSql.checkRows(5)
        tdSql.query("select ts as t, bottom(c1, 5) from stb1 group by tbname")
        tdSql.checkRows(500)
        tdSql.query("select ts as t, bottom(c1, 8) from t1")
        tdSql.checkRows(6)
        tdSql.query("select ts as t, bottom(c2, 8) from t1")
        tdSql.checkRows(6)
        tdSql.error("select ts as t, bottom(c3, 5) from t1")
        tdSql.error("select ts as t, bottom(c4, 5) from t1")
        tdSql.query("select ts as t, bottom(c5, 8) from t1")
        tdSql.checkRows(6)
        tdSql.error("select ts as t, bottom(c6, 5) from t1")
        tdSql.error("select ts as t, bottom(c5, 8) as b from t1 order by b")
        tdSql.error("select ts as t, bottom(t1, 1) from t1")
        tdSql.error("select ts as t, bottom(t1, 1) from stb1")
        tdSql.error("select ts as t, bottom(t1, 3) from stb1 order by c3")
        tdSql.error("select ts as t, bottom(t1, 3) from t1 order by c3")


W
wu champion 已提交
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
        tdSql.error("select ts as t, top(c1, 0) from t1")
        tdSql.query("select ts as t, top(c1, 5) from t1")
        tdSql.checkRows(5)
        tdSql.checkCols(3)
        for i in range(5):
            data=tdSql.getData(i, 0)
            tdSql.checkData(i, 1, data)
        tdSql.query("select ts as t, top(c1, 5) from stb1")
        tdSql.checkRows(5)
        tdSql.query("select ts as t, top(c1, 5) from stb1 group by tbname")
        tdSql.checkRows(500)
        tdSql.query("select ts as t, top(c1, 8) from t1")
        tdSql.checkRows(6)
        tdSql.query("select ts as t, top(c2, 8) from t1")
        tdSql.checkRows(6)
        tdSql.error("select ts as t, top(c3, 5) from t1")
        tdSql.error("select ts as t, top(c4, 5) from t1")
        tdSql.query("select ts as t, top(c5, 8) from t1")
        tdSql.checkRows(6)
        tdSql.error("select ts as t, top(c6, 5) from t1")
        tdSql.error("select ts as t, top(c5, 8) as b from t1 order by b")
        tdSql.error("select ts as t, top(t1, 1) from t1")
        tdSql.error("select ts as t, top(t1, 1) from stb1")
        tdSql.error("select ts as t, top(t1, 3) from stb1 order by c3")
        tdSql.error("select ts as t, top(t1, 3) from t1 order by c3")

        tdDnodes.stop(1)
        tdDnodes.start(1)

        tdSql.query("select ts as t, diff(c1) from t1")
        tdSql.checkRows(5)
        tdSql.checkCols(3)
        for i in range(5):
            data=tdSql.getData(i, 0)
            tdSql.checkData(i, 1, data)
        tdSql.query("select ts as t, diff(c1) from stb1 group by tbname")
        tdSql.checkRows(500)
        tdSql.checkCols(4)
        tdSql.query("select ts as t, diff(c1) from t1")
        tdSql.query("select ts as t, diff(c1) from t1")
        tdSql.error("select ts as t, diff(c1) from stb1")
        tdSql.query("select ts as t, diff(c2) from t1")
        tdSql.checkRows(5)
        tdSql.error("select ts as t, diff(c3) from t1")
        tdSql.error("select ts as t, diff(c4) from t1")
        tdSql.query("select ts as t, diff(c5) from t1")
        tdSql.checkRows(5)
        tdSql.error("select ts as t, diff(c6) from t1")
        tdSql.error("select ts as t, diff(t1) from t1")
        tdSql.error("select ts as t, diff(c1, c2) from t1")

        tdSql.error("select ts as t, bottom(c1, 0) from t1")
        tdSql.query("select ts as t, bottom(c1, 5) from t1")
        tdSql.checkRows(5)
        tdSql.checkCols(3)
        for i in range(5):
            data=tdSql.getData(i, 0)
            tdSql.checkData(i, 1, data)
        tdSql.query("select ts as t, bottom(c1, 5) from stb1")
        tdSql.checkRows(5)
        tdSql.query("select ts as t, bottom(c1, 5) from stb1 group by tbname")
        tdSql.checkRows(500)
        tdSql.query("select ts as t, bottom(c1, 8) from t1")
        tdSql.checkRows(6)
        tdSql.query("select ts as t, bottom(c2, 8) from t1")
        tdSql.checkRows(6)
        tdSql.error("select ts as t, bottom(c3, 5) from t1")
        tdSql.error("select ts as t, bottom(c4, 5) from t1")
        tdSql.query("select ts as t, bottom(c5, 8) from t1")
        tdSql.checkRows(6)
        tdSql.error("select ts as t, bottom(c6, 5) from t1")
        tdSql.error("select ts as t, bottom(c5, 8) as b from t1 order by b")
        tdSql.error("select ts as t, bottom(t1, 1) from t1")
        tdSql.error("select ts as t, bottom(t1, 1) from stb1")
        tdSql.error("select ts as t, bottom(t1, 3) from stb1 order by c3")
        tdSql.error("select ts as t, bottom(t1, 3) from t1 order by c3")


1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
        tdSql.error("select ts as t, top(c1, 0) from t1")
        tdSql.query("select ts as t, top(c1, 5) from t1")
        tdSql.checkRows(5)
        tdSql.checkCols(3)
        for i in range(5):
            data=tdSql.getData(i, 0)
            tdSql.checkData(i, 1, data)
        tdSql.query("select ts as t, top(c1, 5) from stb1")
        tdSql.checkRows(5)
        tdSql.query("select ts as t, top(c1, 5) from stb1 group by tbname")
        tdSql.checkRows(500)
        tdSql.query("select ts as t, top(c1, 8) from t1")
        tdSql.checkRows(6)
        tdSql.query("select ts as t, top(c2, 8) from t1")
        tdSql.checkRows(6)
        tdSql.error("select ts as t, top(c3, 5) from t1")
        tdSql.error("select ts as t, top(c4, 5) from t1")
        tdSql.query("select ts as t, top(c5, 8) from t1")
        tdSql.checkRows(6)
        tdSql.error("select ts as t, top(c6, 5) from t1")
        tdSql.error("select ts as t, top(c5, 8) as b from t1 order by b")
        tdSql.error("select ts as t, top(t1, 1) from t1")
        tdSql.error("select ts as t, top(t1, 1) from stb1")
        tdSql.error("select ts as t, top(t1, 3) from stb1 order by c3")
        tdSql.error("select ts as t, top(t1, 3) from t1 order by c3")
1145 1146
        pass

C
cpwu 已提交
1147
    def apercentile_query_form(self, col="c1", p=0, com=',', algo="'t-digest'", alias="", table_expr="t1", condition=""):
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160

        '''
        apercentile function:
        :param col:         string, column name, required parameters;
        :param p:           float, percentile interval, [0,100], required parameters;
        :param algo:        string, alforithm, real form like: ', algorithm' , algorithm: {type:int, data:[0, 1]};
        :param alias:       string, result column another name;
        :param table_expr:  string or expression, data source(eg,table/stable name, result set), required parameters;
        :param condition:   expression;
        :param args:        other funtions,like: ', last(col)'
        :return:            apercentile query statement,default: select apercentile(c1, 0, 1) from t1
        '''

C
cpwu 已提交
1161
        return f"select apercentile({col}, {p}{com} {algo})  {alias}  from {table_expr} {condition}"
1162

C
cpwu 已提交
1163
    def checkapert(self,col="c1", p=0, com=',', algo='"t-digest"', alias="", table_expr="t1", condition="" ):
1164 1165 1166 1167 1168 1169 1170 1171 1172

        tdSql.query(f"select count({col})  from {table_expr} {condition}")
        if tdSql.queryRows == 0:
            tdSql.query(self.apercentile_query_form(
                col=col, p=p, com=com, algo=algo, alias=alias, table_expr=table_expr, condition=condition
            ))
            tdSql.checkRows(0)
            return

C
cpwu 已提交
1173 1174 1175 1176 1177 1178 1179 1180 1181
        pset = [0, 40,  60, 100]
        if p not in pset:
            pset.append(p)

        if "stb" in table_expr:
            tdSql.query(f"select spread({col})  from stb1")
        else:
            tdSql.query(f"select avg(c1) from (select spread({col.split('.')[-1]}) c1 from stb1  group by tbname)")
        spread_num = tdSql.getData(0, 0)
1182 1183

        for pi in pset:
C
cpwu 已提交
1184

1185
            if "group" in condition:
C
cpwu 已提交
1186
                tdSql.query(f"select last_row({col}) from {table_expr} {condition}")
1187 1188 1189
                query_result = tdSql.queryResult
                query_rows = tdSql.queryRows
                for i in range(query_rows):
C
cpwu 已提交
1190 1191 1192 1193 1194 1195 1196 1197
                    pre_condition = condition.replace("slimit",'limit').replace("group by tbname", "").split("soffset")[0]
                    tbname = query_result[i][-1]
                    tdSql.query(f"select percentile({col}, {pi}) {alias} from {tbname} {pre_condition}")
                    print(tdSql.sql)
                    pre_data = tdSql.getData(0, 0)
                    tdSql.query(self.apercentile_query_form(
                        col=col, p=pi, com=com, algo='"t-digest"', alias=alias, table_expr=table_expr, condition=condition
                    ))
C
cpwu 已提交
1198
                    if abs(tdSql.getData(i, 0)) >= (spread_num*0.02):
C
cpwu 已提交
1199
                        tdSql.checkDeviaRation(i, 0, pre_data, 0.1)
C
cpwu 已提交
1200
                    else:
C
cpwu 已提交
1201
                        devia = abs((tdSql.getData(i, 0) - pre_data) / (spread_num * 0.02))
C
cpwu 已提交
1202
                        if devia < 0.5:
C
cpwu 已提交
1203
                            tdLog.info(f"sql:{tdSql.sql}, result data:{tdSql.getData(i, 0)}, expect data:{pre_data}, "
C
cpwu 已提交
1204 1205 1206 1207
                                           f"actual deviation:{devia} <= expect deviation: 0.01")
                        else:
                            tdLog.exit(
                                    f"[{inspect.getframeinfo(inspect.stack()[1][0]).lineno}],check failed:sql:{tdSql.sql}, "
C
cpwu 已提交
1208
                                    f"result data:{tdSql.getData(i, 0)}, expect data:{pre_data}, "
C
cpwu 已提交
1209 1210
                                    f"actual deviation:{devia} > expect deviation: 0.01")

C
cpwu 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
            # if "group" in condition:
            #     tdSql.query(self.apercentile_query_form(
            #         col=col, p=pi, com=com, algo='"default"', alias=alias, table_expr=table_expr, condition=condition
            #     ))
            #     query_result = tdSql.queryResult
            #     query_rows = tdSql.queryRows
            #     tdSql.query(self.apercentile_query_form(
            #         col=col, p=pi, com=com, algo='"t-digest"', alias=alias, table_expr=table_expr, condition=condition
            #     ))
            #     for i in range(query_rows):
            #         if abs(tdSql.getData(i, 0)) >= (spread_num*0.02):
            #             tdSql.checkDeviaRation(i, 0, query_result[i][0], 0.1)
            #         else:
            #             devia = abs((tdSql.getData(i, 0) - query_result[i][0]) / (spread_num * 0.02))
            #             if devia < 0.5:
            #                 tdLog.info(f"sql:{tdSql.sql}, result data:{tdSql.getData(i, 0)}, expect data:{tdSql.queryResult[i][0]}, "
            #                                f"actual deviation:{devia} <= expect deviation: 0.01")
            #             else:
            #                 tdLog.exit(
            #                         f"[{inspect.getframeinfo(inspect.stack()[1][0]).lineno}],check failed:sql:{tdSql.sql}, "
            #                         f"result data:{tdSql.getData(i, 0)}, expect data:{tdSql.queryResult[i][0]}, "
            #                         f"actual deviation:{devia} > expect deviation: 0.01")

1234
            else:
C
cpwu 已提交
1235 1236 1237 1238 1239 1240 1241 1242
                if ',' in alias or not alias:
                    tdSql.query(f"select {col} from {table_expr} {condition}")
                elif "stb" not in table_expr:
                    tdSql.query(f"select percentile({col}, {pi}) {alias} from {table_expr} {condition}")
                else:
                    tdSql.query(self.apercentile_query_form(
                            col=col, p=pi, com=com, algo='"default"', alias=alias, table_expr=table_expr, condition=condition
                        ))
1243 1244 1245 1246 1247
                query_result = np.array(tdSql.queryResult)[np.array(tdSql.queryResult) != None]
                tdSql.query(self.apercentile_query_form(
                    col=col, p=pi, com=com, algo=algo, alias=alias, table_expr=table_expr, condition=condition
                ))

C
cpwu 已提交
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
                if abs(tdSql.getData(0, 0)) >= (spread_num * 0.02):
                    tdSql.checkDeviaRation(0, 0, np.percentile(query_result, pi), 0.1)
                else:
                    devia = abs((tdSql.getData(0, 0) - np.percentile(query_result, pi)) / (spread_num * 0.02))
                    if devia < 0.5:
                        tdLog.info(
                            f"sql:{tdSql.sql}, result data:{tdSql.getData(0, 0)}, expect data:{np.percentile(query_result, pi)}, "
                            f"actual deviation:{devia} <= expect deviation: 0.01")
                    else:
                        tdLog.exit(
                            f"[{inspect.getframeinfo(inspect.stack()[1][0]).lineno}],check failed:sql:{tdSql.sql}, "
                            f"result data:{tdSql.getData(0, 0)}, expect data:{np.percentile(query_result, pi)}, "
                            f"actual deviation:{devia} > expect deviation: 0.01")

1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302

    def apercentile_query(self):

        # table schema :ts timestamp, c1 int, c2 float, c3 timestamp, c4 binary(16), c5 double, c6 bool
        #                 c7 bigint, c8 smallint, c9 tinyint, c10 nchar(16)

        # case1: int col
        self.checkapert()
        # case2: float col
        case2 = {'col':'c2'}
        self.checkapert(**case2)
        # case3: double col
        case3 = {'col':'c5'}
        self.checkapert(**case3)
        # case4: bigint col
        case4 = {'col':'c7'}
        self.checkapert(**case4)
        # case5: smallint col
        case5 = {'col':'c8'}
        self.checkapert(**case5)
        # case6: tinyint col
        case6 = {'col':'c9'}
        self.checkapert(**case6)
        # case7: stable
        case7 = {'table_expr':'stb1'}
        self.checkapert(**case7)
        # case8: nest query, outquery
        case8 = {'table_expr':'(select c1 from t1)'}
        self.checkapert(**case8)
        # case9: nest query, inquery and out query
        case9 = {'table_expr':'(select apercentile(c1, 0) as c1 from t1)'}
        self.checkapert(**case9)

        # case10: nest query, inquery
        tdSql.query("select * from (select  c1 from stb1)")
        if tdSql.queryRows == 0:
            tdSql.query("select * from (select apercentile(c1,0) c1 from stb1)")
            tdSql.checkRows(0)
        else:
            query_result = np.array(tdSql.queryResult)[np.array(tdSql.queryResult) != None]
            tdSql.query("select * from (select apercentile(c1, 0) c1 from stb1)")
C
cpwu 已提交
1303
            tdSql.checkDeviaRation(0, 0, np.percentile(query_result, 0), 0.1)
1304
            tdSql.query("select * from (select apercentile(c1,100) c1 from stb1)")
C
cpwu 已提交
1305 1306 1307
            tdSql.checkDeviaRation(0, 0, np.percentile(query_result, 100), 0.1)
            tdSql.query("select * from (select apercentile(c1,40) c1 from stb1)")
            tdSql.checkDeviaRation(0, 0, np.percentile(query_result, 40), 0.1)
1308 1309 1310 1311 1312

        # case11: no algorithm = algo:0
        case11 = {'com':'', 'algo': ''}
        self.checkapert(**case11)

C
cpwu 已提交
1313
        # case12~14: p: bin/oct/hex
1314 1315
        case12 = {'p': 0b1100100}
        self.checkapert(**case12)
C
cpwu 已提交
1316
        case13 = {'algo':'"T-DIGEST"'}
1317
        self.checkapert(**case13)
C
cpwu 已提交
1318
        case14 = {'p':0x32, 'algo':'"DEFAULT"'}
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
        self.checkapert(**case14)

        # case15~21: mix with aggregate function
        case15 = {'alias':', count(*)'}
        self.checkapert(**case15)
        case16 = {'alias':', avg(c1)'}
        self.checkapert(**case16)
        case17 = {'alias':', twa(c1)'}
        self.checkapert(**case17)
        case18 = {'alias':', irate(c1)'}
        self.checkapert(**case18)
        case19 = {'alias':', sum(c1)'}
        self.checkapert(**case19)
        case20 = {'alias':', stddev(c1)'}
        self.checkapert(**case20)
        case21 = {'alias':', leastsquares(c1, 1, 1)'}
        self.checkapert(**case21)

        # case22~27:mix with selector function
        case22 = {'alias':', min(c1)'}
        self.checkapert(**case22)
        case23 = {'alias':', max(c1)'}
        self.checkapert(**case23)
        case24 = {'alias':', first(c1)'}
        self.checkapert(**case24)
        case25 = {'alias':', last(c1)'}
        self.checkapert(**case25)
        case26 = {'alias':', percentile(c1, 0)'}
        self.checkapert(**case26)
C
cpwu 已提交
1348
        case27 = {'alias':', apercentile(c1, 0, "t-digest")'}
1349 1350
        self.checkapert(**case27)

1351
        # case28~29: mix with computing function
1352 1353 1354 1355 1356 1357
        case28 = {'alias':', spread(c1)'}
        self.checkapert(**case28)
        # case29: mix with four operation
        case29 = {'alias':'+ spread(c1)'}
        self.checkapert(**case29)

1358
        # case30~36: with condition
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
        case30 = {'condition':'where ts > now'}
        self.checkapert(**case30)
        case31 = {'condition':'where c1 between 1 and 200'}
        self.checkapert(**case31)
        case32 = {'condition':f'where c1 in {tuple(i for i in range(200))}'}
        self.checkapert(**case32)
        case33 = {'condition':'where c1>100 and c2<100'}
        self.checkapert(**case33)
        case34 = {'condition':'where c1 is not null'}
        self.checkapert(**case34)
        case35 = {'condition':'where c4 like "_inary%"'}
        self.checkapert(**case35)
        case36 = {'table_expr':'stb1' ,'condition':'where tbname like "t_"'}
        self.checkapert(**case36)

        # case37~38: with join
        case37 = {'col':'t1.c1','table_expr':'t1, t2 ','condition':'where t1.ts=t2.ts'}
        self.checkapert(**case37)
        case38 = {'col':'stb1.c1', 'table_expr':'stb1, stb2', 'condition':'where stb1.ts=stb2.ts and stb1.st1=stb2.st2'}
C
cpwu 已提交
1378
        self.checkapert(**case38)
1379 1380

        # case39: with group by
1381 1382
        case39 = {'table_expr':'stb1', 'condition':'group by tbname'}
        self.checkapert(**case39)
1383 1384

        # case40: with slimit
1385 1386
        case40 = {'table_expr':'stb1', 'condition':'group by tbname slimit 1'}
        self.checkapert(**case40)
1387

1388 1389 1390
        # case41: with soffset
        case41 = {'table_expr':'stb1', 'condition':'group by tbname slimit 1 soffset 1'}
        self.checkapert(**case41)
1391 1392 1393 1394 1395 1396 1397

        # case42: with order by
        case42 = {'table_expr':'stb1' ,'condition':'order by ts'}
        self.checkapert(**case42)
        case43 = {'table_expr':'t1' ,'condition':'order by ts'}
        self.checkapert(**case43)

1398 1399 1400 1401 1402
        # case44: with limit offset
        case44 = {'table_expr':'stb1', 'condition':'group by tbname limit 1'}
        self.checkapert(**case44)
        case45 = {'table_expr':'stb1', 'condition':'group by tbname limit 1 offset 1'}
        self.checkapert(**case45)
1403 1404 1405

        pass

1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
    def error_apercentile(self):

        # unusual test
        #
        # table schema :ts timestamp, c1 int, c2 float, c3 timestamp, c4 binary(16), c5 double, c6 bool
        #                 c7 bigint, c8 smallint, c9 tinyint, c10 nchar(16)
        #
        # form test
        tdSql.error(self.apercentile_query_form(col="",com='',algo='')) # no col , no algorithm
        tdSql.error(self.apercentile_query_form(col=""))                # no col , algorithm
        tdSql.error(self.apercentile_query_form(p='',com='',algo=''))   # no p , no algorithm
C
cpwu 已提交
1417
        tdSql.error(self.apercentile_query_form(p=''))                  # no p , algorithm
1418 1419 1420 1421 1422 1423 1424 1425 1426
        tdSql.error("apercentile( c1, 100) from t1")                    # no select
        tdSql.error("select apercentile from t1")                       # no algorithm condition
        tdSql.error("select apercentile c1,0 from t1")                  # no brackets
        tdSql.error("select apercentile (c1,0)  t1")                    # no from
        tdSql.error(self.apercentile_query_form(col='(c1,0)',p='',com='',algo=''))   # no p , no algorithm
        tdSql.error("select apercentile( (c1,0) )  from t1")            # no table_expr
        tdSql.error("select apercentile{ (c1,0) } from t1")             # sql form error 1
        tdSql.error("select apercentile[ (c1,0) ] from t1")             # sql form error 2
        tdSql.error("select [apercentile(c1,0) ] from t1")              # sql form error 3
C
cpwu 已提交
1427 1428 1429 1430 1431 1432
        tdSql.error("select apercentile((c1, 0), 'default')  from t1")          # sql form error 5
        tdSql.error("select apercentile(c1, (0, 'default'))  from t1")          # sql form error 6
        tdSql.error("select apercentile(c1, (0), 1)  from t1")          # sql form error 7
        tdSql.error("select apercentile([c1, 0], 'default')  from t1")          # sql form error 8
        tdSql.error("select apercentile(c1, [0, 'default'])  from t1")          # sql form error 9
        tdSql.error("select apercentile(c1, {0, 'default'})  from t1")          # sql form error 10
1433 1434 1435
        tdSql.error("select apercentile([c1, 0])  from t1")             # sql form error 11
        tdSql.error("select apercentile({c1, 0})  from t1")             # sql form error 12
        tdSql.error("select apercentile(c1)  from t1")                  # agrs: 1
C
cpwu 已提交
1436 1437
        tdSql.error("select apercentile(c1, 0, 'default', 0)  from t1")         # agrs: 4
        tdSql.error("select apercentile(c1, 0, 0, 'default')  from t1")         # agrs: 4
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
        tdSql.error("select apercentile()  from t1")                    # agrs: null 1
        tdSql.error("select apercentile  from t1")                      # agrs: null 2
        tdSql.error("select apercentile( , , )  from t1")               # agrs: null 3
        tdSql.error(self.apercentile_query_form(col='', p='', algo='')) # agrs: null 4
        tdSql.error(self.apercentile_query_form(col="st1"))              # col:tag column
        tdSql.error(self.apercentile_query_form(col=123))               # col:numerical
        tdSql.error(self.apercentile_query_form(col=True))              # col:bool
        tdSql.error(self.apercentile_query_form(col=''))                # col:''
        tdSql.error(self.apercentile_query_form(col="last(c1)"))        # col:expr
        tdSql.error(self.apercentile_query_form(col="t%"))              # col:non-numerical
        tdSql.error(self.apercentile_query_form(col="c3"))              # col-type: timestamp
        tdSql.error(self.apercentile_query_form(col="c4"))              # col-type: binary
        tdSql.error(self.apercentile_query_form(col="c6"))              # col-type: bool
        tdSql.error(self.apercentile_query_form(col="c10"))             # col-type: nchar
C
cpwu 已提交
1452
        tdSql.error(self.apercentile_query_form(p=True))                # p:bool
1453 1454 1455
        tdSql.error(self.apercentile_query_form(p='a'))                 # p:str
        tdSql.error(self.apercentile_query_form(p='last(*)'))           # p:expr
        tdSql.error(self.apercentile_query_form(p="2021-08-01 00:00:00.000"))   # p:timestamp
C
cpwu 已提交
1456 1457 1458 1459 1460 1461
        tdSql.error(self.apercentile_query_form(algo='t-digest'))    # algorithm:str
        tdSql.error(self.apercentile_query_form(algo='"t_digest"'))    # algorithm:str
        tdSql.error(self.apercentile_query_form(algo='"t-digest0"'))    # algorithm:str
        tdSql.error(self.apercentile_query_form(algo='"t-digest."'))    # algorithm:str
        tdSql.error(self.apercentile_query_form(algo='"t-digest%"'))    # algorithm:str
        tdSql.error(self.apercentile_query_form(algo='"t-digest*"'))    # algorithm:str
1462 1463
        tdSql.error(self.apercentile_query_form(algo='tdigest'))        # algorithm:str
        tdSql.error(self.apercentile_query_form(algo=2.0))              # algorithm:float
C
cpwu 已提交
1464 1465
        tdSql.error(self.apercentile_query_form(algo=1.9999))         # algorithm:float
        tdSql.error(self.apercentile_query_form(algo=-0.9999))        # algorithm:float
1466
        tdSql.error(self.apercentile_query_form(algo=-1.0))             # algorithm:float
C
cpwu 已提交
1467 1468 1469 1470 1471
        tdSql.error(self.apercentile_query_form(algo=0b1))            # algorithm:float
        tdSql.error(self.apercentile_query_form(algo=0x1))            # algorithm:float
        tdSql.error(self.apercentile_query_form(algo=0o1))            # algorithm:float
        tdSql.error(self.apercentile_query_form(algo=True))           # algorithm:bool
        tdSql.error(self.apercentile_query_form(algo="True"))         # algorithm:bool
1472 1473 1474 1475 1476
        tdSql.error(self.apercentile_query_form(algo='2021-08-01 00:00:00.000'))     # algorithm:timestamp
        tdSql.error(self.apercentile_query_form(algo='last(c1)'))       # algorithm:expr

        # boundary test
        tdSql.error(self.apercentile_query_form(p=-1))                  # p left out of [0, 100]
C
cpwu 已提交
1477
        tdSql.error(self.apercentile_query_form(p=-9223372036854775809))    # p left out of bigint
1478
        tdSql.error(self.apercentile_query_form(p=100.1))               # p right out of [0, 100]
C
cpwu 已提交
1479
        tdSql.error(self.apercentile_query_form(p=18446744073709551616))    # p right out of unsigned-bigint
1480
        tdSql.error(self.apercentile_query_form(algo=-1))           # algorithm left out of [0, 1]
C
cpwu 已提交
1481
        tdSql.error(self.apercentile_query_form(algo=-9223372036854775809)) # algorithm left out of unsigned-bigint
1482
        tdSql.error(self.apercentile_query_form(algo=2))            # algorithm right out of [0, 1]
C
cpwu 已提交
1483
        tdSql.error(self.apercentile_query_form(algo=18446744073709551616)) # algorithm right out of unsigned-bigint
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496

        # mix function test
        tdSql.error(self.apercentile_query_form(alias=', top(c1,1)'))   # mix with top function
        tdSql.error(self.apercentile_query_form(alias=', top(c1,1)'))   # mix with bottom function
        tdSql.error(self.apercentile_query_form(alias=', last_row(c1)'))    # mix with last_row function
        tdSql.error(self.apercentile_query_form(alias=', distinct c1 '))    # mix with distinct function
        tdSql.error(self.apercentile_query_form(alias=', *'))           # mix with  *
        tdSql.error(self.apercentile_query_form(alias=', diff(c1)'))    # mix with  diff function
        tdSql.error(self.apercentile_query_form(alias=', interp(c1)', condition='ts="2021-10-10 00:00:00.000"'))    # mix with  interp function
        tdSql.error(self.apercentile_query_form(alias=', derivative(c1, 10m, 0)'))  # mix with derivative function
        tdSql.error(self.apercentile_query_form(alias=', diff(c1)'))    # mix with  diff function
        tdSql.error(self.apercentile_query_form(alias='+ c1)'))     # mix with  four operation

C
cpwu 已提交
1497 1498 1499
    def apercentile_data(self, tbnum, data_row, basetime):
        for i in range(tbnum):
            for j in range(data_row):
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
                tdSql.execute(
                    f"insert into t{i} values ("
                    f"{basetime + j*10}, {random.randint(-200, -1)}, {random.uniform(200, -1)}, {basetime + random.randint(-200, -1)}, "
                    f"'binary_{j}', {random.uniform(-200, -1)}, {random.choice([0,1])}, {random.randint(-200,-1)}, "
                    f"{random.randint(-200, -1)}, {random.randint(-127, -1)}, 'nchar_{j}' )"
                )

                tdSql.execute(
                    f"insert into t{i} values ("
                    f"{basetime - (j+1) * 10}, {random.randint(1, 200)}, {random.uniform(1, 200)}, {basetime - random.randint(1, 200)}, "
                    f"'binary_{j}_1', {random.uniform(1, 200)}, {random.choice([0, 1])}, {random.randint(1,200)}, "
                    f"{random.randint(1,200)}, {random.randint(1,127)}, 'nchar_{j}_1' )"
                )
                tdSql.execute(
                    f"insert into tt{i} values ( {basetime-(j+1) * 10}, {random.randint(1, 200)} )"
                )
1516 1517 1518

        pass

1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
    def td6108(self):
        tdLog.printNoPrefix("==========TD-6108==========")
        tdSql.execute("drop database if exists db")
        tdSql.execute("create database  if not exists db keep 3650")
        tdSql.execute("use db")

        tdSql.execute(
            "create stable db.stb1 (\
                ts timestamp, c1 int, c2 float, c3 timestamp, c4 binary(16), c5 double, c6 bool, \
                c7 bigint, c8 smallint, c9 tinyint, c10 nchar(16)\
                ) \
            tags(st1 int)"
        )
        tdSql.execute(
            "create stable db.stb2 (ts timestamp, c1 int) tags(st2 int)"
        )
C
cpwu 已提交
1535 1536
        tbnum = 10
        for i in range(tbnum):
1537 1538 1539 1540 1541 1542 1543 1544 1545
            tdSql.execute(f"create table t{i} using stb1 tags({i})")
            tdSql.execute(f"create table tt{i} using stb2 tags({i})")

        tdLog.printNoPrefix("######## no data test:")
        self.apercentile_query()
        self.error_apercentile()

        tdLog.printNoPrefix("######## insert data test:")
        nowtime = int(round(time.time() * 1000))
C
cpwu 已提交
1546
        per_table_rows = 1000
C
cpwu 已提交
1547
        self.apercentile_data(tbnum, per_table_rows, nowtime)
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
        self.apercentile_query()
        self.error_apercentile()

        tdLog.printNoPrefix("######## insert data with NULL test:")
        tdSql.execute(f"insert into t1(ts) values ({nowtime-5})")
        tdSql.execute(f"insert into t1(ts) values ({nowtime+5})")
        self.apercentile_query()
        self.error_apercentile()

        tdLog.printNoPrefix("######## check after WAL test:")
        tdSql.query("show dnodes")
        index = tdSql.getData(0, 0)
        tdDnodes.stop(index)
        tdDnodes.start(index)

        self.apercentile_query()
        self.error_apercentile()

1566

C
cpwu 已提交
1567 1568
    def run(self):

W
wu champion 已提交
1569
        self.td4097()
S
Shengliang Guan 已提交
1570

C
cpwu 已提交
1571
        # master branch
C
cpwu 已提交
1572 1573 1574
        self.td3690()
        self.td4082()
        self.td4288()
W
wu champion 已提交
1575 1576
        self.td4724()
        self.td5935()
1577
        self.td6068()
C
cpwu 已提交
1578

C
cpwu 已提交
1579 1580
        # self.td5168()
        # self.td5433()
W
wu champion 已提交
1581
        # self.td5798()
C
cpwu 已提交
1582

D
dapan1121 已提交
1583
        # develop branch
L
lichuang 已提交
1584
        #self.td4889()
D
dapan1121 已提交
1585 1586
        self.td5798()

C
cpwu 已提交
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
    def stop(self):
        tdSql.close()
        tdLog.success(f"{__file__} successfully executed")


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