github.com/dolthub/go-mysql-server@v0.18.0/enginetest/queries/column_default_queries.go (about)

     1  // Copyright 2023 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package queries
    16  
    17  import (
    18  	"github.com/dolthub/go-mysql-server/sql"
    19  	"github.com/dolthub/go-mysql-server/sql/types"
    20  )
    21  
    22  var ColumnDefaultTests = []ScriptTest{
    23  	{
    24  		Name: "update join ambiguous default",
    25  		SetUpScript: []string{
    26  			"CREATE TABLE t1(name varchar(10) primary key, cnt int, hash varchar(100) NOT NULL DEFAULT (concat('id00',md5(name))))",
    27  			"INSERT INTO t1 (name, cnt) VALUES ('one', 1), ('two', 2)",
    28  			"create view t2 as SELECT name, cnt, hash from t1 where name in ('one', 'two')",
    29  		},
    30  		Assertions: []ScriptTestAssertion{
    31  			{
    32  				Query:    "update t1 n inner join t2 m on n.name = m.name set n.cnt =m.cnt+1;",
    33  				Expected: []sql.Row{{newUpdateResult(2, 2)}},
    34  			},
    35  			{
    36  				Query:    "select name, cnt from t1",
    37  				Expected: []sql.Row{{"one", 2}, {"two", 3}},
    38  			},
    39  		},
    40  	},
    41  	{
    42  		Name: "update join ambiguous generated column",
    43  		SetUpScript: []string{
    44  			"CREATE TABLE t1 (x int primary key, y int generated always as (x + 1) virtual)",
    45  			"INSERT INTO t1 (x) values (1), (2), (3)",
    46  			"create view t2 as SELECT x, y from t1",
    47  		},
    48  		Assertions: []ScriptTestAssertion{
    49  			{
    50  				Query:    "update t1 n inner join t2 m on n.y = m.y set n.x =n.y where n.x = 3;",
    51  				Expected: []sql.Row{{newUpdateResult(1, 1)}},
    52  			},
    53  			{
    54  				Query:    "select * from t1",
    55  				Expected: []sql.Row{{1, 2}, {2, 3}, {4, 5}},
    56  			},
    57  		},
    58  	},
    59  	{
    60  		Name: "Standard default literal",
    61  		SetUpScript: []string{
    62  			"CREATE TABLE t1(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT 2)",
    63  			"INSERT INTO t1 (pk) VALUES (1), (2)",
    64  		},
    65  		Assertions: []ScriptTestAssertion{
    66  			{
    67  				Query:    "SELECT * FROM t1",
    68  				Expected: []sql.Row{{1, 2}, {2, 2}},
    69  			},
    70  			{
    71  				Query: "show create table t1",
    72  				Expected: []sql.Row{{"t1",
    73  					"CREATE TABLE `t1` (\n" +
    74  						"  `pk` bigint NOT NULL,\n" +
    75  						"  `v1` bigint DEFAULT '2',\n" +
    76  						"  PRIMARY KEY (`pk`)\n" +
    77  						") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
    78  			},
    79  		},
    80  	},
    81  	{
    82  		Name: "Default expression with function and referenced column",
    83  		SetUpScript: []string{
    84  			"CREATE TABLE t2(pk BIGINT PRIMARY KEY, v1 SMALLINT DEFAULT (GREATEST(pk, 2)))",
    85  			"INSERT INTO t2 (pk) VALUES (1), (2), (3)",
    86  		},
    87  		Assertions: []ScriptTestAssertion{
    88  			{
    89  				Query:    "SELECT * FROM t2",
    90  				Expected: []sql.Row{{1, 2}, {2, 2}, {3, 3}},
    91  			},
    92  			{
    93  				Query: "show create table t2",
    94  				Expected: []sql.Row{{"t2",
    95  					"CREATE TABLE `t2` (\n" +
    96  						"  `pk` bigint NOT NULL,\n" +
    97  						"  `v1` smallint DEFAULT (greatest(`pk`,2)),\n" +
    98  						"  PRIMARY KEY (`pk`)\n" +
    99  						") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
   100  			},
   101  		},
   102  	},
   103  	{
   104  		Name: "Default expression converting to proper column type",
   105  		SetUpScript: []string{
   106  			"CREATE TABLE t3(pk BIGINT PRIMARY KEY, v1 VARCHAR(20) DEFAULT (GREATEST(pk, 2)))",
   107  			"INSERT INTO t3 (pk) VALUES (1), (2), (3)",
   108  		},
   109  		Assertions: []ScriptTestAssertion{{
   110  			Query:    "SELECT * FROM t3",
   111  			Expected: []sql.Row{{1, "2"}, {2, "2"}, {3, "3"}}},
   112  		},
   113  	},
   114  	{
   115  		Name: "Default literal of different type but implicitly converts",
   116  		SetUpScript: []string{
   117  			"CREATE TABLE t4(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT '4')",
   118  			"INSERT INTO t4 (pk) VALUES (1), (2)",
   119  		},
   120  		Assertions: []ScriptTestAssertion{{
   121  			Query:    "SELECT * FROM t4",
   122  			Expected: []sql.Row{{1, 4}, {2, 4}}},
   123  		},
   124  	},
   125  	{
   126  		Name: "Back reference to default literal",
   127  		SetUpScript: []string{
   128  			"CREATE TABLE t5(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (v2), v2 BIGINT DEFAULT 7)",
   129  			"INSERT INTO t5 (pk) VALUES (1), (2)",
   130  		},
   131  		Assertions: []ScriptTestAssertion{{
   132  			Query:    "SELECT * FROM t5",
   133  			Expected: []sql.Row{{1, 7, 7}, {2, 7, 7}}},
   134  		},
   135  	},
   136  	{
   137  		Name: "Forward reference to default literal",
   138  		SetUpScript: []string{
   139  			"CREATE TABLE t6(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT 9, v2 BIGINT DEFAULT (v1))",
   140  			"INSERT INTO t6 (pk) VALUES (1), (2)",
   141  		},
   142  		Assertions: []ScriptTestAssertion{{
   143  			Query:    "SELECT * FROM t6",
   144  			Expected: []sql.Row{{1, 9, 9}, {2, 9, 9}}},
   145  		},
   146  	},
   147  	{
   148  		Name: "Forward reference to default expression",
   149  		SetUpScript: []string{
   150  			"CREATE TABLE t7(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (8), v2 BIGINT DEFAULT (v1))",
   151  			"INSERT INTO t7 (pk) VALUES (1), (2)",
   152  		},
   153  		Assertions: []ScriptTestAssertion{{
   154  			Query:    "SELECT * FROM t7",
   155  			Expected: []sql.Row{{1, 8, 8}, {2, 8, 8}}},
   156  		},
   157  	},
   158  	{
   159  		Name: "Back reference to value",
   160  		SetUpScript: []string{
   161  			"CREATE TABLE t8(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (v2 + 1), v2 BIGINT)",
   162  			"INSERT INTO t8 (pk, v2) VALUES (1, 4), (2, 6)",
   163  		},
   164  		Assertions: []ScriptTestAssertion{{
   165  			Query:    "SELECT * FROM t8",
   166  			Expected: []sql.Row{{1, 5, 4}, {2, 7, 6}}},
   167  		},
   168  	},
   169  	{
   170  		Name: "TEXT expression",
   171  		SetUpScript: []string{
   172  			"CREATE TABLE t9(pk BIGINT PRIMARY KEY, v1 LONGTEXT DEFAULT (77))",
   173  			"INSERT INTO t9 (pk) VALUES (1), (2)",
   174  		},
   175  		Assertions: []ScriptTestAssertion{{
   176  			Query:    "SELECT * FROM t9",
   177  			Expected: []sql.Row{{1, "77"}, {2, "77"}}},
   178  		},
   179  	},
   180  	{
   181  		Name: "REPLACE INTO with default expression",
   182  		SetUpScript: []string{
   183  			"CREATE TABLE t12(pk BIGINT PRIMARY KEY, v1 SMALLINT DEFAULT (GREATEST(pk, 2)))",
   184  			"INSERT INTO t12 (pk) VALUES (1), (2)",
   185  			"REPLACE INTO t12 (pk) VALUES (2), (3)",
   186  		},
   187  		Assertions: []ScriptTestAssertion{{
   188  			Query:    "SELECT * FROM t12",
   189  			Expected: []sql.Row{{1, 2}, {2, 2}, {3, 3}}},
   190  		},
   191  	},
   192  	{
   193  		Name: "Add column last default literal",
   194  		SetUpScript: []string{
   195  			"CREATE TABLE t13(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT '4')",
   196  			"INSERT INTO t13 (pk) VALUES (1), (2)",
   197  			"ALTER TABLE t13 ADD COLUMN v2 BIGINT DEFAULT 5",
   198  		},
   199  		Assertions: []ScriptTestAssertion{{
   200  			Query:    "SELECT * FROM t13",
   201  			Expected: []sql.Row{{1, 4, 5}, {2, 4, 5}}},
   202  		},
   203  	},
   204  	{
   205  		Name: "Add column implicit last default expression",
   206  		SetUpScript: []string{
   207  			"CREATE TABLE t14(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (pk + 1))",
   208  			"INSERT INTO t14 (pk) VALUES (1), (2)",
   209  			"ALTER TABLE t14 ADD COLUMN v2 BIGINT DEFAULT (v1 + 2)",
   210  		},
   211  		Assertions: []ScriptTestAssertion{{
   212  			Query:    "SELECT * FROM t14",
   213  			Expected: []sql.Row{{1, 2, 4}, {2, 3, 5}}},
   214  		},
   215  	},
   216  	{
   217  		Name: "Add column explicit last default expression",
   218  		SetUpScript: []string{
   219  			"CREATE TABLE t15(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (pk + 1))",
   220  			"INSERT INTO t15 (pk) VALUES (1), (2)",
   221  			"ALTER TABLE t15 ADD COLUMN v2 BIGINT DEFAULT (v1 + 2) AFTER v1",
   222  		},
   223  		Assertions: []ScriptTestAssertion{{
   224  			Query:    "SELECT * FROM t15",
   225  			Expected: []sql.Row{{1, 2, 4}, {2, 3, 5}}},
   226  		},
   227  	},
   228  	{
   229  		Name: "Add column first default literal",
   230  		SetUpScript: []string{
   231  			"CREATE TABLE t16(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT '4')",
   232  			"INSERT INTO t16 (pk) VALUES (1), (2)",
   233  			"ALTER TABLE t16 ADD COLUMN v2 BIGINT DEFAULT 5 FIRST",
   234  		},
   235  		Assertions: []ScriptTestAssertion{{
   236  			Query:    "SELECT * FROM t16",
   237  			Expected: []sql.Row{{5, 1, 4}, {5, 2, 4}}},
   238  		},
   239  	},
   240  	{
   241  		Name: "Add column first default expression",
   242  		SetUpScript: []string{
   243  			"CREATE TABLE t17(pk BIGINT PRIMARY KEY, v1 BIGINT)",
   244  			"INSERT INTO t17 VALUES (1, 3), (2, 4)",
   245  			"ALTER TABLE t17 ADD COLUMN v2 BIGINT DEFAULT (v1 + 2) FIRST",
   246  		},
   247  		Assertions: []ScriptTestAssertion{{
   248  			Query:    "SELECT * FROM t17",
   249  			Expected: []sql.Row{{5, 1, 3}, {6, 2, 4}}},
   250  		},
   251  	},
   252  	{
   253  		Name: "Add column forward reference to default expression",
   254  		SetUpScript: []string{
   255  			"CREATE TABLE t18(pk BIGINT DEFAULT (v1) PRIMARY KEY, v1 BIGINT)",
   256  			"INSERT INTO t18 (v1) VALUES (1), (2)",
   257  			"ALTER TABLE t18 ADD COLUMN v2 BIGINT DEFAULT (pk + 1) AFTER pk",
   258  		},
   259  		Assertions: []ScriptTestAssertion{{
   260  			Query:    "SELECT * FROM t18",
   261  			Expected: []sql.Row{{1, 2, 1}, {2, 3, 2}}},
   262  		},
   263  	},
   264  	{
   265  		Name: "Add column back reference to default literal",
   266  		SetUpScript: []string{
   267  			"CREATE TABLE t19(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT 5)",
   268  			"INSERT INTO t19 (pk) VALUES (1), (2)",
   269  			"ALTER TABLE t19 ADD COLUMN v2 BIGINT DEFAULT (v1 - 1) AFTER pk",
   270  		},
   271  		Assertions: []ScriptTestAssertion{{
   272  			Query:    "SELECT * FROM t19",
   273  			Expected: []sql.Row{{1, 4, 5}, {2, 4, 5}}},
   274  		},
   275  	},
   276  	{
   277  		Name: "Add column first with existing defaults still functioning",
   278  		SetUpScript: []string{
   279  			"CREATE TABLE t20(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (pk + 10))",
   280  			"INSERT INTO t20 (pk) VALUES (1), (2)",
   281  			"ALTER TABLE t20 ADD COLUMN v2 BIGINT DEFAULT (-pk) FIRST",
   282  			"INSERT INTO t20 (pk) VALUES (3)",
   283  		},
   284  		Assertions: []ScriptTestAssertion{{
   285  			Query:    "SELECT * FROM t20",
   286  			Expected: []sql.Row{{-1, 1, 11}, {-2, 2, 12}, {-3, 3, 13}}},
   287  		},
   288  	},
   289  	{
   290  		Name: "Drop column referencing other column",
   291  		SetUpScript: []string{
   292  			"CREATE TABLE t21(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (v2), v2 BIGINT)",
   293  		},
   294  		Assertions: []ScriptTestAssertion{{
   295  			Query:    "ALTER TABLE t21 DROP COLUMN v1",
   296  			Expected: []sql.Row{{types.NewOkResult(0)}}},
   297  		},
   298  	},
   299  	{
   300  		Name: "Modify column move first forward reference default literal",
   301  		SetUpScript: []string{
   302  			"CREATE TABLE t22(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (pk + 2), v2 BIGINT DEFAULT (pk + 1))",
   303  			"INSERT INTO t22 (pk) VALUES (1), (2)",
   304  			"ALTER TABLE t22 MODIFY COLUMN v1 BIGINT DEFAULT (pk + 2) FIRST",
   305  		},
   306  		Assertions: []ScriptTestAssertion{{
   307  			Query:    "SELECT * FROM t22",
   308  			Expected: []sql.Row{{3, 1, 2}, {4, 2, 3}}},
   309  		},
   310  	},
   311  	{
   312  		Name: "Modify column move first add reference",
   313  		SetUpScript: []string{
   314  			"CREATE TABLE t23(pk BIGINT PRIMARY KEY, v1 BIGINT, v2 BIGINT DEFAULT (v1 + 1))",
   315  			"INSERT INTO t23 (pk, v1) VALUES (1, 2), (2, 3)",
   316  			"ALTER TABLE t23 MODIFY COLUMN v1 BIGINT DEFAULT (pk + 5) FIRST",
   317  			"INSERT INTO t23 (pk) VALUES (3)",
   318  		},
   319  		Assertions: []ScriptTestAssertion{
   320  			{
   321  				Query: "SELECT * FROM t23 order by 1",
   322  				Expected: []sql.Row{
   323  					{2, 1, 3},
   324  					{3, 2, 4},
   325  					{8, 3, 9},
   326  				},
   327  			},
   328  		},
   329  	},
   330  	{
   331  		Name: "Modify column move last being referenced",
   332  		SetUpScript: []string{
   333  			"CREATE TABLE t24(pk BIGINT PRIMARY KEY, v1 BIGINT, v2 BIGINT DEFAULT (v1 + 1))",
   334  			"INSERT INTO t24 (pk, v1) VALUES (1, 2), (2, 3)",
   335  			"ALTER TABLE t24 MODIFY COLUMN v1 BIGINT AFTER v2",
   336  			"INSERT INTO t24 (pk, v1) VALUES (3, 4)",
   337  		},
   338  		Assertions: []ScriptTestAssertion{
   339  			{
   340  				Query: "SELECT * FROM t24 order by 1",
   341  				Expected: []sql.Row{
   342  					{1, 3, 2},
   343  					{2, 4, 3},
   344  					{3, 5, 4},
   345  				},
   346  			},
   347  		},
   348  	},
   349  	{
   350  		Name: "Modify column move last add reference",
   351  		SetUpScript: []string{
   352  			"CREATE TABLE t25(pk BIGINT PRIMARY KEY, v1 BIGINT, v2 BIGINT DEFAULT (pk * 2))",
   353  			"INSERT INTO t25 (pk, v1) VALUES (1, 2), (2, 3)",
   354  			"ALTER TABLE t25 MODIFY COLUMN v1 BIGINT DEFAULT (-pk) AFTER v2",
   355  			"INSERT INTO t25 (pk) VALUES (3)",
   356  		},
   357  		Assertions: []ScriptTestAssertion{
   358  			{
   359  				Query: "SELECT * FROM t25",
   360  				Expected: []sql.Row{
   361  					{1, 2, 2},
   362  					{2, 4, 3},
   363  					{3, 6, -3},
   364  				},
   365  			},
   366  		},
   367  	},
   368  	{
   369  		Name: "Modify column no move add reference",
   370  		SetUpScript: []string{
   371  			"CREATE TABLE t26(pk BIGINT PRIMARY KEY, v1 BIGINT, v2 BIGINT DEFAULT (pk * 2))",
   372  			"INSERT INTO t26 (pk, v1) VALUES (1, 2), (2, 3)",
   373  			"ALTER TABLE t26 MODIFY COLUMN v1 BIGINT DEFAULT (-pk)",
   374  			"INSERT INTO t26 (pk) VALUES (3)",
   375  		},
   376  		Assertions: []ScriptTestAssertion{
   377  			{
   378  				Query: "SELECT * FROM t26",
   379  				Expected: []sql.Row{
   380  					{1, 2, 2},
   381  					{2, 3, 4},
   382  					{3, -3, 6},
   383  				},
   384  			},
   385  		},
   386  	},
   387  	{
   388  		Name:        "Negative float literal",
   389  		SetUpScript: []string{"CREATE TABLE t27(pk BIGINT PRIMARY KEY, v1 DOUBLE DEFAULT -1.1)"},
   390  		Assertions: []ScriptTestAssertion{{
   391  			Query:    "DESCRIBE t27",
   392  			Expected: []sql.Row{{"pk", "bigint", "NO", "PRI", "NULL", ""}, {"v1", "double", "YES", "", "-1.1", ""}}},
   393  		},
   394  	},
   395  	{
   396  		Name: "Column referenced with name change",
   397  		SetUpScript: []string{
   398  			"CREATE TABLE t29(pk BIGINT PRIMARY KEY, v1 BIGINT, v2 BIGINT DEFAULT (v1 + 1))",
   399  			"INSERT INTO t29 (pk, v1) VALUES (1, 2)",
   400  			"ALTER TABLE t29 RENAME COLUMN v1 to v1x",
   401  			"INSERT INTO t29 (pk, v1x) VALUES (2, 3)",
   402  			"ALTER TABLE t29 CHANGE COLUMN v1x v1y BIGINT",
   403  			"INSERT INTO t29 (pk, v1y) VALUES (3, 4)",
   404  		},
   405  
   406  		Assertions: []ScriptTestAssertion{
   407  			{
   408  				Query: "SELECT * FROM t29 ORDER BY 1",
   409  				Expected: []sql.Row{
   410  					{1, 2, 3},
   411  					{2, 3, 4},
   412  					{3, 4, 5},
   413  				},
   414  			},
   415  			{
   416  				Query: "SHOW CREATE TABLE t29",
   417  				Expected: []sql.Row{{"t29", "CREATE TABLE `t29` (\n" +
   418  					"  `pk` bigint NOT NULL,\n" +
   419  					"  `v1y` bigint,\n" +
   420  					"  `v2` bigint DEFAULT ((`v1y` + 1)),\n" +
   421  					"  PRIMARY KEY (`pk`)\n" +
   422  					") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
   423  			},
   424  		},
   425  	},
   426  	{
   427  		Name: "Add multiple columns same ALTER",
   428  		SetUpScript: []string{
   429  			"CREATE TABLE t30(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT '4')",
   430  			"INSERT INTO t30 (pk) VALUES (1), (2)",
   431  			"ALTER TABLE t30 ADD COLUMN v2 BIGINT DEFAULT 5, ADD COLUMN V3 BIGINT DEFAULT 7",
   432  		},
   433  		Assertions: []ScriptTestAssertion{
   434  			{
   435  				Query: "SELECT pk, v1, v2, V3 FROM t30",
   436  				Expected: []sql.Row{
   437  					{1, 4, 5, 7},
   438  					{2, 4, 5, 7},
   439  				},
   440  			},
   441  		},
   442  	},
   443  	{
   444  		Name: "Add non-nullable column without default #1",
   445  		SetUpScript: []string{
   446  			"CREATE TABLE t31 (pk BIGINT PRIMARY KEY)",
   447  			"INSERT INTO t31 VALUES (1), (2), (3)",
   448  			"ALTER TABLE t31 ADD COLUMN v1 BIGINT NOT NULL",
   449  		},
   450  		Assertions: []ScriptTestAssertion{
   451  			{
   452  				Query:    "SELECT * FROM t31",
   453  				Expected: []sql.Row{{1, 0}, {2, 0}, {3, 0}},
   454  			},
   455  		},
   456  	},
   457  	{
   458  		Name: "Add non-nullable column without default #2",
   459  		SetUpScript: []string{
   460  			"CREATE TABLE t32 (pk BIGINT PRIMARY KEY)",
   461  			"INSERT INTO t32 VALUES (1), (2), (3)",
   462  			"ALTER TABLE t32 ADD COLUMN v1 VARCHAR(20) NOT NULL",
   463  		},
   464  		Assertions: []ScriptTestAssertion{
   465  			{
   466  				Query:    "SELECT * FROM t32",
   467  				Expected: []sql.Row{{1, ""}, {2, ""}, {3, ""}},
   468  			},
   469  		},
   470  	},
   471  	{
   472  		Name: "Column defaults with functions",
   473  		SetUpScript: []string{
   474  			"CREATE TABLE t33(pk varchar(100) DEFAULT (replace(UUID(), '-', '')), v1 timestamp(6) DEFAULT now(), v2 varchar(100), primary key (pk))",
   475  			"insert into t33 (v2) values ('abc')",
   476  			"alter table t33 add column name varchar(100)",
   477  			"alter table t33 rename column v1 to v1_new",
   478  			"alter table t33 rename column name to name2",
   479  			"alter table t33 drop column name2",
   480  			"alter table t33 add column v3 datetime(6) default CURRENT_TIMESTAMP()",
   481  		},
   482  
   483  		Assertions: []ScriptTestAssertion{
   484  			{
   485  				Query: "desc t33",
   486  				Expected: []sql.Row{
   487  					{"pk", "varchar(100)", "NO", "PRI", "(replace(uuid(), '-', ''))", "DEFAULT_GENERATED"},
   488  					{"v1_new", "timestamp(6)", "YES", "", "CURRENT_TIMESTAMP", "DEFAULT_GENERATED"},
   489  					{"v2", "varchar(100)", "YES", "", "NULL", ""},
   490  					{"v3", "datetime(6)", "YES", "", "CURRENT_TIMESTAMP", "DEFAULT_GENERATED"},
   491  				},
   492  			},
   493  			{
   494  				Query:          "alter table t33 add column v4 date default CURRENT_TIMESTAMP()",
   495  				ExpectedErrStr: "only datetime/timestamp may declare default values of now()/current_timestamp() without surrounding parentheses",
   496  			},
   497  		},
   498  	},
   499  	{
   500  		Name: "Function expressions must be enclosed in parens",
   501  		Assertions: []ScriptTestAssertion{
   502  			{
   503  				Query:       "create table t0 (v0 varchar(100) default repeat(\"_\", 99));",
   504  				ExpectedErr: sql.ErrSyntaxError,
   505  			},
   506  		},
   507  	},
   508  
   509  	{
   510  		Name: "Column references must be enclosed in parens",
   511  		Assertions: []ScriptTestAssertion{
   512  			{
   513  				Query:       "Create table t0 (c0 int, c1 int default c0);",
   514  				ExpectedErr: sql.ErrSyntaxError,
   515  			},
   516  		},
   517  	},
   518  
   519  	{
   520  		Name: "Invalid literal for column type",
   521  		Assertions: []ScriptTestAssertion{
   522  			{
   523  				Query:       "CREATE TABLE t999(pk BIGINT PRIMARY KEY, v1 INT UNSIGNED DEFAULT -1)",
   524  				ExpectedErr: sql.ErrIncompatibleDefaultType,
   525  			},
   526  		},
   527  	},
   528  
   529  	{
   530  		Name: "Invalid literal for column type",
   531  		Assertions: []ScriptTestAssertion{
   532  			{
   533  				Query:       "CREATE TABLE t999(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT 'hi')",
   534  				ExpectedErr: sql.ErrIncompatibleDefaultType,
   535  			},
   536  		},
   537  	},
   538  
   539  	{
   540  		Name: "Expression contains invalid literal once implicitly converted",
   541  		Assertions: []ScriptTestAssertion{
   542  			{
   543  				Query:       "CREATE TABLE t999(pk BIGINT PRIMARY KEY, v1 INT UNSIGNED DEFAULT '-1')",
   544  				ExpectedErr: sql.ErrIncompatibleDefaultType,
   545  			},
   546  		},
   547  	},
   548  
   549  	{
   550  		Name: "Null literal is invalid for NOT NULL",
   551  		Assertions: []ScriptTestAssertion{
   552  			{
   553  				Query:       "CREATE TABLE t999(pk BIGINT PRIMARY KEY, v1 BIGINT NOT NULL DEFAULT NULL)",
   554  				ExpectedErr: sql.ErrIncompatibleDefaultType,
   555  			},
   556  		},
   557  	},
   558  
   559  	{
   560  		Name: "Back reference to expression",
   561  		Assertions: []ScriptTestAssertion{
   562  			{
   563  				Query:       "CREATE TABLE t999(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (v2), v2 BIGINT DEFAULT (9))",
   564  				ExpectedErr: sql.ErrInvalidDefaultValueOrder,
   565  			},
   566  		},
   567  	},
   568  	{
   569  		// Technically, MySQL does NOT allow BLOB/JSON/TEXT types to have a literal default value, and requires them
   570  		// to be specified as an expression (i.e. wrapped in parens). We diverge from this behavior and allow it, for
   571  		// compatibility with MariaDB. For more context, see: https://github.com/dolthub/dolt/issues/7033
   572  		Name: "BLOB types can define defaults with literals",
   573  		Assertions: []ScriptTestAssertion{
   574  			{
   575  				Query:    "CREATE TABLE t997(pk BIGINT PRIMARY KEY, v1 BLOB DEFAULT 0x61)",
   576  				Expected: []sql.Row{{types.NewOkResult(0)}},
   577  			},
   578  			{
   579  				Query:    "INSERT INTO t997 VALUES(42, DEFAULT)",
   580  				Expected: []sql.Row{{types.NewOkResult(1)}},
   581  			},
   582  			{
   583  				Query:    "SELECT * from t997",
   584  				Expected: []sql.Row{{42, []uint8{0x61}}},
   585  			},
   586  			{
   587  				Query:    "CREATE TABLE t998(pk BIGINT PRIMARY KEY, v1 TEXT DEFAULT 'hi')",
   588  				Expected: []sql.Row{{types.NewOkResult(0)}},
   589  			},
   590  			{
   591  				Query:    "INSERT INTO t998 VALUES(1, DEFAULT)",
   592  				Expected: []sql.Row{{types.NewOkResult(1)}},
   593  			},
   594  			{
   595  				Query:    "SELECT * from t998",
   596  				Expected: []sql.Row{{1, "hi"}},
   597  			},
   598  			{
   599  				Query:    "CREATE TABLE t999(pk BIGINT PRIMARY KEY, v1 LONGTEXT DEFAULT 'hi')",
   600  				Expected: []sql.Row{{types.NewOkResult(0)}},
   601  			},
   602  			{
   603  				Query:    "INSERT INTO t999 VALUES(10, DEFAULT)",
   604  				Expected: []sql.Row{{types.NewOkResult(1)}},
   605  			},
   606  			{
   607  				Query:    "SELECT * from t999",
   608  				Expected: []sql.Row{{10, "hi"}},
   609  			},
   610  			{
   611  				Query:    "CREATE TABLE t34(pk INT PRIMARY KEY, v1 JSON)",
   612  				Expected: []sql.Row{{types.NewOkResult(0)}},
   613  			},
   614  			{
   615  				Query:    "ALTER TABLE t34 alter column v1 set default '{}'",
   616  				Expected: []sql.Row{{types.NewOkResult(0)}},
   617  			},
   618  			{
   619  				Query:    "INSERT INTO t34 VALUES(100, DEFAULT)",
   620  				Expected: []sql.Row{{types.NewOkResult(1)}},
   621  			},
   622  			{
   623  				Query:    "SELECT * from t34",
   624  				Expected: []sql.Row{{100, "{}"}},
   625  			},
   626  			{
   627  				Query:    "ALTER TABLE t34 alter column v1 set default ('{}')",
   628  				Expected: []sql.Row{{types.NewOkResult(0)}},
   629  			},
   630  			{
   631  				Query:    "CREATE TABLE t35(i int default 100, j JSON)",
   632  				Expected: []sql.Row{{types.NewOkResult(0)}},
   633  			},
   634  			{
   635  				Query:    "ALTER TABLE t35 alter column j set default '[]'",
   636  				Expected: []sql.Row{{types.NewOkResult(0)}},
   637  			},
   638  			{
   639  				Query:    "ALTER TABLE t35 alter column j set default ('[]')",
   640  				Expected: []sql.Row{{types.NewOkResult(0)}},
   641  			},
   642  		},
   643  	},
   644  	{
   645  		Name: "Other types using NOW/CURRENT_TIMESTAMP literal",
   646  		Assertions: []ScriptTestAssertion{
   647  			{
   648  				Query:       "CREATE TABLE t999(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT NOW())",
   649  				ExpectedErr: sql.ErrColumnDefaultDatetimeOnlyFunc,
   650  			},
   651  			{
   652  				Query:       "CREATE TABLE t999(pk BIGINT PRIMARY KEY, v1 VARCHAR(20) DEFAULT CURRENT_TIMESTAMP())",
   653  				ExpectedErr: sql.ErrColumnDefaultDatetimeOnlyFunc,
   654  			},
   655  			{
   656  				Query:       "CREATE TABLE t999(pk BIGINT PRIMARY KEY, v1 BIT(5) DEFAULT NOW())",
   657  				ExpectedErr: sql.ErrColumnDefaultDatetimeOnlyFunc,
   658  			},
   659  			{
   660  				Query:       "CREATE TABLE t999(pk BIGINT PRIMARY KEY, v1 DATE DEFAULT CURRENT_TIMESTAMP())",
   661  				ExpectedErr: sql.ErrColumnDefaultDatetimeOnlyFunc,
   662  			},
   663  		},
   664  	},
   665  	{
   666  		Name: "Unknown functions return an error",
   667  		Assertions: []ScriptTestAssertion{
   668  			{
   669  				Query:       "CREATE TABLE t999(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (CUSTOMFUNC(1)))",
   670  				ExpectedErr: sql.ErrFunctionNotFound,
   671  			},
   672  		},
   673  	},
   674  	{
   675  		Name:        "Stored procedures are not valid in column default value expressions",
   676  		SetUpScript: []string{"CREATE PROCEDURE testProc()\nBEGIN\n\tSELECT 42 FROM dual;\nEND;"},
   677  		Assertions: []ScriptTestAssertion{
   678  			{
   679  				Query:       "CREATE TABLE t999(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (call testProc()))",
   680  				ExpectedErr: sql.ErrSyntaxError,
   681  			},
   682  		},
   683  	},
   684  	{
   685  		Name: "Default expression references own column",
   686  		Assertions: []ScriptTestAssertion{
   687  			{
   688  				Query:       "CREATE TABLE t999(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (v1))",
   689  				ExpectedErr: sql.ErrInvalidDefaultValueOrder,
   690  			},
   691  		},
   692  	},
   693  	{
   694  		Name:        "Expression contains invalid literal, fails on insertion",
   695  		SetUpScript: []string{"CREATE TABLE t1000(pk BIGINT PRIMARY KEY, v1 INT UNSIGNED DEFAULT (-1))"},
   696  		Assertions: []ScriptTestAssertion{
   697  			{
   698  				Query:          "INSERT INTO t1000 (pk) VALUES (1)",
   699  				ExpectedErrStr: "4294967295 out of range for int unsigned",
   700  			},
   701  		},
   702  	},
   703  	{
   704  		Name:        "Expression contains null on NOT NULL, fails on insertion",
   705  		SetUpScript: []string{"CREATE TABLE t1001(pk BIGINT PRIMARY KEY, v1 BIGINT NOT NULL DEFAULT (NULL))"},
   706  		Assertions: []ScriptTestAssertion{
   707  			{
   708  				Query:       "INSERT INTO t1001 (pk) VALUES (1)",
   709  				ExpectedErr: sql.ErrColumnDefaultReturnedNull,
   710  			},
   711  		},
   712  	},
   713  	{
   714  		Name:        "Add column first back reference to expression",
   715  		SetUpScript: []string{"CREATE TABLE t1002(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (pk + 1))"},
   716  		Assertions: []ScriptTestAssertion{
   717  			{
   718  				Query:       "ALTER TABLE t1002 ADD COLUMN v2 BIGINT DEFAULT (v1 + 2) FIRST",
   719  				ExpectedErr: sql.ErrInvalidDefaultValueOrder,
   720  			},
   721  		},
   722  	},
   723  	{
   724  		Name:        "Add column after back reference to expression",
   725  		SetUpScript: []string{"CREATE TABLE t1003(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (pk + 1))"},
   726  		Assertions: []ScriptTestAssertion{
   727  			{
   728  				Query:       "ALTER TABLE t1003 ADD COLUMN v2 BIGINT DEFAULT (v1 + 2) AFTER pk",
   729  				ExpectedErr: sql.ErrInvalidDefaultValueOrder,
   730  			},
   731  		},
   732  	},
   733  	{
   734  		Name:        "Add column self reference",
   735  		SetUpScript: []string{"CREATE TABLE t1004(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (pk + 1))"},
   736  		Assertions: []ScriptTestAssertion{
   737  			{
   738  				Query:       "ALTER TABLE t1004 ADD COLUMN v2 BIGINT DEFAULT (v2)",
   739  				ExpectedErr: sql.ErrInvalidDefaultValueOrder,
   740  			},
   741  		},
   742  	},
   743  	{
   744  		Name:        "Drop column referenced by other column",
   745  		SetUpScript: []string{"CREATE TABLE t1005(pk BIGINT PRIMARY KEY, v1 BIGINT, v2 BIGINT DEFAULT (v1))"},
   746  		Assertions: []ScriptTestAssertion{
   747  			{
   748  				Query:       "ALTER TABLE t1005 DROP COLUMN v1",
   749  				ExpectedErr: sql.ErrDropColumnReferencedInDefault,
   750  			},
   751  		},
   752  	},
   753  	{
   754  		Name:        "Modify column moving back creates back reference to expression",
   755  		SetUpScript: []string{"CREATE TABLE t1006(pk BIGINT PRIMARY KEY, v1 BIGINT DEFAULT (pk), v2 BIGINT DEFAULT (v1))"},
   756  		Assertions: []ScriptTestAssertion{
   757  			{
   758  				Query:       "ALTER TABLE t1006 MODIFY COLUMN v1 BIGINT DEFAULT (pk) AFTER v2",
   759  				ExpectedErr: sql.ErrInvalidDefaultValueOrder,
   760  			},
   761  		},
   762  	},
   763  	{
   764  		Name:        "Modify column moving forward creates back reference to expression",
   765  		SetUpScript: []string{"CREATE TABLE t1007(pk BIGINT DEFAULT (v2) PRIMARY KEY, v1 BIGINT DEFAULT (pk), v2 BIGINT)"},
   766  		Assertions: []ScriptTestAssertion{
   767  			{
   768  				Query:       "ALTER TABLE t1007 MODIFY COLUMN v1 BIGINT DEFAULT (pk) FIRST",
   769  				ExpectedErr: sql.ErrInvalidDefaultValueOrder,
   770  			},
   771  		},
   772  	},
   773  }