#
# Tests for cassandra storage engine
#
if (`SELECT COUNT(*) = 0 FROM INFORMATION_SCHEMA.ENGINES WHERE engine = 'cassandra' AND support IN ('YES', 'DEFAULT', 'ENABLED')`)
{
  --skip Test requires Cassandra.
}

--disable_warnings
drop table if exists t0, t1;
--enable_warnings

--echo #
--echo # Check variables and status counters
--echo #
show status like 'cassandra%';
show variables like 'cassandra%';

--echo #
--echo # Test various errors on table creation.
--echo #
--error ER_REQUIRES_PRIMARY_KEY
create table t1 (a int) engine=cassandra 
 thrift_host='localhost' keyspace='foo' column_family='colfam';

--error ER_CONNECT_TO_FOREIGN_DATA_SOURCE
create table t1 (a int primary key, b int) engine=cassandra
  thrift_host='localhost' keyspace='foo' column_family='colfam';

--error ER_CONNECT_TO_FOREIGN_DATA_SOURCE
create table t1 (rowkey char(10) primary key, column1 char(10)) engine=cassandra 
  thrift_host='127.0.0.2' keyspace='foo' column_family='colfam';

--error ER_CONNECT_TO_FOREIGN_DATA_SOURCE
create table t1 (rowkey char(10) primary key, column1 char(10)) engine=cassandra 
  thrift_host='localhost' keyspace='no_such_keyspace' column_family='colfam';

# No column family specified
--error ER_CONNECT_TO_FOREIGN_DATA_SOURCE
create table t1 (rowkey char(10) primary key, column1 char(10)) engine=cassandra 
  thrift_host='localhost' keyspace='no_such_keyspace';

############################################################################
## Cassandra initialization
############################################################################

# Step 1: remove the keyspace that could be left over from the previous test
--remove_files_wildcard $MYSQLTEST_VARDIR cassandra_test_cleanup.cql
--write_file $MYSQLTEST_VARDIR/cassandra_test_cleanup.cql
drop keyspace mariadbtest2;
EOF
--error 0,1,2
--system cqlsh -3 -f $MYSQLTEST_VARDIR/cassandra_test_cleanup.cql

# Step 2: create new keyspace and test column families
--remove_files_wildcard $MYSQLTEST_VARDIR cassandra_test_init.cql
--write_file $MYSQLTEST_VARDIR/cassandra_test_init.cql

CREATE KEYSPACE mariadbtest2
  WITH strategy_class = 'org.apache.cassandra.locator.SimpleStrategy'
  AND strategy_options:replication_factor='1';

USE mariadbtest2;
create columnfamily cf1 ( pk varchar primary key, data1 varchar, data2 bigint);

create columnfamily cf2 (rowkey bigint primary key, a bigint);

create columnfamily cf3 (rowkey bigint primary key, intcol int);

create columnfamily cf4 (rowkey bigint primary key, datecol timestamp);

create columnfamily cf5 (rowkey bigint primary key, uuidcol uuid);

create columnfamily cf6 (rowkey uuid primary key, col1 int);

create columnfamily cf7 (rowkey int primary key, boolcol boolean);

create columnfamily cf8 (rowkey varchar primary key, countercol counter);
update cf8 set countercol=countercol+1 where rowkey='cnt1';
update cf8 set countercol=countercol+100 where rowkey='cnt2';

create columnfamily cf9 (rowkey varchar primary key, varint_col varint);
insert into cf9 (rowkey, varint_col) values ('val-01', 1);
insert into cf9 (rowkey, varint_col) values ('val-0x123456', 1193046);
insert into cf9 (rowkey, varint_col) values ('val-0x12345678', 305419896);

create columnfamily cf11 (rowkey varchar primary key, decimal_col decimal);
insert into cf11 (rowkey, decimal_col) values ('val_0.5', 0.5);
insert into cf11 (rowkey, decimal_col) values ('val_1.5', 1.5);
insert into cf11 (rowkey, decimal_col) values ('val_1234', 1234);

create columnfamily cf12 (rowkey varchar primary key, decimal_col decimal);

create columnfamily cf13 (rowkey int primary key, b blob);

create columnfamily cf14 ( rowkey int primary key, a int );

