system sh/stop_dnodes.sh
system sh/deploy.sh -n dnode1 -i 1

print ===== step1

system sh/exec.sh -n dnode1 -s start 
sleep 50
sql connect

print ===== step2
print ===== table name

sql create database result vgroups 1;

sql create database test  vgroups 4;
sql use test;


sql create stable st(ts timestamp,a int,b int,c int) tags(ta int,tb int,tc int);
sql create table t1 using st tags(1,1,1);
sql create table t2 using st tags(2,2,2);

#sql_error create stream streams1 trigger at_once IGNORE EXPIRED 0 IGNORE UPDATE 0  into result.streamt SUBTABLE("aaa") as select  _wstart, count(*) c1 from st interval(10s);
sql create stream streams1 trigger at_once IGNORE EXPIRED 0 IGNORE UPDATE 0  into result.streamt SUBTABLE(concat("aaa-", tbname)) as select  _wstart, count(*) c1 from st partition by tbname interval(10s);
sql insert into t1 values(1648791213000,1,2,3);
sql insert into t2 values(1648791213000,1,2,3);

$loop_count = 0
loop0:

sleep 1000

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

sql select table_name from information_schema.ins_tables where db_name="result" order by 1;

if $rows != 2 then
  print =====rows=$rows
  print $data00
  print $data10
  print $data20
  print $data30
  goto loop0
endi

if $data00 != aaa-t1 then
  print =====data00=$data00
  goto loop0
endi

if $data10 != aaa-t2 then
  print =====data10=$data10
  goto loop0
endi

$loop_count = 0
loop1:

sleep 1000

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

sql select * from result.streamt;

if $rows != 2 then
  print =====rows=$rows
  print $data00 $data10
  goto loop1
endi


print ===== step3
print ===== tag name

sql create database result2 vgroups 1;

sql create database test2  vgroups 4;
sql use test2;


sql create stable st(ts timestamp,a int,b int,c int) tags(ta int,tb int,tc int);
sql create table t1 using st tags(1,1,1);
sql create table t2 using st tags(2,2,2);

sql create stream streams2 trigger at_once IGNORE EXPIRED 0 IGNORE UPDATE 0  into result2.streamt2 TAGS(cc varchar(100)) as select  _wstart, count(*) c1 from st partition by concat("tag-", tbname) as cc interval(10s);
sql insert into t1 values(1648791213000,1,2,3);
sql insert into t2 values(1648791213000,1,2,3);


$loop_count = 0
loop2:

sleep 1000

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

sql select tag_name from information_schema.ins_tags where db_name="result2" and stable_name = "streamt2" order by 1;

if $rows != 2 then
  print =====rows=$rows
  print $data00 $data10
  goto loop2
endi

if $data00 != cc then
  print data00 != cc
  print =====data00=$data00
  goto loop2
endi

if $data10 != cc then
  print =====data10=$data10
  goto loop2
endi

sql select cc from result2.streamt2 order by 1;

if $rows != 2 then
  print =====rows=$rows
  print $data00 $data10
  goto loop2
endi

if $data00 != tag-t1 then
  print data00 != tag-t1
  print =====data00=$data00
  goto loop2
endi

if $data10 != tag-t2 then
  print =====data10=$data10
  goto loop2
endi

$loop_count = 0
loop3:

sleep 1000

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

sql select * from result2.streamt2;

if $rows != 2 then
  print =====rows=$rows
  print $data00 $data10
  goto loop3
endi


print ===== step4
print ===== tag name + table name

sql create database result3 vgroups 1;

sql create database test3  vgroups 4;
sql use test3;


sql create stable st(ts timestamp,a int,b int,c int) tags(ta int,tb int,tc int);
sql create table t1 using st tags(1,1,1);
sql create table t2 using st tags(2,2,2);

sql create stream streams3 trigger at_once IGNORE EXPIRED 0 IGNORE UPDATE 0  into result3.streamt3 TAGS(dd varchar(100)) SUBTABLE(concat("tbn-", tbname)) as select  _wstart, count(*) c1 from st partition by concat("tag-", tbname) as dd, tbname interval(10s);
sql insert into t1 values(1648791213000,1,2,3);
sql insert into t2 values(1648791213000,1,2,3);


$loop_count = 0
loop4:

sleep 1000

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

sql select tag_name from information_schema.ins_tags where db_name="result3" and stable_name = "streamt3" order by 1;

if $rows != 2 then
  print =====rows=$rows
  print $data00 $data10
  goto loop4
endi

if $data00 != dd then
  print =====data00=$data00
  goto loop4
endi

if $data10 != dd then
  print =====data10=$data10
  goto loop4
endi

sql select dd from result3.streamt3 order by 1;

