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 duration 300
sql use d0

print =============== create super table and child table
sql create table stb (ts timestamp, tbcol int) tags (t1 int)
sql show stables
print $rows $data00 $data01 $data02
if $rows != 1 then
  return -1
endi

sql create table ct1 using stb tags ( 1 )
sql create table ct2 using stb tags ( 2 )
sql create table ct3 using stb tags ( 3 )
sql create table ct4 using stb tags ( 4 )
sql show tables
print $rows $data00 $data10 $data20
if $rows != 4 then
  return -1
endi

print =============== insert data into child table ct1 (s)
sql insert into ct1 values ( '2022-01-01 01:01:01.000', 1 )
sql insert into ct1 values ( '2022-01-01 01:01:06.000', 2 )
sql insert into ct1 values ( '2022-01-01 01:01:10.000', 3 )
sql insert into ct1 values ( '2022-01-01 01:01:16.000', 4 )
sql insert into ct1 values ( '2022-01-01 01:01:20.000', 5 )
sql insert into ct1 values ( '2022-01-01 01:01:26.000', 6 )
sql insert into ct1 values ( '2022-01-01 01:01:30.000', 7 )
sql insert into ct1 values ( '2022-01-01 01:01:36.000', 8 )

print =============== insert data into child table ct2 (d)
sql insert into ct2 values ( '2022-01-01 01:00:01.000', 1 )
sql insert into ct2 values ( '2022-01-01 10:00:01.000', 2 )
sql insert into ct2 values ( '2022-01-01 20:00:01.000', 3 )
sql insert into ct2 values ( '2022-01-02 10:00:01.000', 4 )
sql insert into ct2 values ( '2022-01-02 20:00:01.000', 5 )
sql insert into ct2 values ( '2022-01-03 10:00:01.000', 6 )
sql insert into ct2 values ( '2022-01-03 20:00:01.000', 7 )

print =============== insert data into child table ct3 (n)
sql insert into ct3 values ( '2021-12-21 01:01:01.000', NULL )
sql insert into ct3 values ( '2021-12-31 01:01:01.000', 1 )
sql insert into ct3 values ( '2022-01-01 01:01:06.000', 2 )
sql insert into ct3 values ( '2022-01-07 01:01:10.000', 3 )
sql insert into ct3 values ( '2022-01-31 01:01:16.000', 4 )
sql insert into ct3 values ( '2022-02-01 01:01:20.000', 5 )
sql insert into ct3 values ( '2022-02-28 01:01:26.000', 6 )
sql insert into ct3 values ( '2022-03-01 01:01:30.000', 7 )
sql insert into ct3 values ( '2022-03-08 01:01:36.000', 8 )

print =============== insert data into child table ct4 (y)
sql insert into ct4 values ( '2020-10-21 01:01:01.000', 1 )
sql insert into ct4 values ( '2020-12-31 01:01:01.000', 2 )
sql insert into ct4 values ( '2021-01-01 01:01:06.000', 3 )
sql insert into ct4 values ( '2021-05-07 01:01:10.000', 4 )
sql insert into ct4 values ( '2021-09-30 01:01:16.000', 5 )
sql insert into ct4 values ( '2022-02-01 01:01:20.000', 6 )
sql insert into ct4 values ( '2022-10-28 01:01:26.000', 7 )
sql insert into ct4 values ( '2022-12-01 01:01:30.000', 8 )
sql insert into ct4 values ( '2022-12-31 01:01:36.000', 9 )

print ================ start query ======================
sql select _wstart, _wend, _wduration, _qstart, _qend, count(*) from ct1 interval(10s, 2s)
print ===> select _wstart, _wend, _wduration, _qstart, _qend, count(*) from ct1 interval(10s, 2s)
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data05
print ===> rows1: $data10 $data11 $data12 $data15
print ===> rows2: $data20 $data21 $data22 $data25
print ===> rows3: $data30 $data31 $data32 $data35
print ===> rows4: $data40 $data41 $data42 $data45
if $rows != 5 then
  return -1
endi
if $data00 != @22-01-01 01:00:52.000@ then
  return -1
endi
if $data02 != 10000 then
  return -1
endi
if $data45 != 1 then
  return -1
endi

sql select _wstart, _wend, _wduration, _qstart, _qend,  count(*) from ct1 interval(10s, 2s) sliding(10s)
print ===> select _wstart, _wend, _wduration, _qstart, _qend,  count(*) from ct1 interval(10s, 2s) sliding(10s)
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data05
print ===> rows1: $data10 $data11 $data12 $data15
print ===> rows2: $data20 $data21 $data22 $data25
print ===> rows3: $data30 $data31 $data32 $data35
print ===> rows4: $data40 $data41 $data42 $data45
if $rows != 5 then
  return -1