EOF
--error 0,1,2
--system cqlsh -3 -f $MYSQLTEST_VARDIR/cassandra_test_init.cql


# Step 3: Cassandra's CQL doesn't allow certain kinds of queries. Run them in
# CLI
--remove_files_wildcard $MYSQLTEST_VARDIR cassandra_test_init.cli
--write_file $MYSQLTEST_VARDIR/cassandra_test_init.cli
use mariadbtest2;
CREATE COLUMN FAMILY cf10
  WITH comparator = UTF8Type
  AND key_validation_class=UTF8Type
  AND default_validation_class = UTF8Type;

CREATE COLUMN FAMILY cfd1
  WITH comparator = UTF8Type
  AND key_validation_class=UTF8Type 
  AND default_validation_class = UTF8Type;
SET cfd1['1']['[encoded data, 27039 characters]']='1';

CREATE COLUMN FAMILY cfd2 
  WITH comparator = UTF8Type
    AND key_validation_class=Int32Type
    AND default_validation_class = UTF8Type;

EOF

--error 0,1,2
--system cassandra-cli -f $MYSQLTEST_VARDIR/cassandra_test_init.cli

############################################################################
## Cassandra initialization ends
############################################################################

--echo # Now, create a table for real and insert data
create table t1 (pk varchar(36) primary key, data1 varchar(60), data2 bigint) engine=cassandra 
  thrift_host='localhost' keyspace='mariadbtest2' column_family='cf1';

--echo # Just in case there were left-overs from previous:
delete from t1;
select * from t1;

insert into t1 values ('rowkey10', 'data1-value', 123456);
insert into t1 values ('rowkey11', 'data1-value2', 34543);
insert into t1 values ('rowkey12', 'data1-value3', 454);
select * from t1;

explain
select * from t1 where pk='rowkey11';
select * from t1 where pk='rowkey11';

# Deletion functions weirdly: it sets all columns to NULL
#   but when If I do this in cassandra-cli:
#
#     del cf1[ascii('rowkey10')]
#
# Subsequent 'list cf1' command also gives
#
#    RowKey: rowkey10
#
# without any columns.
#
# CQL seems to simply ignore all "incomplete" records. 

delete from t1 where pk='rowkey11';
select * from t1;

delete from t1;
select * from t1;

--echo #
--echo # A query with filesort (check that table_flags() & HA_REC_NOT_IN_SEQ,
--echo #  also check ::rnd_pos()
--echo #
insert into t1 values ('rowkey10', 'data1-value', 123456);
insert into t1 values ('rowkey11', 'data1-value2', 34543);
insert into t1 values ('rowkey12', 'data1-value3', 454);
select * from t1 order by data2;

delete from t1;
drop table t1;

--echo #
--echo # MDEV-476: Cassandra: Server crashes in calculate_key_len on DELETE with ORDER BY
--echo #
CREATE TABLE t1 (rowkey BIGINT PRIMARY KEY, a BIGINT) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf2';

INSERT INTO t1 VALUES (1,1),(2,2);
DELETE FROM t1 ORDER BY a LIMIT 1;

DROP TABLE t1;

--echo #
--echo # Batched INSERT
--echo #
show variables like 'cassandra_insert_batch_size';
show status like 'cassandra_row_insert%';
CREATE TABLE t1 (rowkey BIGINT PRIMARY KEY, a BIGINT) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf2';

delete from t1;
INSERT INTO t1 VALUES (1,1),(2,2);
DELETE FROM t1 ORDER BY a LIMIT 1;

DROP TABLE t1;
show status like 'cassandra_row_insert%';

--echo # FLUSH STATUS doesn't work for our variables, just like with InnoDB.
flush status;
show status like 'cassandra_row_insert%';

--echo #
--echo # Batched Key Access
--echo #

--echo # Control variable (we are not yet able to make use of MRR's buffer)
show variables like 'cassandra_multi%';

--echo # MRR-related status variables:
show status like 'cassandra_multi%';

CREATE TABLE t1 (rowkey BIGINT PRIMARY KEY, a BIGINT) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf2';
delete from t1;
INSERT INTO t1 VALUES (0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);

set @tmp_jcl=@@join_cache_level;
set join_cache_level=8;
explain select * from t1 A, t1 B where B.rowkey=A.a;

