#!/bin/bash

COLL=test_induced_condenser
TYPE=GreySetWithNullvalues
RASQL='rasql --user rasadmin --passwd rasadmin'

query()
{
  $RASQL -q "$1" --out string 2> /dev/null | grep Result | sed 's/Result object 1: //' | tr -d '{' | tr -d '}'
}

# cleanup
$RASQL -q "drop collection $COLL" > /dev/null 2>&1
$RASQL -q "drop type $TYPE" > /dev/null 2>&1

# setup collection
$RASQL -q "create type $TYPE as set (GreyCube null values [10])" --quiet
$RASQL -q "create collection $COLL $TYPE" --quiet

# insert first slice
$RASQL -q "insert into $COLL values marray i in [0, 0:0, 5:9] values 1c" --quiet
# make first half of first slice null values
$RASQL -q "update $COLL as c set c assign marray i in [0:0, 0:0, 0:4] values 10c" --quiet

# insert second slice, doesn't intersect the first slice
$RASQL -q "update $COLL as c set c assign marray i in [1:1, 0:0, 10:19] values 2c" --quiet

echo
echo "N = NULL = 10"
echo
echo "-------------------------------------------------------"
echo "case 1 - area [0:0, 0:9]:"
echo "  slice 1: [N,N,N,N,N,1,1,1,1,1]"
echo "  slice 2: [ , , , , , , , , , ],2,2,2,2,2,2,2,2,2,2"
echo

echo -n "max of slice 1 and 2: "
query "select encode(c[0, 0:0, 0:9] max c[1, 0:0, 0:9], \"csv\") from $COLL as c"
echo -n "min of slice 1 and 2: "
query "select encode(c[0, 0:0, 0:9] min c[1, 0:0, 0:9], \"csv\") from $COLL as c"
echo -n "  + of slice 1 and 2: "
query "select encode(c[0, 0:0, 0:9] + c[1, 0:0, 0:9], \"csv\") from $COLL as c"
echo

# update second slice, to overlap the first slice
$RASQL -q "update $COLL as c set c assign marray i in [1:1, 0:0, 3:7] values 3c" --quiet

echo "-------------------------------------------------------"
echo "case 2 - area [0:0, 0:9]:"
echo "  slice 1: [N,N,N,N,N,1,1,1,1,1]"
echo "  slice 2: [ , , ,3,3,3,3,3, , ],2,2,2,2,2,2,2,2,2,2"
echo

echo -n "max of slice 1 and 2: "
query "select encode(c[0, 0:0, 0:9] max c[1, 0:0, 0:9], \"csv\") from $COLL as c"
echo -n "min of slice 1 and 2: "
query "select encode(c[0, 0:0, 0:9] min c[1, 0:0, 0:9], \"csv\") from $COLL as c"
echo -n "  + of slice 1 and 2: "
query "select encode(c[0, 0:0, 0:9] + c[1, 0:0, 0:9], \"csv\") from $COLL as c"
echo

echo "-------------------------------------------------------"
echo "case 3 - area [0:0, 2:11]:"
echo "  slice 1: N,N,[N,N,N,1,1,1,1,1, , ]"
echo "  slice 2:  , ,[ ,3,3,3,3,3, , ,2,2],2,2,2,2,2,2,2,2"
echo

echo -n "max of slice 1 and 2: "
query "select encode(c[0, 0:0, 2:11] max c[1, 0:0, 2:11], \"csv\") from $COLL as c"
echo -n "min of slice 1 and 2: "
query "select encode(c[0, 0:0, 2:11] min c[1, 0:0, 2:11], \"csv\") from $COLL as c"
echo -n "  + of slice 1 and 2: "
query "select encode(c[0, 0:0, 2:11] + c[1, 0:0, 2:11], \"csv\") from $COLL as c"
echo
