modify_column.py 14.4 KB
Newer Older
H
happyguoxy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 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
###################################################################
#           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 datetime

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


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

    def run(self):
        tdSql.prepare()
        # test case for https://jira.taosdata.com:18080/browse/TD-4541

        tdLog.info("=============== step1,check normal table")    

        tdLog.info("=============== step1.1,drop table && create table")    
        cmd1 = 'drop table if exists length11 ;'
        cmd2 = 'create table length11 (ts timestamp,lengthbia binary(10),lengthnchar nchar(20));'
        tdLog.info(cmd1)
        tdSql.execute(cmd1)
        tdLog.info(cmd2)
        tdSql.execute(cmd2)

        tdLog.info("=============== step1.2,insert table right data") 
        cmd1 = 'insert into length11 values(now,\'aaaaaaaaaa\',\'bbbbbbbbbbbbbbbbbbbb\') ;'
        tdLog.info(cmd1)
        tdSql.execute(cmd1)         
        tdSql.query('select * from length11 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb')

        tdLog.info("=============== step1.3,insert table wrong data") 
        cmd1 = 'insert into length11 values(now,\'aaaaaaaaaa1\',\'bbbbbbbbbbbbbbbbbbbb1\') ;'
        tdLog.info(cmd1)
        tdSql.error(cmd1)
        try:
            tdSql.execute(cmd1)
            tdLog.exit("string data overflow")
        except Exception as e:
            tdLog.info(repr(e))
            tdLog.info("insert wrong data error catched")      
        tdSql.query('select * from length11 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb')

        tdLog.info("=============== step1.4,modify columu length ") 
        cmd1 = 'alter table length11 modify column lengthbia binary(10) ;'
        tdLog.info(cmd1)
        tdSql.error(cmd1)
        try:
            tdSql.execute(cmd1)
            tdLog.exit("new column length should be bigger than old one")
        except Exception as e:
            tdLog.info(repr(e))
            tdLog.info("new column length should be bigger than old one")   

        cmd2 = 'alter table length11 modify column lengthnchar nchar(20);'
        tdLog.info(cmd2)
        tdSql.error(cmd2)
        try:
            tdSql.execute(cmd2)
            tdLog.exit("new column length should be bigger than old one")
        except Exception as e:
            tdLog.info(repr(e))
            tdLog.info("new column length should be bigger than old one") 

        cmd3 = 'alter table length11 modify column lengthbia binary(11) ;'
        cmd4 = 'describe length11 ;'
        tdLog.info(cmd3)
        tdSql.execute(cmd3) 
        tdLog.info(cmd4)
        tdSql.execute(cmd4) 
        tdSql.query('describe length11 ;')
        tdSql.checkData(1,2,11) 

        cmd5 = 'alter table length11 modify column lengthnchar nchar(21);'
        cmd6 = 'describe length11 ;'
        tdLog.info(cmd5)
        tdSql.execute(cmd5) 
        tdLog.info(cmd6)
        tdSql.execute(cmd6) 
        tdSql.query('describe length11 ;')
        tdSql.checkData(2,2,21) 

        tdSql.query('select * from length11 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb')


        tdLog.info("=============== step1.5,insert table right data") 
        cmd1 = 'insert into length11 values(now,\'aaaaaaaaaa1\',\'bbbbbbbbbbbbbbbbbbbb1\') ;'
        tdLog.info(cmd1)    
        tdSql.execute(cmd1)  
        tdSql.query('select * from length11 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa1') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb1')




        tdLog.info("=============== step2,check stable table and tag")    

        tdLog.info("=============== step2.1,drop table && create table")    
        cmd1 = 'drop table if exists length1 ;'
        cmd2 = 'drop table if exists length2 ;'
        cmd3 = 'drop table if exists length2 ;'
        cmd4 = 'drop table if exists lengthsta1 ;'
        cmd5 = 'create stable lengthsta1(ts timestamp,lengthbia binary(10),lengthnchar nchar(20)) tags (tlengthbia binary(15),tlengthnchar nchar(25)) ;'
        cmd6 = 'create table length1 using lengthsta1 tags(\'aaaaabbbbbaaaaa\',\'bbbbbaaaaabbbbbaaaaabbbbb\') ; '
        tdLog.info(cmd1)
        tdSql.execute(cmd1)
        tdLog.info(cmd2)
        tdSql.execute(cmd2)
        tdLog.info(cmd3)
        tdSql.execute(cmd3)
        tdLog.info(cmd4)
        tdSql.execute(cmd4)
        tdLog.info(cmd5)
        tdSql.execute(cmd5)
        tdLog.info(cmd6)
        tdSql.execute(cmd6)

        tdLog.info("=============== step2.2,insert table right data") 
        cmd1 = 'insert into length1 values(now,\'aaaaaaaaaa\',\'bbbbbbbbbbbbbbbbbbbb\') ;'
        tdLog.info(cmd1)
        tdSql.execute(cmd1)         
        tdSql.query('select * from length1 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb')

        tdLog.info("=============== step2.3,insert table wrong data") 
        cmd1 = 'insert into length1 values(now,\'aaaaaaaaaa1\',\'bbbbbbbbbbbbbbbbbbbb1\') ;'
        tdLog.info(cmd1)
        tdSql.error(cmd1)
        try:
            tdSql.execute(cmd1)
            tdLog.exit("string data overflow")
        except Exception as e:
            tdLog.info(repr(e))
            tdLog.info("insert wrong data error catched")      
        tdSql.query('select * from length1 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb')

        tdLog.info("=============== step2.4,modify columu length ") 
        cmd0 = 'alter table length1 modify column lengthbia binary(10) ;'
        tdLog.info(cmd0)
        tdSql.error(cmd0)
        try:
            tdSql.execute(cmd1)
            tdLog.exit("invalid operation: column can only be modified by super table")
        except Exception as e:
            tdLog.info(repr(e))
            tdLog.info("invalid operation: column can only be modified by super table")  

        cmd1 = 'alter table lengthsta1 modify column lengthbia binary(10) ;'
        tdLog.info(cmd1)
        tdSql.error(cmd1)
        try:
            tdSql.execute(cmd1)
            tdLog.exit("new column length should be bigger than old one")
        except Exception as e:
            tdLog.info(repr(e))
            tdLog.info("new column length should be bigger than old one")   

        cmd2 = 'alter table lengthsta1 modify column lengthnchar nchar(20);'
        tdLog.info(cmd2)
        tdSql.error(cmd2)
        try:
            tdSql.execute(cmd2)
            tdLog.exit("new column length should be bigger than old one")
        except Exception as e:
            tdLog.info(repr(e))
            tdLog.info("new column length should be bigger than old one") 

        cmd3 = 'alter table lengthsta1 modify column lengthbia binary(11) ;'
        cmd4 = 'describe lengthsta1 ;'
        tdLog.info(cmd3)
        tdSql.execute(cmd3) 
        tdLog.info(cmd4)
        tdSql.execute(cmd4) 
        tdSql.query('describe length1 ;')
        tdSql.checkData(1,2,11) 

        cmd5 = 'alter table lengthsta1 modify column lengthnchar nchar(21);'
        cmd6 = 'describe lengthsta1 ;'
        tdLog.info(cmd5)
        tdSql.execute(cmd5) 
        tdLog.info(cmd6)
        tdSql.execute(cmd6) 
        tdSql.query('describe lengthsta1 ;')
        tdSql.checkData(2,2,21) 

        tdSql.query('select * from lengthsta1 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb')


        tdLog.info("=============== step2.5,insert table right data") 
        cmd1 = 'insert into length1 values(now,\'aaaaaaaaaa1\',\'bbbbbbbbbbbbbbbbbbbb1\') ;'
        tdLog.info(cmd1)    
        tdSql.execute(cmd1)  
        tdSql.query('select * from length1 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa1') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb1')



        tdLog.info("=============== step2.6,create table wrong tag") 
        cmd1 = 'create table length2 using lengthsta1 tags(\'aaaaabbbbbaaaaa1\',\'bbbbbaaaaabbbbbaaaaabbbbb1\')  ;'
        tdLog.info(cmd1)
        tdSql.error(cmd1)
        try:
            tdSql.execute(cmd1)
            tdLog.exit("invalid operation: tag value too long")
        except Exception as e:
            tdLog.info(repr(e))
            tdLog.info("invalid operation: tag value too long")      
        tdSql.query('select * from lengthsta1 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa1') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb1')

        tdLog.info("=============== step2.7,modify tag columu length ")  
        cmd1 = 'alter table lengthsta1 modify tag tlengthbia binary(15) ;'
        tdLog.info(cmd1)
        tdSql.error(cmd1)
        try:
            tdSql.execute(cmd1)
            tdLog.exit("new column length should be bigger than old one")
        except Exception as e:
            tdLog.info(repr(e))
            tdLog.info("new column length should be bigger than old one")   

        cmd2 = 'alter table lengthsta1 modify tag tlengthnchar nchar(25);'
        tdLog.info(cmd2)
        tdSql.error(cmd2)
        try:
            tdSql.execute(cmd2)
            tdLog.exit("new column length should be bigger than old one")
        except Exception as e:
            tdLog.info(repr(e))
            tdLog.info("new column length should be bigger than old one") 

        cmd3 = 'alter table lengthsta1 modify tag tlengthbia binary(16) ;'
        cmd4 = 'describe lengthsta1 ;'
        tdLog.info(cmd3)
        tdSql.execute(cmd3) 
        tdLog.info(cmd4)
        tdSql.execute(cmd4) 
        tdSql.query('describe lengthsta1 ;')
        tdSql.checkData(3,2,16) 

        cmd5 = 'alter table lengthsta1 modify tag tlengthnchar nchar(26);'
        cmd6 = 'describe lengthsta1 ;'
        tdLog.info(cmd5)
        tdSql.execute(cmd5) 
        tdLog.info(cmd6)
        tdSql.execute(cmd6) 
        tdSql.query('describe lengthsta1 ;')
        tdSql.checkData(4,2,26) 

        tdSql.query('select * from lengthsta1 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa1') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb1')
        tdSql.checkData(0,3,'aaaaabbbbbaaaaa') 
        tdSql.checkData(0,4,'bbbbbaaaaabbbbbaaaaabbbbb')        


        tdLog.info("=============== step2.8,creat tag right data and insert data") 
        cmd1 = 'create table length2 using lengthsta1 tags(\'aaaaabbbbbaaaaa1\',\'bbbbbaaaaabbbbbaaaaabbbbb1\')  ;'
        tdLog.info(cmd1)    
        tdSql.execute(cmd1)  
        tdSql.query('describe length2 ;')
        tdSql.checkData(3,2,16) 
        tdSql.checkData(4,2,26) 

        cmd2 = 'insert into length2 values(now,\'aaaaaaaaaa1\',\'bbbbbbbbbbbbbbbbbbbb1\') ;'
        tdLog.info(cmd2)    
        tdSql.execute(cmd2)  
        tdSql.query('select * from length2 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa1') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb1')
        tdSql.query('select * from lengthsta1 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa1') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb1')
        tdSql.checkData(0,3,'aaaaabbbbbaaaaa1') 
        tdSql.checkData(0,4,'bbbbbaaaaabbbbbaaaaabbbbb1')  


        tdLog.info("=============== step2.9,modify tag columu length again ")  
        cmd1 = 'alter table lengthsta1 modify tag tlengthbia binary(16) ;'
        tdLog.info(cmd1)
        tdSql.error(cmd1)
        try:
            tdSql.execute(cmd1)
            tdLog.exit("new column length should be bigger than old one")
        except Exception as e:
            tdLog.info(repr(e))
            tdLog.info("new column length should be bigger than old one")   

        cmd2 = 'alter table lengthsta1 modify tag tlengthnchar nchar(26);'
        tdLog.info(cmd2)
        tdSql.error(cmd2)
        try:
            tdSql.execute(cmd2)
            tdLog.exit("new column length should be bigger than old one")
        except Exception as e:
            tdLog.info(repr(e))
            tdLog.info("new column length should be bigger than old one") 

        cmd3 = 'alter table lengthsta1 modify tag tlengthbia binary(20) ;'
        cmd4 = 'describe lengthsta1 ;'
        tdLog.info(cmd3)
        tdSql.execute(cmd3) 
        tdLog.info(cmd4)
        tdSql.execute(cmd4) 
        tdSql.query('describe lengthsta1 ;')
        tdSql.checkData(3,2,20) 

        cmd5 = 'alter table lengthsta1 modify tag tlengthnchar nchar(30);'
        cmd6 = 'describe lengthsta1 ;'
        tdLog.info(cmd5)
        tdSql.execute(cmd5) 
        tdLog.info(cmd6)
        tdSql.execute(cmd6) 
        tdSql.query('describe lengthsta1 ;')
        tdSql.checkData(4,2,30) 

        tdSql.query('select * from lengthsta1 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa1') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb1')
        tdSql.checkData(0,3,'aaaaabbbbbaaaaa1') 
        tdSql.checkData(0,4,'bbbbbaaaaabbbbbaaaaabbbbb1')        


        tdLog.info("=============== step2.10,creat tag right data and insert data again") 
        cmd1 = 'create table length3 using lengthsta1 tags(\'aaaaabbbbbaaaaabbbbb\',\'bbbbbaaaaabbbbbaaaaabbbbbaaaaa\')  ;'
        tdLog.info(cmd1)    
        tdSql.execute(cmd1)  
        tdSql.query('describe length3 ;')
        tdSql.checkData(3,2,20) 
        tdSql.checkData(4,2,30) 

        cmd2 = 'insert into length3 values(now,\'aaaaaaaaaa1\',\'bbbbbbbbbbbbbbbbbbbb1\') ;'
        tdLog.info(cmd2)    
        tdSql.execute(cmd2)  
        tdSql.query('select * from length3 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa1') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb1')
        tdSql.query('select * from lengthsta1 order by ts desc')
        tdSql.checkData(0,1,'aaaaaaaaaa1') 
        tdSql.checkData(0,2,'bbbbbbbbbbbbbbbbbbbb1')
        tdSql.checkData(0,3,'aaaaabbbbbaaaaabbbbb') 
        tdSql.checkData(0,4,'bbbbbaaaaabbbbbaaaaabbbbbaaaaa')  




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


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