select * from t1 A, t1 B where B.rowkey=A.a;
show status like 'cassandra_multi%';

# The following INSERTs are really UPDATEs
insert into t1 values(1, 8);
insert into t1 values(3, 8);
insert into t1 values(5, 8);
insert into t1 values(7, 8);

select * from t1 A, t1 B where B.rowkey=A.a;
show status like 'cassandra_multi%';

delete from t1;
drop table t1;

--echo #
--echo # MDEV-480: TRUNCATE TABLE on a Cassandra table does not remove rows
--echo #
CREATE TABLE t1 (rowkey BIGINT PRIMARY KEY, a BIGINT) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf2';
INSERT INTO t1 VALUES (0,0),(1,1),(2,2);
truncate table t1;
select * from t1;
drop table t1;
 
--echo #
--echo # MDEV-494, part #1: phantom row for big full-scan selects
--echo #
create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

CREATE TABLE t1 (rowkey BIGINT PRIMARY KEY, a BIGINT) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf2';

insert into t1 select A.a + 10 * B.a + 100*C.a, 12345 from t0 A, t0 B, t0 C;

select count(*) from t1;
select count(*) from t1 where a=12345;

delete from t1;
drop table t1;
drop table t0;

--echo # 32-bit INT type support
CREATE TABLE t1 (rowkey BIGINT PRIMARY KEY, intcol INT) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf3';
insert into t1 values (10,10);
insert into t1 values (12,12);
delete from t1;
drop table t1;

--echo # 
--echo # Try accessing column family w/o explicitly defined columns
--echo #
--error ER_INTERNAL_ERROR
CREATE TABLE t1 (my_primary_key varchar(10) PRIMARY KEY) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf10';

CREATE TABLE t1 (rowkey varchar(10) PRIMARY KEY) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf10';

DROP TABLE t1;

--echo # 
--echo # Timestamp datatype support
--echo #
CREATE TABLE t2 (rowkey bigint PRIMARY KEY, datecol timestamp) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf4';

delete from t2;
insert into t2 values (1, '2012-08-29 01:23:45');
select * from t2;
delete from t2;

--echo # MDEV-498: Cassandra: Inserting a timestamp does not work on a 32-bit system
INSERT INTO t2 VALUES (10,'2012-12-12 12:12:12');
SELECT * FROM t2;
delete from t2;

--echo #
--echo # (no MDEV#) Check that insert counters work correctly
--echo #
create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
let $start_inserts=`select variable_value from information_schema.SESSION_STATUS 
                   where variable_name ='Cassandra_row_inserts'`;
let $start_insert_batches=`select variable_value from information_schema.SESSION_STATUS 
                          where variable_name ='Cassandra_row_insert_batches'`;

set cassandra_insert_batch_size=10;
insert into t2 select A.a+10*B.a, now() from t0 A, t0 B;

--disable_query_log
eval select
   (select variable_value - $start_inserts from information_schema.SESSION_STATUS 
     where variable_name ='Cassandra_row_inserts') 
   AS 'inserts',
   (select variable_value - $start_insert_batches from information_schema.SESSION_STATUS 
     where variable_name ='Cassandra_row_insert_batches') 
   AS 'insert_batches';
--enable_query_log

let $start_inserts=`select variable_value from information_schema.SESSION_STATUS 
                   where variable_name ='Cassandra_row_inserts'`;
let $start_insert_batches=`select variable_value from information_schema.SESSION_STATUS 
                          where variable_name ='Cassandra_row_insert_batches'`;

set cassandra_insert_batch_size=1;
insert into t2 select A.a+10*B.a+100, now() from t0 A, t0 B;

--disable_query_log
eval select
   (select variable_value - $start_inserts from information_schema.SESSION_STATUS 
     where variable_name ='Cassandra_row_inserts') 
   AS 'inserts',
   (select variable_value - $start_insert_batches from information_schema.SESSION_STATUS 
     where variable_name ='Cassandra_row_insert_batches') 
   AS 'insert_batches';
--enable_query_log

delete from t2;
drop table t2;
drop table t0;

--echo # 
--echo # UUID datatype support
--echo #
#create columnfamily cf5 (rowkey bigint primary key, uuidcol uuid);
CREATE TABLE t2 (rowkey bigint PRIMARY KEY, uuidcol char(36)) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf5';
delete from t2;

