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

$dbPrefix = sc_db
$tbPrefix = sc_tb
$stbPrefix = sc_stb
$tbNum = 10
$rowNum = 1000
$totalNum = $tbNum * $rowNum
$loops = 200000
$log = 1000
$ts0 = 1537146000000
$delta = 600000
print ========== selectResNum.sim
$i = 0
$db = $dbPrefix . $i
$stb = $stbPrefix . $i

sql drop database $db -x step1
step1:
sql create database $db
print ====== create tables
sql use $db
sql create table $stb (ts timestamp, c1 int, c2 bigint, c3 float, c4 double, c5 smallint, c6 tinyint, c7 bool, c8 binary(10), c9 nchar(10)) tags(t1 int)

$i = 0
$ts = $ts0
$halfNum = $tbNum / 2
while $i < $halfNum
  $tbId = $i + $halfNum
  $tb = $tbPrefix . $i
  $tb1 = $tbPrefix . $tbId
  sql create table $tb using $stb tags( $i )
  sql create table $tb1 using $stb tags( $tbId )

  $x = 0
  while $x < $rowNum
    $xs = $x * $delta
    $ts = $ts0 + $xs
    $c = $x / 10
    $c = $c * 10
    $c = $x - $c
    $binary = 'binary . $c
    $binary = $binary . '
    $nchar = 'nchar . $c
    $nchar = $nchar . '
    sql insert into $tb values ( $ts , $c , $c , $c , $c , $c , $c , true, $binary , $nchar )  $tb1 values ( $ts , $c , NULL , $c , NULL , $c , $c , true, $binary , $nchar )
    $x = $x + 1
  endw

  $i = $i + 1
endw
print ====== tables created

sql use $db
##### select from table
print ====== select from table and check num of rows returned
$loop = 1
$i = 0
while $loop <= $loops
  $remainder = $loop / $log
  $remainder = $remainder * $log
  $remainder = $loop - $remainder
  if $remainder == 0 then
     print loop: $loop
  endi
  while $i < 10
    sql select ts from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c1 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c2 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c3 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c4 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c5 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c6 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c7 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c8 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c9 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    $i = $i + 1
  endw
  $loop = $loop + 1
endw

print ====== restart server to commit data into disk
system sh/exec.sh -n dnode1 -s stop -x SIGINT
system sh/exec.sh -n dnode1 -s start
print ====== server restart completed
sql use $db

##### repeat test after server restart
print ====== repeat test after restarting server
$loop = 1
$i = 0
while $loop <= $loops
  $remainder = $loop / $log
  $remainder = $remainder * $log
  $remainder = $loop - $remainder
  if $remainder == 0 then
     print loop: $loop
  endi
  while $i < 10
    sql select ts from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c1 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c2 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c3 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c4 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c5 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c6 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c7 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    sql select c8 from $stb where t1 = $i
    if $rows != $rowNum then
      print expect $rowNum, actual: $rows
      return -1
    endi
    sql select c9 from $stb where t1 = $i
    if $rows != $rowNum then
      return -1
    endi
    $i = $i + 1
  endw
  $loop = $loop + 1
endw

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