Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -7900,6 +7900,31 @@ where
},
},
},
{
Name: "special case for not null default enum",
Dialect: "mysql",
SetUpScript: []string{
"create table t (i int primary key, e enum('abc', 'def', 'ghi') not null);",
},
Assertions: []ScriptTestAssertion{
{
Query: "insert into t(i) values (1)",
Expected: []sql.Row{
{types.NewOkResult(1)},
},
},
{
Query: "insert into t values (2, null)",
ExpectedErr: sql.ErrInsertIntoNonNullableProvidedNull,
},
{
Query: "select * from t;",
Expected: []sql.Row{
{1, "abc"},
},
},
},
},
{
Name: "not expression optimization",
Dialect: "mysql",
Expand Down
12 changes: 12 additions & 0 deletions sql/rowexec/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ func (i *insertIter) Next(ctx *sql.Context) (returnRow sql.Row, returnErr error)
row = row[len(row)-len(i.schema):]
}

// This is a special case in MySQL.
// When there's an enum column with a NOT NULL constraint, the DEFAULT value is the first entry.
for idx, col := range i.schema {
if idx >= len(i.insertExprs) {
break
}
_, isColDefVal := i.insertExprs[idx].(*sql.ColumnDefaultValue)
if row[idx] == nil && types.IsEnum(col.Type) && isColDefVal {
row[idx] = 1
}
}

err = i.validateNullability(ctx, i.schema, row)
if err != nil {
return nil, i.ignoreOrClose(ctx, row, err)
Expand Down