1--source include/have_rocksdb.inc
2--source include/have_partition.inc
3--source include/have_write_committed.inc
4
5#
6# RocksDB Storage Engine tests
7#
8select ENGINE,COMMENT,TRANSACTIONS,XA,SAVEPOINTS from information_schema.engines where engine = 'rocksdb';
9
10--disable_warnings
11drop table if exists t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
12drop table if exists t11,t12,t13,t14,t15,t16,t17,t18,t19,t20;
13drop table if exists t21,t22,t23,t24,t25,t26,t27,t28,t29;
14drop table if exists t30,t31,t32,t33,t34,t35,t36,t37,t38,t39;
15drop table if exists t40,t41,t42,t43,t44,t45,t46,t47,t48,t49;
16--enable_warnings
17
18# Disable background compaction to prevent stats from affect explain output
19SET @ORIG_PAUSE_BACKGROUND_WORK = @@ROCKSDB_PAUSE_BACKGROUND_WORK;
20SET GLOBAL ROCKSDB_PAUSE_BACKGROUND_WORK = 1;
21
22--echo #
23--echo # Issue #1: Don't update indexes if index values have not changed
24--echo #
25# [Jay Edgar] I moved this test first because it uses the
26# rocksdb_number_keys_written value, but this value is affected out of band
27# by drop tables.  There is a background thread that periodically processes
28# through the list of dropped keys and if any are gone from the database it
29# deletes information related to the key - and this delete causes this count
30# to be incorrect.  I moved this test first and made the whole test require
31# a fresh server to hopefully avoid tihs.
32create table t1 (
33  pk int primary key,
34  a int,
35  b int,
36  key(a)
37) engine=rocksdb;
38
39insert into t1 values
40(1,1,1), (2,2,2), (3,3,3), (4,4,4);
41
42set @var1=(select variable_value
43           from information_schema.global_status
44           where variable_name='rocksdb_number_keys_written');
45
46--echo # Do an update that doesn't change the key 'a'.
47update t1 set b=3334341 where a=2;
48
49set @var2=(select variable_value
50           from information_schema.global_status
51           where variable_name='rocksdb_number_keys_written');
52--echo # The following should produce 1
53select @var2 - @var1;
54
55--echo # Do an update that sets the key to the same value
56update t1 set a=pk where a=3;
57set @var3=(select variable_value
58           from information_schema.global_status
59           where variable_name='rocksdb_number_keys_written');
60--echo # We have 'updated' column to the same value, so the following must return 0:
61select @var3 - @var2;
62drop table t1;
63
64create table t0 (a int primary key) engine=rocksdb;
65show create table t0;
66drop table t0;
67
68create table t1 (a int primary key, b int) engine=rocksdb;
69insert into t1 values (1,1);
70insert into t1 values (2,2);
71
72select * from t1;
73
74--echo # Check that we can create another table and insert there
75create table t2 (a varchar(10) primary key, b varchar(10)) engine=rocksdb;
76insert into t2 value ('abc','def');
77insert into t2 value ('hijkl','mnopq');
78select * from t2;
79
80--echo # Select again from t1 to see that records from different tables dont mix
81select * from t1;
82
83explain select * from t2 where a='no-such-key';
84--replace_column 9 #
85explain select * from t2 where a='abc';
86select * from t2 where a='abc';
87
88--echo # Try a composite PK
89create table t3 (
90  pk1 int,
91  pk2 varchar(10),
92  col1 varchar(10),
93  primary key(pk1, pk2)
94) engine=rocksdb;
95
96insert into t3 values (2,'two', 'row#2');
97insert into t3 values (3,'three', 'row#3');
98insert into t3 values (1,'one', 'row#1');
99
100select * from t3;
101select * from t3 where pk1=3 and pk2='three';
102
103drop table t1, t2, t3;
104
105--echo #
106--echo # Test blob values
107--echo #
108
109create table t4 (a int primary key, b blob) engine=rocksdb;
110insert into t4 values (1, repeat('quux-quux', 60));
111insert into t4 values (10, repeat('foo-bar', 43));
112insert into t4 values (5, repeat('foo-bar', 200));
113
114insert into t4 values (2, NULL);
115
116
117select
118 a,
119 (case a
120   when 1  then b=repeat('quux-quux', 60)
121   when 10 then b=repeat('foo-bar', 43)
122   when 5  then b=repeat('foo-bar', 200)
123   when 2  then b is null
124   else 'IMPOSSIBLE!' end) as CMP
125from t4;
126
127drop table t4;
128
129--echo #
130--echo # Test blobs of various sizes
131--echo #
132
133--echo # TINYBLOB
134create table t5 (a int primary key, b tinyblob) engine=rocksdb;
135insert into t5 values (1, repeat('quux-quux', 6));
136insert into t5 values (10, repeat('foo-bar', 4));
137insert into t5 values (5, repeat('foo-bar', 2));
138select
139 a,
140 (case a
141   when 1  then b=repeat('quux-quux', 6)
142   when 10 then b=repeat('foo-bar', 4)
143   when 5  then b=repeat('foo-bar', 2)
144   else 'IMPOSSIBLE!' end) as CMP
145from t5;
146drop table t5;
147
148--echo # MEDIUMBLOB
149create table t6 (a int primary key, b mediumblob) engine=rocksdb;
150insert into t6 values (1, repeat('AB', 65000));
151insert into t6 values (10, repeat('bbb', 40000));
152insert into t6 values (5, repeat('foo-bar', 2));
153select
154 a,
155 (case a
156   when 1  then b=repeat('AB', 65000)
157   when 10 then b=repeat('bbb', 40000)
158   when 5  then b=repeat('foo-bar', 2)
159   else 'IMPOSSIBLE!' end) as CMP
160from t6;
161drop table t6;
162
163--echo # LONGBLOB
164create table t7 (a int primary key, b longblob) engine=rocksdb;
165insert into t7 values (1, repeat('AB', 65000));
166insert into t7 values (10, repeat('bbb', 40000));
167insert into t7 values (5, repeat('foo-bar', 2));
168select
169 a,
170 (case a
171   when 1  then b=repeat('AB', 65000)
172   when 10 then b=repeat('bbb', 40000)
173   when 5  then b=repeat('foo-bar', 2)
174   else 'IMPOSSIBLE!' end) as CMP
175from t7;
176drop table t7;
177
178
179--echo #
180--echo # Check if DELETEs work
181--echo #
182create table t8 (a varchar(10) primary key, col1 varchar(12)) engine=rocksdb;
183
184insert into t8 values
185 ('one', 'eins'),
186 ('two', 'zwei'),
187 ('three', 'drei'),
188 ('four', 'vier'),
189 ('five', 'funf');
190
191--echo # Delete by PK
192--replace_column 9 #
193explain delete from t8 where a='three';
194delete from t8 where a='three';
195
196select * from t8;
197
198--echo # Delete while doing a full table scan
199delete from t8 where col1='eins' or col1='vier';
200select * from t8;
201
202--echo # delete w/o WHERE:
203delete from t8;
204select * from t8;
205
206--echo #
207--echo # Test UPDATEs
208--echo #
209insert into t8 values
210 ('one', 'eins'),
211 ('two', 'zwei'),
212 ('three', 'drei'),
213 ('four', 'vier'),
214 ('five', 'funf');
215
216update t8 set col1='dva' where a='two';
217
218update t8 set a='fourAAA' where col1='vier';
219
220select * from t8;
221delete from t8;
222
223--echo #
224--echo # Basic transactions tests
225--echo #
226begin;
227insert into t8 values ('trx1-val1', 'data');
228insert into t8 values ('trx1-val2', 'data');
229rollback;
230select * from t8;
231
232begin;
233insert into t8 values ('trx1-val1', 'data');
234insert into t8 values ('trx1-val2', 'data');
235commit;
236select * from t8;
237
238drop table t8;
239
240--echo #
241--echo # Check if DROP TABLE works
242--echo #
243create table t8 (a varchar(10) primary key, col1 varchar(12)) engine=rocksdb;
244select * from t8;
245insert into t8 values ('foo','foo');
246drop table t8;
247create table t8 (a varchar(10) primary key, col1 varchar(12)) engine=rocksdb;
248select * from t8;
249drop table t8;
250
251--echo #
252--echo # MDEV-3961: Assertion ... on creating a TEMPORARY RocksDB table
253--echo #
254--error ER_ILLEGAL_HA_CREATE_OPTION
255CREATE TEMPORARY TABLE t10 (pk INT PRIMARY KEY) ENGINE=RocksDB;
256
257--echo #
258--echo # MDEV-3963: JOIN or WHERE conditions involving keys on RocksDB tables don't work
259--echo #
260CREATE TABLE t10 (i INT PRIMARY KEY) ENGINE=RocksDB;
261INSERT INTO t10 VALUES (1),(3);
262CREATE TABLE t11 (j INT PRIMARY KEY) ENGINE=RocksDB;
263INSERT INTO t11 VALUES (1),(4);
264
265select * from t10;
266select * from t11;
267--replace_column 9 #
268EXPLAIN
269SELECT * FROM t10, t11 WHERE i=j;
270SELECT * FROM t10, t11 WHERE i=j;
271
272DROP TABLE t10,t11;
273
274--echo #
275--echo # MDEV-3962: SELECT with ORDER BY causes "ERROR 1030 (HY000): Got error 122
276--echo #
277CREATE TABLE t12 (pk INT PRIMARY KEY) ENGINE=RocksDB;
278INSERT INTO t12 VALUES (2),(1);
279SELECT * FROM t12 ORDER BY pk;
280DROP TABLE t12;
281
282--echo #
283--echo # MDEV-3964: Assertion `!pk_descr' fails in ha_rocksdb::open on adding partitions ...
284--echo #
285create table t14 (pk int primary key) engine=RocksDB partition by hash(pk) partitions 2;
286#--error ER_GET_ERRNO
287#alter table t14 add partition partitions 2;
288# ^^ works, but causes weird warnings in error log.
289drop table t14;
290
291--echo #
292--echo # MDEV-3960: Server crashes on running DISCARD TABLESPACE on a RocksDB table
293--echo #
294create table t9 (i int primary key) engine=rocksdb;
295--error ER_ILLEGAL_HA
296alter table t9 discard tablespace;
297drop table t9;
298
299--echo #
300--echo # MDEV-3959: Assertion `slice->size() == table->s->reclength' fails ...
301--echo #   on accessing a table after ALTER
302--echo #
303CREATE TABLE t15 (a INT, rocksdb_pk INT PRIMARY KEY) ENGINE=RocksDB;
304INSERT INTO t15 VALUES (1,1),(5,2);
305#--error ER_ILLEGAL_HA
306ALTER TABLE t15 DROP COLUMN a;
307DROP TABLE t15;
308
309--echo #
310--echo # MDEV-3968: UPDATE produces a wrong result while modifying a PK on a RocksDB table
311--echo #
312create table t16 (pk int primary key, a char(8)) engine=RocksDB;
313insert into t16 values (1,'a'),(2,'b'),(3,'c'),(4,'d');
314
315#
316# Not anymore: The following query will still eat a record because of CANT-SEE-OWN-CHANGES
317# property.
318#
319--error ER_DUP_ENTRY
320update t16 set pk=100, a = 'updated' where a in ('b','c');
321select * from t16;
322drop table t16;
323
324--echo #
325--echo # MDEV-3970: A set of assorted crashes on inserting a row into a RocksDB table
326--echo #
327--disable_warnings
328drop table if exists t_very_long_table_name;
329--enable_warnings
330
331CREATE TABLE `t_very_long_table_name` (
332   `c` char(1) NOT NULL,
333   `c0` char(0) NOT NULL,
334   `c1` char(1) NOT NULL,
335   `c20` char(20) NOT NULL,
336   `c255` char(255) NOT NULL,
337   PRIMARY KEY (`c255`)
338 ) ENGINE=RocksDB DEFAULT CHARSET=latin1;
339INSERT INTO t_very_long_table_name VALUES ('a', '', 'c', REPEAT('a',20), REPEAT('x',255));
340drop table t_very_long_table_name;
341
342
343--echo #
344--echo # Test table locking and read-before-write checks.
345--echo #
346create table t17 (pk varchar(12) primary key, col1 varchar(12)) engine=rocksdb;
347insert into t17 values ('row1', 'val1');
348
349--error ER_DUP_ENTRY
350insert into t17 values ('row1', 'val1-try2');
351--error ER_DUP_ENTRY
352insert into t17 values ('ROW1', 'val1-try2');
353
354insert into t17 values ('row2', 'val2');
355insert into t17 values ('row3', 'val3');
356
357--echo # This is ok
358update t17 set pk='row4' where pk='row1';
359
360--echo # This will try to overwrite another row:
361--error ER_DUP_ENTRY
362update t17 set pk='row3' where pk='row2';
363
364select * from t17;
365
366--echo #
367--echo # Locking tests
368--echo #
369
370connect (con1,localhost,root,,);
371
372--echo # First, make sure there's no locking when transactions update different rows
373connection con1;
374set autocommit=0;
375update t17 set col1='UPD1' where pk='row2';
376
377connection default;
378update t17 set col1='UPD2' where pk='row3';
379
380connection con1;
381commit;
382
383connection default;
384select * from t17;
385
386--echo # Check the variable
387show variables like 'rocksdb_lock_wait_timeout';
388set rocksdb_lock_wait_timeout=2; # seconds
389show variables like 'rocksdb_lock_wait_timeout';
390
391--echo # Try updating the same row from two transactions
392connection con1;
393begin;
394update t17 set col1='UPD2-AA' where pk='row2';
395
396connection default;
397--error ER_LOCK_WAIT_TIMEOUT
398update t17 set col1='UPD2-BB' where pk='row2';
399
400set rocksdb_lock_wait_timeout=1000; # seconds
401--send
402  update t17 set col1='UPD2-CC' where pk='row2';
403
404connection con1;
405rollback;
406
407connection default;
408reap;
409select * from t17 where pk='row2';
410
411drop table t17;
412
413disconnect con1;
414--echo #
415--echo #  MDEV-4035: RocksDB: SELECT produces different results inside a transaction (read is not repeatable)
416--echo #
417--enable_connect_log
418
419create table t18 (pk int primary key, i int) engine=RocksDB;
420begin;
421select * from t18;
422select * from t18 where pk = 1;
423
424--connect (con1,localhost,root,,)
425insert into t18 values (1,100);
426
427--connection default
428select * from t18;
429select * from t18 where pk = 1;
430commit;
431
432drop table t18;
433
434--echo #
435--echo # MDEV-4036: RocksDB: INSERT .. ON DUPLICATE KEY UPDATE does not work, produces ER_DUP_KEY
436--echo #
437create table t19 (pk int primary key, i int) engine=RocksDB;
438insert into t19 values (1,1);
439insert into t19 values (1,100) on duplicate key update i = 102;
440select * from t19;
441drop table t19;
442
443--echo # MDEV-4037: RocksDB: REPLACE doesn't work, produces ER_DUP_KEY
444create table t20 (pk int primary key, i int) engine=RocksDB;
445insert into t20 values (1,1);
446replace into t20 values (1,100);
447select * from t20;
448drop table t20;
449
450--echo #
451--echo # MDEV-4041: Server crashes in Primary_key_comparator::get_hashnr on INSERT
452--echo #
453create table t21 (v varbinary(16) primary key, i int) engine=RocksDB;
454insert into t21 values ('a',1);
455select * from t21;
456drop table t21;
457
458--echo #
459--echo # MDEV-4047: RocksDB: Assertion `0' fails in Protocol::end_statement() on multi-table INSERT IGNORE
460--echo #
461
462CREATE TABLE t22 (a int primary key) ENGINE=RocksDB;
463INSERT INTO t22 VALUES (1),(2);
464CREATE TABLE t23 (b int primary key) ENGINE=RocksDB;
465INSERT INTO t23 SELECT * FROM t22;
466DELETE IGNORE t22.*, t23.* FROM t22, t23 WHERE b < a;
467DROP TABLE t22,t23;
468
469--echo #
470--echo # MDEV-4046: RocksDB: Multi-table DELETE locks itself and ends with ER_LOCK_WAIT_TIMEOUT
471--echo #
472CREATE TABLE t24 (pk int primary key) ENGINE=RocksDB;
473INSERT INTO t24 VALUES (1),(2);
474
475CREATE TABLE t25 LIKE t24;
476INSERT INTO t25 SELECT * FROM t24;
477
478DELETE t25.* FROM t24, t25;
479DROP TABLE t24,t25;
480
481--echo #
482--echo # MDEV-4044: RocksDB: UPDATE or DELETE with ORDER BY locks itself
483--echo #
484create table t26 (pk int primary key, c char(1)) engine=RocksDB;
485insert into t26 values (1,'a'),(2,'b');
486update t26 set c = 'x' order by pk limit 1;
487delete from t26 order by pk limit 1;
488select * from t26;
489drop table t26;
490
491
492--echo #
493--echo # Test whether SELECT ... FOR UPDATE puts locks
494--echo #
495create table t27(pk varchar(10) primary key, col1 varchar(20)) engine=RocksDB;
496insert into t27 values
497  ('row1', 'row1data'),
498  ('row2', 'row2data'),
499  ('row3', 'row3data');
500
501connection con1;
502begin;
503select * from t27 where pk='row3' for update;
504
505connection default;
506set rocksdb_lock_wait_timeout=1;
507--error ER_LOCK_WAIT_TIMEOUT
508update t27 set col1='row2-modified' where pk='row3';
509
510connection con1;
511rollback;
512connection default;
513disconnect con1;
514
515drop table t27;
516
517--echo #
518--echo # MDEV-4060: RocksDB: Assertion `! trx->batch' fails in
519--echo #
520create table t28 (pk int primary key, a int) engine=RocksDB;
521insert into t28 values (1,10),(2,20);
522begin;
523update t28 set a = 100 where pk = 3;
524rollback;
525select * from t28;
526drop table t28;
527
528
529--echo #
530--echo # Secondary indexes
531--echo #
532create table t30 (
533  pk varchar(16) not null primary key,
534  key1 varchar(16) not null,
535  col1 varchar(16) not null,
536  key(key1)
537) engine=rocksdb;
538
539insert into t30 values ('row1', 'row1-key', 'row1-data');
540insert into t30 values ('row2', 'row2-key', 'row2-data');
541insert into t30 values ('row3', 'row3-key', 'row3-data');
542
543--replace_column 9 #
544explain
545select * from t30 where key1='row2-key';
546select * from t30 where key1='row2-key';
547
548--replace_column 9 #
549explain
550select * from t30 where key1='row1';
551--echo # This will produce nothing:
552select * from t30 where key1='row1';
553
554--replace_column 9 #
555explain
556select key1 from t30;
557select key1 from t30;
558
559--echo # Create a duplicate record
560insert into t30 values ('row2a', 'row2-key', 'row2a-data');
561
562--echo # Can we see it?
563select * from t30 where key1='row2-key';
564
565delete from t30 where pk='row2';
566select * from t30 where key1='row2-key';
567
568--echo #
569--echo # Range scans on secondary index
570--echo #
571delete from t30;
572insert into t30 values
573  ('row1', 'row1-key', 'row1-data'),
574  ('row2', 'row2-key', 'row2-data'),
575  ('row3', 'row3-key', 'row3-data'),
576  ('row4', 'row4-key', 'row4-data'),
577  ('row5', 'row5-key', 'row5-data');
578
579--replace_column 9 #
580explain
581select * from t30 where key1 <='row3-key';
582select * from t30 where key1 <='row3-key';
583
584--replace_column 9 #
585explain
586select * from t30 where key1 between 'row2-key' and 'row4-key';
587select * from t30 where key1 between 'row2-key' and 'row4-key';
588
589--replace_column 9 #
590explain
591select * from t30 where key1 in ('row2-key','row4-key');
592select * from t30 where key1 in ('row2-key','row4-key');
593
594--replace_column 9 #
595explain
596select key1 from t30 where key1 in ('row2-key','row4-key');
597select key1 from t30 where key1 in ('row2-key','row4-key');
598
599--replace_column 9 #
600explain
601select * from t30 where key1 > 'row1-key' and key1 < 'row4-key';
602select * from t30 where key1 > 'row1-key' and key1 < 'row4-key';
603
604--replace_column 9 #
605explain
606select * from t30 order by key1 limit 3;
607select * from t30 order by key1 limit 3;
608
609--replace_column 9 #
610explain
611select * from t30 order by key1 desc limit 3;
612select * from t30 order by key1 desc limit 3;
613
614--echo #
615--echo # Range scans on primary key
616--echo #
617--replace_column 9 #
618explain
619select * from t30 where pk <='row3';
620select * from t30 where pk <='row3';
621
622--replace_column 9 #
623explain
624select * from t30 where pk between 'row2' and 'row4';
625select * from t30 where pk between 'row2' and 'row4';
626
627--replace_column 9 #
628explain
629select * from t30 where pk in ('row2','row4');
630select * from t30 where pk in ('row2','row4');
631
632--replace_column 9 #
633explain
634select * from t30 order by pk limit 3;
635select * from t30 order by pk limit 3;
636
637drop table t30;
638
639
640--echo #
641--echo # MDEV-3841: RocksDB: Reading by PK prefix does not work
642--echo #
643create table t31 (i int, j int, k int, primary key(i,j,k)) engine=RocksDB;
644insert into t31 values (1,10,100),(2,20,200);
645select * from t31 where i = 1;
646select * from t31 where j = 10;
647select * from t31 where k = 100;
648select * from t31 where i = 1 and j = 10;
649select * from t31 where i = 1 and k = 100;
650select * from t31 where j = 10 and k = 100;
651select * from t31 where i = 1 and j = 10 and k = 100;
652drop table t31;
653
654--echo #
655--echo # MDEV-4055: RocksDB: UPDATE/DELETE by a multi-part PK does not work
656--echo #
657create table t32 (i int, j int, k int, primary key(i,j,k), a varchar(8)) engine=RocksDB;
658insert into t32 values
659  (1,10,100,''),
660  (2,20,200,'');
661select * from t32 where i = 1 and j = 10 and k = 100;
662update t32 set a = 'updated' where i = 1 and j = 10 and k = 100;
663select * from t32;
664drop table t32;
665
666--echo #
667--echo # MDEV-3841: RocksDB: Assertion `0' fails in ha_rocksdb::index_read_map on range select with ORDER BY .. DESC
668--echo #
669CREATE TABLE t33 (pk INT PRIMARY KEY, a CHAR(1)) ENGINE=RocksDB;
670INSERT INTO t33 VALUES (1,'a'),(2,'b');
671SELECT * FROM t33 WHERE pk <= 10 ORDER BY pk DESC;
672DROP TABLE t33;
673
674--echo #
675--echo # MDEV-4081: RocksDB throws error 122 on an attempt to create a table with unique index
676--echo #
677#--error ER_GET_ERRMSG
678--echo #  Unique indexes can be created, but uniqueness won't be enforced
679create table t33 (pk int primary key, u int, unique index(u)) engine=RocksDB;
680drop table t33;
681
682--echo #
683--echo # MDEV-4077: RocksDB: Wrong result (duplicate row) on select with range
684--echo #
685CREATE TABLE t34 (pk INT PRIMARY KEY) ENGINE=RocksDB;
686INSERT INTO t34 VALUES (10),(11);
687SELECT pk FROM t34 WHERE pk > 5 AND pk < 15;
688SELECT pk FROM t34 WHERE pk BETWEEN 5 AND 15;
689SELECT pk FROM t34 WHERE pk > 5;
690SELECT pk FROM t34 WHERE pk < 15;
691drop table t34;
692
693--echo #
694--echo # MDEV-4086: RocksDB does not allow a query with multi-part pk and index and ORDER BY .. DEC
695--echo #
696create table t35 (a int, b int, c int, d int, e int, primary key (a,b,c), key (a,c,d,e)) engine=RocksDB;
697insert into t35 values (1,1,1,1,1),(2,2,2,2,2);
698select * from t35 where a = 1 and c = 1 and d = 1 order by e desc;
699drop table t35;
700
701--echo #
702--echo # MDEV-4084: RocksDB: Wrong result on IN subquery with index
703--echo #
704CREATE TABLE t36 (pk INT PRIMARY KEY, a INT, KEY(a)) ENGINE=RocksDB;
705INSERT INTO t36 VALUES (1,10),(2,20);
706SELECT 3 IN ( SELECT a FROM t36 );
707drop table t36;
708
709--echo #
710--echo # MDEV-4084: RocksDB: Wrong result on IN subquery with index
711--echo #
712CREATE TABLE t37 (pk INT PRIMARY KEY, a INT, b CHAR(1), KEY(a), KEY(a,b))
713  ENGINE=RocksDB;
714INSERT INTO t37 VALUES (1,10,'x'), (2,20,'y');
715SELECT MAX(a) FROM t37 WHERE a < 100;
716DROP TABLE t37;
717
718--echo #
719--echo # MDEV-4090: RocksDB: Wrong result (duplicate rows) on range access with secondary key and ORDER BY DESC
720--echo #
721CREATE TABLE t38 (pk INT PRIMARY KEY, i INT, KEY(i)) ENGINE=RocksDB;
722INSERT INTO t38 VALUES (1,10), (2,20);
723SELECT i FROM t38 WHERE i NOT IN (8) ORDER BY i DESC;
724drop table t38;
725
726--echo #
727--echo # MDEV-4092: RocksDB: Assertion `in_table(pa, a_len)' fails in Rdb_key_def::cmp_full_keys
728--echo #            with a multi-part key and ORDER BY .. DESC
729--echo #
730CREATE TABLE t40 (pk1 INT PRIMARY KEY, a INT, b VARCHAR(1), KEY(b,a)) ENGINE=RocksDB;
731INSERT INTO t40 VALUES (1, 7,'x'),(2,8,'y');
732
733CREATE TABLE t41 (pk2 INT PRIMARY KEY) ENGINE=RocksDB;
734INSERT INTO t41 VALUES (1),(2);
735
736SELECT * FROM t40, t41 WHERE pk1 = pk2 AND b = 'o' ORDER BY a DESC;
737DROP TABLE t40,t41;
738
739--echo #
740--echo # MDEV-4093: RocksDB: IN subquery by secondary key with NULL among values returns true instead of NULL
741--echo #
742CREATE TABLE t42 (pk INT PRIMARY KEY, a INT, KEY(a)) ENGINE=RocksDB;
743INSERT INTO t42 VALUES (1, NULL),(2, 8);
744SELECT ( 3 ) NOT IN ( SELECT a FROM t42 );
745DROP TABLE t42;
746
747--echo #
748--echo # MDEV-4094: RocksDB: Wrong result on SELECT and ER_KEY_NOT_FOUND on
749--echo #            DELETE with search by NULL-able secondary key ...
750--echo #
751CREATE TABLE t43 (pk INT PRIMARY KEY, a INT, b CHAR(1), KEY(a)) ENGINE=RocksDB;
752INSERT INTO t43 VALUES (1,8,'g'),(2,9,'x');
753UPDATE t43 SET pk = 10 WHERE a = 8;
754REPLACE INTO t43 ( a ) VALUES ( 8 );
755REPLACE INTO t43 ( b ) VALUES ( 'y' );
756SELECT * FROM t43 WHERE a = 8;
757DELETE FROM t43 WHERE a = 8;
758DROP TABLE t43;
759
760--echo #
761--echo # Basic AUTO_INCREMENT tests
762--echo #
763create table t44(pk int primary key auto_increment, col1 varchar(12)) engine=rocksdb;
764insert into t44 (col1) values ('row1');
765insert into t44 (col1) values ('row2');
766insert into t44 (col1) values ('row3');
767select * from t44;
768drop table t44;
769
770--echo #
771--echo # ALTER TABLE tests
772--echo #
773create table t45 (pk int primary key, col1 varchar(12)) engine=rocksdb;
774insert into t45 values (1, 'row1');
775insert into t45 values (2, 'row2');
776alter table t45 rename t46;
777select * from t46;
778drop table t46;
779--error ER_BAD_TABLE_ERROR
780drop table t45;
781
782
783--echo #
784--echo # Check Bulk loading
785--echo # Bulk loading used to overwrite existing data
786--echo # Now it fails if there is data overlap with what
787--echo # already exists
788--echo #
789# We exclude rocksdb_max_open_files here because it value is dependent on
790# the value of the servers open_file_limit and is expected to be different
791# across distros and installs
792
793--replace_regex /[a-f0-9]{40}/#/
794show variables
795where
796  variable_name like 'rocksdb%' and
797  variable_name not like 'rocksdb_max_open_files' and
798  variable_name not like 'rocksdb_supported_compression_types';
799
800create table t47 (pk int primary key, col1 varchar(12)) engine=rocksdb;
801insert into t47 values (1, 'row1');
802insert into t47 values (2, 'row2');
803set rocksdb_bulk_load=1;
804insert into t47 values (3, 'row3'),(4, 'row4');
805set rocksdb_bulk_load=0;
806# Check concurrent bulk loading
807--connect (con1,localhost,root,,)
808set rocksdb_bulk_load=1;
809insert into t47 values (10, 'row10'),(11, 'row11');
810--connection default
811set rocksdb_bulk_load=1;
812insert into t47 values (100, 'row100'),(101, 'row101');
813--disconnect con1
814--connection default
815set rocksdb_bulk_load=0;
816--disable_query_log
817let $wait_condition = select count(*) = 8 as c from t47;
818--source include/wait_condition.inc
819--enable_query_log
820select * from t47;
821drop table t47;
822
823--echo #
824--echo # Fix TRUNCATE over empty table (transaction is committed when it wasn't
825--echo # started)
826--echo #
827create table t48(pk int primary key auto_increment, col1 varchar(12)) engine=rocksdb;
828set autocommit=0;
829#--error ER_ILLEGAL_HA
830truncate table t48;
831set autocommit=1;
832drop table t48;
833
834--echo #
835--echo # MDEV-4059: RocksDB: query waiting for a lock cannot be killed until query timeout exceeded
836--echo #
837--enable_connect_log
838
839create table t49 (pk int primary key, a int) engine=RocksDB;
840insert into t49 values (1,10),(2,20);
841begin;
842update t49 set a = 100 where pk = 1;
843
844--connect (con1,localhost,root,,)
845--let $con1_id = `SELECT CONNECTION_ID()`
846set rocksdb_lock_wait_timeout=60;
847set @var1= to_seconds(now());
848send update t49 set a = 1000 where pk = 1;
849
850--connect (con2,localhost,root,,)
851--echo kill query \$con1_id;
852--disable_query_log
853# If we immeditely kill the query - internally the condition broadcast can
854# occur before the lock is waiting on the condition, thus the broadcast call
855# is lost.  Sleep 1 second to avoid this condition.
856--sleep 1
857eval kill query $con1_id;
858--enable_query_log
859--connection con1
860--error ER_QUERY_INTERRUPTED
861--reap
862set @var2= to_seconds(now());
863
864# We expect the time to kill query in con1 should be below
865# rocksdb_lock_wait_timeout (60).
866select if ((@var2 - @var1) < 60, "passed", (@var2 - @var1)) as 'result';
867
868--connection default
869--disconnect con1
870
871commit;
872drop table t49;
873
874--echo #
875--echo # Index-only tests for INT-based columns
876--echo #
877create table t1 (pk int primary key, key1 int, col1 int, key(key1)) engine=rocksdb;
878insert into t1 values (1,1,1);
879insert into t1 values (2,2,2);
880insert into t1 values (-5,-5,-5);
881--echo # INT column uses index-only:
882--replace_column 9 #
883explain
884select key1 from t1 where key1=2;
885select key1 from t1 where key1=2;
886select key1 from t1 where key1=-5;
887drop table t1;
888
889
890create table t2 (pk int primary key, key1 int unsigned, col1 int, key(key1)) engine=rocksdb;
891insert into t2 values (1,1,1), (2,2,2);
892--echo # INT UNSIGNED column uses index-only:
893--replace_column 9 #
894explain
895select key1 from t2 where key1=2;
896select key1 from t2 where key1=2;
897drop table t2;
898
899
900create table t3 (pk bigint primary key, key1 bigint, col1 int, key(key1)) engine=rocksdb;
901insert into t3 values (1,1,1), (2,2,2);
902--echo # BIGINT uses index-only:
903--replace_column 9 #
904explain
905select key1 from t3 where key1=2;
906select key1 from t3 where key1=2;
907drop table t3;
908
909--echo #
910--echo # Index-only reads for string columns
911--echo #
912create table t1 (
913  pk int primary key,
914  key1 char(10) character set binary,
915  col1 int,
916  key (key1)
917) engine=rocksdb;
918insert into t1 values(1, 'one',11), (2,'two',22);
919--replace_column 9 #
920explain
921select key1 from t1 where key1='one';
922--echo # The following will produce no rows. This looks like a bug,
923--echo #  but it is actually correct behavior. Binary strings are end-padded
924--echo #  with \0 character (and not space).  Comparison does not ignore
925--echo #   the tail of \0.
926select key1 from t1 where key1='one';
927--replace_column 9 #
928explain
929select hex(key1) from t1 where key1='one\0\0\0\0\0\0\0';
930select hex(key1) from t1 where key1='one\0\0\0\0\0\0\0';
931drop table t1;
932
933
934create table t2 (
935  pk int primary key,
936  key1 char(10) collate latin1_bin,
937  col1 int,
938  key (key1)
939) engine=rocksdb;
940insert into t2 values(1, 'one',11), (2,'two',22);
941--replace_column 9 #
942explain
943select key1 from t2 where key1='one';
944select key1 from t2 where key1='one';
945drop table t2;
946
947
948create table t3 (
949  pk int primary key,
950  key1 char(10) collate utf8_bin,
951  col1 int,
952  key (key1)
953) engine=rocksdb;
954insert into t3 values(1, 'one',11), (2,'two',22);
955--replace_column 9 #
956explain
957select key1 from t3 where key1='one';
958select key1 from t3 where key1='one';
959drop table t3;
960
961
962--echo # a VARCHAR column
963create table t4 (
964  pk int primary key,
965  key1 varchar(10) collate latin1_bin,
966  key(key1)
967) engine=rocksdb;
968insert into t4 values(1, 'one'), (2,'two'),(3,'threee'),(55,'fifty-five');
969
970--replace_column 9 #
971explain
972select key1 from t4 where key1='two';
973select key1 from t4 where key1='two';
974
975select key1 from t4 where key1='fifty-five';
976
977--replace_column 9 #
978explain
979select key1 from t4 where key1 between 's' and 'u';
980select key1 from t4 where key1 between 's' and 'u';
981
982drop table t4;
983
984--echo #
985--echo # MDEV-4305: RocksDB: Assertion `((keypart_map + 1) & keypart_map) == 0' fails in calculate_key_len
986--echo #
987CREATE TABLE t1 (pk1 INT, pk2 CHAR(32), i INT, PRIMARY KEY(pk1,pk2), KEY(i)) ENGINE=RocksDB;
988INSERT INTO t1 VALUES (1,'test1',6),(2,'test2',8);
989SELECT * FROM t1 WHERE i != 3 OR  pk1 > 9;
990DROP TABLE t1;
991
992--echo #
993--echo # MDEV-4298: RocksDB: Assertion `thd->is_error() || kill_errno' fails in ha_rows filesort
994--echo #
995call mtr.add_suppression("Sort aborted");
996CREATE TABLE t1 (pk INT PRIMARY KEY, i INT, KEY(i)) ENGINE=RocksDB;
997INSERT INTO t1 VALUES (1,1),(2,2);
998BEGIN;
999UPDATE t1 SET i = 100;
1000
1001--connect (con1,localhost,root,,test)
1002--error ER_LOCK_WAIT_TIMEOUT
1003DELETE IGNORE FROM t1 ORDER BY i;
1004--disconnect con1
1005
1006--connection default
1007COMMIT;
1008DROP TABLE t1;
1009
1010--echo #
1011--echo # MDEV-4324: RocksDB: Valgrind "Use of uninitialised value" warnings on inserting value into varchar field
1012--echo #  (testcase only)
1013--echo #
1014CREATE TABLE t1 (pk INT PRIMARY KEY, c VARCHAR(4)) ENGINE=RocksDB;
1015INSERT INTO t1 VALUES (1,'foo'), (2,'bar');
1016DROP TABLE t1;
1017
1018--echo #
1019--echo # MDEV-4304: RocksDB: Index-only scan by a field with utf8_bin collation returns garbage symbols
1020--echo #
1021CREATE TABLE t1 (pk INT PRIMARY KEY, c1 CHAR(1), c2 CHAR(1), KEY(c1)) ENGINE=RocksDB CHARSET utf8 COLLATE utf8_bin;
1022INSERT INTO t1 VALUES (1,'h','h');
1023SELECT * FROM t1;
1024SELECT c1 FROM t1;
1025DROP TABLE t1;
1026
1027--echo #
1028--echo # MDEV-4300: RocksDB: Server crashes in inline_mysql_mutex_lock on SELECT .. FOR UPDATE
1029--echo #
1030CREATE TABLE t2 (pk INT PRIMARY KEY, i INT, KEY (i)) ENGINE=RocksDB;
1031INSERT INTO t2 VALUES (1,4),(2,5);
1032SELECT 1 FROM t2 WHERE i < 0 FOR UPDATE;
1033DROP TABLE t2;
1034
1035--echo #
1036--echo # MDEV-4301: RocksDB: Assertion `pack_info != __null' fails in Rdb_key_def::unpack_record
1037--echo #
1038CREATE TABLE t1 (pk INT PRIMARY KEY, i INT, c CHAR(1), KEY(c,i)) ENGINE=RocksDB;
1039INSERT INTO t1 VALUES (1,4,'d'),(2,8,'e');
1040SELECT MAX( pk ) FROM t1 WHERE i = 105 AND c = 'h';
1041DROP TABLE t1;
1042
1043--echo #
1044--echo # MDEV-4337: RocksDB: Inconsistent results comparing a char field with an int field
1045--echo #
1046create table t1 (c char(1), i int, primary key(c), key(i)) engine=RocksDB;
1047insert into t1 values ('2',2),('6',6);
1048select * from t1 where c = i;
1049select * from t1 ignore index (i) where c = i;
1050drop table t1;
1051
1052
1053--echo #
1054--echo # Test statement rollback inside a transaction
1055--echo #
1056create table t1 (pk varchar(12) primary key) engine=rocksdb;
1057insert into t1 values ('old-val1'),('old-val2');
1058
1059create table t2 (pk varchar(12) primary key) engine=rocksdb;
1060insert into t2 values ('new-val2'),('old-val1');
1061
1062begin;
1063insert into t1 values ('new-val1');
1064--error ER_DUP_ENTRY
1065insert into t1 select * from t2;
1066commit;
1067
1068select * from t1;
1069drop table t1, t2;
1070
1071--echo #
1072--echo # MDEV-4383: RocksDB: Wrong result of DELETE .. ORDER BY .. LIMIT:
1073--echo #   rows that should be deleted remain in the table
1074--echo #
1075CREATE TABLE t2 (pk INT AUTO_INCREMENT PRIMARY KEY) ENGINE=RocksDB;
1076CREATE TABLE t1 (pk INT AUTO_INCREMENT PRIMARY KEY) ENGINE=RocksDB;
1077
1078INSERT INTO t1 (pk) VALUES (NULL),(NULL);
1079BEGIN;
1080INSERT INTO t2 (pk) VALUES (NULL),(NULL);
1081INSERT INTO t1 (pk) VALUES (NULL),(NULL),(NULL),(NULL),(NULL),(NULL);
1082
1083--enable_info
1084SELECT * FROM t1 ORDER BY pk LIMIT 9;
1085DELETE FROM t1 ORDER BY pk LIMIT 9;
1086SELECT * FROM t1 ORDER BY pk LIMIT 9;
1087--disable_info
1088
1089DROP TABLE t1,t2;
1090
1091--echo #
1092--echo # MDEV-4374: RocksDB: Valgrind warnings 'Use of uninitialised value' on
1093--echo #   inserting into a varchar column
1094--echo #
1095CREATE TABLE t1 (pk INT PRIMARY KEY, a VARCHAR(32)) ENGINE=RocksDB;
1096INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
1097DROP TABLE t1;
1098
1099
1100--echo #
1101--echo # MDEV-4061: RocksDB: Changes from an interrupted query are still applied
1102--echo #
1103
1104--enable_connect_log
1105
1106create table t1 (pk int primary key, a int) engine=RocksDB;
1107insert into t1 values (1,10),(2,20);
1108
1109--let $con_id = `select connection_id()`
1110
1111set autocommit = 1;
1112--send
1113update t1 set a = sleep(100) where pk = 1;
1114
1115--connect (con1,localhost,root,,)
1116
1117let $wait_condition= select State='User sleep' from information_schema.processlist where id=$con_id/* or srv_id=$con_id*/;
1118--source include/wait_condition.inc
1119
1120--echo kill query \$con_id;
1121--disable_query_log
1122eval kill query $con_id;
1123--enable_query_log
1124
1125--connection default
1126--error ER_QUERY_INTERRUPTED
1127--reap
1128
1129select * from t1;
1130--disconnect con1
1131--disable_connect_log
1132drop table t1;
1133
1134
1135--echo #
1136--echo # MDEV-4099: RocksDB: Wrong results with index and range access after INSERT IGNORE or REPLACE
1137--echo #
1138CREATE TABLE t1 (pk INT PRIMARY KEY, a SMALLINT, b INT, KEY (a)) ENGINE=RocksDB;
1139INSERT IGNORE INTO t1 VALUES (1, 157, 0), (2, 1898, -504403), (1, -14659,  0);
1140SELECT * FROM t1;
1141SELECT pk FROM t1;
1142SELECT * FROM t1 WHERE a != 97;
1143DROP TABLE t1;
1144
1145
1146--echo #
1147--echo # Test @@rocksdb_max_row_locks
1148--echo #
1149CREATE TABLE t1 (pk INT PRIMARY KEY, a int) ENGINE=RocksDB;
1150set @a=-1;
1151insert into t1 select (@a:=@a+1), 1234 from information_schema.session_variables limit 100;
1152set @tmp1= @@rocksdb_max_row_locks;
1153set rocksdb_max_row_locks= 20;
1154--error ER_GET_ERRMSG
1155update t1 set a=a+10;
1156DROP TABLE t1;
1157
1158
1159--echo #
1160--echo # Test AUTO_INCREMENT behavior problem,
1161--echo #  "explicit insert into an auto-inc column is not noticed by RocksDB"
1162--echo #
1163create table t1 (i int primary key auto_increment) engine=RocksDB;
1164
1165insert into t1 values (null);
1166insert into t1 values (null);
1167select * from t1;
1168drop table t1;
1169
1170create table t2 (i int primary key auto_increment) engine=RocksDB;
1171
1172insert into t2 values (1);
1173select * from t2;
1174
1175--echo # this fails (ie. used to fail), RocksDB engine did not notice use of '1' above
1176insert into t2 values (null);
1177select * from t2;
1178
1179--echo # but then this succeeds, so previous statement must have incremented next number counter
1180insert into t2 values (null);
1181select * from t2;
1182drop table t2;
1183
1184--echo #
1185--echo # Fix Issue#2: AUTO_INCREMENT value doesn't survive server shutdown
1186--echo #
1187create table t1 (i int primary key auto_increment) engine=RocksDB;
1188
1189insert into t1 values (null);
1190insert into t1 values (null);
1191
1192SET GLOBAL ROCKSDB_PAUSE_BACKGROUND_WORK = @ORIG_PAUSE_BACKGROUND_WORK;
1193
1194--source include/restart_mysqld.inc
1195
1196SET @ORIG_PAUSE_BACKGROUND_WORK = @@ROCKSDB_PAUSE_BACKGROUND_WORK;
1197SET GLOBAL ROCKSDB_PAUSE_BACKGROUND_WORK = 1;
1198
1199insert into t1 values (null);
1200select * from t1;
1201
1202drop table t1;
1203
1204--echo #
1205--echo # Fix Issue #3: SHOW TABLE STATUS shows Auto_increment=0
1206--echo #
1207create table t1 (i int primary key auto_increment) engine=RocksDB;
1208
1209insert into t1 values (null),(null);
1210--replace_column 7 # 12 # 13 #
1211show table status like 't1';
1212drop table t1;
1213
1214--echo #
1215--echo # Fix Issue #4: Crash when using pseudo-unique keys
1216--echo #
1217CREATE TABLE t1 (
1218  i INT,
1219  t TINYINT,
1220  s SMALLINT,
1221  m MEDIUMINT,
1222  b BIGINT,
1223  pk MEDIUMINT AUTO_INCREMENT PRIMARY KEY,
1224  UNIQUE KEY b_t (b,t)
1225) ENGINE=rocksdb;
1226
1227INSERT INTO t1 (i,t,s,m,b) VALUES (1,2,3,4,5),(1000,100,10000,1000000,1000000000000000000),(5,100,10000,1000000,100000000000000000),(2,3,4,5,6),(3,4,5,6,7),(101,102,103,104,105),(10001,103,10002,10003,10004),(10,11,12,13,14),(11,12,13,14,15),(12,13,14,15,16);
1228
1229SELECT b+t FROM t1 WHERE (b,t) IN ( SELECT b, t FROM t1 WHERE i>1 ) ORDER BY b+t;
1230DROP TABLE t1;
1231
1232--echo #
1233--echo # Fix issue #5: Transaction rollback doesn't undo all changes.
1234--echo #
1235create table t0 (a int) engine=myisam;
1236insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1237
1238create table t1 (id int auto_increment primary key, value int) engine=rocksdb;
1239
1240set autocommit=0;
1241begin;
1242set @a:=0;
1243insert into t1 select @a:=@a+1, @a from t0 A, t0 B, t0 C, t0 D where D.a<4;
1244insert into t1 select @a:=@a+1, @a from t0 A, t0 B, t0 C, t0 D where D.a<4;
1245insert into t1 select @a:=@a+1, @a from t0 A, t0 B, t0 C, t0 D where D.a<4;
1246rollback;
1247select count(*) from t1;
1248
1249set autocommit=1;
1250drop table t0, t1;
1251
1252--echo #
1253--echo # Check status variables
1254--echo # NOTE: We exclude rocksdb_num_get_for_update_calls because it's a debug only status var
1255--echo #
1256--replace_column 2 #
1257show status where variable_name like 'rocksdb%' and variable_name not like '%num_get_for_update%';
1258
1259select VARIABLE_NAME from INFORMATION_SCHEMA.global_status where VARIABLE_NAME LIKE 'rocksdb%' and VARIABLE_NAME NOT LIKE '%num_get_for_update%';
1260--echo # RocksDB-SE's status variables are global internally
1261--echo #  but they are shown as both session and global, like InnoDB's status vars.
1262select VARIABLE_NAME from INFORMATION_SCHEMA.session_status where VARIABLE_NAME LIKE 'rocksdb%' and VARIABLE_NAME NOT LIKE '%num_get_for_update%';
1263
1264
1265--echo #
1266--echo # Fix issue #9: HA_ERR_INTERNAL_ERROR when running linkbench
1267--echo #
1268create table t0 (a int) engine=myisam;
1269insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1270
1271create table t1 (
1272  pk int primary key,
1273  col1 varchar(255),
1274  key(col1)
1275) engine=rocksdb;
1276insert into t1 select a, repeat('123456789ABCDEF-', 15) from t0;
1277select * from t1 where pk=3;
1278drop table t0, t1;
1279
1280--echo #
1281--echo # Fix issue #10: Segfault in Rdb_key_def::get_primary_key_tuple
1282--echo #
1283create table t0 (a int) engine=myisam;
1284insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1285
1286CREATE TABLE t1 (
1287  id1 bigint(20) unsigned NOT NULL DEFAULT '0',
1288  id2 bigint(20) unsigned NOT NULL DEFAULT '0',
1289  link_type bigint(20) unsigned NOT NULL DEFAULT '0',
1290  visibility tinyint(3) NOT NULL DEFAULT '0',
1291  data varchar(255) NOT NULL DEFAULT '',
1292  time bigint(20) unsigned NOT NULL DEFAULT '0',
1293  version int(11) unsigned NOT NULL DEFAULT '0',
1294  PRIMARY KEY (link_type,id1,id2)
1295) engine=rocksdb;
1296
1297insert into t1 select a,a,a,1,a,a,a from t0;
1298
1299alter table t1 add index id1_type (id1,link_type,visibility,time,version,data);
1300select * from t1 where id1 = 3;
1301
1302drop table t0,t1;
1303
1304--echo #
1305--echo # Test column families
1306--echo #
1307
1308create table t1 (
1309  pk int primary key,
1310  col1 int,
1311  col2 int,
1312  key(col1) comment 'cf3',
1313  key(col2) comment 'cf4'
1314) engine=rocksdb;
1315
1316insert into t1 values (1,1,1), (2,2,2), (3,3,3), (4,4,4), (5,5,5);
1317
1318--replace_column 9 #
1319explain
1320select * from t1 where col1=2;
1321select * from t1 where col1=2;
1322
1323--replace_column 9 #
1324explain
1325select * from t1 where col2=3;
1326select * from t1 where col2=3;
1327
1328select * from t1 where pk=4;
1329
1330drop table t1;
1331
1332--echo #
1333--echo # Try primary key in a non-default CF:
1334--echo #
1335create table t1 (
1336  pk int,
1337  col1 int,
1338  col2 int,
1339  key(col1) comment 'cf3',
1340  key(col2) comment 'cf4',
1341  primary key (pk) comment 'cf5'
1342) engine=rocksdb;
1343insert into t1 values (1,1,1), (2,2,2), (3,3,3), (4,4,4), (5,5,5);
1344
1345--replace_column 9 #
1346explain
1347select * from t1 where col1=2;
1348select * from t1 where col1=2;
1349
1350select * from t1 where pk=4;
1351
1352drop table t1;
1353
1354--echo #
1355--echo # Issue #15: SIGSEGV from reading in blob data
1356--echo #
1357CREATE TABLE t1 (
1358  id int not null,
1359  blob_col text,
1360  PRIMARY KEY (id)
1361) ENGINE=ROCKSDB CHARSET=latin1;
1362
1363INSERT INTO t1 SET id=123, blob_col=repeat('z',64000) ON DUPLICATE KEY UPDATE blob_col=VALUES(blob_col);
1364INSERT INTO t1 SET id=123, blob_col=''                ON DUPLICATE KEY UPDATE blob_col=VALUES(blob_col);
1365DROP TABLE t1;
1366
1367
1368--echo #
1369--echo # Issue #17: Automatic per-index column families
1370--echo # (Now deprecated)
1371--echo #
1372--error ER_PER_INDEX_CF_DEPRECATED
1373create table t1 (
1374  id int not null,
1375  key1 int,
1376  PRIMARY KEY (id),
1377  index (key1) comment '$per_index_cf'
1378) engine=rocksdb;
1379
1380
1381--echo #
1382--echo # Issue #22: SELECT ... FOR UPDATE takes a long time
1383--echo #
1384create table t0 (a int) engine=myisam;
1385insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1386
1387create table t1 (
1388  id1 int,
1389  id2 int,
1390  value1 int,
1391  value2 int,
1392  primary key(id1, id2) COMMENT 'new_column_family',
1393  key(id2)
1394) engine=rocksdb default charset=latin1 collate=latin1_bin;
1395
1396insert into t1 select A.a, B.a, 31, 1234 from t0 A, t0 B;
1397
1398--replace_column 9 #
1399explain
1400select * from t1 where id1=30 and value1=30 for update;
1401
1402set @var1=(select variable_value
1403           from information_schema.global_status
1404           where variable_name='rocksdb_number_keys_read');
1405
1406select * from t1 where id1=3 and value1=3 for update;
1407
1408set @var2=(select variable_value
1409           from information_schema.global_status
1410           where variable_name='rocksdb_number_keys_read');
1411--echo # The following must return true (before the fix, the difference was 70):
1412select if((@var2 - @var1) < 30, 1, @var2-@var1);
1413
1414drop table t0,t1;
1415
1416--echo #
1417--echo # Issue #33: SELECT ... FROM rocksdb_table ORDER BY primary_key uses sorting
1418--echo #
1419create table t1 (id int primary key, value int) engine=rocksdb;
1420insert into t1 values (1,1),(2,2),(3,3);
1421--echo # The following must not use 'Using filesort':
1422--replace_column 9 #
1423explain select * from t1 ORDER BY id;
1424drop table t1;
1425
1426--echo #
1427--echo # Issue #26: Index-only scans for DATETIME and TIMESTAMP
1428--echo #
1429create table t0 (a int) engine=myisam;
1430insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1431
1432--echo # Try a DATETIME column:
1433create table t1 (
1434  pk int auto_increment primary key,
1435  kp1 datetime,
1436  kp2 int,
1437  col1 int,
1438  key(kp1, kp2)
1439) engine=rocksdb;
1440insert into t1 (kp1,kp2)
1441select date_add('2015-01-01 12:34:56', interval a day), a from t0;
1442
1443select * from t1;
1444
1445--echo # This must show 'Using index'
1446--replace_column 9 #
1447explain
1448select kp1,kp2 from t1 force index (kp1)
1449where kp1 between '2015-01-01 00:00:00' and '2015-01-05 23:59:59';
1450
1451select kp1,kp2 from t1 force index (kp1)
1452where kp1 between '2015-01-01 00:00:00' and '2015-01-05 23:59:59';
1453
1454--echo # Now, the same with NOT NULL column
1455create table t2 (
1456  pk int auto_increment primary key,
1457  kp1 datetime not null,
1458  kp2 int,
1459  col1 int,
1460  key(kp1, kp2)
1461) engine=rocksdb;
1462insert into t2 select * from t1;
1463--echo # This must show 'Using index'
1464--replace_column 9 #
1465explain
1466select kp1,kp2 from t2 force index (kp1)
1467where kp1 between '2015-01-01 00:00:00' and '2015-01-05 23:59:59';
1468
1469select kp1,kp2 from t2 force index (kp1)
1470where kp1 between '2015-01-01 00:00:00' and '2015-01-05 23:59:59';
1471drop table t1,t2;
1472
1473--echo # Try a DATE column:
1474create table t1 (
1475  pk int auto_increment primary key,
1476  kp1 date,
1477  kp2 int,
1478  col1 int,
1479  key(kp1, kp2)
1480) engine=rocksdb;
1481insert into t1 (kp1,kp2)
1482select date_add('2015-01-01', interval a day), a from t0;
1483
1484select * from t1;
1485
1486--echo # This must show 'Using index'
1487--replace_column 9 #
1488explain
1489select kp1,kp2 from t1 force index (kp1)
1490where kp1 between '2015-01-01' and '2015-01-05';
1491
1492select kp1,kp2 from t1 force index (kp1)
1493where kp1 between '2015-01-01' and '2015-01-05';
1494
1495--echo # Now, the same with NOT NULL column
1496create table t2 (
1497  pk int auto_increment primary key,
1498  kp1 date not null,
1499  kp2 int,
1500  col1 int,
1501  key(kp1, kp2)
1502) engine=rocksdb;
1503insert into t2 select * from t1;
1504--echo # This must show 'Using index'
1505--replace_column 9 #
1506explain
1507select kp1,kp2 from t2 force index (kp1)
1508where kp1 between '2015-01-01 00:00:00' and '2015-01-05 23:59:59';
1509
1510select kp1,kp2 from t2 force index (kp1)
1511where kp1 between '2015-01-01 00:00:00' and '2015-01-05 23:59:59';
1512drop table t1,t2;
1513
1514--echo #
1515--echo # Try a TIMESTAMP column:
1516--echo #
1517create table t1 (
1518  pk int auto_increment primary key,
1519  kp1 timestamp,
1520  kp2 int,
1521  col1 int,
1522  key(kp1, kp2)
1523) engine=rocksdb;
1524insert into t1 (kp1,kp2)
1525select date_add('2015-01-01 12:34:56', interval a day), a from t0;
1526
1527select * from t1;
1528
1529--echo # This must show 'Using index'
1530--replace_column 9 #
1531explain
1532select kp1,kp2 from t1 force index (kp1)
1533where kp1 between '2015-01-01 00:00:00' and '2015-01-05 23:59:59';
1534
1535select kp1,kp2 from t1 force index (kp1)
1536where kp1 between '2015-01-01 00:00:00' and '2015-01-05 23:59:59';
1537
1538--echo # Now, the same with NOT NULL column
1539create table t2 (
1540  pk int auto_increment primary key,
1541  kp1 timestamp not null,
1542  kp2 int,
1543  col1 int,
1544  key(kp1, kp2)
1545) engine=rocksdb;
1546insert into t2 select * from t1;
1547--echo # This must show 'Using index'
1548--replace_column 9 #
1549explain
1550select kp1,kp2 from t2 force index (kp1)
1551where kp1 between '2015-01-01 00:00:00' and '2015-01-05 23:59:59';
1552
1553select kp1,kp2 from t2 force index (kp1)
1554where kp1 between '2015-01-01 00:00:00' and '2015-01-05 23:59:59';
1555drop table t1,t2;
1556
1557--echo #
1558--echo # Try a TIME column:
1559--echo #
1560create table t1 (
1561  pk int auto_increment primary key,
1562  kp1 time,
1563  kp2 int,
1564  col1 int,
1565  key(kp1, kp2)
1566) engine=rocksdb;
1567--disable_warnings
1568insert into t1 (kp1,kp2)
1569select date_add('2015-01-01 09:00:00', interval a minute), a from t0;
1570--enable_warnings
1571
1572select * from t1;
1573
1574--echo # This must show 'Using index'
1575--replace_column 9 #
1576explain
1577select kp1,kp2 from t1 force index (kp1)
1578where kp1 between '09:01:00' and '09:05:00';
1579
1580select kp1,kp2 from t1 force index (kp1)
1581where kp1 between '09:01:00' and '09:05:00';
1582
1583--echo # Now, the same with NOT NULL column
1584create table t2 (
1585  pk int auto_increment primary key,
1586  kp1 time not null,
1587  kp2 int,
1588  col1 int,
1589  key(kp1, kp2)
1590) engine=rocksdb;
1591insert into t2 select * from t1;
1592--echo # This must show 'Using index'
1593--replace_column 9 #
1594explain
1595select kp1,kp2 from t2 force index (kp1)
1596where kp1 between '09:01:00' and '09:05:00';
1597
1598select kp1,kp2 from t2 force index (kp1)
1599where kp1 between '09:01:00' and '09:05:00';
1600drop table t1,t2;
1601
1602--echo #
1603--echo # Try a YEAR column:
1604--echo #
1605create table t1 (
1606  pk int auto_increment primary key,
1607  kp1 year,
1608  kp2 int,
1609  col1 int,
1610  key(kp1, kp2)
1611) engine=rocksdb;
1612--disable_warnings
1613insert into t1 (kp1,kp2) select 2015+a, a from t0;
1614--enable_warnings
1615
1616select * from t1;
1617
1618--echo # This must show 'Using index'
1619--replace_column 9 #
1620explain
1621select kp1,kp2 from t1 force index (kp1)
1622where kp1 between '2016' and '2020';
1623
1624select kp1,kp2 from t1 force index (kp1)
1625where kp1 between '2016' and '2020';
1626
1627--echo # Now, the same with NOT NULL column
1628create table t2 (
1629  pk int auto_increment primary key,
1630  kp1 year not null,
1631  kp2 int,
1632  col1 int,
1633  key(kp1, kp2)
1634) engine=rocksdb;
1635insert into t2 select * from t1;
1636--echo # This must show 'Using index'
1637--replace_column 9 #
1638explain
1639select kp1,kp2 from t2 force index (kp1)
1640where kp1 between '2016' and '2020';
1641
1642select kp1,kp2 from t2 force index (kp1)
1643where kp1 between '2016' and '2020';
1644
1645drop table t1,t2;
1646
1647--echo #
1648--echo # Issue #57: Release row locks on statement errors
1649--echo #
1650create table t1 (id int primary key) engine=rocksdb;
1651insert into t1 values (1), (2), (3);
1652begin;
1653insert into t1 values (4), (5), (6);
1654--error ER_DUP_ENTRY
1655insert into t1 values (7), (8), (2), (9);
1656select * from t1;
1657
1658-- connect(con1,localhost,root,,)
1659--connection con1
1660begin;
1661--error ER_LOCK_WAIT_TIMEOUT
1662select * from t1 where id=4 for update;
1663
1664select * from t1 where id=7 for update;
1665
1666select * from t1 where id=9 for update;
1667
1668--connection default
1669-- disconnect con1
1670drop table t1;
1671
1672--echo #Index on blob column
1673SET @old_mode = @@sql_mode;
1674SET sql_mode = 'strict_all_tables';
1675create table t1 (a int, b text, c varchar(400), Primary Key(a), Key(c, b(255))) engine=rocksdb;
1676drop table t1;
1677set global rocksdb_large_prefix=1;
1678create table t1 (a int, b text, c varchar(400), Primary Key(a), Key(b(1255))) engine=rocksdb;
1679set global rocksdb_large_prefix=0;
1680insert into t1 values (1, '1abcde', '1abcde'), (2, '2abcde', '2abcde'), (3, '3abcde', '3abcde');
1681select * from t1;
1682--replace_column 9 #
1683explain select * from t1 where b like '1%';
1684--replace_column 9 #
1685explain select b, a from t1 where b like '1%';
1686update t1 set b= '12345' where b = '2abcde';
1687select * from t1;
1688drop table t1;
1689# In MariaDB, the error becomes a warning:
1690# --error ER_TOO_LONG_KEY
1691create table t1 (a int, b text, c varchar(400), Primary Key(a), Key(b(2255))) engine=rocksdb;
1692drop table t1;
1693SET sql_mode = @old_mode;
1694
1695drop table t0;
1696
1697--echo #
1698--echo # Fix assertion failure (attempt to overrun the key buffer) for prefix indexes
1699--echo #
1700
1701create table t1 (
1702  pk int primary key,
1703  col1 varchar(100),
1704  key (col1(10))
1705) engine=rocksdb;
1706
1707insert into t1 values (1, repeat('0123456789', 9));
1708
1709drop table t1;
1710
1711--echo #
1712--echo # Issue #76: Assertion `buf == table->record[0]' fails in virtual int ha_rocksdb::delete_row(const uchar*)
1713--echo #
1714
1715CREATE TABLE t1 (pk INT PRIMARY KEY, f1 INT) ENGINE=RocksDB;
1716CREATE TABLE t2 (pk INT PRIMARY KEY, f1 INT) ENGINE=RocksDB;
1717
1718CREATE TRIGGER tr AFTER DELETE ON t1 FOR EACH ROW DELETE FROM t2 WHERE pk = old.pk;
1719
1720INSERT INTO t1 VALUES (1,1);
1721REPLACE INTO t1 VALUES (1,2);
1722
1723SELECT * FROM t1;
1724DROP TABLE t1, t2;
1725
1726--echo #
1727--echo # Issue #99: UPDATE for table with VARCHAR pk gives "Can't find record" error
1728--echo #
1729create table t1(a int primary key);
1730insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
1731
1732create table t2 (
1733  a varchar(32) primary key,
1734  col1 int
1735) engine=rocksdb;
1736
1737insert into t2
1738select concat('v-', 100 + A.a*100 + B.a), 12345 from t1 A, t1 B;
1739update t2 set a=concat('x-', a) where a between 'v-1002' and 'v-1004';
1740
1741drop table t1,t2;
1742
1743--echo #
1744--echo # Issue #131: Assertion `v->cfd_->internal_comparator().Compare(start, end) <= 0' failed
1745--echo #
1746CREATE TABLE t2(c1 INTEGER UNSIGNED NOT NULL, c2 INTEGER NULL, c3 TINYINT, c4 SMALLINT , c5 MEDIUMINT, c6 INT, c7 BIGINT, PRIMARY KEY(c1,c6)) ENGINE=RocksDB;
1747INSERT INTO t2 VALUES (1,1,1,1,1,1,1);
1748SELECT * FROM t2 WHERE c1 > 4294967295 ORDER BY c1,c6;
1749EXPLAIN SELECT * FROM t2 WHERE c1 > 4294967295 ORDER BY c1,c6;
1750drop table t2;
1751
1752--echo #
1753--echo # Issue #135: register transaction was not being called for statement
1754--echo #
1755--disable_warnings
1756DROP DATABASE IF EXISTS test_db;
1757--enable_warnings
1758CREATE DATABASE test_db;
1759CREATE TABLE test_db.t1(c1 INT PRIMARY KEY);
1760LOCK TABLES test_db.t1 READ;
1761SET AUTOCOMMIT=0;
1762SELECT c1 FROM test_db.t1;
1763START TRANSACTION WITH CONSISTENT SNAPSHOT, READ ONLY;
1764DROP DATABASE test_db;
1765
1766--echo #
1767--echo # Issue #143: Split rocksdb_bulk_load option into two
1768--echo #
1769CREATE TABLE t1 (id int primary key, value int) engine=RocksDB;
1770SET unique_checks=0;
1771INSERT INTO t1 VALUES(1, 1);
1772INSERT INTO t1 VALUES(1, 2);
1773INSERT INTO t1 VALUES(1, 3);
1774SELECT * FROM t1;
1775--error ER_ON_DUPLICATE_DISABLED
1776REPLACE INTO t1 VALUES(4, 4);
1777--error ER_ON_DUPLICATE_DISABLED
1778INSERT INTO t1 VALUES(5, 5) ON DUPLICATE KEY UPDATE value=value+1;
1779TRUNCATE TABLE t1;
1780SET @save_rocksdb_bulk_load_size= @@rocksdb_bulk_load_size;
1781SET unique_checks=1;
1782SET rocksdb_commit_in_the_middle=1;
1783SET rocksdb_bulk_load_size=10;
1784BEGIN;
1785INSERT INTO t1 (id) VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),
1786  (11),(12),(13),(14),(15),(16),(17),(18),(19);
1787ROLLBACK;
1788SELECT * FROM t1;
1789INSERT INTO t1 (id) VALUES (11),(12),(13),(14),(15);
1790BEGIN;
1791UPDATE t1 SET value=100;
1792ROLLBACK;
1793SELECT * FROM t1;
1794BEGIN;
1795DELETE FROM t1;
1796ROLLBACK;
1797SELECT * FROM t1;
1798SET rocksdb_commit_in_the_middle=0;
1799SET rocksdb_bulk_load_size= @save_rocksdb_bulk_load_size;
1800DROP TABLE t1;
1801
1802--echo #
1803--echo # Issue #185 Assertion `BaseValid()' failed in void rocksdb::BaseDeltaIterator::Advance()
1804--echo #
1805CREATE TABLE t2(id INT NOT NULL PRIMARY KEY, data INT) Engine=MEMORY;
1806INSERT INTO t2 VALUES (100,NULL),(150,"long varchar"),(200,"varchar"),(250,"long long long varchar");
1807create TABLE t1 (a int not null, b int not null, primary key(a,b));
1808INSERT INTO t1  VALUES (1,1);
1809SELECT a FROM t1, t2 WHERE a=b AND (b NOT IN (SELECT a FROM t1 WHERE a > 4));
1810DROP TABLE t1, t2;
1811
1812--echo #
1813--echo # Issue #189 ha_rocksdb::load_auto_incr_value() creates implicit snapshot and doesn't release
1814--echo #
1815--connect (con1,localhost,root,,)
1816create table r1 (id int auto_increment primary key, value int);
1817insert into r1 (id) values (null), (null), (null), (null), (null);
1818connection con1;
1819create table r2 like r1;
1820show create table r2;
1821connection default;
1822begin;
1823insert into r1 values (10, 1);
1824commit;
1825connection con1;
1826begin;
1827select * from r1;
1828commit;
1829connection default;
1830drop table r1, r2;
1831
1832# hidden primary key
1833create table r1 (id int auto_increment, value int, index i(id));
1834insert into r1 (id) values (null), (null), (null), (null), (null);
1835connection con1;
1836create table r2 like r1;
1837show create table r2;
1838connection default;
1839begin;
1840insert into r1 values (10, 1);
1841commit;
1842connection con1;
1843begin;
1844select * from r1;
1845commit;
1846connection default;
1847drop table r1, r2;
1848
1849disconnect con1;
1850
1851--echo #
1852--echo # Issue#211 Crash on LOCK TABLES + START TRANSACTION WITH CONSISTENT SNAPSHOT
1853--echo #
1854CREATE TABLE t1(c1 INT);
1855lock TABLE t1 read local;
1856SELECT 1 FROM t1 GROUP BY TRIM(LEADING RAND()FROM'');
1857set AUTOCOMMIT=0;
1858start transaction with consistent snapshot;
1859SELECT * FROM t1;
1860COMMIT;
1861UNLOCK TABLES;
1862DROP TABLE t1;
1863
1864--echo #
1865--echo # Issue#213 Crash on LOCK TABLES + partitions
1866--echo #
1867CREATE TABLE t1(a INT,b INT,KEY (b)) engine=rocksdb PARTITION BY HASH(a) PARTITIONS 2;
1868INSERT INTO t1(a)VALUES (20010101101010.999949);
1869lock tables t1 write,t1 as t0 write,t1 as t2 write;
1870SELECT a FROM t1 ORDER BY a;
1871truncate t1;
1872INSERT INTO t1 VALUES(X'042000200020',X'042000200020'),(X'200400200020',X'200400200020');
1873UNLOCK TABLES;
1874DROP TABLE t1;
1875
1876--echo #
1877--echo # Issue#250: MyRocks/Innodb different output from query with order by on table with index and decimal type
1878--echo #  (the test was changed to use VARCHAR, because DECIMAL now supports index-only, and this issue
1879--echo #   needs a datype that doesn't support index-inly)
1880--echo #
1881
1882CREATE TABLE t1(
1883  c1 varchar(10) character set utf8 collate utf8_general_ci NOT NULL,
1884  c2 varchar(10) character set utf8 collate utf8_general_ci,
1885  c3 INT,
1886  INDEX idx(c1,c2)
1887);
1888INSERT INTO t1 VALUES ('c1-val1','c2-val1',5);
1889INSERT INTO t1 VALUES ('c1-val2','c2-val3',6);
1890INSERT INTO t1 VALUES ('c1-val3','c2-val3',7);
1891SELECT * FROM t1 force index(idx) WHERE c1 <> 'c1-val2' ORDER BY c1 DESC;
1892--replace_column 9 #
1893explain SELECT * FROM t1  force index(idx) WHERE c1 <> '1' ORDER BY c1 DESC;
1894drop table t1;
1895
1896--echo #
1897--echo # Issue#267: MyRocks issue with no matching min/max row and count(*)
1898--echo #
1899CREATE TABLE t1(c1 INT UNSIGNED, c2 INT SIGNED, INDEX idx2(c2));
1900INSERT INTO t1 VALUES(1,null);
1901INSERT INTO t1 VALUES(2,null);
1902SELECT count(*) as total_rows, min(c2) as min_value FROM t1;
1903DROP TABLE t1;
1904
1905--echo #
1906--echo # Issue#263: MyRocks auto_increment skips values if you insert a negative value
1907--echo #
1908# We have slightly different behavior regarding auto-increment values than
1909# InnoDB, so the results of the SHOW TABLE STATUS command will be slightly
1910# different.  InnoDB will reserve 3 values but only use 2 of them (because
1911# the user hard-coded a -1 as the second value).  MyRocks will only reserve
1912# the values as needed, so only 2 values will be used.  This means that the
1913# SHOW TABLE STATUS in InnoDB will indicate that the next auto-increment
1914# value is 4 while MyRocks will show it as 3.
1915CREATE TABLE t1(a INT AUTO_INCREMENT KEY);
1916INSERT INTO t1 VALUES(0),(-1),(0);
1917--replace_column 12 # 13 #
1918SHOW TABLE STATUS LIKE 't1';
1919SELECT * FROM t1;
1920DROP TABLE t1;
1921CREATE TABLE t1(a INT AUTO_INCREMENT KEY);
1922INSERT INTO t1 VALUES(0),(10),(0);
1923--replace_column 12 # 13 #
1924SHOW TABLE STATUS LIKE 't1';
1925SELECT * FROM t1;
1926DROP TABLE t1;
1927
1928--echo #
1929--echo # Issue #411: Setting rocksdb_commit_in_the_middle commits transaction
1930--echo # without releasing iterator
1931--echo #
1932
1933CREATE TABLE t1 (id1 bigint(20),
1934                 id2 bigint(20),
1935                 id3 bigint(20),
1936                 PRIMARY KEY (id1, id2, id3))
1937                 DEFAULT CHARSET=latin1;
1938
1939CREATE TABLE t2 (id1 bigint(20),
1940                 id2 bigint(20),
1941                 PRIMARY KEY (id1, id2))
1942                 DEFAULT CHARSET=latin1;
1943
1944
1945set rocksdb_commit_in_the_middle=1;
1946SET @save_rocksdb_bulk_load_size= @@rocksdb_bulk_load_size;
1947set rocksdb_bulk_load_size = 100;
1948
1949--disable_query_log
1950let $j = 10000;
1951while ($j)
1952{
1953  --eval insert into t1 (id1, id2, id3) values (0, $j, 0);
1954  --eval insert into t2 (id1, id2) values (0, $j);
1955  dec $j;
1956}
1957--enable_query_log
1958
1959DELETE t2, t1 FROM t2 LEFT JOIN t1 ON t2.id2 = t1.id2 AND t2.id1 = t1.id1 WHERE t2.id1 = 0;
1960
1961SET rocksdb_bulk_load_size= @save_rocksdb_bulk_load_size;
1962SET rocksdb_commit_in_the_middle=0;
1963DROP TABLE t1, t2;
1964
1965
1966SET GLOBAL ROCKSDB_PAUSE_BACKGROUND_WORK = @ORIG_PAUSE_BACKGROUND_WORK;
1967