system sh/stop_dnodes.sh
system sh/deploy.sh -n dnode1 -i 1
system sh/exec.sh -n dnode1 -s start
sql connect

print ========== prepare stb and ctb
sql create database db vgroups 1
sql create table db.stb (ts timestamp, c1 int, c2 binary(4)) tags(t1 int, t2 binary(16)) comment "abd"
sql create table db.ctb using db.stb tags(101, "102")
sql insert into db.ctb values(now, 1, "2")

sql select * from information_schema.ins_stables where db_name = 'db'
if $rows != 1 then
  return -1
endi
if $data[0][0] != stb then
  return -1
endi
if $data[0][1] != db then
  return -1
endi
if $data[0][3] != 3 then
  return -1
endi
if $data[0][4] != 2 then
  return -1
endi
if $data[0][6] != abd then
  return -1
endi

sql select * from information_schema.ins_tables where db_name = 'db'
if $rows != 1 then
  return -1
endi
if $data[0][0] != ctb then
  return -1
endi
if $data[0][1] != db then
  return -1
endi
if $data[0][3] != 3 then
  return -1
endi
if $data[0][4] != stb then
  return -1
endi
if $data[0][6] != 2 then
  return -1
endi
if $data[0][9] != CHILD_TABLE then
  return -1
endi

sql select * from db.stb
if $rows != 1 then
  return -1
endi
if $data[0][1] != 1 then
  return -1
endi
if $data[0][2] != 2 then
  return -1
endi
if $data[0][3] != 101 then
  return -1
endi
if $data[0][4] != 102 then
  return -1
endi

sql_error alter table db.stb drop tag ts int
sql_error alter table db.stb drop tag t3 int
sql_error alter table db.stb drop tag t4 int
sql_error alter table db.stb drop tag c1 int
sql_error alter table db.stb drop tag c2 int

print ========== step1 drop tag t2
sql alter table db.stb drop tag t2

sql select * from information_schema.ins_stables where db_name = 'db'
if $data[0][4] != 1 then
  return -1
endi

sql describe db.ctb
if $rows != 4 then
  return -1
endi
if $data[4][0] != null then
  return -1
endi

sql select * from db.stb
print $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6]
if $rows != 1 then
  return -1
endi
if $data[0][1] != 1 then
  return -1
endi
if $data[0][2] != 2 then
  return -1
endi
if $data[0][3] != 101 then
  return -1
endi
if $data[0][4] != null then
  return -1
endi

print ========== step2 add tag t3
sql alter table db.stb add tag t3 int

sql select * from information_schema.ins_stables where db_name = 'db'
if $data[0][4] != 2 then
  return -1
endi

sql describe db.ctb
if $rows != 5 then
  return -1
endi
if $data[4][0] != t3 then
  return -1
endi
if $data[4][1] != INT then
  return -1
endi
if $data[4][2] != 4 then
  return -1
endi

sql select * from db.stb
print $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6]
if $rows != 1 then
  return -1
endi
if $data[0][1] != 1 then
  return -1
endi
if $data[0][2] != 2 then
  return -1
endi
if $data[0][3] != 101 then
  return -1
endi
if $data[0][4] != NULL then
  return -1
endi

print ========== step3 add tag t4
sql alter table db.stb add tag t4 bigint
sql select * from db.stb
sql select * from db.stb
print $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6]
print $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6]
print $data[2][0] $data[2][1] $data[2][2] $data[2][3] $data[2][4] $data[2][5] $data[2][6]

if $rows != 1 then
  return -1
endi
if $data[0][1] != 1 then
  return -1
endi
if $data[0][2] != 2 then
  return -1
endi
if $data[0][3] != 101 then
  return -1
endi
if $data[0][4] != NULL then
  return -1
endi
if $data[0][5] != NULL then
  return -1
endi
if $data[0][6] != null then
  return -1
endi

sql_error create table db.ctb2 using db.stb tags(101, "102")
sql create table db.ctb2 using db.stb tags(201, 202, 203)
sql insert into db.ctb2 values(now, 1, "2")

sql select * from db.stb where tbname = 'ctb2';
print $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6]
print $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6]
print $data[2][0] $data[2][1] $data[2][2] $data[2][3] $data[2][4] $data[2][5] $data[2][6]

if $rows != 1 then
  return -1
endi
if $data[0][1] != 1 then
  return -1
endi
if $data[0][2] != 2 then
  return -1
endi
if $data[0][3] != 201 then
  return -1
endi
if $data[0][4] != 202 then
  return -1
endi
if $data[0][5] != 203 then
  return -1
endi

print ========== step4 describe
sql describe db.ctb
if $rows != 6 then
  return -1
endi

print ========== step5 add tag2
sql alter table db.stb add tag t2 bigint
sql select * from db.stb where tbname = 'ctb2';
sql select * from db.stb where tbname = 'ctb2';
print $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6]
print $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6]
print $data[2][0] $data[2][1] $data[2][2] $data[2][3] $data[2][4] $data[2][5] $data[2][6]

if $rows != 1 then
  return -1
endi
if $data[0][1] != 1 then
  return -1
endi
if $data[0][2] != 2 then
  return -1
endi
if $data[0][3] != 201 then
  return -1
endi
if $data[0][4] != 202 then
  return -1
endi
if $data[0][5] != 203 then
  return -1
endi
if $data[0][6] != NULL then
  return -1
endi

sql_error create table db.ctb2 using db.stb tags(101, "102")
sql_error create table db.ctb2 using db.stb tags(201, 202, 203)
sql create table db.ctb3 using db.stb tags(301, 302, 303, 304)
sql insert into db.ctb3 values(now, 1, "2")

sql select * from db.stb where tbname = 'ctb3';
print $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6]
print $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6]
print $data[2][0] $data[2][1] $data[2][2] $data[2][3] $data[2][4] $data[2][5] $data[2][6]

if $rows != 1 then
  return -1
endi
if $data[0][1] != 1 then
  return -1
endi
if $data[0][2] != 2 then
  return -1
endi
if $data[0][3] != 301 then
  return -1
endi
if $data[0][4] != 302 then
  return -1
endi
if $data[0][5] != 303 then
  return -1
endi
if $data[0][6] != 304 then
  return -1
endi

print ========== step6 describe
sql describe db.ctb
if $rows != 7 then
  return -1
endi

if $data[3][0] != t1 then
  return -1
endi
if $data[4][0] != t3 then
  return -1
endi
if $data[5][0] != t4 then
  return -1
endi
if $data[6][0] != t2 then
  return -1
endi
if $data[6][1] != BIGINT then
  return -1
endi

print ========== step7 drop tag t1
sql alter table db.stb drop tag t1

sql select * from information_schema.ins_stables where db_name = 'db'
if $data[0][4] != 3 then
  return -1
endi

sql describe db.ctb
if $rows != 6 then
  return -1
endi

sql select * from db.stb where tbname = 'ctb3';
print $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6]
print $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6]
print $data[2][0] $data[2][1] $data[2][2] $data[2][3] $data[2][4] $data[2][5] $data[2][6]

if $rows != 1 then
  return -1
endi
if $data[0][1] != 1 then
  return -1
endi
if $data[0][2] != 2 then
  return -1
endi
if $data[0][3] != 302 then
  return -1
endi
if $data[0][4] != 303 then
  return -1
endi
if $data[0][5] != 304 then
  return -1
endi

system sh/exec.sh -n dnode1 -s stop -x SIGINT
