--echo #
--echo # MDEV-16325 CREATE..SELECT..UNION creates a wrong field type for old varchar
--echo #


DELIMITER $$;
CREATE PROCEDURE p1(col VARCHAR(32))
BEGIN
  EXECUTE IMMEDIATE REPLACE('CREATE TABLE t2_simple AS SELECT col FROM t1old','col',col);
  SHOW CREATE TABLE t2_simple;
  DROP TABLE t2_simple;

  EXECUTE IMMEDIATE REPLACE('CREATE TABLE t2_union_vv AS SELECT col FROM t1old UNION SELECT col FROM t1old','col',col);
  SHOW CREATE TABLE t2_union_vv;
  DROP TABLE t2_union_vv;

  EXECUTE IMMEDIATE REPLACE('CREATE TABLE t2_union_vn AS SELECT col FROM t1old UNION SELECT NULL','col',col);
  SHOW CREATE TABLE t2_union_vn;
  DROP TABLE t2_union_vn;

  EXECUTE IMMEDIATE REPLACE('CREATE TABLE t2_union_nv AS SELECT NULL AS col UNION SELECT col FROM t1old','col',col);
  SHOW CREATE TABLE t2_union_nv;
  DROP TABLE t2_union_nv;

  EXECUTE IMMEDIATE REPLACE('CREATE TABLE t2 AS SELECT
    COALESCE(col),
    COALESCE(col,col),
    COALESCE(col,NULL),
    COALESCE(NULL,col)
  FROM t1old', 'col', col);
  SHOW CREATE TABLE t2;
  DROP TABLE t2;

  EXECUTE IMMEDIATE REPLACE('CREATE TABLE t2 AS SELECT
    LEAST(col,col),
    LEAST(col,NULL),
    LEAST(NULL,col)
  FROM t1old','col',col);
  SHOW CREATE TABLE t2;
  DROP TABLE t2;
END;
$$
DELIMITER ;$$

let $MYSQLD_DATADIR= `SELECT @@datadir`;
copy_file $MYSQL_TEST_DIR/std_data/vchar.frm $MYSQLD_DATADIR/test/t1old.frm;
TRUNCATE TABLE t1old;
SHOW CREATE TABLE t1old;
CALL p1('v');
DROP TABLE t1old;

let $MYSQLD_DATADIR= `SELECT @@datadir`;
copy_file $MYSQL_TEST_DIR/std_data/bug19371.frm $MYSQLD_DATADIR/test/t1old.frm;
TRUNCATE TABLE t1old;
SHOW CREATE TABLE t1old;
CALL p1('a');
DROP TABLE t1old;

DROP PROCEDURE p1;

--echo #
--echo # Start of 10.11 tests
--echo #

--echo #
--echo # MDEV-33442 REPAIR TABLE corrupts UUIDs
--echo #

DELIMITER $$;
CREATE PROCEDURE show_table()
BEGIN
  SHOW CREATE TABLE t1;
  SELECT VERSION FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='t1' AND TABLE_SCHEMA='test';
  SELECT * FROM t1 ORDER BY a,b;
END;
$$
DELIMITER ;$$


--echo # Upgrade using REPAIR

--copy_file $MYSQL_TEST_DIR/std_data/bug19371.frm $MYSQLD_DATADIR/test/t1.frm
TRUNCATE TABLE t1;
CALL show_table;

CHECK TABLE t1 FOR UPGRADE;
CALL show_table;

CHECK TABLE t1 FOR UPGRADE;
CALL show_table;

REPAIR TABLE t1;
CALL show_table;

CHECK TABLE t1 FOR UPGRADE;
CALL show_table;

DROP TABLE t1;


--echo # Upgrade using ALTER, adding a table COMMENT

--copy_file $MYSQL_TEST_DIR/std_data/bug19371.frm $MYSQLD_DATADIR/test/t1.frm
TRUNCATE TABLE t1;
CALL show_table;

--echo # ALTER..INPLACE should fail - the old columns need upgrade
--error ER_ALTER_OPERATION_NOT_SUPPORTED
ALTER IGNORE TABLE t1 ALGORITHM=INPLACE, COMMENT 'test10';
ALTER IGNORE TABLE t1 COMMENT 'test11';
CALL show_table;

--echo # Now ALTER..INPLACE should work
ALTER IGNORE TABLE t1 ALGORITHM=INPLACE, COMMENT 'test12';
CALL show_table;

DROP TABLE t1;


--echo # Upgrade using ALTER, adding a column DEFAULT

--copy_file $MYSQL_TEST_DIR/std_data/bug19371.frm $MYSQLD_DATADIR/test/t1.frm
TRUNCATE TABLE t1;
CALL show_table;

--echo # ALTER..INPLACE should fail - the old columns need upgrade
--error ER_ALTER_OPERATION_NOT_SUPPORTED
ALTER IGNORE TABLE t1 ALGORITHM=INPLACE, MODIFY a VARBINARY(255) DEFAULT 'a10';
ALTER IGNORE TABLE t1 MODIFY a VARBINARY(255) DEFAULT 'a11';
CALL show_table;

--echo # Now ALTER..INPLACE should work
ALTER IGNORE TABLE t1 MODIFY a VARBINARY(255) DEFAULT 'a12';
CALL show_table;

DROP TABLE t1;


DROP PROCEDURE show_table;


--echo #
--echo # End of 10.11 tests
--echo #
