--source include/innodb_page_size_small.inc
--source include/innodb_checksum_algorithm.inc
SET default_storage_engine=InnoDB;

--disable_query_log
let $MYSQLD_DATADIR = `select @@datadir`;
let $INNODB_PAGE_SIZE = `select @@innodb_page_size`;

call mtr.add_suppression("Cannot add field .* in table .* because after adding it, the row size is");
--enable_query_log

--echo # Test 1) Show the page size from Information Schema
SELECT variable_value FROM information_schema.global_status
       WHERE LOWER(variable_name) = 'innodb_page_size';

--echo # Test 3) Query some information_shema tables that are dependent upon
--echo #         the page size.
# Show the metadata for tables in schema 'mysql'.
# Pulled from innodb-system-table-view.test
# The IDs of mysql.innodb_table_stats and mysql.innodb_index_stats are
# unpredictable. They depend on whether mtr has created the database for
# this test from scratch or is using a previously created database where
# those tables have been dropped and recreated. Since we cannot force mtr
# to use a freshly created database for this test we do not return the
# table or index IDs. We can return the space IS of mysql schema tables
# since they are created consistently during bootstrap.
SELECT	t.name table_name, t.n_cols, t.flag table_flags,
	i.name index_name, i.page_no root_page, i.type,
	i.n_fields, i.merge_threshold
	FROM	INFORMATION_SCHEMA.INNODB_SYS_TABLES  t,
		INFORMATION_SCHEMA.INNODB_SYS_INDEXES i
	WHERE	t.table_id = i.table_id
	AND	t.name LIKE 'mysql%'
	ORDER BY t.name, i.index_id;

CREATE TABLE t1 (a INT KEY, b TEXT) ROW_FORMAT=REDUNDANT ENGINE=innodb;
CREATE TABLE t2 (a INT KEY, b TEXT) ROW_FORMAT=COMPACT ENGINE=innodb;
CREATE TABLE t3 (a INT KEY, b TEXT) ROW_FORMAT=COMPRESSED ENGINE=innodb;
CREATE TABLE t4 (a INT KEY, b TEXT) ROW_FORMAT=DYNAMIC ENGINE=innodb;

# Show the metadata for tables in schema 'test'.
# Do not return the space ID since this tablespace may have existed before
# this test runs.  The root page number of each index should be consistent
# within a file-per-table tablespace.
SELECT	t.name table_name, t.n_cols, t.flag table_flags,
	i.name index_name, i.page_no root_page, i.type,
	i.n_fields, i.merge_threshold
	FROM	INFORMATION_SCHEMA.INNODB_SYS_TABLES  t,
		INFORMATION_SCHEMA.INNODB_SYS_INDEXES i
	WHERE	t.table_id = i.table_id
	AND	t.name LIKE 'test%'
	ORDER BY t.name, i.name;
--source suite/innodb/include/show_i_s_tablespaces.inc
DROP TABLE t1, t2, t3, t4;

--echo # Test 4) The maximum row size is dependent upon the page size.
--echo #         Each row format has its own amount of overhead that
--echo #         varies depending on number of fields and other overhead.

SET SESSION innodb_strict_mode = ON;

if ($INNODB_PAGE_SIZE == 4096) {
# Redundant table; 1927 bytes with 10 CHAR fields
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(127)
) ROW_FORMAT=redundant;
DROP TABLE t1;
--replace_regex /> [0-9]*/> max_row_size/
--error ER_TOO_BIG_ROWSIZE
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(128)
) ROW_FORMAT=redundant;

# Compact table; 1955 bytes with 10 CHAR fields
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(155)
) ROW_FORMAT=compact;
DROP TABLE t1;
--replace_regex /> [0-9]*/> max_row_size/
--error ER_TOO_BIG_ROWSIZE
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(156)
) ROW_FORMAT=compact;

# Compressed table
# MDEV-27634 stricter compressBound() limit for the s390x DFLTCC instruction
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(176)
) ROW_FORMAT=compressed;
DROP TABLE t1;
--replace_regex /> [0-9]*/> max_row_size/
--error ER_TOO_BIG_ROWSIZE
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(79)
) ROW_FORMAT=compressed;

# Dynamic table; 1955 bytes with 10 CHAR fields
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(155)
) ROW_FORMAT=dynamic;
DROP TABLE t1;
--replace_regex /> [0-9]*/> max_row_size/
--error ER_TOO_BIG_ROWSIZE
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(156)
) ROW_FORMAT=dynamic;

#
# Test the maximum key length
# Moved from innodb-index.test since each page size has its own max key length.
# Max Key Length is 1173 for 4k pages.
#
# InnoDB assumes 3 bytes for each UTF8 character.
#
CREATE TABLE t1 (a varchar(98) character set utf8,
                 b varchar(98) character set utf8,
                 c varchar(98) character set utf8,
                 d varchar(97) character set utf8,
                 PRIMARY KEY (a,b,c,d))
		 ENGINE=innodb;