insert into t2 values(1,'9b5658dc-f32f-11e1-94cd-f46d046e9f09');

--error ER_WARN_DATA_OUT_OF_RANGE
insert into t2 values(2,'not-an-uuid');

--error ER_WARN_DATA_OUT_OF_RANGE
insert into t2 values(3,'9b5658dc-f32f-11e1=94cd-f46d046e9f09');

--error ER_WARN_DATA_OUT_OF_RANGE
insert into t2 values(4,'9b5658dc-fzzf-11e1-94cd-f46d046e9f09');

--error ER_WARN_DATA_OUT_OF_RANGE
insert into t2 values
 (5,'9b5658dc-f11f-11e1-94cd-f46d046e9f09'),
 (6,'9b5658dc-f11f011e1-94cd-f46d046e9f09');

select * from t2;

delete from t2;
drop table t2;

# create columnfamily cf6 (rowkey uuid primary key, col1 int);
CREATE TABLE t2 (rowkey char(36) PRIMARY KEY, col1 int) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf6';
delete from t2;

insert into t2 values('9b5658dc-f32f-11e1-94cd-f46d046e9f09', 1234);

--error ER_WARN_DATA_OUT_OF_RANGE
insert into t2 values('not-an-uuid', 563);

select * from t2;
delete from t2;
drop table t2;


--echo # 
--echo # boolean datatype support
--echo #
# create columnfamily cf7 (rowkey int primary key, boolcol boolean);
CREATE TABLE t2 (rowkey int PRIMARY KEY, boolcol bool) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf7';
insert into t2 values (0, 0);
insert into t2 values (1, 1);
select * from t2;
delete from t2;
drop table t2;


--echo # 
--echo # Counter datatype support (read-only)
--echo #
# create columnfamily cf8 (rowkey int primary key, countercol counter);
CREATE TABLE t2 (rowkey varchar(32) PRIMARY KEY, countercol bigint) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf8';
select * from t2;
drop table t2;

--echo # 
--echo # Check that @@cassandra_default_thrift_host works
--echo #
show variables like 'cassandra_default_thrift_host';
set @tmp=@@cassandra_default_thrift_host;
--error ER_GLOBAL_VARIABLE
set cassandra_default_thrift_host='localhost';
set global cassandra_default_thrift_host='localhost';

--echo # Try creating a table without specifying thrift_host:
CREATE TABLE t2 (rowkey varchar(32) PRIMARY KEY, countercol bigint) ENGINE=CASSANDRA
  keyspace='mariadbtest2' column_family = 'cf8';
select * from t2;
drop table t2;

set global cassandra_default_thrift_host=@tmp;

--echo #
--echo # Consistency settings
--echo #
show variables like 'cassandra_%consistency';
set @tmp=@@cassandra_write_consistency;

--echo # Unfortunately, there is no easy way to check if setting have the effect..
set cassandra_write_consistency='ONE';
set cassandra_write_consistency='QUORUM';
set cassandra_write_consistency='LOCAL_QUORUM';
set cassandra_write_consistency='EACH_QUORUM';
set cassandra_write_consistency='ALL';
set cassandra_write_consistency='ANY';
set cassandra_write_consistency='TWO';
set cassandra_write_consistency='THREE';

set cassandra_write_consistency=@tmp;

--echo #
--echo # varint datatype support
--echo #
# create columnfamily cf9 (rowkey varchar primary key, varint_col varint);
CREATE TABLE t2 (rowkey varchar(32) PRIMARY KEY, varint_col varbinary(32)) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf9';
--sorted_result
select rowkey, hex(varint_col) from t2;
drop table t2;

--echo # now, let's check what happens when MariaDB's column is not wide enough:
CREATE TABLE t2 (rowkey varchar(32) PRIMARY KEY, varint_col varbinary(2)) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf9';
--sorted_result
--error ER_INTERNAL_ERROR
select rowkey, hex(varint_col) from t2;
drop table t2;

--echo #
--echo # Decimal datatype support
--echo #
CREATE TABLE t2 (rowkey varchar(32) PRIMARY KEY, decimal_col varbinary(32)) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf11';
select rowkey, hex(decimal_col) from t2;
drop table t2;

