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(4)) comment "abd"

sql_error alter table db.stb rename tag c2 c3
sql_error alter table db.stb rename tag c2 c3
sql_error alter table db.stb rename tag c1 c3
sql_error alter table db.stb rename tag ts c3
sql_error alter table db.stb rename tag t2 t1
sql_error alter table db.stb rename tag t2 t2
sql_error alter table db.stb rename tag t1 t2
sql create table db.ctb using db.stb tags(101, "123")
sql insert into db.ctb values(now, 1, "1234")

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] != 1234 then
  return -1
endi  
if $data[0][3] != 101 then
  return -1
endi
if $data[0][4] != 123 then
  return -1
endi

print ========== step1 rename tag
sql alter table db.stb rename tag t1 t3
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]

if $rows != 1 then
  return -1
endi
if $data[0][1] != 1 then
  return -1
endi
if $data[0][2] != 1234 then
  return -1
endi  
if $data[0][3] != 101 then
  return -1
endi
if $data[0][4] != 123 then
  return -1
endi

print ========== step2 describe
sql describe db.ctb
if $rows != 5 then
  return -1
endi
if $data[0][0] != ts then
  return -1
endi
if $data[1][0] != c1 then
  return -1
endi
if $data[2][0] != c2 then
  return -1
endi
if $data[3][0] != t3 then
  return -1
endi
if $data[4][0] != t2 then
  return -1
endi
if $data[4][1] != VARCHAR then
  return -1
endi
if $data[4][2] != 4 then
  return -1
endi

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

sql connect
sql describe db.ctb
if $rows != 5 then
  return -1
endi
if $data[0][0] != ts then
  return -1
endi
if $data[1][0] != c1 then
  return -1
endi
if $data[2][0] != c2 then
  return -1
endi
if $data[3][0] != t3 then
  return -1
endi
if $data[4][0] != t2 then
  return -1
endi
if $data[4][1] != VARCHAR then
  return -1
endi
if $data[4][2] != 4 then
  return -1
endi

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