DROP TABLE t1;
--error ER_TOO_LONG_KEY
CREATE TABLE t1 (a varchar(98) character set utf8,
                 b varchar(98) character set utf8,
                 c varchar(98) character set utf8,
                 d varchar(98) character set utf8,
                 PRIMARY KEY (a,b,c,d))
		 ENGINE=innodb;
CREATE TABLE t1 (a varchar(98) character set utf8,
                 b varchar(98) character set utf8,
                 c varchar(98) character set utf8,
                 d varchar(98) character set utf8,
                 e varchar(97) character set utf8,
                 PRIMARY KEY (a), KEY (b,c,d,e))
		 ENGINE=innodb;
DROP TABLE t1;
--error ER_TOO_LONG_KEY
CREATE TABLE t1 (a varchar(98) character set utf8,
                 b varchar(98) character set utf8,
                 c varchar(98) character set utf8,
                 d varchar(98) character set utf8,
                 e varchar(98) character set utf8,
                 PRIMARY KEY (a), KEY (b,c,d,e))
		 ENGINE=innodb;
}
if ($INNODB_PAGE_SIZE == 8192) {
# Redundant table; 3955 bytes with 20 CHAR fields
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(155)
) ROW_FORMAT=redundant;
DROP TABLE t1;
--replace_regex /> [0-9]*/> max_row_size/
--error ER_TOO_BIG_ROWSIZE
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(156)
) ROW_FORMAT=redundant;

# Compact table; 4002 bytes with 20 CHAR fields
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(202)
) ROW_FORMAT=compact;
DROP TABLE t1;
--replace_regex /> [0-9]*/> max_row_size/
--error ER_TOO_BIG_ROWSIZE
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(203)
) ROW_FORMAT=compact;

# Compressed table
# MDEV-27634 stricter compressBound() limit for the s390x DFLTCC instruction
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(182)
) ROW_FORMAT=compressed;
DROP TABLE t1;
--replace_regex /> [0-9]*/> max_row_size/
--error ER_TOO_BIG_ROWSIZE
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(106)
) ROW_FORMAT=compressed;

# Dynamic table; 4002 bytes with 20 CHAR fields
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(202)
) ROW_FORMAT=dynamic;
DROP TABLE t1;
--replace_regex /> [0-9]*/> max_row_size/
--error ER_TOO_BIG_ROWSIZE
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(203)
) ROW_FORMAT=dynamic;

#
# Test the maximum key length
# Moved from innodb-index.test since each page size has its own max key length.
# Max Key Length is 1536 for 8k pages.
#
# InnoDB assumes 3 bytes for each UTF8 character.
#
CREATE TABLE t1 (a varchar(128) character set utf8,
                 b varchar(128) character set utf8,
                 c varchar(128) character set utf8,
                 d varchar(128) character set utf8,
                 PRIMARY KEY (a,b,c,d))
                 ENGINE=innodb;
DROP TABLE t1;
--error ER_TOO_LONG_KEY
CREATE TABLE t1 (a varchar(128) character set utf8,
                 b varchar(128) character set utf8,
                 c varchar(128) character set utf8,
                 d varchar(129) character set utf8,
                 PRIMARY KEY (a,b,c,d))
                 ENGINE=innodb;
CREATE TABLE t1 (a varchar(128) character set utf8,
                 b varchar(128) character set utf8,
                 c varchar(128) character set utf8,
                 d varchar(128) character set utf8,
                 e varchar(128) character set utf8,
                 PRIMARY KEY (a), KEY (b,c,d,e))
                 ENGINE=innodb;
DROP TABLE t1;
--error ER_TOO_LONG_KEY
CREATE TABLE t1 (a varchar(128) character set utf8,
                 b varchar(128) character set utf8,
                 c varchar(128) character set utf8,
                 d varchar(128) character set utf8,
                 e varchar(129) character set utf8,
                 PRIMARY KEY (a), KEY (b,c,d,e))
                 ENGINE=innodb;
}
if ($INNODB_PAGE_SIZE == 16384) {
# Redundant table; 8011 bytes with 40 char fields
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(200),
c21 char(200), c22 char(200), c23 char(200), c24 char(200), c25 char(200),
c26 char(200), c27 char(200), c28 char(200), c29 char(200), c30 char(200),
c31 char(200), c32 char(200), c33 char(200), c34 char(200), c35 char(200),
c36 char(200), c37 char(200), c38 char(200), c39 char(200), c40 char(211)
) ROW_FORMAT=redundant;
DROP TABLE t1;
--error ER_TOO_BIG_ROWSIZE
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(200),
c21 char(200), c22 char(200), c23 char(200), c24 char(200), c25 char(200),
c26 char(200), c27 char(200), c28 char(200), c29 char(200), c30 char(200),
c31 char(200), c32 char(200), c33 char(200), c34 char(200), c35 char(200),
c36 char(200), c37 char(200), c38 char(200), c39 char(200), c40 char(212)
) ROW_FORMAT=redundant;

