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 ===== step1
sql drop stream if exists streams1;
sql drop database if exists test;
sql create database test  vgroups 10;
sql use test;
sql create stable st(ts timestamp, a int, b int , c int, d double) tags(ta int,tb int,tc int);
sql create table ts1 using st tags(1,1,1);
sql create table ts2 using st tags(2,2,2);
sql create table ts3 using st tags(3,2,2);
sql create table ts4 using st tags(4,2,2);
sql create stream streams1 trigger at_once IGNORE EXPIRED 0 IGNORE UPDATE 0 watermark 1d into streamt1 as select  _wstart, count(*) c1, sum(a) c3 from st interval(10s);

sql pause stream streams1;

sql insert into ts1 values(1648791213001,1,12,3,1.0);
sql insert into ts2 values(1648791213001,1,12,3,1.0);

sql insert into ts3 values(1648791213001,1,12,3,1.0);
sql insert into ts4 values(1648791213001,1,12,3,1.0);

sleep 1000
print 1 select * from streamt1;
sql select * from streamt1;

if $rows != 0 then
  print =1====rows=$rows
  print $data00 $data01 $data02
  print $data10 $data11 $data12
  print $data20 $data21 $data22
  return -1
endi

sql resume stream streams1;

$loop_count = 0
loop0:

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

sleep 500

print 2 select * from streamt1;
sql select * from streamt1;

if $rows != 1 then
  print =====rows=$rows
  print $data00 $data01 $data02
  print $data10 $data11 $data12
  print $data20 $data21 $data22
  goto loop0
endi

if $data01 != 4 then
  print =====data01=$data01
  goto loop0
endi

sql insert into ts1 values(1648791223002,2,2,3,1.1);
sql insert into ts2 values(1648791223002,3,2,3,2.1);
sql insert into ts3 values(1648791223002,4,2,43,73.1);
sql insert into ts4 values(1648791223002,24,22,23,4.1);

$loop_count = 0
loop1:

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

sleep 500

print 3 select * from streamt1;
sql select * from streamt1;

if $rows != 2 then
  print =====rows=$rows
  print $data00 $data01 $data02
  print $data10 $data11 $data12
  print $data20 $data21 $data22
  goto loop1
endi

if $data01 != 4 then
  print =====data01=$data01
  goto loop1
endi

if $data11 != 4 then
  print =====data01=$data01
  goto loop1
endi

print ===== step 1 over

print ===== step2
sql drop stream if exists streams2;
sql drop database if exists test2;
sql create database test2  vgroups 1;
sql use test2;
sql create table t1(ts timestamp, a int, b int , c int, d double);

sql create stream streams2 trigger at_once IGNORE EXPIRED 0 IGNORE UPDATE 0 watermark 1d into streamt2 as select  _wstart, count(*) c1, sum(a) c3 from t1 interval(10s);

sql pause stream streams2;

sql insert into t1 values(1648791213001,1,12,3,1.0);

sleep 1000
print 1 select * from streamt2;
sql select * from streamt2;

if $rows != 0 then
  print =1====rows=$rows
  print $data00 $data01 $data02
  print $data10 $data11 $data12
  print $data20 $data21 $data22
  return -1
endi

sql resume stream streams2;

$loop_count = 0
loop10:

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

sleep 500

print 2 select * from streamt2;
sql select * from streamt2;

if $rows != 1 then
  print =====rows=$rows
  print $data00 $data01 $data02
  print $data10 $data11 $data12
  print $data20 $data21 $data22
  goto loop10
endi

if $data01 != 1 then
  print =====data01=$data01
  goto loop10
endi

sql insert into t1 values(1648791223002,2,2,3,1.1);

$loop_count = 0

loop2:

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

sleep 500

print 3 select * from streamt2;
sql select * from streamt2;

if $rows != 2 then
  print =====rows=$rows
  print $data00 $data01 $data02
  print $data10 $data11 $data12
  print $data20 $data21 $data22
  goto loop2
endi

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

if $data11 != 1 then
  print =====data01=$data01
  goto loop2
endi

sql pause stream streams2;

sql insert into t1 values(1648791223003,2,2,3,1.1);
sql insert into t1 values(1648791233003,2,2,3,1.1);

sql resume stream  IGNORE UNTREATED streams2;

$loop_count = 0

loop3:

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

sleep 500

print 4 select * from streamt2;
sql select * from streamt2;

