CREATE TABLE t1(id INT, x xmltype);
SHOW CREATE TABLE t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `id` int(11) DEFAULT NULL,
  `x` xmltype DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1;
CREATE TABLE t1 (a XMLTYPE(6));
ERROR HY000: Data type 'XMLTYPE' doesn't support LENGTH attribute.
CREATE TABLE t1 (a XMLTYPE REF_SYSTEM_ID=4);
ERROR HY000: Data type 'XMLTYPE' doesn't support REF_SYSTEM_ID attribute.
CREATE TABLE t1(id INT, x xmltype  CHARACTER SET utf8mb3) CHARACTER SET utf8mb4;
SHOW CREATE TABLE t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `id` int(11) DEFAULT NULL,
  `x` xmltype CHARACTER SET utf8mb3 COLLATE utf8mb3_uca1400_ai_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1;
CREATE TABLE t1 (a xmltype binary) CHARACTER SET utf8mb4;
SHOW CREATE TABLE t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `a` xmltype CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1;
CREATE TABLE t1 (a xmltype CHARACTER SET binary);
ERROR HY000: Illegal parameter data type xmltype for operation 'CHARACTER SET binary'
CREATE TABLE t1 (a xmltype collate default) CHARACTER SET utf8mb4 collate utf8mb4_bin;
SHOW CREATE TABLE t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `a` xmltype CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
DROP TABLE t1;
CREATE TABLE t1(id INT, x xmltype(10, 2));
ERROR HY000: Data type 'xmltype' doesn't support LENGTH attribute.
CREATE TABLE t1(id INT, x xmltype REF_SYSTEM_ID=1001);
ERROR HY000: Data type 'xmltype' doesn't support REF_SYSTEM_ID attribute.
CREATE TABLE t1(id int, x xmltype, INDEX ix(x(12)));
SHOW CREATE TABLE t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `id` int(11) DEFAULT NULL,
  `x` xmltype DEFAULT NULL,
  KEY `ix` (`x`(12))
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1;
CREATE TABLE t1(id INT, x xmltype);
INSERT INTO t1 VALUES (1, '<one></one>'), (2, '<two></two>'), (3, '<three></three>');
SELECT * FROM t1;
id	x
1	<one></one>
2	<two></two>
3	<three></three>
SHOW CREATE TABLE t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `id` int(11) DEFAULT NULL,
  `x` xmltype DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
CREATE TABLE t2 SELECT * FROM t1;
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL,
  `x` xmltype DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
SELECT * FROM t2;
id	x
1	<one></one>
2	<two></two>
3	<three></three>
DROP TABLE t2;
INSERT INTO t1 VALUES (1, '<one></one>'), (2, '<two></two>'), (3, '<three></three>'), (4, '<four></four>');
EXPLAIN SELECT * FROM t1 GROUP BY id;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	7	Using temporary; Using filesort
CREATE TABLE t2 SELECT * FROM t1 GROUP BY id;
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL,
  `x` xmltype DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t2;
CREATE TABLE t2 SELECT id, COALESCE(x, x) as ce FROM t1;
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL,
  `ce` xmltype DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t2;
CREATE TABLE t2 SELECT id, LEAST(x, x) as ce FROM t1;
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL,
  `ce` xmltype DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t2;
CREATE TABLE t2 SELECT id, MIN(x), MAX(x) FROM t1 GROUP BY id;
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL,
  `MIN(x)` xmltype DEFAULT NULL,
  `MAX(x)` xmltype DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t2;
CREATE TABLE t2 SELECT DISTINCT(x) FROM t1;
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `x` xmltype DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t2;
CREATE TABLE t2 AS SELECT x FROM t1 UNION SELECT x FROM t1;
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `x` xmltype DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t2;
DROP TABLE t1;
CREATE TABLE t1(id int);
ALTER TABLE t1 ADD COLUMN x xmltype;
SHOW CREATE TABLE t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `id` int(11) DEFAULT NULL,
  `x` xmltype DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1;