# Compact table; 8096 bytes with 40 CHAR fields
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(200),
c21 char(200), c22 char(200), c23 char(200), c24 char(200), c25 char(200),
c26 char(200), c27 char(200), c28 char(200), c29 char(200), c30 char(200),
c31 char(200), c32 char(200), c33 char(200), c34 char(200), c35 char(200),
c36 char(200), c37 char(200), c38 char(200), c39 char(250), c40 char(246)
) ROW_FORMAT=compact;
DROP TABLE t1;
--error ER_TOO_BIG_ROWSIZE
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(200),
c21 char(200), c22 char(200), c23 char(200), c24 char(200), c25 char(200),
c26 char(200), c27 char(200), c28 char(200), c29 char(200), c30 char(200),
c31 char(200), c32 char(200), c33 char(200), c34 char(200), c35 char(200),
c36 char(200), c37 char(200), c38 char(200), c39 char(250), c40 char(247)
) ROW_FORMAT=compact;

# Compressed table
# MDEV-27634 stricter compressBound() limit for the s390x DFLTCC instruction
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(200),
c21 char(200), c22 char(200), c23 char(200), c24 char(200), c25 char(200),
c26 char(200), c27 char(200), c28 char(200), c29 char(200), c30 char(200),
c31 char(200), c32 char(200), c33 char(200), c34 char(200), c35 char(200),
c36 char(200), c37 char(200), c38 char(196)
) ROW_FORMAT=compressed;
DROP TABLE t1;
--error ER_TOO_BIG_ROWSIZE
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(200),
c21 char(200), c22 char(200), c23 char(200), c24 char(200), c25 char(200),
c26 char(200), c27 char(200), c28 char(200), c29 char(200), c30 char(200),
c31 char(200), c32 char(200), c33 char(200), c34 char(200), c35 char(200),
c36 char(200), c37 char(200), c38 char(200), c39 char(200), c40 char(160)
) ROW_FORMAT=compressed;

# Dynamic table; 8096 bytes with 40 CHAR fields
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(200),
c21 char(200), c22 char(200), c23 char(200), c24 char(200), c25 char(200),
c26 char(200), c27 char(200), c28 char(200), c29 char(200), c30 char(200),
c31 char(200), c32 char(200), c33 char(200), c34 char(200), c35 char(200),
c36 char(200), c37 char(200), c38 char(200), c39 char(250), c40 char(246)
) ROW_FORMAT=dynamic;
DROP TABLE t1;
--error ER_TOO_BIG_ROWSIZE
CREATE TABLE t1 (
c01 char(200), c02 char(200), c03 char(200), c04 char(200), c05 char(200),
c06 char(200), c07 char(200), c08 char(200), c09 char(200), c10 char(200),
c11 char(200), c12 char(200), c13 char(200), c14 char(200), c15 char(200),
c16 char(200), c17 char(200), c18 char(200), c19 char(200), c20 char(200),
c21 char(200), c22 char(200), c23 char(200), c24 char(200), c25 char(200),
c26 char(200), c27 char(200), c28 char(200), c29 char(200), c30 char(200),
c31 char(200), c32 char(200), c33 char(200), c34 char(200), c35 char(200),
c36 char(200), c37 char(200), c38 char(200), c39 char(250), c40 char(247)
) ROW_FORMAT=dynamic;

#
# Test the maximum key length
# Moved from innodb-index.test since each page size has its own max key length.
# Max Key Length is 3072 for 16k pages.
# Max key Part length is 767
# InnoDB assumes 3 bytes for each UTF8 character.
#
CREATE TABLE t1 (a varchar(255) character set utf8,
                 b varchar(255) character set utf8,
                 c varchar(255) character set utf8,
                 d varchar(255) character set utf8,
                 e varchar(4) character set utf8,
                 PRIMARY KEY (a,b,c,d,e))
		 ENGINE=innodb;
DROP TABLE t1;
--error ER_TOO_LONG_KEY
CREATE TABLE t1 (a varchar(255) character set utf8,
                 b varchar(255) character set utf8,
                 c varchar(255) character set utf8,
                 d varchar(255) character set utf8,
                 e varchar(5) character set utf8,
                 PRIMARY KEY (a,b,c,d,e))
		 ENGINE=innodb;
CREATE TABLE t1 (a varchar(255) character set utf8,
                 b varchar(255) character set utf8,
                 c varchar(255) character set utf8,
                 d varchar(255) character set utf8,
                 e varchar(255) character set utf8,
                 f varchar(4)   character set utf8,
                 PRIMARY KEY (a), KEY (b,c,d,e,f))
		 ENGINE=innodb;
