--source include/have_rocksdb.inc
--source include/have_debug.inc

#
# BLOB columns with indexes
#

SET @ORIG_PAUSE_BACKGROUND_WORK = @@ROCKSDB_PAUSE_BACKGROUND_WORK;
SET GLOBAL ROCKSDB_PAUSE_BACKGROUND_WORK = 1;

--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings

CREATE TABLE t1 (
  b BLOB,
  t TINYBLOB,
  m MEDIUMBLOB,
  l LONGBLOB,
  PRIMARY KEY b (b(32))
) ENGINE=rocksdb;

SHOW INDEX IN t1;

INSERT INTO t1 (b,t,m,l) VALUES
('','','',''),
('a','b','c','d'),
('b','d','c','b'),
('test1','test2','test3','test4'),
(REPEAT('a',128),REPEAT('b',128),REPEAT('c',128),REPEAT('d',128)),
(HEX('abcd'),HEX('def'),HEX('a'),HEX('abc')),
('abc','def','ghi','jkl'),
('test2','test3','test4','test5'),
('test3','test4','test5','test6'),
(REPEAT('b',128),REPEAT('f',128),REPEAT('e',128),REPEAT('d',128)),
(REPEAT('c',128),REPEAT('b',128),REPEAT('c',128),REPEAT('e',128));

--replace_column 1 # 2 # 3 # 4 # 5 # 7 # 8 # 9 # 10 #
EXPLAIN SELECT SUBSTRING(b,16) AS f FROM t1 WHERE b IN ('test1','test2') ORDER BY f;
SELECT SUBSTRING(b,16) AS f FROM t1 WHERE b IN ('test1','test2') ORDER BY f;

--replace_column 1 # 2 # 3 # 4 # 5 # 7 # 8 # 9 # 10 #
EXPLAIN SELECT SUBSTRING(b,16) AS f FROM t1 USE INDEX () WHERE b IN ('test1','test2') ORDER BY f;
SELECT SUBSTRING(b,16) AS f FROM t1 USE INDEX () WHERE b IN ('test1','test2') ORDER BY f;

DROP TABLE t1;


CREATE TABLE t1 (
  b BLOB,
  t TINYBLOB,
  m MEDIUMBLOB,
  l LONGBLOB,
  pk INT AUTO_INCREMENT PRIMARY KEY,
  UNIQUE INDEX l_t (l(256),t(64))
) ENGINE=rocksdb;

--replace_column 6 # 7 # 10 # 11 #
SHOW INDEX IN t1;

INSERT INTO t1 (b,t,m,l) VALUES
('','','',''),
('a','b','c','d'),
('b','d','c','b'),
('test1','test2','test3','test4'),
(REPEAT('a',128),REPEAT('b',128),REPEAT('c',128),REPEAT('d',128)),
(HEX('abcd'),HEX('def'),HEX('a'),HEX('abc')),
('abc','def','ghi','jkl'),
('test2','test3','test4','test5'),
('test3','test4','test5','test6'),
(REPEAT('b',128),REPEAT('f',128),REPEAT('e',128),REPEAT('d',128)),
(REPEAT('c',128),REPEAT('b',128),REPEAT('c',128),REPEAT('e',128));

# Here we are getting possible key l_t, but not the final key
--replace_column 9 #
EXPLAIN SELECT SUBSTRING(t,64), SUBSTRING(l,256) FROM t1 WHERE t!=l AND l NOT IN ('test1') ORDER BY t, l DESC;
SELECT SUBSTRING(t,64), SUBSTRING(l,256) FROM t1 WHERE t!=l AND l NOT IN ('test1') ORDER BY t, l DESC;

--replace_column 9 #
EXPLAIN SELECT SUBSTRING(t,64), SUBSTRING(l,256) FROM t1 FORCE INDEX (l_t) WHERE t!=l AND l NOT IN ('test1') ORDER BY t, l DESC;
SELECT SUBSTRING(t,64), SUBSTRING(l,256) FROM t1 FORCE INDEX (l_t) WHERE t!=l AND l NOT IN ('test1') ORDER BY t, l DESC;

DROP TABLE t1;


CREATE TABLE t1 (
  b BLOB,
  t TINYBLOB,
  m MEDIUMBLOB,
  l LONGBLOB,
  pk INT AUTO_INCREMENT PRIMARY KEY,
  INDEX (m(128))
) ENGINE=rocksdb;

SHOW INDEX IN t1;

INSERT INTO t1 (b,t,m,l) VALUES
('','','',''),
('a','b','c','d'),
('b','d','c','b'),
('test1','test2','test3','test4'),
(REPEAT('a',128),REPEAT('b',128),REPEAT('c',128),REPEAT('d',128)),
(HEX('abcd'),HEX('def'),HEX('a'),HEX('abc')),
('abc','def','ghi','jkl'),
('test2','test3','test4','test5'),
('test3','test4','test5','test6'),
(REPEAT('b',128),REPEAT('f',128),REPEAT('e',128),REPEAT('d',128)),
(REPEAT('c',128),REPEAT('b',128),REPEAT('c',128),REPEAT('e',128));

--replace_column 9 #
EXPLAIN SELECT SUBSTRING(m,128) AS f FROM t1 WHERE m = 'test1' ORDER BY f DESC;
SELECT SUBSTRING(m,128) AS f FROM t1 WHERE m = 'test1' ORDER BY f DESC;

--replace_column 9 #
EXPLAIN SELECT SUBSTRING(m,128) AS f FROM t1 IGNORE INDEX FOR ORDER BY (m) WHERE m = 'test1' ORDER BY f DESC;
SELECT SUBSTRING(m,128) AS f FROM t1 IGNORE INDEX FOR ORDER BY (m) WHERE m = 'test1' ORDER BY f DESC;

DROP TABLE t1;

CREATE TABLE t1 (
  b BLOB,
  PRIMARY KEY b (b(32))
) ENGINE=rocksdb;

INSERT INTO t1 (b) VALUES
('00000000000000000000000000000000'),
('00000000000000000000000000000001'),
('00000000000000000000000000000002');

SELECT b FROM t1;

DROP TABLE t1;

CREATE TABLE t1 (
  b TINYBLOB,
  PRIMARY KEY b (b(32))
) ENGINE=rocksdb;

INSERT INTO t1 (b) VALUES
('00000000000000000000000000000000'),
('00000000000000000000000000000001'),
('00000000000000000000000000000002');

SELECT b FROM t1;

DROP TABLE t1;

CREATE TABLE t1 (
  b MEDIUMBLOB,
  PRIMARY KEY b (b(32))
) ENGINE=rocksdb;

INSERT INTO t1 (b) VALUES
('00000000000000000000000000000000'),
('00000000000000000000000000000001'),
('00000000000000000000000000000002');

SELECT b FROM t1;

DROP TABLE t1;

CREATE TABLE t1 (
  b LONGBLOB,
  PRIMARY KEY b (b(32))
) ENGINE=rocksdb;

INSERT INTO t1 (b) VALUES
('00000000000000000000000000000000'),
('00000000000000000000000000000001'),
('00000000000000000000000000000002');

SELECT b FROM t1;

DROP TABLE t1;

SET GLOBAL ROCKSDB_PAUSE_BACKGROUND_WORK = @ORIG_PAUSE_BACKGROUND_WORK;
