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

$dbPrefix = m_in_db
$tbPrefix = m_in_tb
$mtPrefix = m_in_mt
$tbNum = 10
$rowNum = 20
$totalNum = 200

print =============== step1
$i = 0
$db = $dbPrefix . $i
$mt = $mtPrefix . $i

sql drop database $db -x step1
step1:
sql create database $db
sql use $db
sql create table $mt (ts timestamp, tbcol int) TAGS(tgcol int)

print ====== start create child tables and insert data
$i = 0
while $i < $tbNum
  $tb = $tbPrefix . $i
  sql create table $tb using $mt tags( $i )

  $x = 0
  while $x < $rowNum
    $cc = $x * 60000
    $ms = 1601481600000 + $cc

    sql insert into $tb values ($ms , $x )
    $x = $x + 1
  endw

  $i = $i + 1
endw

print =============== step2
$i = 1
$tb = $tbPrefix . $i

sql select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(tbcol) from $tb interval(1m)
print ===> select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(tbcol) from $tb interval(1m)
print ===> $rows $data01 $data05
if $rows != $rowNum then
  return -1
endi
if $data00 != 1 then
  return -1
endi
if $data04 != 1 then
  return -1
endi

print =============== step3
#$cc = 4 * 60000
#$ms = 1601481600000 + $cc
#sql select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(tbcol) from $tb  where ts <= $ms interval(1m)
#print ===> select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(tbcol) from $tb  where ts <= $ms interval(1m)
#print ===> $rows $data01 $data05
#if $rows != 5 then
#  return -1
#endi
#if $data00 != 1 then
#  return -1
#endi
#if $data04 != 1 then
#  return -1
#endi

print =============== step4
#$cc = 40 * 60000
#$ms = 1601481600000 + $cc

#$cc = 1 * 60000
#$ms2 = 1601481600000 - $cc

sql select _wstart, _wend, _wduration, _qstart, _qend, count(tbcol) from $tb  interval(1m)
print ===> select _wstart, _wend, _wduration, _qstart, _qend, count(tbcol) from $tb  interval(1m)
print ===> $rows $data01 $data05
if $rows != $rowNum then
  return -1
endi
if $data05 != 1 then
  return -1
endi
if $data02 != 60000 then
  return -1
endi

#print =============== step5
#$cc = 40 * 60000
#$ms = 1601481600000 + $cc

#$cc = 1 * 60000
#$ms2 = 1601481600000 - $cc

#sql select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(tbcol) from $tb  where ts <= $ms and ts > $ms2 interval(1m) fill(value,0)
#print ===> select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(tbcol) from $tb  where ts <= $ms and ts > $ms2 interval(1m) fill(value,0)
#print ===> $rows $data21 $data25
#if $rows != 42 then
#  return -1
#endi
#if $data20 != 1 then
#  return -1
#endi
#if $data24 != 1 then
#  return -1
#endi

#print =============== step6
#sql select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(tbcol) from $mt interval(1m)
#print ===> select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(tbcol) from $mt interval(1m)
#print ===> $rows $data11
#if $rows != 20 then
#  return -1
#endi
#if $data11 != 10 then
#  return -1
#endi

#print =============== step7
#$cc = 4 * 60000
#$ms = 1601481600000 + $cc
#sql select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(tbcol) from $mt  where ts <= $ms interval(1m)
#print ===> select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(tbcol) from $mt  where ts <= $ms interval(1m)
#print ===> $rows $data11
#if $rows != 5 then
#  return -1
#endi
#if $data11 != 10 then
#  return -1
#endi

#print =============== step8
#$cc = 40 * 60000
#$ms1 = 1601481600000 + $cc
#
#$cc = 1 * 60000
#$ms2 = 1601481600000 - $cc
#
#sql select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(tbcol) from $mt  where ts <= $ms1 and ts > $ms2 interval(1m)
#print ===> select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(tbcol) from $mt  where ts <= $ms1 and ts > $ms2 interval(1m)
#print ===> $rows $data11
#if $rows != 20 then
#  return -1
#endi
#if $data11 != 10 then
#  return -1
#endi
#
#print =============== step9
#$cc = 40 * 60000
#$ms1 = 1601481600000 + $cc
#
#$cc = 1 * 60000
#$ms2 = 1601481600000 - $cc
#
#sql select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(tbcol) from $mt  where ts <= $ms1 and ts > $ms2 interval(1m) fill(value, 0)
#print ===> select count(tbcol), sum(tbcol), max(tbcol), min(tbcol), count(tbcol) from $mt  where ts <= $ms1 and ts > $ms2 interval(1m) fill(value, 0)
#print ===> $rows $data11
#if $rows != 42 then
#  return -1
#endi
#if $data11 != 10 then
#  return -1
#endi

print ================ step10
print -------- create database and table
sql create database if not exists test
sql use test
sql create stable st (ts timestamp, c2 int) tags(tg int)
print -------- insert 300 rows data
$i = 0
while $i < 300
  $t = 1577807983000
  $cc = $i * 1000
  $t = $t + $cc
  sql select $i % 3
  if $data00 != 0.000000000 then
    goto continue_while
  endi
  sql select $i % 4
  if $data00 == 0.000000000 then
    goto continue_while
  endi
  sql insert into t1 using st tags(1) values ( $t , $i )
  continue_while:
    $i = $i + 1
endw

$ms1 = 1577808120000
$ms2 = 1577808000000
sql select * from (select _wstart, last(ts) as ts, avg(c2) as av from t1 where ts <= $ms1 and ts >= $ms2 interval(10s) sliding(1s) fill(NULL)) order by ts asc
print ----> select asc rows: $rows
$asc_rows = $rows
sql select * from (select _wstart, last(ts) as ts, avg(c2) as av from t1 where ts <= $ms1 and ts >= $ms2 interval(10s) sliding(1s) fill(NULL)) order by ts desc
print ----> select desc rows: $rows
$desc_rows = $rows
if $desc_rows != $asc_rows then
  return -1
endi

print =============== clear
#sql drop database $db
#sql select * from information_schema.ins_databases
#if $rows != 0 then
#  return -1
#endi

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