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

print =============== create database
sql create database d0
sql select * from information_schema.ins_databases
if $rows != 3 then
  return -1
endi

print $data00 $data01 $data02

sql use d0

print =============== create super table, include column type for count/sum/min/max/first
sql create table if not exists stb (ts timestamp, c1 int, c2 float, c3 double, c4 bigint) tags (t1 int unsigned)

sql show stables
if $rows != 1 then
  return -1
endi

print =============== create child table
sql create table ct1 using stb tags(1000)
sql create table ct2 using stb tags(2000)
sql create table ct3 using stb tags(3000)

sql show tables
if $rows != 3 then
  return -1
endi

print =============== insert data, include NULL
sql insert into ct1 values (now+0s, 10, 2.0, 3.0, 90)(now+1s, NULL, NULL, NULL, NULL)(now+2s, NULL, 2.1, 3.1, 91)(now+3s, 11, NULL, 3.2, 92)(now+4s, 12, 2.2, NULL, 93)(now+5s, 13, 2.3, 3.3, NULL)
sql insert into ct1 values (now+6s, NULL, 2.4, 3.4, 94)
sql insert into ct1 values (now+7s, 14, NULL, 3.5, 95)
sql insert into ct1 values (now+8s, 15, 2.5, NULL, 96)
sql insert into ct1 values (now+9s, 16, 2.6, 3.6, NULL)
sql insert into ct1 values (now+10s, NULL, NULL, NULL, NULL)
sql insert into ct1 values (now+11s, -2147483647, 2.7, 3.7, 97)

#===================================================================
#===================================================================
print =============== query data from child table
sql select * from ct1
print ===> select * from ct1
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
print ===> rows1: $data10 $data11 $data12 $data13 $data14
print ===> rows2: $data20 $data21 $data22 $data23 $data24
print ===> rows3: $data30 $data31 $data32 $data33 $data34
print ===> rows4: $data40 $data41 $data42 $data43 $data44
if $rows != 12 then
  return -1
endi
if $data01 != 10 then
  return -1
endi
if $data02 != 2.00000 then
  return -1
endi
if $data03 != 3.000000000 then
  return -1
endi
#if $data41 != -14 then
#  return -1
#endi
#if $data42 != -2.40000 then
#  return -1
#endi
#if $data43 != -3.400000000 then
#  return -1
#endi

print =============== select count(*) from child table
sql select count(*) from ct1
print ===> select count(*) from ct1
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
if $rows != 1 then
  return -1
endi
if $data00 != 12 then
  return -1
endi

print =============== select count(column) from child table
sql select count(ts), count(c1), count(c2), count(c3) from ct1
print ===> select count(ts), count(c1), count(c2), count(c3) from ct1
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
if $data00 != 12 then
  return -1
endi
if $data01 != 8 then
  return -1
endi
if $data02 != 8 then
  return -1
endi
if $data03 != 8 then
  return -1
endi

#print =============== select first(*)/first(column) from child table
#sql select first(*) from ct1
#sql select first(ts), first(c1), first(c2), first(c3) from ct1

print =============== select min(column) from child table
sql select min(c1), min(c2), min(c3) from ct1
print ===> select min(c1), min(c2), min(c3) from ct1
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
if $rows != 1 then
  return -1
endi
if $data00 != -2147483647 then
  return -1
endi
if $data01 != 2.00000 then
  return -1
endi
if $data02 != 3.000000000 then
  return -1
endi

print =============== select max(column) from child table
sql select max(c1), max(c2), max(c3) from ct1
print ===> select max(c1), max(c2), max(c3) from ct1
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
if $rows != 1 then
  return -1
endi
if $data00 != 16 then
  return -1
endi
if $data01 != 2.70000 then
  return -1
endi
if $data02 != 3.700000000 then
  return -1
endi

print =============== select sum(column) from child table
sql select sum(c1), sum(c2), sum(c3) from ct1
print ===> select sum(c1), sum(c2), sum(c3) from ct1
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
if $rows != 1 then
  return -1
endi
if $data00 != -2147483556  then
  return -1
endi
if $data01 != 18.799999952 then
  return -1
endi
if $data02 != 26.800000000 then
  return -1
endi

print =============== select column, from child table
sql select c1, c2, c3 from ct1
print ===> select c1, c2, c3 from ct1
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
if $rows != 12 then
  return -1
endi
if $data00 != 10 then
  return -1
endi
if $data01 != 2.00000 then
  return -1
endi
if $data02 != 3.000000000 then
  return -1
endi
if $data10 != NULL then
  return -1
endi
if $data11 != NULL then
  return -1
endi
if $data12 != NULL then
  return -1
endi
if $data30 != 11 then
  return -1
endi
if $data31 != NULL then
  return -1
endi
if $data32 != 3.200000000 then
  return -1
endi
if $data90 != 16 then
  return -1
endi
if $data91 != 2.60000 then
  return -1
