[MDEV-14270] Dynamic CREATE TABLE does not preserve the data type of SP variables with NULL value Created: 2017-11-03  Updated: 2022-08-04

Status: Open
Project: MariaDB Server
Component/s: None
Affects Version/s: 10.2, 10.3, 10.4
Fix Version/s: 10.3

Type: Bug Priority: Major
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Unresolved Votes: 0
Labels: None

Issue Links:
Blocks
is blocked by MDEV-14467 Item_param: replace {INT|DECIMAL|REAL... Closed
Relates
relates to MDEV-10709 Expressions as parameters to Dynamic SQL Closed
relates to MDEV-14271 Dynamic SQL: TIMESTAMP parameter valu... Open
Sprint: 10.2.11

 Description   

DELIMITER $$
BEGIN NOT ATOMIC
  DECLARE d0 DATETIME DEFAULT NULL;
  DECLARE d1 DATETIME DEFAULT '2001-01-01 00:00:00';
  DECLARE i0 INT DEFAULT NULL;
  DECLARE i1 INT DEFAULT 10;
  PREPARE stmt FROM 'CREATE OR REPLACE TABLE t1 AS SELECT ? AS c1,? AS c2,? AS c3,? AS c4';
  EXECUTE stmt USING d0,d1,i0,i1;
  DEALLOCATE PREPARE stmt;
  SHOW CREATE TABLE t1;
END;
$$
DELIMITER ;

+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                      |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `c1` binary(0) DEFAULT NULL,
  `c2` datetime DEFAULT NULL,
  `c3` binary(0) DEFAULT NULL,
  `c4` bigint(21) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

The expected data type for c0 should be DATETIME (like for c1).
The expected data type for c3 should be BIGINT (like for c3).



 Comments   
Comment by Alexander Barkov [ 2017-11-17 ]

The same problem is repeatable with functions returning NULL:

DELIMITER $$
BEGIN NOT ATOMIC
  PREPARE stmt FROM 'CREATE OR REPLACE TABLE t1 AS SELECT ? AS d0,? AS d1';
  EXECUTE stmt USING
    DATE_ADD(TIMESTAMP'9999-12-31 23:59:59', INTERVAL 0 SECOND),
    DATE_ADD(TIMESTAMP'9999-12-31 23:59:59', INTERVAL 1 SECOND);
  DEALLOCATE PREPARE stmt;
  SHOW CREATE TABLE t1;
END;
$$
DELIMITER ;

+-------+------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                           |
+-------+------------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `d0` datetime DEFAULT NULL,
  `d1` binary(0) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------+

Notice, the data type for d1 is wrong. It should be datetime.

Note, this script, with direct non-prepared execution, correctly creates two datetime columns:

SET sql_mode='';
CREATE OR REPLACE TABLE t1 AS SELECT
  DATE_ADD(TIMESTAMP'9999-12-31 23:59:59', INTERVAL 0 SECOND) AS d0,
  DATE_ADD(TIMESTAMP'9999-12-31 23:59:59', INTERVAL 1 SECOND) AS d1;
SHOW CREATE TABLE t1;

+-------+-----------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                          |
+-------+-----------------------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `d0` datetime DEFAULT NULL,
  `d1` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------------------------------------------------------+

Generated at Thu Feb 08 08:12:14 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.