system sh/stop_dnodes.sh


system sh/deploy.sh -n dnode1 -i 1
system sh/cfg.sh -n dnode1 -c walLevel -v 1
system sh/exec.sh -n dnode1 -s start

sleep 2000
sql connect

$i = 0
$dbPrefix = ca_nm_db
$tbPrefix = ca_nm_tb
$mtPrefix = ca_nm_mt
$db = $dbPrefix . $i
$tb = $tbPrefix . $i
$mt = $mtPrefix . $i
$tbNum = 10
$rowNum = 20

print =============== step1
sql create database $db
sql use $db

sql create table $mt (ts timestamp, tbcol int) TAGS(tgcol bool)
$i = 0
while $i < 5
  $tb = $tbPrefix . $i
  sql create table $tb using $mt tags( $i )  
  $x = 0
  while $x < $rowNum
    $val = $x * 60000
    $ms = 1519833600000 + $val 
    sql insert into $tb values ($ms , $x ) 
    $x = $x + 1
  endw  
  $i = $i + 1
endw 

print =============== step2

sql select * from $tb order by  ts desc
print ===>rows $rows, data $data01
if $rows != 20 then
  return -1
endi  
if $data01 != 19 then
  return -1
endi  

sql select * from $mt 
print ===>rows $rows, data $data01
if $rows != 100 then
  return -1
endi  
if $data01 != 0 then
  return -1
endi  

sql select count(*) from $mt
print ===>rows $rows, data $data00
if $rows != 1 then
  return -1
endi  
if $data00 != 100 then
  return -1
endi  

print =============== step3

sql show stables
if $rows != 1 then 
  return -1
endi
if $data00 != $mt then 
  return -1
endi
if $data04 != 5 then 
  return -1
endi

print =============== step4

while $i < 10
  $tb = $tbPrefix . $i
  sql create table $tb using $mt tags( 1 )  
  $x = 0
  while $x < $rowNum
    $val = $x * 60000
    $ms = 1519833600000 + $val 
    sql insert into $tb values ($ms , $x ) 
    $x = $x + 1
  endw  
  $i = $i + 1
endw 

print ==> sleep 1 seconds to renew cache
sql reset query cache
sleep 1000

print =============== step5

sql select * from $tb order by  ts desc
print ===>rows $rows, data $data01
if $rows != 20 then
  return -1
endi  
if $data01 != 19 then
  return -1
endi  

sql select * from $mt 
print ===>rows $rows, data $data01
if $rows != 200 then
  return -1
endi  
if $data01 != 0 then
  return -1
endi  

sql select count(*) from $mt
print ===>rows $rows, data $data01
if $rows != 1 then
  return -1
endi  
if $data00 != 200 then
  return -1
endi  

print =============== step6

sql show stables
if $rows != 1 then 
  return -1
endi
if $data00 != $mt then 
  return -1
endi
if $data04 != 10 then 
  return -1
endi

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