#
# TEXT columns with indexes
#

--source have_engine.inc
--source have_default_index.inc


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

# Default index as configured in define_engine

let $create_definition =
  t TEXT $col_indexed_opts,
  tt TINYTEXT $col_opts,
  m MEDIUMTEXT $col_opts,
  l LONGTEXT $col_opts,
  $default_index t (t(32))
;
--source create_table.inc
if ($mysql_errname)
{
  --let $my_last_stmt = $create_statement
  --let $functionality = TEXT types or indexes on them
  --source unexpected_result.inc
}
if (!$mysql_errname)
{
  --replace_column 3 # 6 # 7 # 10 # 11 #
  SHOW INDEX IN t1;

  INSERT INTO t1 (t,tt,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)),
  ('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));

  SELECT SUBSTRING(t,16) AS f FROM t1 WHERE t IN ('test1','test2') ORDER BY f;

  DROP TABLE t1;
}

# PK, UNIQUE INDEX, INDEX

let $create_definition =
  t TEXT $col_indexed_opts,
  tt TINYTEXT $col_opts,
  m MEDIUMTEXT $col_opts,
  l LONGTEXT $col_opts,
  PRIMARY KEY t (t(32))
;
--source create_table.inc
if ($mysql_errname)
{
  --let $my_last_stmt = $create_statement
  --let $functionality = TEXT types or PK
  --source unexpected_result.inc
}
if (!$mysql_errname)
{
  --replace_column 6 # 7 # 10 # 11 #
  SHOW INDEX IN t1;

  INSERT INTO t1 (t,tt,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)),
  ('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(t,16) AS f FROM t1 WHERE t IN ('test1','test2') ORDER BY f;
  SELECT SUBSTRING(t,16) AS f FROM t1 WHERE t IN ('test1','test2') ORDER BY f;

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

let $create_definition =
  t TEXT $col_opts,
  tt TINYTEXT $col_indexed_opts,
  m MEDIUMTEXT $col_opts,
  l LONGTEXT $col_indexed_opts,
  UNIQUE INDEX l_tt (l(256),tt(64))
;
--source create_table.inc
if ($mysql_errname)
{
  --let $my_last_stmt = $create_statement
  --let $functionality = TEXT types or unique keys or multi-part keys
  --source unexpected_result.inc
}
if (!$mysql_errname)
{
  --replace_column 6 # 7 # 10 # 11 #
  SHOW INDEX IN t1;

  INSERT INTO t1 (t,tt,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)),
  ('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 1 # 2 # 3 # 4 # 6 # 7 # 8 # 9 # 10 #
  EXPLAIN SELECT SUBSTRING(tt,64), SUBSTRING(l,256) FROM t1 WHERE tt!=l AND l NOT IN ('test1') ORDER BY tt, l DESC;
  SELECT SUBSTRING(tt,64), SUBSTRING(l,256) FROM t1 WHERE tt!=l AND l NOT IN ('test1') ORDER BY tt, l DESC;

  --replace_column 1 # 2 # 3 # 4 # 7 # 8 # 9 # 10 #
  EXPLAIN SELECT SUBSTRING(tt,64), SUBSTRING(l,256) FROM t1 FORCE INDEX (l_t) WHERE tt!=l AND l NOT IN ('test1') ORDER BY tt, l DESC;
  SELECT SUBSTRING(tt,64), SUBSTRING(l,256) FROM t1 FORCE INDEX (l_t) WHERE tt!=l AND l NOT IN ('test1') ORDER BY tt, l DESC;
  DROP TABLE t1;
}

let $create_definition =
  t TEXT $col_opts,
  tt TINYTEXT $col_opts,
  m MEDIUMTEXT $col_indexed_opts,
  l LONGTEXT $col_opts,
  INDEX (m(128))
;
--source create_table.inc
if ($mysql_errname)
{
  --let $my_last_stmt = $create_statement
  --let $functionality = TEXT types or non-unique keys
  --source unexpected_result.inc
}
if (!$mysql_errname)
{
  --replace_column 6 # 7 # 10 # 11 #
  SHOW INDEX IN t1;

  INSERT INTO t1 (t,tt,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)),
  ('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(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;

  DROP TABLE t1;
}

--source cleanup_engine.inc

