Skip to content

Commit 0aac419

Browse files
authored
Fix alter NULL enum value panic (#2750)
* Test for alter NULL enum value * delete old bench * error for enum truncation * fix test
1 parent 2d21230 commit 0aac419

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

enginetest/queries/script_queries.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ var ScriptTests = []ScriptTest{
120120
},
121121
},
122122
},
123+
{
124+
Name: "alter nil enum",
125+
SetUpScript: []string{
126+
"create table xy (x int primary key, y enum ('a', 'b'));",
127+
"insert into xy values (0, NULL),(1, 'b')",
128+
},
129+
Assertions: []ScriptTestAssertion{
130+
{
131+
Query: "alter table xy modify y enum('a','b','c')",
132+
},
133+
{
134+
Query: "alter table xy modify y enum('a')",
135+
ExpectedErr: sql.ErrEnumTypeTruncated,
136+
},
137+
},
138+
},
123139
{
124140
Name: "issue 7958, update join uppercase table name validation",
125141
SetUpScript: []string{
@@ -7494,8 +7510,8 @@ where
74947510
},
74957511
},
74967512
{
7497-
Query: "alter table t modify column e enum('abc');",
7498-
ExpectedErrStr: "value 2 is not valid for this Enum",
7513+
Query: "alter table t modify column e enum('abc');",
7514+
ExpectedErr: sql.ErrEnumTypeTruncated,
74997515
},
75007516
},
75017517
},

sql/analyzer/validate_create_table.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,15 @@ func validateModifyColumn(ctx *sql.Context, initialSch sql.Schema, schema sql.Sc
470470
return nil, err
471471
}
472472

473+
if e1, ok := newCol.Type.(sql.EnumType); ok {
474+
oldCol := initialSch[initialSch.IndexOfColName(oldColName)]
475+
if e2, ok := oldCol.Type.(sql.EnumType); ok {
476+
if len(e1.Values()) < len(e2.Values()) {
477+
return nil, sql.ErrEnumTypeTruncated.New()
478+
}
479+
}
480+
}
481+
473482
// TODO: When a column is being modified, we should ideally check that any existing table check constraints
474483
// are still valid (e.g. if the column type changed) and throw an error if they are invalidated.
475484
// That would be consistent with MySQL behavior.

sql/errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,8 @@ var (
924924
ErrInvalidTypeForLimit = errors.NewKind("invalid limit. expected %T, found %T")
925925

926926
ErrColumnSpecifiedTwice = errors.NewKind("column '%v' specified twice")
927+
928+
ErrEnumTypeTruncated = errors.NewKind("new enum type change truncates value")
927929
)
928930

929931
// CastSQLError returns a *mysql.SQLError with the error code and in some cases, also a SQL state, populated for the

sql/rowexec/ddl_iters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ func (i *modifyColumnIter) rewriteTable(ctx *sql.Context, rwt sql.RewritableTabl
565565
}
566566

567567
// remap old enum values to new enum values
568-
if isOldEnum && isNewEnum {
568+
if isOldEnum && isNewEnum && newRow[newColIdx] != nil {
569569
oldIdx := int(newRow[newColIdx].(uint16))
570570
oldStr, _ := oldEnum.At(oldIdx)
571571
newIdx := newEnum.IndexOf(oldStr)

sql/rowexec/merge_join.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (i *mergeJoinIter) Next(ctx *sql.Context) (sql.Row, error) {
180180
// We use two variables to manage the lookahead state management.
181181
// |matchedleft| is a forward-looking indicator of whether the current left
182182
// row has satisfied a join condition. It is reset to false when we
183-
// increment left. |matchincleft| is true when the most recent call to
183+
// increment left. |matchingleft| is true when the most recent call to
184184
// |incmatch| incremented the left row. The two vars combined let us
185185
// lookahead during msSelect to 1) identify proper nullified row matches,
186186
// and 2) maintain forward-looking state for the next |i.fullrow|.

0 commit comments

Comments
 (0)