SET sql_mode=ORACLE;

--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT DECODE(10);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT DECODE(10,10);

SELECT DECODE(10,10,'x10');
SELECT DECODE(11,10,'x10');

SELECT DECODE(10,10,'x10','def');
SELECT DECODE(11,10,'x10','def');

SELECT DECODE(10,10,'x10',11,'x11','def');
SELECT DECODE(11,10,'x10',11,'x11','def');
SELECT DECODE(12,10,'x10',11,'x11','def');

EXPLAIN EXTENDED SELECT DECODE(12,10,'x10',11,'x11','def');

CREATE TABLE decode (decode int);
DROP TABLE decode;


--echo #
--echo # MDEV-13863 sql_mode=ORACLE: DECODE does not treat two NULLs as equivalent
--echo #

--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT DECODE(10);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT DECODE(10,10);

--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT DECODE_ORACLE(10);
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
SELECT DECODE_ORACLE(10,10);


EXPLAIN EXTENDED SELECT DECODE(12,10,'x10',11,'x11');
EXPLAIN EXTENDED SELECT DECODE(12,10,'x10',11,'x11','def');
EXPLAIN EXTENDED SELECT DECODE_ORACLE(12,10,'x10',11,'x11');
EXPLAIN EXTENDED SELECT DECODE_ORACLE(12,10,'x10',11,'x11','def');

CREATE TABLE t1 (a INT);
CREATE VIEW v1 AS
  SELECT
    DECODE(a,1,'x1',NULL,'xNULL') AS d1,
    DECODE(a,1,'x1',NULL,'xNULL','xELSE') AS d2,
    DECODE_ORACLE(a,1,'x1',NULL,'xNULL') AS d3,
    DECODE_ORACLE(a,1,'x1',NULL,'xNULL','xELSE') AS d4
  FROM t1;
SHOW CREATE VIEW v1;
DROP VIEW v1;
DROP TABLE t1;

SELECT DECODE(TIME'10:20:31','10:20:31','then1','10:20:32','then2','def');
SELECT DECODE(TIME'10:20:32','10:20:31','then1','10:20:32','then2','def');
SELECT DECODE(TIME'10:20:33','10:20:31','then1',NULL,'then2NULL','10:20:33','then3','def');
SELECT DECODE(NULL,TIME'10:20:31','then1',NULL,'then2NULL','10:20:33','then3','def');

SELECT DECODE(TIMESTAMP'2001-01-01 10:20:31','2001-01-01 10:20:31','then1','2001-01-01 10:20:32','then2','def');
SELECT DECODE(TIMESTAMP'2001-01-01 10:20:32','2001-01-01 10:20:31','then1','2001-01-01 10:20:32','then2','def');
SELECT DECODE(TIMESTAMP'2001-01-01 10:20:33','2001-01-01 10:20:31','then1',NULL,'then2NULL','2001-01-01 10:20:33','then3','def');
SELECT DECODE(NULL,TIMESTAMP'2001-01-01 10:20:31','then1',NULL,'then2NULL','2001-01-01 10:20:33','then3','def');

SELECT DECODE('w1','w1','then1','w2','then2','def');
SELECT DECODE('w2','w1','then1','w2','then2','def');
SELECT DECODE('w3','w1','then1',NULL,'then2NULL','w3','then3','def');
SELECT DECODE(NULL,'w1','then1',NULL,'then2NULL','w3','then3','def');

SELECT DECODE(1,1,'then1',2,'then2','def');
SELECT DECODE(2,1,'then1',2,'then2','def');
SELECT DECODE(3,1,'then1',NULL,'then2NULL',3,'then3','def');
SELECT DECODE(NULL,1,'then1',NULL,'then2NULL',3,'then3','def');
SELECT DECODE(CAST(NULL AS SIGNED),1,'then1',NULL,'then2NULL',3,'then3','def');

SELECT DECODE(1.0,1.0,'then1',2.0,'then2','def');
SELECT DECODE(2.0,1.0,'then1',2.0,'then2','def');
SELECT DECODE(3.0,1.0,'then1',NULL,'then2NULL',3.0,'then3','def');
SELECT DECODE(NULL,1.0,'then1',NULL,'then2NULL',3.0,'then3','def');
SELECT DECODE(CAST(NULL AS DECIMAL),1.0,'then1',NULL,'then2NULL',3.0,'then3','def');

SELECT DECODE(1e0,1e0,'then1',2e0,'then2','def');
SELECT DECODE(2e0,1e0,'then1',2e0,'then2','def');
SELECT DECODE(3e0,1e0,'then1',NULL,'then2NULL',3e0,'then3','def');
SELECT DECODE(NULL,1e0,'then1',NULL,'then2NULL',3e0,'then3','def');
SELECT DECODE(CAST(NULL AS DOUBLE),1e0,'then1',NULL,'then2NULL',3e0,'then3','def');

SELECT DECODE(NULL,NULL,1,2) FROM DUAL;
SELECT DECODE(NULL,10,10,NULL,1,2) FROM DUAL;

SELECT DECODE_ORACLE(NULL,NULL,1,2) FROM DUAL;
SELECT DECODE_ORACLE(NULL,10,10,NULL,1,2) FROM DUAL;

CREATE OR REPLACE TABLE t1 (a VARCHAR(10) DEFAULT NULL);
INSERT INTO t1 VALUES (NULL),(1);
SELECT a, DECODE(a,NULL,1,2) FROM t1;
DROP TABLE t1;
