Skip to content

Commit e865d1c

Browse files
authored
fix default value for non null enum columns
1 parent 4e9919a commit e865d1c

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

enginetest/queries/script_queries.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7900,6 +7900,31 @@ where
79007900
},
79017901
},
79027902
},
7903+
{
7904+
Name: "special case for not null default enum",
7905+
Dialect: "mysql",
7906+
SetUpScript: []string{
7907+
"create table t (i int primary key, e enum('abc', 'def', 'ghi') not null);",
7908+
},
7909+
Assertions: []ScriptTestAssertion{
7910+
{
7911+
Query: "insert into t(i) values (1)",
7912+
Expected: []sql.Row{
7913+
{types.NewOkResult(1)},
7914+
},
7915+
},
7916+
{
7917+
Query: "insert into t values (2, null)",
7918+
ExpectedErr: sql.ErrInsertIntoNonNullableProvidedNull,
7919+
},
7920+
{
7921+
Query: "select * from t;",
7922+
Expected: []sql.Row{
7923+
{1, "abc"},
7924+
},
7925+
},
7926+
},
7927+
},
79037928
{
79047929
Name: "not expression optimization",
79057930
Dialect: "mysql",

sql/rowexec/insert.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ func (i *insertIter) Next(ctx *sql.Context) (returnRow sql.Row, returnErr error)
8080
row = row[len(row)-len(i.schema):]
8181
}
8282

83+
// This is a special case in MySQL.
84+
// When there's an enum column with a NOT NULL constraint, the DEFAULT value is the first entry.
85+
for idx, col := range i.schema {
86+
if idx >= len(i.insertExprs) {
87+
break
88+
}
89+
_, isColDefVal := i.insertExprs[idx].(*sql.ColumnDefaultValue)
90+
if row[idx] == nil && types.IsEnum(col.Type) && isColDefVal {
91+
row[idx] = 1
92+
}
93+
}
94+
8395
err = i.validateNullability(ctx, i.schema, row)
8496
if err != nil {
8597
return nil, i.ignoreOrClose(ctx, row, err)

0 commit comments

Comments
 (0)