if $rows != 2 then
  print =====rows=$rows
  print $data00 $data10
  goto loop4
endi

if $data00 != tag-t1 then
  print =====data00=$data00
  goto loop4
endi

if $data10 != tag-t2 then
  print =====data10=$data10
  goto loop4
endi

$loop_count = 0
loop5:

sleep 1000

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

sql select * from result3.streamt3;

if $rows != 2 then
  print =====rows=$rows
  print $data00 $data10
  goto loop5
endi

$loop_count = 0
loop6:

sleep 1000

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

sql select table_name from information_schema.ins_tables where db_name="result3" order by 1;

if $rows != 2 then
  print =====rows=$rows
  print $data00 $data10
  goto loop6
endi

if $data00 != tbn-t1 then
  print =====data00=$data00
  goto loop6
endi

if $data10 != tbn-t2 then
  print =====data10=$data10
  goto loop6
endi


print ===== step5
print ===== tag name + table name

sql create database result4 vgroups 1;

sql create database test4  vgroups 4;
sql use test4;


sql create stable st(ts timestamp,a int,b int,c int) tags(ta int,tb int,tc int);
sql create table t1 using st tags(1,1,1);
sql create table t2 using st tags(2,2,2);
sql create table t3 using st tags(3,3,3);

sql create stream streams4 trigger at_once IGNORE EXPIRED 0 IGNORE UPDATE 0  into result4.streamt4 TAGS(dd varchar(100)) SUBTABLE(concat("tbn-", tbname)) as select  _wstart, count(*) c1 from st partition by concat("tag-", tbname) as dd, tbname interval(10s);
sql insert into t1 values(1648791213000,1,1,1) t2 values(1648791213000,2,2,2) t3 values(1648791213000,3,3,3);


$loop_count = 0
loop7:

sleep 1000

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

sql select table_name from information_schema.ins_tables where db_name="result4" order by 1;

if $rows != 3 then
  print =====rows=$rows
  print $data00 $data10
  goto loop7
endi

if $data00 != tbn-t1 then
  print =====data00=$data00
  goto loop7
endi

if $data10 != tbn-t2 then
  print =====data10=$data10
  goto loop7
endi

if $data20 != tbn-t3 then
  print =====data20=$data20
  goto loop7
endi

$loop_count = 0
loop8:

sleep 1000

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

sql select * from result4.streamt4 order by 3;

if $rows != 3 then
  print =====rows=$rows
  print $data00 $data10
  goto loop8
endi

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

if $data02 != tag-t1 then
  print =====data02=$data02
  goto loop8
endi

if $data11 != 1 then
  print =====data11=$data11
  goto loop8
endi

if $data12 != tag-t2 then
  print =====data12=$data12
  goto loop8
endi

if $data21 != 1 then
  print =====data21=$data21
  goto loop8
endi

if $data22 != tag-t3 then
  print =====data22=$data22
  goto loop8
endi

print ===== step6
print ===== transform tag value

sql drop stream if exists streams1;
sql drop stream if exists streams2;
sql drop stream if exists streams3;
sql drop stream if exists streams4;
sql drop stream if exists streams5;

sql drop database if exists test1;
sql drop database if exists test2;
sql drop database if exists test3;
sql drop database if exists test4;
sql drop database if exists test5;

sql drop database if exists result1;
sql drop database if exists result2;
sql drop database if exists result3;
sql drop database if exists result4;
sql drop database if exists result5;



sql create database result6 vgroups 1;

sql create database test6  vgroups 4;
sql use test6;


sql create stable st(ts timestamp,a int,b int,c int) tags(ta varchar(20), tb int, tc int);
sql create table t1 using st tags("1",1,1);
sql create table t2 using st tags("2",2,2);
sql create table t3 using st tags("3",3,3);

sql create stream streams6 trigger at_once IGNORE EXPIRED 0 IGNORE UPDATE 0  into result6.streamt6 TAGS(dd int) as select  _wstart, count(*) c1 from st partition by concat(ta, "0") as dd, tbname interval(10s);
sql insert into t1 values(1648791213000,1,1,1) t2 values(1648791213000,2,2,2) t3 values(1648791213000,3,3,3);


$loop_count = 0
loop9:

sleep 1000

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

sql select * from result6.streamt6 order by 3;

if $rows != 3 then
  print =====rows=$rows
  print $data00 $data10
  goto loop9
endi

if $data02 != 10 then
  print =====data02=$data02
  goto loop9
endi

if $data12 != 20 then
  print =====data12=$data12
  goto loop9
endi

if $data22 != 30 then
  print =====data22=$data22
  goto loop8
endi

print ======over

system sh/stop_dnodes.sh