--echo #
--echo # Mapping TIMESTAMP -> int64
--echo #
set @save_tz= @@time_zone;
set time_zone='UTC';
CREATE TABLE t2 (rowkey bigint PRIMARY KEY, datecol timestamp) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf4';
insert into t2 values (1, '2012-08-29 01:23:45');
INSERT INTO t2 VALUES (10,'2012-08-29 01:23:46');
drop table t2;

CREATE TABLE t2 (rowkey bigint PRIMARY KEY, datecol bigint) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf4';
select * from t2;
delete from t2;
drop table t2;
set time_zone=@save_tz;

--echo #
--echo # Check whether changing parameters with ALTER TABLE works.
--echo #
CREATE TABLE t2 (rowkey varchar(32) PRIMARY KEY, decimal_col varbinary(32)) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf11';

#--error ER_INTERNAL_ERROR
#alter table t2 column_family='cf9';

drop table t2;

CREATE TABLE t2 (rowkey varchar(32) PRIMARY KEY, decimal_col varbinary(32)) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf11';

let $c1= `select variable_value from information_schema.global_status 
          where variable_name like 'cassandra_row_inserts'`;
alter table t2 column_family='cf12';
let $c2= `select variable_value from information_schema.global_status 
          where variable_name like 'cassandra_row_inserts'`;

--disable_query_log
eval select ($c2 - $c1) as 'Writes made during ALTER TABLE';
--enable_query_log

drop table t2;

--echo #
--echo # UPDATE command support
--echo #
create table t1 (pk varchar(36) primary key, data1 varchar(60), data2 bigint) engine=cassandra 
  thrift_host='localhost' keyspace='mariadbtest2' column_family='cf1';

insert into t1 values ('rowkey10', 'data1-value', 123456);
insert into t1 values ('rowkey11', 'data1-value2', 34543);
insert into t1 values ('rowkey12', 'data1-value3', 454);
select * from t1;

update t1 set data1='updated-1' where pk='rowkey11';
select * from t1;
update t1 set pk='new-rowkey12' where pk='rowkey12';
select * from t1;

delete from t1;
drop table t1;

--echo #
--echo # Dynamic columns support
--echo #
CREATE TABLE t2 (rowkey bigint PRIMARY KEY, uuidcol blob DYNAMIC_COLUMN_STORAGE=1) ENGINE=CASSANDRA thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf5';
drop table t2;

--echo #error: dynamic column is not a blob
--error ER_WRONG_FIELD_SPEC
CREATE TABLE t2 (rowkey bigint PRIMARY KEY, uuidcol char(36) DYNAMIC_COLUMN_STORAGE=1) ENGINE=CASSANDRA thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf5';

--echo #error: double dynamic column
--error ER_WRONG_FIELD_SPEC
CREATE TABLE t2 (rowkey bigint PRIMARY KEY, uuidcol blob DYNAMIC_COLUMN_STORAGE=1, textcol blob DYNAMIC_COLUMN_STORAGE=1) ENGINE=CASSANDRA thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf5';

--echo #
--echo # Dynamic column read
--echo #
#prepare data
CREATE TABLE t2 (rowkey bigint PRIMARY KEY, uuidcol char(36)) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf5';
delete from t2;
insert into t2 values(1,'9b5658dc-f32f-11e1-94cd-f46d046e9f09');
insert into t2 values(2,'9b5658dc-f32f-11e1-94cd-f46d046e9f0a');
drop table t2;

#test dynamic column read
CREATE TABLE t2 (rowkey bigint PRIMARY KEY, dyn blob DYNAMIC_COLUMN_STORAGE=yes) ENGINE=CASSANDRA thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf5';
select rowkey, column_list(dyn), column_get(dyn, 'uuidcol' as char) from t2;
drop table t2;

#cleanup data
CREATE TABLE t2 (rowkey bigint PRIMARY KEY, uuidcol char(36)) ENGINE=CASSANDRA
  thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf5';
delete from t2;
drop table t2;

