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

print =============== create database
sql create database test vgroups 1;
sql show databases;
if $rows != 3 then
  return -1
endi

print $data00 $data01 $data02

sql use test;


sql create table t1(ts timestamp, a int, b int , c int, d double,id int);
sql create stream streams1 trigger at_once  into streamt as select  _wstart, count(*) c1,  sum(a), max(a), min(d), stddev(a), last(a), first(d), max(id) s from t1 session(ts,10s);
sql insert into t1 values(1648791213000,NULL,NULL,NULL,NULL,1);
sql insert into t1 values(1648791223001,10,2,3,1.1,2);
sql insert into t1 values(1648791233002,3,2,3,2.1,3);
sql insert into t1 values(1648791243003,NULL,NULL,NULL,NULL,4);
sql insert into t1 values(1648791213002,NULL,NULL,NULL,NULL,5) (1648791233012,NULL,NULL,NULL,NULL,6);

$loop_count = 0
loop0:

sleep 300
sql select * from streamt order by s desc;

$loop_count = $loop_count + 1
if $loop_count == 10 then
  return -1
endi

# row 0
if $data01 != 3 then
  print ======data01=$data01
  goto loop0
endi

if $data02 != 3 then
  print ======data02=$data02
  goto loop0
endi

if $data03 != 3 then
  print ======data03=$data03
  goto loop0
endi

if $data04 != 2.100000000 then
  print ======data04=$data04
  return -1
endi

if $data05 != 0.000000000 then
  print ======data05=$data05
  return -1
endi

if $data06 != 3 then
  print ======data06=$data06
  return -1
endi

if $data07 != 2.100000000 then
  print ======data07=$data07
  return -1
endi

if $data08 != 6 then
  print ======data08=$data08
  return -1
endi

# row 1

if $data11 != 3 then
  print ======data11=$data11
  goto loop0
endi

if $data12 != NULL then
  print ======data12=$data12
  goto loop0
endi

if $data13 != NULL then
  print ======data13=$data13
  goto loop0
endi

if $data14 != NULL then
  print ======data14=$data14
  return -1
endi

if $data15 != NULL then
  print ======data15=$data15
  return -1
endi

if $data16 != 10 then
  print ======data16=$data16
  return -1
endi

if $data17 != 1.100000000 then
  print ======data17=$data17
  return -1
endi

if $data18 != 5 then
  print ======data18=$data18
  return -1
endi

sql insert into t1 values(1648791213000,1,2,3,1.0,7);
sql insert into t1 values(1648791223001,2,2,3,1.1,8);
sql insert into t1 values(1648791233002,3,2,3,2.1,9);
sql insert into t1 values(1648791243003,4,2,3,3.1,10);
sql insert into t1 values(1648791213002,4,2,3,4.1,11) ;
sql insert into t1 values(1648791213002,4,2,3,4.1,12) (1648791223009,4,2,3,4.1,13);

$loop_count = 0
loop1:
sleep 300
sql select * from streamt order by s desc ;

$loop_count = $loop_count + 1
if $loop_count == 10 then
  return -1
endi

# row 0
if $data01 != 7 then
  print =====data01=$data01
  goto loop1
endi

if $data02 != NULL then
  print =====data02=$data02
  goto loop1
endi

if $data03 != NULL then
  print =====data03=$data03
  goto loop1
endi

if $data04 != NULL then
  print ======$data04
  return -1
endi

if $data05 != NULL then
  print ======$data05
  return -1
endi

if $data06 != 4 then
  print ======$data06
  return -1
endi

if $data07 != 1.000000000 then
  print ======$data07
  return -1
endi

if $data08 != 13 then
  print ======$data08
  return -1
endi

