--echo #
--echo # MDEV-20016 Add MariaDB_DATA_TYPE_PLUGIN
--echo #

--vertical_results
SELECT
  PLUGIN_NAME,
  PLUGIN_VERSION,
  PLUGIN_STATUS,
  PLUGIN_TYPE,
  PLUGIN_AUTHOR,
  PLUGIN_DESCRIPTION,
  PLUGIN_LICENSE,
  PLUGIN_MATURITY,
  PLUGIN_AUTH_VERSION
FROM INFORMATION_SCHEMA.PLUGINS
  WHERE PLUGIN_TYPE='DATA TYPE'
    AND PLUGIN_NAME='test_int8';
--horizontal_results

CREATE TABLE t1 (a TEST_INT8);
SHOW CREATE TABLE t1;
DROP TABLE t1;

CREATE TABLE t1 (a TEST_INT8(4));
SHOW CREATE TABLE t1;
DROP TABLE t1;

CREATE TABLE t1 (a TEST_INT8(10));
SHOW CREATE TABLE t1;
DROP TABLE t1;

CREATE TABLE t1 (a TEST_INT8(20));
SHOW CREATE TABLE t1;
DROP TABLE t1;


--disable_ps_protocol
--enable_metadata
SELECT CAST('100' AS TEST_INT8) AS cast;
--disable_metadata
--enable_ps_protocol

--disable_ps_protocol
--enable_metadata
DELIMITER $$;
BEGIN NOT ATOMIC
  DECLARE a TEST_INT8 DEFAULT 256;
  SELECT HEX(a), a;
END;
$$
DELIMITER ;$$
--disable_metadata
--enable_ps_protocol

CREATE FUNCTION f1(p TEST_INT8) RETURNS TEST_INT8 RETURN 1;
SHOW CREATE FUNCTION f1;
--disable_ps_protocol
--enable_metadata
SELECT f1(10);
--disable_metadata
--enable_ps_protocol
DROP FUNCTION f1;

CREATE TABLE t1 (a TEST_INT8);
CREATE TABLE t2 AS SELECT a FROM t1;
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (a TEST_INT8);
CREATE TABLE t2 AS SELECT COALESCE(a,a) FROM t1;
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (a TEST_INT8);
CREATE TABLE t2 AS SELECT LEAST(a,a) FROM t1;
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (a TEST_INT8);
INSERT INTO t1 VALUES (1),(2);
CREATE TABLE t2 AS SELECT MIN(a), MAX(a) FROM t1;
--disable_ps_protocol
--enable_metadata
SELECT * FROM t2;
--disable_metadata
--enable_ps_protocol
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (id INT, a TEST_INT8);
INSERT INTO t1 VALUES (1,1),(1,2),(2,1),(2,2);
CREATE TABLE t2 AS SELECT id, MIN(a), MAX(a) FROM t1 GROUP BY id;
--disable_ps_protocol
--enable_metadata
SELECT * FROM t2;
--disable_metadata
--enable_ps_protocol
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (a TEST_INT8);
INSERT INTO t1 VALUES (1),(2);
CREATE TABLE t2 AS SELECT DISTINCT a FROM t1;
--disable_ps_protocol
--enable_metadata
SELECT * FROM t2;
--disable_metadata
--enable_ps_protocol
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (a TEST_INT8);
INSERT INTO t1 VALUES (1);
CREATE TABLE t2 AS SELECT (SELECT a FROM t1) AS c1;
--disable_ps_protocol
--enable_metadata
SELECT * FROM t2;
--disable_metadata
--enable_ps_protocol
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (a TEST_INT8);
CREATE TABLE t2 AS SELECT a FROM t1 UNION SELECT a FROM t1;
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;

--echo # Testing CREATE..LIKE

CREATE TABLE t1 (a TEST_INT8);
CREATE TABLE t2 LIKE t1;
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;

--echo # Testing CREATE..SELECT

CREATE TABLE t1 (a TEST_INT8);
CREATE TABLE t2 AS SELECT * FROM t1;
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;


--echo # Testing ALTER

CREATE TABLE t1 (a TEST_INT8);
INSERT INTO t1 VALUES (10),(20);
SELECT * FROM t1;
ALTER TABLE t1 MODIFY a INT;
SHOW CREATE TABLE t1;
SELECT * FROM t1;
ALTER TABLE t1 MODIFY a TEST_INT8;
SHOW CREATE TABLE t1;
SELECT * FROM t1;
DROP TABLE t1;