CREATE FUNCTION xmi(id int) RETURNS xmltype RETURN '<a></a>';
SELECT xmi(1);
xmi(1)
<a></a>
CREATE TABLE t1 SELECT xmi(10) as x;
SHOW CREATE TABLE t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `x` xmltype DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1;
DROP FUNCTION xmi;
create procedure p1(in a int)
begin
declare x xmltype;
end $
SHOW CREATE PROCEDURE p1;
Procedure	sql_mode	Create Procedure	character_set_client	collation_connection	Database Collation
p1	STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION	CREATE DEFINER=`root`@`localhost` PROCEDURE `p1`(in a int)
begin
declare x xmltype;
end	latin1	latin1_swedish_ci	utf8mb4_uca1400_ai_ci
DROP PROCEDURE p1;
create procedure p1(in a int)
begin
declare x xmltype;
end $
SHOW CREATE PROCEDURE p1;
Procedure	sql_mode	Create Procedure	character_set_client	collation_connection	Database Collation
p1	STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION	CREATE DEFINER=`root`@`localhost` PROCEDURE `p1`(in a int)
begin
declare x xmltype;
end	latin1	latin1_swedish_ci	utf8mb4_uca1400_ai_ci
DROP PROCEDURE p1;
CREATE TABLE t1 SELECT cast('<a><b>b</b><c>c</c></a>' as XMLTYPE) as CA;
SHOW CREATE TABLE t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `CA` xmltype CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1;
CREATE TABLE t1(id INT, x xmltype);
SHOW CREATE TABLE t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `id` int(11) DEFAULT NULL,
  `x` xmltype DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