DROP TABLE t1;
--error ER_TOO_LONG_KEY
CREATE TABLE t1 (a varchar(255) character set utf8,
                 b varchar(255) character set utf8,
                 c varchar(255) character set utf8,
                 d varchar(255) character set utf8,
                 e varchar(255) character set utf8,
                 f varchar(5)   character set utf8,
                 PRIMARY KEY (a), KEY (b,c,d,e,f))
		 ENGINE=innodb;
}

--echo # Test 5) KEY_BLOCK_SIZE validation

if ($INNODB_PAGE_SIZE == 4096) {
--error ER_CANT_CREATE_TABLE
CREATE TABLE t1 (i int) ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=16;
SHOW WARNINGS;

--error ER_CANT_CREATE_TABLE
CREATE TABLE t1 ( i INT ) ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;
SHOW WARNINGS;

CREATE TABLE t1 ( i INT ) ROW_FORMAT=COMPRESSED;
}
if ($INNODB_PAGE_SIZE == 8192) {
--error ER_CANT_CREATE_TABLE
CREATE TABLE t1 ( i INT ) ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=16;
SHOW WARNINGS;

CREATE TABLE t1 ( i INT ) ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;
SHOW WARNINGS;
SELECT table_name, row_format, create_options
   FROM information_schema.tables WHERE table_name = 't1';
}
if ($INNODB_PAGE_SIZE == 16384) {
CREATE TABLE t1 (i int) ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=16;
SHOW WARNINGS;
SELECT table_name, row_format, create_options
   FROM information_schema.tables WHERE table_name = 't1';

ALTER TABLE t1 KEY_BLOCK_SIZE=8;
SHOW WARNINGS;
SELECT table_name, row_format, create_options
   FROM information_schema.tables WHERE table_name = 't1';
}
ALTER TABLE t1 KEY_BLOCK_SIZE=4;
SHOW WARNINGS;
SELECT table_name, row_format, create_options
   FROM information_schema.tables WHERE table_name = 't1';

ALTER TABLE t1 KEY_BLOCK_SIZE=2;
SHOW WARNINGS;
SELECT table_name, row_format, create_options
   FROM information_schema.tables WHERE table_name = 't1';

ALTER TABLE t1 KEY_BLOCK_SIZE=1;
SHOW WARNINGS;
SELECT table_name, row_format, create_options
   FROM information_schema.tables WHERE table_name = 't1';

ALTER TABLE t1 KEY_BLOCK_SIZE=0;
SHOW WARNINGS;
SELECT table_name, row_format, create_options
   FROM information_schema.tables WHERE table_name = 't1';
DROP TABLE t1;

SET SESSION innodb_strict_mode = OFF;

CREATE TABLE t1 (i int) ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=16;
SHOW WARNINGS;
SELECT table_name, row_format, create_options
   FROM information_schema.tables WHERE table_name = 't1';

ALTER TABLE t1 KEY_BLOCK_SIZE=8;
SHOW WARNINGS;
SELECT table_name, row_format, create_options
   FROM information_schema.tables WHERE table_name = 't1';

ALTER TABLE t1 KEY_BLOCK_SIZE=4;
SHOW WARNINGS;
SELECT table_name, row_format, create_options
   FROM information_schema.tables WHERE table_name = 't1';

ALTER TABLE t1 KEY_BLOCK_SIZE=2;
SHOW WARNINGS;
SELECT table_name, row_format, create_options
   FROM information_schema.tables WHERE table_name = 't1';

ALTER TABLE t1 KEY_BLOCK_SIZE=1;
SHOW WARNINGS;
SELECT table_name, row_format, create_options
   FROM information_schema.tables WHERE table_name = 't1';

ALTER TABLE t1 KEY_BLOCK_SIZE=0;
SHOW WARNINGS;
SELECT table_name, row_format, create_options
   FROM information_schema.tables WHERE table_name = 't1';
DROP TABLE t1;


--echo # Test 6) KEY_BLOCK_SIZE with innodb_file_per_table=OFF
# Moved from innodb-zip.test
SET SESSION innodb_strict_mode = ON;
SET GLOBAL innodb_file_per_table = OFF;
SHOW VARIABLES LIKE 'innodb_file_per_table';
--error ER_CANT_CREATE_TABLE
CREATE TABLE t4 (id int PRIMARY KEY) ENGINE=innodb KEY_BLOCK_SIZE=8;
SHOW WARNINGS;
--error ER_CANT_CREATE_TABLE
CREATE TABLE t5 (id int PRIMARY KEY) ENGINE=innodb KEY_BLOCK_SIZE=16;
SHOW WARNINGS;
SET GLOBAL innodb_file_per_table = ON;

