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

$dbPrefix = alt1_db

print ========== alter1.sim
$db = $dbPrefix

sql drop database if exists $db
sql create database $db
sql use $db

sql create table stb (ts timestamp, speed double, mileage double) tags(carId int, carModel int)
sql create table car1 using stb tags (1, 1)
sql create table car2 using stb tags (2, 1)
sql create table car3 using stb tags (3, 2)
sql insert into car1 values (now-1s, 100, 10000)
sql insert into car2 values (now, 100, 10000)
sql insert into car3 values (now, 100, 10000)
sql insert into car1 values (now, 120, 11000)
print ================== add a column
sql alter table stb add column c1 int
sql describe stb
if $rows != 6 then
  return -1
endi

sql select * from stb
print rows = $rows
if $rows != 4 then
  return -1
endi
if $data03 != NULL then
  return -1
endi
if $data13 != NULL then
  return -1
endi
if $data23 != NULL then
  return -1
endi
if $data33 != NULL then
  return -1
endi

sql select c1 from stb
if $rows != 4 then
  return -1
endi
if $data00 != NULL then
  return -1
endi
if $data30 != NULL then
  return -1
endi

sql select c1+speed from stb
if $rows != 4 then
  return -1
endi

sql select c1+speed from car1
if $rows != 2 then
  return -1
endi
if $data00 != NULL then
  return -1
endi
if $data10 != NULL then
  return -1
endi

sql select * from car1
if $rows != 2 then
  return -1
endi
if $data03 != NULL then
  return -1
endi
if $data13 != NULL then
  return -1
endi

print ================== insert values into table
sql insert into car1 values (now, 1, 1,1 ) (now +1s, 2,2,2) car2 values (now, 1,3,3)

sql select c1+speed from stb where c1 > 0
if $rows != 3 then
  print $rows , expect 3
  return -1
endi

print ================== add a tag
sql alter table stb add tag t1 int
sql describe stb
if $rows != 7 then
  return -1
endi
if $data60 != t1 then
  return -1
endi

print ================== change a tag value
sql alter table car1 set tag carid=10
sql select distinct carId, carmodel from car1
if $rows != 1 then
  return -1
endi

if $data00 != 10 then
  return -1
endi

sql select * from stb where carid = 10
if $rows != 4 then
  return -1
endi

sql alter table car2 set tag carmodel = 2
sql select * from stb where carmodel = 2
print $rows
if $rows != 3 then
  return -1
endi

#sql drop database $db
#sql select * from information_schema.ins_databases
#if $rows != 0 then
#  return -1
#endi

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