INSERT INTO t1 VALUES (1, CONCAT("<a>", REPEAT("<b></b>", 16384), "</a>"));
SELECT LENGTH(x) FROM t1;
LENGTH(x)
114695
DROP TABLE t1;
EXPLAIN EXTENDED SELECT CAST('a' as xmltype);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	No tables used
Warnings:
Note	1003	select cast('a' as xmltype charset latin1) AS `CAST('a' as xmltype)`
SELECT CAST('<a></a>' AS xmltype CHARACTER SET utf8mb4) as c;
c
<a></a>
SELECT CAST('<a></a>' AS xmltype CHARACTER SET binary) as c;
ERROR HY000: Illegal parameter data type xmltype for operation 'CHARACTER SET binary'
CREATE OR REPLACE TABLE t1 (a text);
INSERT INTO t1 VALUES ('<a></a>'),('<b></b>'),('<c></c>');
EXPLAIN EXTENDED SELECT * FROM t1 WHERE CAST(a AS char)='a' and CAST(a AS xmltype)='a';
id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
Warnings:
Note	1003	select `test`.`t1`.`a` AS `a` from `test`.`t1` where cast(`test`.`t1`.`a` as char charset latin1) = 'a' and cast(`test`.`t1`.`a` as xmltype charset latin1) = 'a'
DROP TABLE t1;
#
# MDEV-38548 Xmltype default values inconsistency is not checked.
#
CREATE TABLE t1(id INT, x xmltype);
INSERT INTO t1 VALUES (1, "<a></a>");
SELECT x + 1 from t1;
ERROR HY000: Illegal parameter data types xmltype and int for operation '+'
DROP TABLE t1;
CREATE TABLE t1 (a INT, b XMLTYPE DEFAULT (a+1));
ERROR HY000: Cannot cast 'bigint' as 'xmltype' in assignment of `test`.`t1`.`b`
CREATE SEQUENCE s;
CREATE TABLE t ( a XMLTYPE DEFAULT NEXTVAL(s));
ERROR HY000: Cannot cast 'bigint' as 'xmltype' in assignment of `test`.`t`.`a`
DROP SEQUENCE s;
CREATE TABLE t1 AS SELECT UpdateXML('<a><b>ccc</b><d></d></a>', '/a', '<e>fff</e>') as x;
SHOW CREATE TABLE t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `x` longtext CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1;
CREATE TABLE t1(a XMLTYPE);
INSERT INTO t1 VALUES(1);
ERROR HY000: Cannot cast 'int' as 'xmltype' in assignment of `test`.`t1`.`a`
INSERT INTO t1 VALUES(11.2);
ERROR HY000: Cannot cast 'decimal' as 'xmltype' in assignment of `test`.`t1`.`a`
INSERT INTO t1 VALUES(11e-2);
ERROR HY000: Cannot cast 'double' as 'xmltype' in assignment of `test`.`t1`.`a`
INSERT INTO t1 VALUES("one");
ERROR HY000: Incorrect XMLTYPE value: 'one'
INSERT INTO t1 VALUES("<a><b></b>");
ERROR HY000: Incorrect XMLTYPE value: '<a><b></b>'
INSERT INTO t1 VALUES("<a><b></b></a>");
INSERT INTO t1 VALUES("<to>JOHN</to><from></from>");
ERROR HY000: Incorrect XMLTYPE value: '<to>JOHN</to><from></from>'
INSERT INTO t1 VALUES('<note><to>JOHN</to><from></from></note>');
INSERT INTO t1 VALUES('<?xml version="1.0" ?> <note><to>JOHN</to><from></from></note>');
INSERT INTO t1 VALUES('<?xml version="1.0" ?> <?xml?> <note><to>JOHN</to><from></from></note>');
ERROR HY000: Incorrect XMLTYPE value: '<?xml version="1.0" ?> <?xml?> <note><to>JOHN</to><from></from></note>'
INSERT INTO t1 VALUES('<note><to>JOHN</to><from></from></note> <?xml version="1.0" ?> ');
ERROR HY000: Incorrect XMLTYPE value: '<note><to>JOHN</to><from></from></note> <?xml version="1.0" ?> '
INSERT INTO t1 VALUES('<?somePI pi content ?> <?xml version="1.0" ?> <note><from>Lisa</from></note>');
ERROR HY000: Incorrect XMLTYPE value: '<?somePI pi content ?> <?xml version="1.0" ?> <note><from>Lisa</from></note>'
INSERT INTO t1 VALUES('<?somePI pi content ?> <note><from>Lisa</from></note>');
INSERT INTO t1 VALUES('<?xml version="1.0" ?> <note><?somePI pi content ?><from>Lisa</from></note>');
DROP TABLE t1;
# End of 11.8 tests
#
# MDEV-39570 LeakSanitizer: detected memory leaks after insert with xmltype.
#
CREATE TABLE t1(a XMLTYPE);
SET @x = CONCAT(REPEAT('<element_with_long_name>', 30), 'leaf',
REPEAT('</element_with_long_name>', 30));
INSERT INTO t1 VALUES(@x);
DROP TABLE t1;
#
# MDEV-39575 Protocol::end_statement(): Assertion `0' failed after insert into xmltype union of valid and invalid xml.
#
CREATE TABLE t1(x XMLTYPE) Engine=MyISAM;
INSERT INTO t1 SELECT '<a/>' UNION SELECT 'a';
ERROR HY000: Incorrect XMLTYPE value: 'a'
SELECT * FROM t1;
x
<a/>
DROP TABLE t1;
#
# MDEV-39577 Protocol::end_statement(): Assertion `0' failed with invalid xmltype + sql_mode=''
#
set sql_mode='';
CREATE TABLE t1(x XMLTYPE);
INSERT INTO t1 SELECT 'a';
ERROR HY000: Incorrect XMLTYPE value: 'a'
DROP TABLE t1;
set sql_mode= DEFAULT;
#
# MDEV-39573 Server crashes in Item_char_typecast::fix_length_and_dec_internal after select xmltype
#
SELECT XMLTYPE(1);
XMLTYPE(1)
NULL
SELECT XMLTYPE("<a></a>");
XMLTYPE("<a></a>")
<a></a>
SELECT CAST(1 AS XMLTYPE);
CAST(1 AS XMLTYPE)
NULL
SELECT CAST("<a></a>" AS XMLTYPE);
CAST("<a></a>" AS XMLTYPE)
<a></a>
SELECT CAST(1 AS XMLTYPE CHARSET utf32);
ERROR 42000: This version of MariaDB doesn't yet support 'CAST(AS XMLTYPE CHARACTER SET ucs2/utf16/utf32)'
SELECT CAST("<a></a>" AS XMLTYPE CHARSET utf32);
ERROR 42000: This version of MariaDB doesn't yet support 'CAST(AS XMLTYPE CHARACTER SET ucs2/utf16/utf32)'
#
# MDEV-39589 Wrong results with xmltype as a virtual column.
#
CREATE TABLE t1(a VARCHAR(50), b XMLTYPE AS (a) VIRTUAL);
INSERT INTO t1(a) VALUES('<yyyyyyyyy>xxxxxxxxxxxxxxxxxxx</yyyyyyyyy>');
INSERT INTO t1(a) VALUES('a');
INSERT INTO t1(a) VALUES('b');
INSERT INTO t1(a) VALUES('<dddddddddd>tttttttttttttttttttttt</dddddddddd>');
INSERT INTO t1(a) VALUES('e');
SELECT * FROM t1;
a	b
<yyyyyyyyy>xxxxxxxxxxxxxxxxxxx</yyyyyyyyy>	<yyyyyyyyy>xxxxxxxxxxxxxxxxxxx</yyyyyyyyy>
a	NULL
b	NULL
<dddddddddd>tttttttttttttttttttttt</dddddddddd>	<dddddddddd>tttttttttttttttttttttt</dddddddddd>
e	NULL
Warnings:
Warning	1525	Incorrect XMLTYPE value: 'a'
Warning	1525	Incorrect XMLTYPE value: 'b'
Warning	1525	Incorrect XMLTYPE value: 'e'
DROP TABLE t1;
#
# MDEV-39592 ALTER.. with xmltype silently overwrites row with prior rows data.
#
SET sql_mode = '';
CREATE TABLE t(id INT, s VARCHAR(100));
INSERT INTO t VALUES (1,'<abc>aaaaaaaaaa</abc>'), (2,'a'), (3,'b'), (4,'<c/>');
ALTER TABLE t MODIFY COLUMN s XMLTYPE;
ERROR HY000: Incorrect XMLTYPE value: 'a'
SELECT id, s FROM t;
id	s
1	<abc>aaaaaaaaaa</abc>
2	a
3	b
4	<c/>
SHOW CREATE TABLE t;
Table	Create Table
t	CREATE TABLE `t` (
  `id` int(11) DEFAULT NULL,
  `s` varchar(100) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t;
SET sql_mode= default;
#
# MDEV-39599 heap-use-after-free in escape_string_for_mysql after sp with invalid xml (xmltype))
#
SET sql_mode = '';
CREATE TABLE t(s VARCHAR(50));
INSERT INTO t VALUES ('<a/>'), ('a');
CREATE PROCEDURE sp()
BEGIN
DECLARE v XMLTYPE;
SELECT s INTO v FROM t WHERE s = '<a/>';
SELECT s INTO v FROM t WHERE s = 'a';
SELECT v;
END//
CALL sp();
ERROR HY000: Incorrect XMLTYPE value: 'a'
SET sql_mode= default;
DROP PROCEDURE sp;
DROP TABLE t;
#
# MDEV-39653 AddressSanitizer: heap-use-after-free in my_mb_wc_latin1/../Field_xmltype::store after invalid xml.
#
CREATE TABLE t1 (i INT);
INSERT INTO t1 VALUES (1),(2),(3),(4);
CREATE TABLE t2 (x XMLTYPE not null);
INSERT IGNORE INTO t2 SELECT i FROM t1;
ERROR HY000: Incorrect XMLTYPE value: '1'
DROP TABLE t1, t2;
# End of 12.3 tests