if ($INNODB_PAGE_SIZE == 16384) {
--echo # Test 7) This series of tests were moved from innodb-index to here
--echo # because the second alter table t1 assumes a 16k page size.
--echo # Moving the test allows the rest of innodb-index to be run on all
--echo # page sizes.  The previously disabled portions of this test were
--echo # moved as well.

CREATE TABLE t2(d varchar(17) PRIMARY KEY) ENGINE=innodb DEFAULT CHARSET=utf8;
CREATE TABLE t3(a int PRIMARY KEY) ENGINE=innodb;

INSERT INTO t3 VALUES (22),(44),(33),(55),(66);

INSERT INTO t2 VALUES ('jejdkrun87'),('adfd72nh9k'),
('adfdpplkeock'),('adfdijnmnb78k'),('adfdijn0loKNHJik');

CREATE TABLE t1(a int, b blob, c text, d text NOT NULL)
ENGINE=innodb DEFAULT CHARSET=utf8 STATS_PERSISTENT=0;

INSERT INTO t1
SELECT a,LEFT(REPEAT(d,100*a),65535),REPEAT(d,20*a),d FROM t2,t3 order by a, d;
DROP TABLE t2, t3;
SELECT COUNT(*) FROM t1 WHERE a=44;
SELECT a,
LENGTH(b),b=LEFT(REPEAT(d,100*a),65535),LENGTH(c),c=REPEAT(d,20*a),d FROM t1
ORDER BY 1, 2, 3, 4, 5, 6;
# in-place alter table should trigger ER_PRIMARY_CANT_HAVE_NULL
--error ER_DUP_ENTRY
ALTER TABLE t1 ADD PRIMARY KEY (a), ADD KEY (b(20));
DELETE FROM t1 WHERE d='null';
--error ER_DUP_ENTRY
ALTER TABLE t1 ADD PRIMARY KEY (a), ADD KEY (b(20));
DELETE FROM t1 WHERE a%2;
CHECK TABLE t1;
# NULL -> NOT NULL only allowed INPLACE if strict sql_mode is on.
# And adding a PRIMARY KEY will also add NOT NULL implicitly!
ALTER TABLE t1 ADD PRIMARY KEY (a,b(255),c(255)), ADD KEY (b(767));
SELECT COUNT(*) FROM t1 WHERE a=44;
SELECT a,
LENGTH(b), b=LEFT(REPEAT(d,100*a), 65535),LENGTH(c), c=REPEAT(d,20*a), d FROM t1;
SHOW CREATE TABLE t1;
CHECK TABLE t1;
--replace_column 9 #
EXPLAIN SELECT * FROM t1 WHERE b LIKE 'adfd%';

# The following tests are disabled because of the introduced timeouts for
# metadata locks at the MySQL level as part of the fix for
# Bug#45225 Locking: hang if drop table with no timeout
# The following commands now play with MySQL metadata locks instead of
# InnoDB locks
# start disabled45225_1
##
## Test locking
##
#
#CREATE TABLE t2(a int, b varchar(255), PRIMARY KEY(a,b)) ENGINE=innodb;
#INSERT INTO t2 SELECT a,LEFT(b,255) FROM t1;
#DROP TABLE t1;
#RENAME TABLE t2 to t1;
#
#connect (a,localhost,root,,);
#connect (b,localhost,root,,);
#connection a;
#SET innodb_lock_wait_timeout=1;
#begin;
## Obtain an IX lock on the table
#SELECT a FROM t1 limit 1 FOR UPDATE;
#connection b;
#SET innodb_lock_wait_timeout=1;
## This would require an S lock on the table, conflicting with the IX lock.
#--error ER_LOCK_WAIT_TIMEOUT
#CREATE INDEX t1ba ON t1 (b,a);
#connection a;
#commit;
#begin;
## Obtain an IS lock on the table
#SELECT a FROM t1 limit 1 lock in share mode;
#connection b;
## This will require an S lock on the table.  No conflict with the IS lock.
#CREATE INDEX t1ba ON t1 (b,a);
## This would require an X lock on the table, conflicting with the IS lock.
#--error ER_LOCK_WAIT_TIMEOUT
#DROP INDEX t1ba ON t1;
#connection a;
#commit;
#EXPLAIN SELECT a FROM t1 ORDER BY b;
#--send
#SELECT a,sleep(2+a/100) FROM t1 ORDER BY b limit 3;
#
## The following DROP INDEX will succeed, altough the SELECT above has
## opened a read view.  However, during the execution of the SELECT,
## MySQL should hold a table lock that should block the execution
## of the DROP INDEX below.
#
#connection b;
#SELECT sleep(1);
#DROP INDEX t1ba ON t1;
#
## After the index was dropped, subsequent SELECTs will use the same
## read view, but they should not be accessing the dropped index any more.
#
#connection a;
#reap;
#EXPLAIN SELECT a FROM t1 ORDER BY b;
#SELECT a FROM t1 ORDER BY b limit 3;
#commit;
#
#connection default;
#disconnect a;
#disconnect b;
#
# end disabled45225_1
DROP TABLE t1;
}