endi
if $data00 != @22-01-01 01:00:52.000@ then
  return -1
endi
if $data02 != 10000 then
  return -1
endi
if $data45 != 1 then
  return -1
endi

sql select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(*) from ct1 interval(10s, 2s) sliding(5s)
print ===> select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(*) from ct1 interval(10s, 2s) sliding(5s)
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
print ===> rows5: $data50 $data51 $data52 $data53 $data54
print ===> rows6: $data60 $data61 $data62 $data63 $data64
print ===> rows7: $data70 $data71 $data72 $data73 $data74
print ===> rows8: $data80 $data81 $data82 $data83 $data84
if $rows != 9 then
  return -1
endi
if $data00 != 1 then
  return -1
endi
if $data70 != 2 then
  return -1
endi
if $data80 != 1 then
  return -1
endi

sql select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(*) from ct2 interval(1d, 2h)
print ===> select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(*) from ct2 interval(1d, 2h)
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 != 4 then
  return -1
endi
if $data00 != 1 then
  return -1
endi
if $data10 != 2 then
  return -1
endi

sql select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(*) from ct2 interval(1d, 2h) sliding(12h)
print ===> select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(*) from ct2 interval(1d, 2h) sliding(12h)
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04 $data05
print ===> rows1: $data10 $data11 $data12 $data13 $data14 $data15
print ===> rows2: $data20 $data21 $data22 $data23 $data24 $data25
print ===> rows3: $data30 $data31 $data32 $data33 $data34 $data35
print ===> rows4: $data40 $data41 $data42 $data43 $data44 $data45
print ===> rows5: $data50 $data51 $data52 $data53 $data54 $data55
print ===> rows6: $data60 $data61 $data62 $data63 $data64 $data65
print ===> rows7: $data70 $data71 $data72 $data73 $data74 $data75
if $rows != 8 then
  return -1
endi
if $data00 != 1 then
  return -1
endi
if $data10 != 2 then
  return -1
endi
if $data70 != 1 then
  return -1
endi

sql select _wstart, count(tbcol), _wduration, _wstart, count(*) from ct3 interval(1n, 1w)
print ===> select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(*) from ct3 interval(1n, 1w)
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
if $rows != 4 then
  print expect 4, actual: $rows
  return -1
endi
if $data00 != @21-12-08 00:00:00.000@ then
  return -1
endi
if $data31 != 1 then
  return -1
endi
if $data34 != $data31 then
  return -1
endi
if $data02 != 2678400000 then
  return -1
endi

sql_error select  _wstart, count(tbcol), _wduration, _wstart, count(*) from ct3 interval(1n, 1w) sliding(2w)
sql_error select _wstart, count(tbcol), _wduration, _wstart, count(*)  from ct3 interval(1n, 1w) sliding(4w)

sql select _wstart, count(tbcol), _wduration, _wstart, count(*) from ct4 interval(1y, 6n)
print ===> select _wstart, count(tbcol), _wduration, _wstart, count(*) from ct4 interval(1y, 6n)
print ===> rows: $rows
print ===> rows0: $data00 $data01 $data02 $data03 $data04
print ===> rows1: $data10 $data11 $data12 $data13 $data14
print ===> rows2: $data20 $data21 $data22 $data23 $data24
if $rows != 3 then
  return -1
endi
if $data01 != 2 then
  return -1
endi
if $data04 != 2 then
  return -1
endi

sql_error select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(*) from ct4 interval(1y, 6n) sliding(6n)
sql_error select _wstart, count(tbcol), _wduration, _wstart, count(*) from ct4 interval(1y, 6n) sliding(12n)

#=================================================
print =============== stop and restart taosd
system sh/exec.sh -n dnode1 -s stop -x SIGINT
system sh/exec.sh -n dnode1 -s start

$loop_cnt = 0
check_dnode_ready:
	$loop_cnt = $loop_cnt + 1
	sleep 200
	if $loop_cnt == 10 then
	  print ====> dnode not ready!
		return -1
	endi
sql select * from information_schema.ins_dnodes
print ===> $rows $data00 $data01 $data02 $data03 $data04 $data05
if $data00 != 1 then
  return -1
endi
if $data04 != ready then
  goto check_dnode_ready
endi



#sql select count(*) from car interval(1n, 10d) order by ts desc
#sql select count(*) from car interval(2n, 5d)
#sql select count(*) from car interval(2n) order by ts desc
#sql select count(*) from car interval(1y, 1n)
#sql select count(*) from car interval(1y, 2n)
#sql select count(*) from car where ts > '2019-05-14 00:00:00' interval(1y, 5d)


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