endi
if $data92 != 3.600000000 then
  return -1
endi
#===================================================================
#===================================================================

#print =============== query data from stb
sql select * from stb
print ===>
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
if $rows != 12 then
  return -1
endi

#print =============== select count(*) from supter table
sql select count(*) from stb
print $data00 $data01 $data02
if $rows != 1 then
  return -1
endi
if $data00 != 12 then
  return -1
endi

#print =============== select count(column) from supter table
sql select count(ts), count(c1), count(c2), count(c3) from stb
print $data00 $data01 $data02  $data03
if $data00 != 12 then
  return -1
endi
if $data01 != 8 then
  return -1
endi
if $data02 != 8 then
  return -1
endi
if $data03 != 8 then
  return -1
endi

#===================================================================

print =============== stop and restart taosd, then again do query above
system sh/exec.sh -n dnode1 -s stop -x SIGINT
system sh/exec.sh -n dnode1 -s start

#===================================================================

print =============== query data from child table
sql select * from ct1
print ===> select * from ct1
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
print ===> rows1: $data10 $data11 $data12 $data13 $data14
print ===> rows2: $data20 $data21 $data22 $data23 $data24
print ===> rows3: $data30 $data31 $data32 $data33 $data34
print ===> rows4: $data40 $data41 $data42 $data43 $data44
if $rows != 12 then
  return -1
endi
if $data01 != 10 then
  return -1
endi
if $data02 != 2.00000 then
  return -1
endi
if $data03 != 3.000000000 then
  return -1
endi
if $data41 != 12 then
  return -1
endi
if $data42 != 2.20000 then
  return -1
endi
if $data43 != NULL then
  return -1
endi

print =============== select count(*) from child table
sql select count(*) from ct1
print ===> select count(*) from ct1
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
if $rows != 1 then
  return -1
endi
if $data00 != 12 then
  return -1
endi

print =============== select count(column) from child table
sql select count(ts), count(c1), count(c2), count(c3) from ct1
print ===> select count(ts), count(c1), count(c2), count(c3) from ct1
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
if $data00 != 12 then
  return -1
endi
if $data01 != 8 then
  return -1
endi
if $data02 != 8 then
  return -1
endi
if $data03 != 8 then
  return -1
endi

#print =============== select first(*)/first(column) from child table
#sql select first(*) from ct1
#sql select first(ts), first(c1), first(c2), first(c3) from ct1

print =============== select min(column) from child table
sql select min(c1), min(c2), min(c3) from ct1
print ===> select min(c1), min(c2), min(c3) from ct1
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
if $rows != 1 then
  return -1
endi
if $data00 != -2147483647 then
  return -1
endi
if $data01 != 2.00000 then
  return -1
endi
if $data02 != 3.000000000 then
  return -1
endi

print =============== select max(column) from child table
sql select max(c1), max(c2), max(c3) from ct1
print ===> select max(c1), max(c2), max(c3) from ct1
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
if $rows != 1 then
  return -1
endi
if $data00 != 16 then
  return -1
endi
if $data01 != 2.70000 then
  return -1
endi
if $data02 != 3.700000000 then
  return -1
endi

print =============== select sum(column) from child table
sql select sum(c1), sum(c2), sum(c3) from ct1
print ===> select sum(c1), sum(c2), sum(c3) from ct1
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
if $rows != 1 then
  return -1
endi
if $data00 != -2147483556  then
  return -1
endi
if $data01 != 18.799999952 then
  return -1
endi
if $data02 != 26.800000000 then
  return -1
endi

print =============== select column, from child table
sql select c1, c2, c3 from ct1
print ===> select c1, c2, c3 from ct1
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
if $rows != 12 then
  return -1
endi
if $data00 != 10 then
  return -1
endi
if $data01 != 2.00000 then
  return -1
endi
if $data02 != 3.000000000 then
  return -1
endi
if $data10 != NULL then
  return -1
endi
if $data11 != NULL then
  return -1
endi
if $data12 != NULL then
  return -1
endi
if $data30 != 11 then
  return -1
endi
if $data31 != NULL then
  return -1
endi
if $data32 != 3.200000000 then
  return -1
endi
if $data90 != 16 then
  return -1
endi
if $data91 != 2.60000 then
  return -1
endi
if $data92 != 3.600000000 then
  return -1
endi
#===================================================================

print =============== query data from stb
sql select * from stb
print ===>
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
if $rows != 12 then
  return -1
endi
print =============== select count(*) from supter table
sql select count(*) from stb
print $data00 $data01 $data02
if $rows != 1 then
  return -1
endi
if $data00 != 12 then
  return -1
endi

print =============== select count(column) from supter table
sql select count(ts), count(c1), count(c2), count(c3) from stb
print $data00 $data01 $data02  $data03
if $data00 != 12 then
  return -1
endi
if $data01 != 8 then
  return -1
endi
if $data02 != 8 then
  return -1
endi
if $data03 != 8 then
  return -1
endi

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