#
# INT column types with indexes
#

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


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


# Default index as set in define_engine.inc

let $create_definition =
  i INT $col_indexed_opts,
  t TINYINT $col_opts,
  s SMALLINT $col_opts,
  m MEDIUMINT $col_opts,
  b BIGINT $col_opts,
  $default_index i (i)
;
--source create_table.inc
if ($mysql_errname)
{
  --let $my_last_stmt = $create_statement
  --let $functionality = INT 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 (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);

  SELECT i FROM t1 ORDER BY i;

  DROP TABLE t1;
}


# PK, UNIQUE INDEX, INDEX

let $create_definition =
  i INT $col_indexed_opts PRIMARY KEY,
  t TINYINT $col_opts,
  s SMALLINT $col_opts,
  m MEDIUMINT $col_opts,
  b BIGINT $col_opts
;
--source create_table.inc
if ($mysql_errname)
{
  --let $my_last_stmt = $create_statement
  --let $functionality = INT types or PK
  --source unexpected_result.inc
}
if (!$mysql_errname)
{
  INSERT 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);

  --replace_column 1 # 2 # 3 # 4 # 5 # 7 # 8 # 9 # 
  EXPLAIN SELECT i FROM t1 ORDER BY i;
  SELECT i FROM t1 ORDER BY i;

  --let $alter_definition = DROP PRIMARY KEY
  --source alter_table.inc
  if ($mysql_errname)
  {
    --let $my_last_stmt = $alter_statement
    --let $functionality = ALTER TABLE
    --source unexpected_result.inc
  }
  if (!$mysql_errname)
  {
    --let $alter_definition = ADD PRIMARY KEY (i,t)
    --source alter_table.inc

    INSERT INTO t1 (i,t,s,m,b) VALUES (1,3,2,4,5);
    --replace_column 1 # 2 # 3 # 4 # 5 # 7 # 8 # 9 #
    EXPLAIN SELECT i, t FROM t1 ORDER BY i, t;
    SELECT i, t FROM t1 ORDER BY i, t;
  }

  DROP TABLE t1;
}

let $create_definition =
  i INT $col_opts,
  t TINYINT $col_opts,
  s SMALLINT $col_indexed_opts,
  m MEDIUMINT $col_indexed_opts,
  b BIGINT $col_opts,
  INDEX s_m (s,m)
;
--source create_table.inc
if ($mysql_errname)
{
  --let $my_last_stmt = $create_statement
  --let $functionality = INT types or non-unique indexes or multi-part indexes
  --source unexpected_result.inc
}
if (!$mysql_errname)
{
  INSERT 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);
  
  --replace_column 1 # 2 # 3 # 4 # 5 # 7 # 8 # 9 #
  EXPLAIN SELECT s, m FROM t1 WHERE s != 10 AND m != 1;
  --sorted_result
  SELECT s, m FROM t1 WHERE s != 10 AND m != 1;

  DROP TABLE t1;
}

let $create_definition =
  i INT $col_opts,
  t TINYINT $col_indexed_opts,
  s SMALLINT $col_opts,
  m MEDIUMINT $col_opts,
  b BIGINT $col_indexed_opts,
  UNIQUE KEY b_t (b,t)
;
--source create_table.inc
if ($mysql_errname)
{
  --let $my_last_stmt = $create_statement
  --let $functionality = INT types or unique keys or multi-part keys
  --source unexpected_result.inc
}
if (!$mysql_errname)
{
  INSERT 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);
  
  # This query should use the index b_t, we just don't want to run EXPLAIN
  # (to avoid mismatches due to different subquery-related plans)
  SELECT b+t FROM t1 WHERE (b,t) IN ( SELECT b, t FROM t1 WHERE i>1 ) ORDER BY b+t;
  SELECT b+t FROM t1 FORCE INDEX (b_t) WHERE (b,t) IN ( SELECT b, t FROM t1 WHERE i>1 ) ORDER BY b+t;
  SELECT b+t FROM t1 IGNORE INDEX (b_t) WHERE (b,t) IN ( SELECT b, t FROM t1 WHERE i>1 ) ORDER BY b+t;

  DROP TABLE t1;
}

--source cleanup_engine.inc