CREATE TABLE t1 (a TEST_INT8);
INSERT INTO t1 VALUES (10),(20);
ALTER TABLE t1 ADD b TEST_INT8 DEFAULT 0;
SELECT * FROM t1;
DROP TABLE t1;

--echo # Testing metadata views

CREATE TABLE t1 (a TEST_INT8);
--vertical_results
SELECT
  TABLE_CATALOG,
  TABLE_SCHEMA,
  TABLE_NAME,
  COLUMN_NAME,
  ORDINAL_POSITION,
  COLUMN_DEFAULT,
  IS_NULLABLE,
  DATA_TYPE,
  CHARACTER_MAXIMUM_LENGTH,
  CHARACTER_OCTET_LENGTH,
  NUMERIC_PRECISION,
  NUMERIC_SCALE,
  DATETIME_PRECISION,
  CHARACTER_SET_NAME,
  COLLATION_NAME,
  COLUMN_TYPE,
  EXTRA
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='t1' AND TABLE_SCHEMA='test';
--horizontal_results
SHOW COLUMNS FROM t1;
DROP TABLE t1;


--echo # Testing indexing

CREATE TABLE t1 (a TEST_INT8, KEY(a));
INSERT INTO t1 VALUES (0),(1),(2),(3),(4),(5),(6),(7);
EXPLAIN SELECT * FROM t1 WHERE a=3;
DROP TABLE t1;

--echo # Testing aggregation for result

CREATE TABLE t1 (a TEST_INT8);
INSERT INTO t1 VALUES (0),(1),(2),(3),(4),(5),(6),(7);
CREATE TABLE t2 AS SELECT
  COALESCE(a,1) AS c1,
  COALESCE(a,1.0) AS c2,
  COALESCE(a,1e0) AS c3
FROM t1;
SHOW CREATE TABLE t2;
SELECT * FROM t2 ORDER BY c1;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (
  c TEST_INT8,
  c_tinyint TINYINT,
  c_smallint SMALLINT,
  c_mediumint MEDIUMINT,
  c_int INT,
  c_bigint BIGINT
);
CREATE TABLE t2 AS SELECT
  COALESCE(c,c_tinyint),
  COALESCE(c,c_smallint),
  COALESCE(c,c_mediumint),
  COALESCE(c,c_bigint)
FROM t1;
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (
  c TEST_INT8,
  c_time TIME,
  c_date DATE,
  c_datetime DATETIME,
  c_timestamp TIMESTAMP
);
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT COALESCE(c, c_time) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT COALESCE(c, c_date) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT COALESCE(c, c_datetime) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT COALESCE(c, c_timestamp) FROM t1;
DROP TABLE t1;

CREATE TABLE t1 (
  c TEST_INT8,
  c_char CHAR(32),
  c_varchar VARCHAR(32),
  c_tinytext TINYTEXT,
  c_text TEXT,
  c_mediumtext MEDIUMTEXT,
  c_longtext LONGTEXT
);
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT COALESCE(c, c_char) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT COALESCE(c, c_varchar) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT COALESCE(c, c_tinytext) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT COALESCE(c, c_text) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT COALESCE(c, c_mediumtext) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT COALESCE(c, c_longtext) FROM t1;
DROP TABLE t1;

--echo # Testing aggregation for min/max

CREATE TABLE t1 (a TEST_INT8);
INSERT INTO t1 VALUES (0),(1),(2),(3),(4),(5),(6),(7);
CREATE TABLE t2 AS SELECT
  LEAST(a,1) AS c1,
  LEAST(a,1.0) AS c2,
  LEAST(a,1e0) AS c3
FROM t1;
SHOW CREATE TABLE t2;
SELECT * FROM t2 ORDER BY c1;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (
  c TEST_INT8,
  c_tinyint TINYINT,
  c_smallint SMALLINT,
  c_mediumint MEDIUMINT,
  c_int INT,
  c_bigint BIGINT
);
CREATE TABLE t2 AS SELECT
  LEAST(c,c_tinyint),
  LEAST(c,c_smallint),
  LEAST(c,c_mediumint),
  LEAST(c,c_bigint)
FROM t1;
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (
  c TEST_INT8,
  c_time TIME,
  c_date DATE,
  c_datetime DATETIME,
  c_timestamp TIMESTAMP
);
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT LEAST(c, c_time) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT LEAST(c, c_date) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT LEAST(c, c_datetime) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT LEAST(c, c_timestamp) FROM t1;
DROP TABLE t1;

