[MDEV-11875] Inconsistent behavior of CREATE TABLE AS ... <string function> depending on strict mode Created: 2017-01-23  Updated: 2019-04-30

Status: Open
Project: MariaDB Server
Component/s: Server
Affects Version/s: 5.5, 10.0, 10.1, 10.2
Fix Version/s: 10.2

Type: Bug Priority: Minor
Reporter: Elena Stepanova Assignee: Alexander Barkov
Resolution: Unresolved Votes: 0
Labels: upstream

Issue Links:
Blocks
is blocked by MDEV-19362 New STRICT flags for sql_mode Open
Relates
relates to MDEV-7635 update defaults and simplify mysqld c... Closed

 Description   

If CREATE TABLE .. AS uses a string function, e.g. REPEAT (but not only it), NULL-ability and default value of the resulting column depends on the strict mode.

MariaDB [test]> set sql_mode='';
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [test]> create table t2 as select repeat('foo',1) as a;
Query OK, 1 row affected (0.34 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
MariaDB [test]> show create table t2;
+-------+---------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                            |
+-------+---------------------------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `a` varchar(3) CHARACTER SET utf8 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [test]> set sql_mode='STRICT_ALL_TABLES';
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [test]> create table t4 as select repeat('foo',1) as a;
Query OK, 1 row affected (0.38 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
MariaDB [test]> show create table t4;
+-------+-------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                |
+-------+-------------------------------------------------------------------------------------------------------------+
| t4    | CREATE TABLE `t4` (
  `a` varchar(3) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

It doesn't happen if a constant value is used.

MariaDB [test]> set sql_mode='';
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [test]> create table t1 as select 'foo' as a;
Query OK, 1 row affected (0.34 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
MariaDB [test]> show create table t1;
+-------+---------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                            |
+-------+---------------------------------------------------------------------------------------------------------+
| t1    | CREATE TABLE `t1` (
  `a` varchar(3) CHARACTER SET utf8 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [test]> set sql_mode='STRICT_ALL_TABLES';
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [test]> create table t3 as select 'foo' as a;
Query OK, 1 row affected (0.46 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
MariaDB [test]> show create table t3;
+-------+---------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                            |
+-------+---------------------------------------------------------------------------------------------------------+
| t3    | CREATE TABLE `t3` (
  `a` varchar(3) CHARACTER SET utf8 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Same in MySQL 5.7.

In 5.5 the behavior is similar, only the NOT NULL column also gets a default value ''.



 Comments   
Comment by Alexander Barkov [ 2017-01-23 ]

Some simple cases for constant arguments can be fixed. But I'm afraid there is no a general solution.

The problem is that the server behavior differs in the default and the strict modes on character set conversion failures:

  • in strict mode: NULL is returned on character set conversion failures
  • in not strict mode: the input string is truncated up to the last good character

When the column data type is determined, it takes into account the current sql_mode.

Comment by Alexander Barkov [ 2017-01-23 ]

Sergei told on slack:
as we've discussed similar issues before, I don't think that STRICT_ALL_TABLES should affect anything when no tables are involved

Generated at Thu Feb 08 07:53:20 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.