Skip to content

Commit 729f39b

Browse files
authored
Merge pull request #2819 from dolthub/nicktobey/enum
Don't force a table rewrite when appending extra values to the end of an enum.
2 parents ac55f62 + c7e0544 commit 729f39b

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

enginetest/queries/script_queries.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7620,6 +7620,20 @@ where
76207620
{3, "c", float64(4)},
76217621
},
76227622
},
7623+
{
7624+
Query: "alter table t modify column e enum('asdf', 'a', 'b', 'c', 'd');",
7625+
Expected: []sql.Row{
7626+
{types.NewOkResult(0)},
7627+
},
7628+
},
7629+
{
7630+
Query: "select i, e, e + 0 from t;",
7631+
Expected: []sql.Row{
7632+
{1, "a", float64(2)},
7633+
{2, "b", float64(3)},
7634+
{3, "c", float64(4)},
7635+
},
7636+
},
76237637
{
76247638
Query: "alter table t modify column e enum('abc');",
76257639
ExpectedErr: types.ErrConvertingToEnum,

sql/rowexec/ddl_iters.go

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

526526
oldEnum, isOldEnum := oldCol.Type.(sql.EnumType)
527527
newEnum, isNewEnum := newCol.Type.(sql.EnumType)
528-
if isOldEnum && isNewEnum && !oldEnum.Equals(newEnum) {
528+
if isOldEnum && isNewEnum && !oldEnum.IsSubsetOf(newEnum) {
529529
rewriteRequired = true
530530
}
531531

sql/type.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ type EnumType interface {
186186
Collation() CollationID
187187
// IndexOf returns the index of the given string. If the string was not found, then this returns -1.
188188
IndexOf(v string) int
189+
// IsSubsetOf returns whether every element in this is also in |otherType|, with the same indexes.
190+
// |otherType| may contain additional elements not in this.
191+
IsSubsetOf(otherType EnumType) bool
189192
// NumberOfElements returns the number of enumerations.
190193
NumberOfElements() uint16
191194
// Values returns the elements, in order, of every enumeration.

sql/types/enum.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,19 @@ func (t EnumType) IndexOf(v string) int {
335335
return -1
336336
}
337337

338+
// IsSubsetOf implements the sql.EnumType interface.
339+
func (t EnumType) IsSubsetOf(otherType sql.EnumType) bool {
340+
if ot, ok := otherType.(EnumType); ok && t.collation.Equals(ot.collation) && len(t.idxToVal) <= len(ot.idxToVal) {
341+
for i, val := range t.idxToVal {
342+
if ot.idxToVal[i] != val {
343+
return false
344+
}
345+
}
346+
return true
347+
}
348+
return false
349+
}
350+
338351
// NumberOfElements implements EnumType interface.
339352
func (t EnumType) NumberOfElements() uint16 {
340353
return uint16(len(t.idxToVal))

0 commit comments

Comments
 (0)