alter_table.py 5.9 KB
Newer Older
sangshuduo's avatar
sangshuduo 已提交
1 2 3 4 5 6 7 8 9 10 11
# -*- coding: utf-8 -*-

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


class TDTestCase:
S
Shuduo Sang 已提交
12
    def init(self, conn, logSql):
sangshuduo's avatar
sangshuduo 已提交
13
        tdLog.debug("start to execute %s" % __file__)
S
Shuduo Sang 已提交
14
        tdSql.init(conn.cursor(), logSql)
sangshuduo's avatar
sangshuduo 已提交
15 16 17 18 19 20 21
        self.types = [
            "int",
            "bigint",
            "float",
            "double",
            "smallint",
            "tinyint",
L
liuyq-617 已提交
22 23 24 25
            "int unsigned",
            "bigint unsigned",
            "smallint unsigned",
            "tinyint unsigned",
sangshuduo's avatar
sangshuduo 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
            "binary(10)",
            "nchar(10)",
            "timestamp"]
        self.rowNum = 300
        self.ts = 1537146000000
        self.step = 1000
        self.sqlHead = "select count(*), count(c1) "
        self.sqlTail = " from tb"

    def addColumnAndCount(self):
        for colIdx in range(len(self.types)):
            tdSql.execute(
                "alter table tb add column c%d %s" %
                (colIdx + 2, self.types[colIdx]))
            self.sqlHead = self.sqlHead + ",count(c%d) " % (colIdx + 2)
            tdSql.query(self.sqlHead + self.sqlTail)

            # count non-NULL values in each column
            tdSql.checkData(0, 0, self.rowNum * (colIdx + 1))
            tdSql.checkData(0, 1, self.rowNum * (colIdx + 1))
            for i in range(2, colIdx + 2):
                print("check1: i=%d colIdx=%d" % (i, colIdx))
                tdSql.checkData(0, i, self.rowNum * (colIdx - i + 2))

            # insert more rows
            for k in range(self.rowNum):
                self.ts += self.step
                sql = "insert into tb values (%d, %d" % (self.ts, colIdx + 2)
                for j in range(colIdx + 1):
                    sql += ", %d" % (colIdx + 2)
                sql += ")"
                tdSql.execute(sql)

            # count non-NULL values in each column
            tdSql.query(self.sqlHead + self.sqlTail)
            tdSql.checkData(0, 0, self.rowNum * (colIdx + 2))
            tdSql.checkData(0, 1, self.rowNum * (colIdx + 2))
            for i in range(2, colIdx + 2):
                print("check2: i=%d colIdx=%d" % (i, colIdx))
                tdSql.checkData(0, i, self.rowNum * (colIdx - i + 3))

    def dropColumnAndCount(self):

        tdSql.query(self.sqlHead + self.sqlTail)
        res = []
        for i in range(len(self.types)):
            res[i] = tdSql.getData(0, i + 2)

        print(res.join)

        for colIdx in range(len(self.types), 0, -1):
            tdSql.execute("alter table tb drop column c%d" % (colIdx + 2))
            # self.sqlHead = self.sqlHead + ",count(c%d) " %(colIdx + 2)
            tdSql.query(self.sqlHead + self.sqlTail)

            # count non-NULL values in each column
            tdSql.checkData(0, 0, self.rowNum * (colIdx + 1))
            tdSql.checkData(0, 1, self.rowNum * (colIdx + 1))
            for i in range(2, colIdx + 2):
                print("check1: i=%d colIdx=%d" % (i, colIdx))
                tdSql.checkData(0, i, self.rowNum * (colIdx - i + 2))

            # insert more rows
            for k in range(self.rowNum):
                self.ts += self.step
                sql = "insert into tb values (%d, %d" % (self.ts, colIdx + 2)
                for j in range(colIdx + 1):
                    sql += ", %d" % (colIdx + 2)
                sql += ")"
                tdSql.execute(sql)

            # count non-NULL values in each column
            tdSql.query(self.sqlHead + self.sqlTail)
            tdSql.checkData(0, 0, self.rowNum * (colIdx + 2))
            tdSql.checkData(0, 1, self.rowNum * (colIdx + 2))
            for i in range(2, colIdx + 2):
                print("check2: i=%d colIdx=%d" % (i, colIdx))
                tdSql.checkData(0, i, self.rowNum * (colIdx - i + 3))

L
liuyq-617 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118
    def alter_table_255_times(self):   # add case for TD-6207
        for i in range(255):
            tdLog.info("alter table st add column cb%d int"%i)
            tdSql.execute("alter table st add column cb%d int"%i)
            tdSql.execute("insert into t0 (ts,c1) values(now,1)")
            tdSql.execute("reset query cache")
            tdSql.query("select * from st")
        tdSql.execute("create table mt(ts timestamp, i int)")
        tdSql.execute("insert into mt values(now,11)")
        tdSql.query("select * from mt")
        tdDnodes.stop(1)        
        tdDnodes.start(1) 
        tdSql.query("describe db.st")

sangshuduo's avatar
sangshuduo 已提交
119 120 121 122 123 124 125
    def run(self):
        # Setup params
        db = "db"

        # Create db
        tdSql.execute("drop database if exists %s" % (db))
        tdSql.execute("reset query cache")
P
Ping Xiao 已提交
126
        tdSql.execute("create database %s maxrows 200" % (db))
sangshuduo's avatar
sangshuduo 已提交
127 128 129
        tdSql.execute("use %s" % (db))

        # Create a table with one colunm of int type and insert 300 rows
P
Ping Xiao 已提交
130
        tdLog.info("create table tb")
sangshuduo's avatar
sangshuduo 已提交
131 132 133 134 135 136 137 138 139
        tdSql.execute("create table tb (ts timestamp, c1 int)")
        tdLog.info("Insert %d rows into tb" % (self.rowNum))
        for k in range(1, self.rowNum + 1):
            self.ts += self.step
            tdSql.execute("insert into tb values (%d, 1)" % (self.ts))

        # Alter tb and add a column of smallint type, then query tb to see if
        # all added column are NULL
        self.addColumnAndCount()
P
Ping Xiao 已提交
140 141
        tdDnodes.stop(1)        
        tdDnodes.start(1)        
sangshuduo's avatar
sangshuduo 已提交
142
        tdSql.query(self.sqlHead + self.sqlTail)
P
Ping Xiao 已提交
143 144 145
        size = len(self.types) + 2
        for i in range(2, size):             
            tdSql.checkData(0, i, self.rowNum * (size - i))
sangshuduo's avatar
sangshuduo 已提交
146

147

L
liuyq-617 已提交
148 149
        tdSql.execute("create table st(ts timestamp, c1 int) tags(t1 float,t2 int,t3 double)")
        tdSql.execute("create table t0 using st tags(null,1,2.3)")
P
Ping Xiao 已提交
150
        tdSql.execute("alter table t0 set tag t1=2.1")
sangshuduo's avatar
sangshuduo 已提交
151

P
Ping Xiao 已提交
152
        tdSql.query("show tables")
P
Ping Xiao 已提交
153
        tdSql.checkRows(2)
L
liuyq-617 已提交
154 155
        self.alter_table_255_times()

P
Ping Xiao 已提交
156
        
sangshuduo's avatar
sangshuduo 已提交
157 158 159 160 161
    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)

tdCases.addLinux(__file__, TDTestCase())