--echo # Test 8) Test creating a table that could lead to undo log overflow.
CREATE TABLE t1(a blob,b blob,c blob,d blob,e blob,f blob,g blob,
                h blob,i blob,j blob,k blob,l blob,m blob,n blob,
		o blob,p blob,q blob,r blob,s blob,t blob,u blob)
		ENGINE=InnoDB ROW_FORMAT=dynamic;
SET @a = repeat('a', 767);
SET @b = repeat('b', 767);
SET @c = repeat('c', 767);
SET @d = repeat('d', 767);
SET @e = repeat('e', 767);

# With no indexes defined, we can update all columns to max key part length.
INSERT INTO t1 VALUES (@a,@a,@a,@a,@a,@a,@a,@a,@a,@a,@a,@a,@a,@a,@a,@a,@a,@a,@a,@a,@a);
UPDATE t1 SET a=@b,b=@b,c=@b,d=@b,e=@b,f=@b,g=@b,h=@b,i=@b,j=@b,
              k=@b,l=@b,m=@b,n=@b,o=@b,p=@b,q=@b,r=@b,s=@b,t=@b,u=@b;

# With this many indexes defined, we can still update all fields.
CREATE INDEX t1a ON t1 (a(767));
if ($INNODB_PAGE_SIZE == 16384) {
CREATE INDEX t1b ON t1 (b(767));
CREATE INDEX t1c ON t1 (c(767));
CREATE INDEX t1d ON t1 (d(767));
CREATE INDEX t1e ON t1 (e(767));
}
UPDATE t1 SET a=@c,b=@c,c=@c,d=@c,e=@c,f=@c,g=@c,h=@c,i=@c,j=@c,
              k=@c,l=@c,m=@c,n=@c,o=@c,p=@c,q=@c,r=@c,s=@c,t=@c,u=@c;

# Add one more index and the UNDO record becomes too big to update all columns.
# But a single transaction can update the columns in separate statements.
# because the UNDO records will be smaller.
CREATE INDEX t1f ON t1 (f(767));
BEGIN;
UPDATE t1 SET a=@d,b=@d,c=@d,d=@d,e=@d,f=@d,g=@d,h=@d,i=@d,j=@d,
              k=@d,l=@d,m=@d,n=@d,o=@d,p=@d,q=@d,r=@d,s=@d,t=@d,u=@d;
ROLLBACK;
BEGIN;
UPDATE t1 SET a=@d,b=@d,c=@d,d=@d,e=@d;
UPDATE t1 SET f=@d,g=@d,h=@d,i=@d,j=@d,k=@d,l=@d,m=@d,
              n=@d,o=@d,p=@d,q=@d,r=@d,s=@d,t=@d,u=@d;
COMMIT;

# More indexes can still be added and a single field can still be updated
CREATE INDEX t1g ON t1 (g(767));
UPDATE t1 SET g=@e;
if ($INNODB_PAGE_SIZE == 16384) {
CREATE INDEX t1h ON t1 (h(767));
UPDATE t1 SET h=@e;
CREATE INDEX t1i ON t1 (i(767));
UPDATE t1 SET i=@e;
CREATE INDEX t1j ON t1 (j(767));
UPDATE t1 SET j=@e;
CREATE INDEX t1k ON t1 (k(767));
UPDATE t1 SET k=@e;
CREATE INDEX t1l ON t1 (l(767));
UPDATE t1 SET l=@e;
CREATE INDEX t1m ON t1 (m(767));
UPDATE t1 SET m=@e;
CREATE INDEX t1n ON t1 (n(767));
UPDATE t1 SET n=@e;
CREATE INDEX t1o ON t1 (o(767));
UPDATE t1 SET o=@e;
CREATE INDEX t1p ON t1 (p(767));
UPDATE t1 SET p=@e;
CREATE INDEX t1q ON t1 (q(767));
UPDATE t1 SET q=@e;
CREATE INDEX t1r ON t1 (r(767));
UPDATE t1 SET r=@e;
CREATE INDEX t1s ON t1 (s(767));
UPDATE t1 SET s=@e;
}

# Add one more index and we cannot update a column to its defined index length.
# This is a problem.  It means that the DDL is allowed to create a table
# that CANNOT be updated.  See bug#12953735.
CREATE INDEX t1t ON t1 (t(767));
BEGIN;
UPDATE t1 SET t=@e;
ROLLBACK;

