Bug #121290 CTAS with a JSON expression results in literal NULL default, broken SHOW CREATE TABLE
Submitted: 16 Sep 7:08 Modified: 16 Sep 11:22
Reporter: Laurynas Biveinis (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DDL Severity:S3 (Non-critical)
Version:8.4.11, 9.7.2, 26.7.0 OS:Any
Assigned to: CPU Architecture:Any

[16 Sep 7:08] Laurynas Biveinis
Description:
On 8.4.11:

CREATE TABLE t1 (j JSON NOT NULL);
CREATE TABLE t2 AS SELECT COALESCE(j, j) as c FROM t1;
SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `c` json NOT NULL DEFAULT 'null'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

# t2 DEFAULT is active:
INSERT INTO t2 VALUES ();

# SHOW CREATE TABLE cannot be used as CREATE TABLE:
CREATE TABLE t3 (c JSON NOT NULL DEFAULT 'null');
ERROR 42000: BLOB, TEXT, GEOMETRY or JSON column 'c' can't have a default value

DROP TABLE t2, t1;

On 9.7.2 we get warnings and "correct" SHOW CREATE TABLE but the default is still active:

CREATE TABLE t1 (j JSON NOT NULL);
CREATE TABLE t2 AS SELECT COALESCE(j, j) as c FROM t1;
Warnings:
Warning	1101	BLOB, TEXT, GEOMETRY or JSON column 'c' can't have a default value

SHOW CREATE TABLE t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `c` json NOT NULL /*  DEFAULT 'null' */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
Warnings:
Warning	1101	BLOB, TEXT, GEOMETRY or JSON column 'c' can't have a default value

# DEFAULT is active:
INSERT INTO t2 VALUES ();

DROP TABLE t2, t1;

How to repeat:
MTR:

CREATE TABLE t1 (j JSON NOT NULL);
CREATE TABLE t2 AS SELECT COALESCE(j, j) as c FROM t1;
SHOW CREATE TABLE t2;

INSERT INTO t2 VALUES ();

--error 1101
CREATE TABLE t3 (c JSON NOT NULL DEFAULT 'null');

DROP TABLE t2, t1;
[16 Sep 7:53] Laurynas Biveinis
26.7.0 behaves like 9.7.2
[16 Sep 11:22] Roy Lyseng
Thank you for the bug report.
Verified as described.