CREATE TABLE t1 (
  c TEST_INT8,
  c_char CHAR(32),
  c_varchar VARCHAR(32),
  c_tinytext TINYTEXT,
  c_text TEXT,
  c_mediumtext MEDIUMTEXT,
  c_longtext LONGTEXT
);
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT LEAST(c, c_char) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT LEAST(c, c_varchar) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT LEAST(c, c_tinytext) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT LEAST(c, c_text) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT LEAST(c, c_mediumtext) FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT LEAST(c, c_longtext) FROM t1;
DROP TABLE t1;


--echo # Testing aggregation for numeric operation - plus

CREATE TABLE t1 (a TEST_INT8);
INSERT INTO t1 VALUES (0),(1),(2),(3),(4),(5),(6),(7);
CREATE TABLE t2 AS SELECT
  a+1 AS c1,
  a+1.0 AS c2,
  a+1e0 AS c3
FROM t1;
SHOW CREATE TABLE t2;
SELECT * FROM t2 ORDER BY c1;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (
  c TEST_INT8,
  c_tinyint TINYINT,
  c_smallint SMALLINT,
  c_mediumint MEDIUMINT,
  c_int INT,
  c_bigint BIGINT
);
CREATE TABLE t2 AS SELECT
  c + c_tinyint,
  c + c_smallint,
  c + c_mediumint,
  c + c_bigint
FROM t1;
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (
  c TEST_INT8,
  c_time TIME,
  c_date DATE,
  c_datetime DATETIME,
  c_timestamp TIMESTAMP
);
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c + c_time FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c + c_date FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c + c_datetime FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c + c_timestamp FROM t1;
DROP TABLE t1;

CREATE TABLE t1 (
  c TEST_INT8,
  c_char CHAR(32),
  c_varchar VARCHAR(32),
  c_tinytext TINYTEXT,
  c_text TEXT,
  c_mediumtext MEDIUMTEXT,
  c_longtext LONGTEXT
);
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c + c_char FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c + c_varchar FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c + c_tinytext FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c + c_text FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c + c_mediumtext FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c + c_longtext FROM t1;
DROP TABLE t1;

--echo # Testing aggregation for numeric operation - minus

CREATE TABLE t1 (a TEST_INT8);
INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8);
CREATE TABLE t2 AS SELECT
  a-1 AS c1,
  a-1.0 AS c2,
  a-1e0 AS c3
FROM t1;
SHOW CREATE TABLE t2;
SELECT * FROM t2 ORDER BY c1;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (
  c TEST_INT8,
  c_tinyint TINYINT,
  c_smallint SMALLINT,
  c_mediumint MEDIUMINT,
  c_int INT,
  c_bigint BIGINT
);
CREATE TABLE t2 AS SELECT
  c - c_tinyint,
  c - c_smallint,
  c - c_mediumint,
  c - c_bigint
FROM t1;
SHOW CREATE TABLE t2;
DROP TABLE t2;
DROP TABLE t1;

CREATE TABLE t1 (
  c TEST_INT8,
  c_time TIME,
  c_date DATE,
  c_datetime DATETIME,
  c_timestamp TIMESTAMP
);
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c - c_time FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c - c_date FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c - c_datetime FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c - c_timestamp FROM t1;
DROP TABLE t1;

CREATE TABLE t1 (
  c TEST_INT8,
  c_char CHAR(32),
  c_varchar VARCHAR(32),
  c_tinytext TINYTEXT,
  c_text TEXT,
  c_mediumtext MEDIUMTEXT,
  c_longtext LONGTEXT
);
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c - c_char FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c - c_varchar FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c - c_tinytext FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c - c_text FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c - c_mediumtext FROM t1;
--error ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION
SELECT c - c_longtext FROM t1;
DROP TABLE t1;


--echo # Testing CAST to other data types

CREATE TABLE t1 (a TEST_INT8);
INSERT INTO t1 VALUES (20000102);
CREATE TABLE t2 AS SELECT
  a,
  CAST(a AS CHAR),
  CAST(a AS DECIMAL),
  CAST(a AS DOUBLE),
  CAST(a AS SIGNED),
  CAST(a AS UNSIGNED),
  CAST(a AS TIME),
  CAST(a AS DATETIME),
  CAST(a AS DATE)
FROM t1;
SHOW CREATE TABLE t2;
--vertical_results
SELECT * FROM t2;
--horizontal_results
DROP TABLE t2;
DROP TABLE t1;
