--echo #
--echo # MDEV-5271 Support engine-defined attributes per partition
--echo #

--source include/have_partition.inc
--source include/have_innodb.inc

--echo # partitioned tables

CREATE TABLE `t1` (
  `id` INT
) ENGINE=InnoDB ENCRYPTED="YES" PARTITION BY RANGE(id) (
    PARTITION pt1 VALUES LESS THAN (100) ENCRYPTED="NO" ENCRYPTION_KEY_ID=1,
    PARTITION pt2 VALUES LESS THAN MAXVALUE ENCRYPTED="DEFAULT" ENCRYPTION_KEY_ID=1
);
SHOW CREATE TABLE `t1`;

INSERT INTO t1 VALUES (1), (2), (3);
DELETE FROM t1 WHERE id = 1;
UPDATE t1 SET id = 4 WHERE id = 3;
SELECT * FROM t1 WHERE id IN (2, 3);

DROP TABLE `t1`;

--error ER_CANT_CREATE_TABLE
CREATE TABLE `t2` (
  `id` INT
) ENGINE=InnoDB ENCRYPTED="YES" ENCRYPTION_KEY_ID=2 PARTITION BY RANGE(id) (
    PARTITION pt1 VALUES LESS THAN (100),
    PARTITION pt2 VALUES LESS THAN MAXVALUE
);

--error ER_CANT_CREATE_TABLE
CREATE TABLE `t3` (
  `id` INT
) ENGINE=InnoDB ENCRYPTED="NO" PARTITION BY RANGE(id) (
    PARTITION pt1 VALUES LESS THAN (100) ENCRYPTED="YES" ENCRYPTION_KEY_ID=2,
    PARTITION pt2 VALUES LESS THAN MAXVALUE
);

CREATE TABLE `t4` (
  `id` INT
) ENGINE=InnoDB ENCRYPTED="NO";
SHOW CREATE TABLE `t4`;

ALTER TABLE `t4` PARTITION BY RANGE(id) (
    PARTITION pt1 VALUES LESS THAN (100) ENCRYPTED="NO",
    PARTITION pt2 VALUES LESS THAN MAXVALUE ENCRYPTED="DEFAULT"
);
SHOW CREATE TABLE `t4`;

ALTER TABLE `t4` PARTITION BY RANGE(id) (
    PARTITION pt1 VALUES LESS THAN (100),
    PARTITION pt2 VALUES LESS THAN MAXVALUE
);
SHOW CREATE TABLE `t4`;

--error ER_CANT_CREATE_TABLE
ALTER TABLE `t4` PARTITION BY RANGE(id) (
    PARTITION pt1 VALUES LESS THAN (100) ENCRYPTED="YES" ENCRYPTION_KEY_ID=2,
    PARTITION pt2 VALUES LESS THAN MAXVALUE ENCRYPTED="DEFAULT"
);

DROP TABLE `t4`;

--echo # subpartitioned tables

CREATE TABLE `t5` (
  `id` INT
) ENGINE=InnoDB ENCRYPTED="NO" PARTITION BY RANGE(id)
SUBPARTITION BY HASH(id)
SUBPARTITIONS 2 (
  PARTITION pt1 VALUES LESS THAN (100),
  PARTITION pt2 VALUES LESS THAN MAXVALUE
);
SHOW CREATE TABLE `t5`;

DROP TABLE `t5`;

--error ER_CANT_CREATE_TABLE
CREATE TABLE `t6` (
  `id` INT
) ENGINE=InnoDB PARTITION BY RANGE(id)
SUBPARTITION BY HASH(id)
SUBPARTITIONS 2 (
  PARTITION pt1 VALUES LESS THAN (100) ENCRYPTED="YES",
  PARTITION pt2 VALUES LESS THAN MAXVALUE
);

--error ER_PARSE_ERROR
CREATE TABLE `t7` (
  id INT
) ENGINE=InnoDB PARTITION BY RANGE(id)
SUBPARTITION BY HASH(id) (
  PARTITION pt1 VALUES LESS THAN (100)(
    SUBPARTITION spt1 ENCRYPTED="NO",
    SUBPARTITION spt2
  ),
  PARTITION pt2 VALUES LESS THAN MAXVALUE (
    SUBPARTITION spt3,
    SUBPARTITION spt4
  )
);