--echo #
--echo # Dynamic column insert
--echo #
CREATE TABLE t2 (rowkey bigint PRIMARY KEY, dyn blob DYNAMIC_COLUMN_STORAGE=yes) ENGINE=CASSANDRA thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf5';
insert into t2 values (1, column_create("dyn1", 1, "dyn2", "two")); 
select rowkey, column_json(dyn) from t2;
delete from t2;
drop table t2;
--echo # bigint
CREATE TABLE t1 (rowkey bigint PRIMARY KEY, dyn blob DYNAMIC_COLUMN_STORAGE=yes) ENGINE=CASSANDRA thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf2';
insert into t1 values (1, column_create("dyn1", 1, "dyn2", "two", 'a', 254324)); 
insert into t1 values (2, column_create("dyn1", 1, "dyn2", "two", 'a', 2543)); 
select rowkey, column_json(dyn) from t1;
delete from t1;
drop table t1;
--echo # int
CREATE TABLE t1 (rowkey bigint PRIMARY KEY, dyn blob DYNAMIC_COLUMN_STORAGE=yes) ENGINE=CASSANDRA thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf3';
insert into t1 values (1, column_create("dyn1", 1, "dyn2", "two", 'intcol', 254324)); 
insert into t1 values (2, column_create("dyn1", 1, "dyn2", "two", 'intcol', 2543)); 
select rowkey, column_json(dyn) from t1;
delete from t1;
drop table t1;
--echo # timestamp
CREATE TABLE t1 (rowkey bigint PRIMARY KEY, dyn blob DYNAMIC_COLUMN_STORAGE=yes) ENGINE=CASSANDRA thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf4';
insert into t1 values (1, column_create("dyn1", 1, "dyn2", "two", 'datecol', 254324)); 
insert into t1 values (2, column_create("dyn1", 1, "dyn2", "two", 'datecol', 2543)); 
select rowkey, column_json(dyn) from t1;
delete from t1;
drop table t1;
--echo # boolean
CREATE TABLE t1 (rowkey int PRIMARY KEY, dyn blob DYNAMIC_COLUMN_STORAGE=yes) ENGINE=CASSANDRA thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cf7';
insert into t1 values (1, column_create("dyn1", 1, "dyn2", "two", 'boolcol', 254324)); 
insert into t1 values (2, column_create("dyn1", 1, "dyn2", "two", 'boolcol', 0)); 
select rowkey, column_json(dyn) from t1;
select rowkey, column_json(dyn) from t1;
update t1 set dyn=column_add(dyn, "dyn2", null, "dyn3", "3"); 
select rowkey, column_json(dyn) from t1;
update t1 set dyn=column_add(dyn, "dyn1", null) where rowkey= 1;
select rowkey, column_json(dyn) from t1;
update t1 set dyn=column_add(dyn, "dyn3", null, "a", "ddd");
select rowkey, column_json(dyn) from t1;
update t1 set dyn=column_add(dyn, "12345678901234", "ddd");
select rowkey, column_json(dyn) from t1;
update t1 set dyn=column_add(dyn, "12345678901234", null);
select rowkey, column_json(dyn) from t1;
update t1 set dyn=column_add(dyn, 'boolcol', null) where rowkey= 2;
select rowkey, column_json(dyn) from t1;
update t1 set rowkey= 3, dyn=column_add(dyn, "dyn1", null, 'boolcol', 0) where rowkey= 2;
select rowkey, column_json(dyn) from t1;
delete from t1;
drop table t1;

CREATE TABLE t1 (rowkey varchar(10) PRIMARY KEY, dyn blob DYNAMIC_COLUMN_STORAGE=yes) ENGINE=CASSANDRA thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cfd1';
--error ER_INTERNAL_ERROR
select * from t1;
drop table t1;

# MDEV-560
CREATE TABLE t1 (rowkey int PRIMARY KEY, dyn blob DYNAMIC_COLUMN_STORAGE=yes) 
ENGINE=CASSANDRA thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cfd2';
DELETE FROM t1;
insert into t1 values (1, column_create("dyn", 1));
select rowkey, column_list(dyn) from t1;
# Cleanup
delete from t1;
DROP TABLE t1;

# MDEV-561 (incorrect format data to dynamic column)
CREATE TABLE t1 (rowkey int PRIMARY KEY, dyn blob DYNAMIC_COLUMN_STORAGE=yes) 
ENGINE=CASSANDRA thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cfd2';
--error ER_DYN_COL_WRONG_FORMAT
insert into t1 values (1,'9b5658dc-f32f-11e1-94cd-f46d046e9f0a');
delete from t1;
DROP TABLE t1;