if $rows != 2 then
  print =====rows=$rows
  print $data00 $data01 $data02
  print $data10 $data11 $data12
  print $data20 $data21 $data22
  goto loop3
endi

if $data01 != 1 then
  print =====data01=$data01
  goto loop3
endi

if $data11 != 1 then
  print =====data01=$data01
  goto loop3
endi


print ===== step 2 over


print ===== step3
sql drop stream if exists streams3;
sql drop database if exists test3;
sql create database test3  vgroups 10;
sql use test3;
sql create stable st(ts timestamp, a int, b int , c int, d double) tags(ta int,tb int,tc int);
sql create table ts1 using st tags(1,1,1);
sql create table ts2 using st tags(2,2,2);
sql create table ts3 using st tags(3,2,2);
sql create table ts4 using st tags(4,2,2);
sql create stream streams3 trigger at_once IGNORE EXPIRED 0 IGNORE UPDATE 0 watermark 1d into streamt3 as select  _wstart, count(*) c1, sum(a) c3 from st interval(10s);
sql create stream streams4 trigger at_once IGNORE EXPIRED 0 IGNORE UPDATE 0 watermark 1d into streamt4 as select  _wstart, count(*) c1, sum(a) c3 from st interval(10s);
sql create stream streams5 trigger at_once IGNORE EXPIRED 0 IGNORE UPDATE 0 watermark 1d into streamt5 as select  _wstart, count(*) c1, sum(a) c3 from ts1 interval(10s);

sql pause stream streams3;

sql insert into ts1 values(1648791213001,1,12,3,1.0);
sql insert into ts2 values(1648791213001,1,12,3,1.0);

sql insert into ts3 values(1648791213001,1,12,3,1.0);
sql insert into ts4 values(1648791213001,1,12,3,1.0);


$loop_count = 0
loop3:

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

sleep 500

print 1 select * from streamt4;
sql select * from streamt4;

if $rows != 1 then
  print =====rows=$rows
  print $data00 $data01 $data02
  print $data10 $data11 $data12
  print $data20 $data21 $data22
  goto loop3
endi

print 2 select * from streamt5;
sql select * from streamt5;

if $rows != 1 then
  print =====rows=$rows
  print $data00 $data01 $data02
  print $data10 $data11 $data12
  print $data20 $data21 $data22
  goto loop3
endi

print 3 select * from streamt3;
sql select * from streamt3;
if $rows != 0 then
 print =====rows=$rows
 return -1
endi

print ===== step 3 over

print ===== step 4

sql_error pause stream streams3333333;
sql pause stream IF EXISTS streams44444;

sql_error resume stream streams5555555;
sql resume stream IF EXISTS streams66666666;

print ===== step 4 over

print ===== step5
sql drop stream if exists streams6;
sql drop database if exists test6;
sql create database test6  vgroups 10;
sql use test6;
sql create stable st(ts timestamp, a int, b int , c int, d double) tags(ta int,tb int,tc int);
sql create table ts1 using st tags(1,1,1);
sql create table ts2 using st tags(2,2,2);
sql create table ts3 using st tags(3,2,2);
sql create table ts4 using st tags(4,2,2);
sql create stream streams6 trigger at_once IGNORE EXPIRED 0 IGNORE UPDATE 0 watermark 1d into streamt6 as select  _wstart, count(*) c1 from st interval(10s);

sql insert into ts1 values(1648791213001,1,12,3,1.0);
sql insert into ts2 values(1648791213001,1,12,3,1.0);

sql insert into ts3 values(1648791213001,1,12,3,1.0);
sql insert into ts4 values(1648791213001,1,12,3,1.0);

sleep 1000

sql pause stream streams6;

sleep 1000


sql insert into ts1 values(1648791223001,1,12,3,1.0);
sql insert into ts2 values(1648791233001,1,12,3,1.0);

sql resume stream streams6;

sql insert into ts3 values(1648791243001,1,12,3,1.0);
sql insert into ts4 values(1648791253001,1,12,3,1.0);

$loop_count = 0
loop6:

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

sleep 500

print 2 select * from streamt6;
sql select * from streamt6;

if $rows != 5 then
  print =====rows=$rows
  print $data00 $data01 $data02
  print $data10 $data11 $data12
  print $data20 $data21 $data22
  print $data30 $data31 $data32
  print $data40 $data41 $data42
  print $data50 $data51 $data52
  goto loop6
endi

print ===== step5 over

system sh/stop_dnodes.sh