sql create database test2 vgroups 1;
sql use test2;
sql create table t2(ts timestamp, a int, b int , c int, d double, id int);
sql create stream streams2 trigger at_once  watermark 1d into streamt2 as select  _wstart,apercentile(a,30) c1, apercentile(a,70), apercentile(a,20,"t-digest") c2, apercentile(a,60,"t-digest") c3, max(id) c4 from t2 session(ts,10s);
sql insert into t2 values(1648791213001,1,1,3,1.0,1);
sql insert into t2 values(1648791213002,2,2,6,3.4,2);
sql insert into t2 values(1648791213003,4,9,3,4.8,3);
sql insert into t2 values(1648791233003,3,4,3,2.1,4);
sql insert into t2 values(1648791233004,3,5,3,3.4,5);
sql insert into t2 values(1648791233005,3,6,3,7.6,6);

#
sql insert into t2 values(1648791223003,20,7,3,10.1,7);

$loop_count = 0
loop2:
sleep 300
sql select * from streamt2 where c4=7;

$loop_count = $loop_count + 1
if $loop_count == 10 then
  return -1
endi

if $rows != 1 then
  print ======$rows
  goto loop2
endi

# row 0
if $data01 != 2.091607978 then
  print =====data01=$data01
  goto loop2
endi

if $data02 != 3.274823935 then
  print =====data02=$data02
  goto loop2
endi

if $data03 != 1.800000000 then
  print ======$data03
  return -1
endi

if $data04 != 3.350000000 then
  print ======$data04
  return -1
endi

sql create database test3 vgroups 1;
sql use test3;
sql create table t1(ts timestamp, a int, b int , c int, d double);
sql create stream streams3 trigger at_once  watermark 1d into streamt3 as select  _wstart, min(b), a,c from t1 session(ts,10s);
sql create stream streams4 trigger at_once  watermark 1d into streamt4 as select  _wstart, max(b), a,c from t1 session(ts,10s);
# sql create stream streams5 trigger at_once  watermark 1d into streamt5 as select  _wstart, top(b,3), a,c from t1 session(ts,10s);
# sql create stream streams6 trigger at_once  watermark 1d into streamt6 as select  _wstart, bottom(b,3), a,c from t1 session(ts,10s);
# sql create stream streams7 trigger at_once  watermark 1d into streamt7 as select  _wstart, spread(a), elapsed(ts), hyperloglog(a) from t1 session(ts,10s);
sql create stream streams7 trigger at_once  watermark 1d into streamt7 as select  _wstart, spread(a), hyperloglog(a) from t1 session(ts,10s);
# sql create stream streams8 trigger at_once  watermark 1d into streamt8 as select  _wstart, histogram(a,"user_input", "[1,3,5,7]", 1), histogram(a,"user_input", "[1,3,5,7]", 0) from t1 session(ts,10s);
sql insert into t1 values(1648791213001,1,1,1,1.0);
sql insert into t1 values(1648791213002,2,3,2,3.4);
sql insert into t1 values(1648791213003,4,9,3,4.8);
sql insert into t1 values(1648791213004,4,5,4,4.8);

sql insert into t1 values(1648791233004,3,4,0,2.1);
sql insert into t1 values(1648791233005,3,0,6,3.4);
sql insert into t1 values(1648791233006,3,6,7,7.6);
sql insert into t1 values(1648791233007,3,13,8,7.6);


sql insert into t1 values(1648791223004,20,7,9,10.1);

$loop_count = 0
loop3:
sleep 300

$loop_count = $loop_count + 1
if $loop_count == 10 then
  return -1
endi

sql select * from streamt3;
if $rows == 0 then
  print ======$rows
  goto loop3
endi

sql select * from streamt4;
if $rows == 0 then
  print ======$rows
  goto loop3
endi

#sql select * from streamt5;
if $rows == 0 then
  print ======$rows
  # goto loop3
endi

# sql select * from streamt6;
if $rows == 0 then
  print ======$rows
  goto loop3
endi

sql select * from streamt7;
if $rows == 0 then
  print ======$rows
  goto loop3
endi

#sql select * from streamt8;
#if $rows == 0 then
#  print ======$rows
#  goto loop3
#endi

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