--echo #
--echo # MDEV-565: Server crashes in ha_cassandra::write_row on
--echo # inserting NULL into a dynamic column
--echo #
CREATE TABLE t1 (rowkey int PRIMARY KEY, dyn blob DYNAMIC_COLUMN_STORAGE=yes) 
ENGINE=CASSANDRA thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cfd2';
insert into t1 values (1, NULL);
delete from t1;
DROP TABLE t1;

--echo #
--echo # strange side effect of Cassandra - remiving all columns of primary
--echo # key removes all row.
--echo #
CREATE TABLE t1 (rowkey int PRIMARY KEY, dyn blob DYNAMIC_COLUMN_STORAGE=yes) 
ENGINE=CASSANDRA thrift_host='localhost' keyspace='mariadbtest2' column_family = 'cfd2';
INSERT INTO t1 VALUES(2,column_create("ab","ab"));
select rowkey, column_json(dyn) from t1;
UPDATE t1 set dyn=NULL;
select rowkey, column_json(dyn) from t1;
INSERT INTO t1 VALUES(2,column_create("ab","ab"));
select rowkey, column_json(dyn) from t1;
UPDATE t1 set dyn="";
select rowkey, column_json(dyn) from t1;
delete from t1;
DROP TABLE t1;

--echo #
--echo # MDEV-4005 #Server crashes on creating a Cassandra table
--echo # with a mix of static and dynamic columns
--echo #
--disable_warnings
DROP TABLE IF EXISTS t1, t2;
--enable_warnings

--remove_files_wildcard $MYSQLTEST_VARDIR cassandra_test_cleanup.cql
--write_file $MYSQLTEST_VARDIR/cassandra_test_cleanup.cql
drop keyspace bug;
EOF
--error 0,1,2
--system cqlsh -3 -f $MYSQLTEST_VARDIR/cassandra_test_cleanup.cql

--remove_files_wildcard $MYSQLTEST_VARDIR cassandra_test_init.cql
--write_file $MYSQLTEST_VARDIR/cassandra_test_init.cql

CREATE KEYSPACE bug
  WITH strategy_class = 'org.apache.cassandra.locator.SimpleStrategy'
  AND strategy_options:replication_factor='1';

USE bug;
create columnfamily cf1 ( pk int primary key, col_int int, a bigint );
EOF

--system cqlsh -3 -f $MYSQLTEST_VARDIR/cassandra_test_init.cql


CREATE TABLE t1 (
 pk int primary key,
 col_int int,
 dyncol blob DYNAMIC_COLUMN_STORAGE=yes
) ENGINE=cassandra keyspace='bug' thrift_host = '127.0.0.1' column_family='cf1';

drop table t1;


--echo #
--echo # MDEV-4000: Mapping between Cassandra blob (BytesType) and MySQL BLOB does not work
--echo #

create table t1 (rowkey int primary key, b blob ) ENGINE=CASSANDRA thrift_host = '127.0.0.1' `keyspace`='mariadbtest2' `column_family`='cf13';

insert into t1 values (1, 'fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo-bar');
insert into t1 values (2, 'qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq-baz');

select * from t1;
drop table t1;

--echo #
--echo # MDEV-4001: Cassandra: server crashes in ha_cassandra::end_bulk_insert on INSERT .. SELECT with a non-existing column
--echo #
create table t1 (rowkey int primary key, a int)  ENGINE=cassandra thrift_host='127.0.0.1' keyspace='mariadbtest2' column_family='cf14';
--error ER_BAD_FIELD_ERROR
insert into t1 (a) select b from t1;
drop table t1;

############################################################################
## Cassandra cleanup
############################################################################
--disable_parsing
drop columnfamily cf1;
drop columnfamily cf2;
drop columnfamily cf3;
drop columnfamily cf4;
drop columnfamily cf5;
drop columnfamily cf6;
drop columnfamily cf7;

drop columnfamily cf13;
drop columnfamily cf14;
--enable_parsing
############################################################################
## Cassandra cleanup ends
############################################################################