CREATE TABLE `t8` (
  id INT
) ENGINE=InnoDB ENCRYPTED="NO" PARTITION BY RANGE(id)
SUBPARTITION BY HASH(id) (
  PARTITION pt1 VALUES LESS THAN (100) (
    SUBPARTITION spt1,
    SUBPARTITION spt2
  ),
  PARTITION pt2 VALUES LESS THAN MAXVALUE (
    SUBPARTITION spt3,
    SUBPARTITION spt4
  )
);
SHOW CREATE TABLE `t8`;

DROP TABLE `t8`;

CREATE TABLE `t9` (
  id INT
) ENGINE=InnoDB PARTITION BY RANGE(id)
SUBPARTITION BY HASH(id) (
  PARTITION pt1 VALUES LESS THAN (100) ENCRYPTED="NO" (
    SUBPARTITION spt1,
    SUBPARTITION spt2
  ),
  PARTITION pt2 VALUES LESS THAN MAXVALUE (
    SUBPARTITION spt3,
    SUBPARTITION spt4
  )
);
SHOW CREATE TABLE `t9`;

DROP TABLE `t9`;

--error ER_CANT_CREATE_TABLE
CREATE TABLE `t10` (
  id INT
) ENGINE=InnoDB PARTITION BY RANGE(id)
SUBPARTITION BY HASH(id) (
  PARTITION pt1 VALUES LESS THAN (100) ENCRYPTED="YES" (
    SUBPARTITION spt1,
    SUBPARTITION spt2
  ),
  PARTITION pt2 VALUES LESS THAN MAXVALUE (
    SUBPARTITION spt3,
    SUBPARTITION spt4
  )
);

CREATE TABLE `t11` (
  id INT
) ENGINE=InnoDB ENCRYPTED="YES" PARTITION BY RANGE(id)
SUBPARTITION BY HASH(id) (
  PARTITION pt1 VALUES LESS THAN (100) ENCRYPTED="NO" (
    SUBPARTITION spt1,
    SUBPARTITION spt2
  ),
  PARTITION pt2 VALUES LESS THAN MAXVALUE ENCRYPTED="NO" (
    SUBPARTITION spt3,
    SUBPARTITION spt4
  )
);
SHOW CREATE TABLE `t11`;

DROP TABLE `t11`;

--echo #
--echo # MDEV-27605 ALTER .. ADD PARTITION uses wrong partition-level option values
--echo #

--let $restart_parameters= --innodb-sys-tablespaces
--source include/restart_mysqld.inc

CREATE TABLE `t12` (
  id INT
) ENGINE=InnoDB PARTITION BY HASH(id)
(
  pt1 PAGE_COMPRESSED=0
);
--sorted_result
SELECT name, flag FROM information_schema.innodb_sys_tablespaces WHERE name like 'test/t12%';

ALTER TABLE `t12` ADD PARTITION (
  PARTITION pt2 PAGE_COMPRESSED=1
);
--sorted_result
SELECT name, flag FROM information_schema.innodb_sys_tablespaces WHERE name like 'test/t12%';

ALTER TABLE `t12` ADD PARTITION (
  PARTITION pt3 PAGE_COMPRESSED=1 PAGE_COMPRESSION_LEVEL=3
);
--sorted_result
SELECT name, flag FROM information_schema.innodb_sys_tablespaces WHERE name like 'test/t12%';

DROP TABLE `t12`;

CREATE TABLE `t13` (
  `id` INT
) ENGINE=InnoDB PAGE_COMPRESSED=1 PARTITION BY RANGE(id) (
    PARTITION pt1 VALUES LESS THAN (100) PAGE_COMPRESSED=0,
    PARTITION pt2 VALUES LESS THAN (200) PAGE_COMPRESSED=1 PAGE_COMPRESSION_LEVEL=3,
    PARTITION pt3 VALUES LESS THAN MAXVALUE
);
SHOW CREATE TABLE `t13`;
--sorted_result
SELECT name, flag FROM information_schema.innodb_sys_tablespaces WHERE name like 'test/t13%';

ALTER TABLE `t13` PARTITION BY RANGE(id) (
    PARTITION pt1 VALUES LESS THAN (100) PAGE_COMPRESSED=1 PAGE_COMPRESSION_LEVEL=3,
    PARTITION pt2 VALUES LESS THAN (200),
    PARTITION pt3 VALUES LESS THAN MAXVALUE PAGE_COMPRESSED=0
);
SHOW CREATE TABLE `t13`;
--sorted_result
SELECT name, flag FROM information_schema.innodb_sys_tablespaces WHERE name like 'test/t13%';

DROP TABLE `t13`;