CREATE INDEX t1u ON t1 (u(767));
if ($INNODB_PAGE_SIZE == 4096)
{
CREATE INDEX t1ut ON t1 (u(767));
CREATE INDEX t1st ON t1 (s(767));
}
if ($INNODB_PAGE_SIZE != 4096)
{
CREATE INDEX t1ut ON t1 (u(767), t(767));
CREATE INDEX t1st ON t1 (s(767), t(767));
}

SHOW CREATE TABLE t1;
DROP TABLE t1;

--echo # Bug#12547647 UPDATE LOGGING COULD EXCEED LOG PAGE SIZE
# InnoDB cannot know that this undo record would be too big for the undo
# page. Too much of text field is stored in the clustered record in this
# DYNAMIC row formatted record.

CREATE TABLE bug12547647(
a int NOT NULL, b blob NOT NULL, c text,
PRIMARY KEY (b(10), a), INDEX (c(767)), INDEX(b(767))
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC;
INSERT INTO bug12547647 VALUES (5,REPEAT('khdfo5AlOq',1900),REPEAT('g',7751));
COMMIT;
# The following used to cause a hang while doing infinite undo log allocation.
BEGIN;
UPDATE bug12547647 SET c = REPEAT('b',16928);
SHOW WARNINGS;
ROLLBACK;
DROP TABLE bug12547647;

# make sure the largest possible key entry can be added to the insert buffer.
# Make enough records so that the root page is not a leaf page.
SET SESSION innodb_strict_mode = OFF;
CREATE TABLE t1(
   pk01 varchar(48), pk02 varchar(48), pk03 varchar(48), pk04 varchar(48),
   pk05 varchar(48), pk06 varchar(48), pk07 varchar(48), pk08 varchar(48),
   pk09 varchar(48), pk10 varchar(48), pk11 varchar(48), pk12 varchar(48),
   pk13 varchar(48), pk14 varchar(48), pk15 varchar(48), pk16 varchar(48),
   sk01 varchar(48), sk02 varchar(48), sk03 varchar(48), sk04 varchar(48),
   sk05 varchar(48), sk06 varchar(48), sk07 varchar(48), sk08 varchar(48),
   sk09 varchar(48), sk10 varchar(48), sk11 varchar(48), sk12 varchar(48),
   sk13 varchar(48), sk14 varchar(48), sk15 varchar(48), sk16 varchar(48),
   PRIMARY KEY pk(pk01,pk02,pk03,pk04,pk05,pk06,pk07,pk08,
                  pk09,pk10,pk11,pk12,pk13,pk14,pk15,pk16),
   KEY pk(sk01,sk02,sk03,sk04,sk05,sk06,sk07,sk08,
          sk09,sk10,sk11,sk12,sk13,sk14,sk15,sk16))
   ROW_FORMAT=Redundant ENGINE=InnoDB;
SET @r = repeat('a', 48);
INSERT INTO t1 VALUES(@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,
                      @r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r);
SET @r = repeat('b', 48);
INSERT INTO t1 VALUES(@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,
                      @r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r);
SET @r = repeat('c', 48);
INSERT INTO t1 VALUES(@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,
                      @r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r);
SET @r = repeat('d', 48);
INSERT INTO t1 VALUES(@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,
                      @r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r);
SET @r = repeat('e', 48);
INSERT INTO t1 VALUES(@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,
                      @r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r);
DELETE from t1;
DROP TABLE t1;

# Compressed tables do not compress parent pages.  So the whole uncompressed
# secondary tuple including the primary key must be able to fit in half the
# compressed page size. This record length is enforced at index creation.
# So the only way to get an ibuf tuple too big is to make the KEY_BLOCK_SIZE
# the same as the page size.
CREATE TABLE t1(
   pk01 varchar(48), pk02 varchar(48), pk03 varchar(48), pk04 varchar(48),
   pk05 varchar(48), pk06 varchar(48), pk07 varchar(48), pk08 varchar(48),
   pk09 varchar(48), pk10 varchar(48), pk11 varchar(48), pk12 varchar(48),
   pk13 varchar(48), pk14 varchar(48), pk15 varchar(48), pk16 varchar(48),
   sk01 varchar(48), sk02 varchar(48), sk03 varchar(48), sk04 varchar(48),
   sk05 varchar(48), sk06 varchar(48), sk07 varchar(48), sk08 varchar(48),
   sk09 varchar(48), sk10 varchar(48), sk11 varchar(48), sk12 varchar(48),
   sk13 varchar(48), sk14 varchar(48), sk15 varchar(48), sk16 varchar(48),
   PRIMARY KEY pk(pk01,pk02,pk03,pk04,pk05,pk06,pk07,pk08,
                  pk09,pk10,pk11,pk12,pk13,pk14,pk15,pk16),
   KEY pk(sk01,sk02,sk03,sk04,sk05,sk06,sk07,sk08,
          sk09,sk10,sk11,sk12,sk13,sk14,sk15,sk16))
   ROW_FORMAT=Compressed KEY_BLOCK_SIZE=4 ENGINE=InnoDB;
SET @r = repeat('a', 48);
INSERT INTO t1 VALUES(@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,
                      @r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r);
SET @r = repeat('b', 48);
INSERT INTO t1 VALUES(@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,
                      @r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r);
SET @r = repeat('c', 48);
INSERT INTO t1 VALUES(@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,
                      @r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r);
SET @r = repeat('d', 48);
INSERT INTO t1 VALUES(@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,
                      @r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r);
SET @r = repeat('e', 48);
INSERT INTO t1 VALUES(@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,
                      @r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r,@r);
DELETE from t1;
DROP TABLE t1;

# The following should fail in non-strict mode too.
# (The fix of Bug #50945 only affects REDUNDANT and COMPACT tables.)
if ($INNODB_PAGE_SIZE == 4096)
{
CREATE TABLE t1(
	c text NOT NULL, d text NOT NULL,
	PRIMARY KEY (c(767)))
ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=1 CHARSET=ASCII;
DROP TABLE t1;
CREATE TABLE t1(
	c text NOT NULL, d text NOT NULL,
	PRIMARY KEY (c(767)))
ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=2 CHARSET=ASCII;
DROP TABLE t1;
CREATE TABLE t1(
	c text NOT NULL, d text NOT NULL,
	PRIMARY KEY (c(767)))
ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4 CHARSET=ASCII;
}
if ($INNODB_PAGE_SIZE != 4096)
{
CREATE TABLE t1(
	c text NOT NULL, d text NOT NULL,
	PRIMARY KEY (c(767),d(767)))
ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=1 CHARSET=ASCII;
DROP TABLE t1;
CREATE TABLE t1(
	c text NOT NULL, d text NOT NULL,
	PRIMARY KEY (c(767),d(767)))
ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=2 CHARSET=ASCII;
DROP TABLE t1;
CREATE TABLE t1(
	c text NOT NULL, d text NOT NULL,
	PRIMARY KEY (c(767),d(767)))
ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4 CHARSET=ASCII;
}
DROP TABLE t1;
CREATE TABLE t1(c text, PRIMARY KEY (c(440)))
ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=1 CHARSET=ASCII;
DROP TABLE t1;
--disable_warnings
CREATE TABLE t1(c text, PRIMARY KEY (c(293)))
ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=1 CHARSET=ASCII;
--enable_warnings
INSERT INTO t1 VALUES(REPEAT('A',512)),(REPEAT('B',512));
DROP TABLE t1;

--echo #
--echo # MDEV-28950 Assertion `*err == DB_SUCCESS' in btr_page_split_and_insert
--echo #
CREATE TABLE t1(c CHAR(255) NOT NULL) ENGINE=InnoDB;
INSERT INTO t1 VALUES ('');
ALTER TABLE t1 ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=1;
INSERT INTO t1 VALUES (''),(''),('');
SET @save_innodb_compression_level= @@innodb_compression_level;
SET GLOBAL innodb_compression_level=0;
INSERT INTO t1 VALUES ('');
SET GLOBAL innodb_compression_level= @save_innodb_compression_level;
DROP TABLE t1;

--echo #
--echo # MDEV-31158 Assertion ...MTR_MEMO_X_LOCKED in btr_attach_half_pages()
--echo #
--source include/have_innodb.inc

SET @save_compression_level=@@GLOBAL.innodb_compression_level;
SET GLOBAL innodb_compression_level=0;
CREATE TEMPORARY TABLE t(a SERIAL, prefix VARBINARY(4), pad INT);
INSERT INTO t(prefix, pad) VALUES
(_binary 0xff,160),('',19),(_binary 0x0001,253),(_binary 0x0b11,169),
(_binary 0x0b010001,23),(_binary 0x0b100001,251),(_binary 0x0d,163),
(_binary 0xb3,254),(_binary 0x96,254),(_binary 0xeb,61),
(_binary 0xf231,253),(_binary 0x1db0,253),(_binary 0x0005,101),
(_binary 0x6370,253),(_binary 0x0b12,112),(_binary 0x0b010002,23),
(_binary 0x0b100002,80),(_binary 0x181984,163),(_binary 0x181926,168),
(_binary 0xe1,176),(_binary 0xe2,187),(_binary 0xe6,254),(_binary 0xbb,51),
(_binary 0x1c,248),(_binary 0x8a,94),(_binary 0x14,254);
CREATE TABLE u(a SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
               b VARBINARY(255), KEY(b)) ENGINE=InnoDB
               KEY_BLOCK_SIZE=1 ROW_FORMAT=COMPRESSED;
INSERT INTO u SELECT a,CONCAT(prefix,REPEAT(chr(0),pad)) FROM t;
DROP TABLE u, t;
SET GLOBAL innodb_compression_level=@save_compression_level;

--echo # End of 10.6 tests
