Skip to content

Commit f9c02bf

Browse files
committed
Correctly map enum values when altering an enum column.
1 parent 7c05ba9 commit f9c02bf

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

enginetest/queries/script_queries.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ CREATE TABLE teams (
181181
},
182182
{
183183
Query: "alter table xy modify y enum('a')",
184-
ExpectedErr: types.ErrConvertingToEnum,
184+
ExpectedErr: types.ErrDataTruncatedForColumn,
185185
},
186186
},
187187
},
@@ -7625,6 +7625,7 @@ where
76257625
Name: "preserve enums through alter statements",
76267626
SetUpScript: []string{
76277627
"create table t (i int primary key, e enum('a', 'b', 'c'));",
7628+
"insert into t values (0, 0);",
76287629
"insert into t values (1, 'a');",
76297630
"insert into t values (2, 'b');",
76307631
"insert into t values (3, 'c');",
@@ -7633,6 +7634,7 @@ where
76337634
{
76347635
Query: "select i, e, e + 0 from t;",
76357636
Expected: []sql.Row{
7637+
{0, "", float64(0)},
76367638
{1, "a", float64(1)},
76377639
{2, "b", float64(2)},
76387640
{3, "c", float64(3)},
@@ -7647,6 +7649,7 @@ where
76477649
{
76487650
Query: "select i, e, e + 0 from t;",
76497651
Expected: []sql.Row{
7652+
{0, "", float64(0)},
76507653
{1, "a", float64(2)},
76517654
{2, "b", float64(3)},
76527655
{3, "c", float64(1)},
@@ -7661,6 +7664,7 @@ where
76617664
{
76627665
Query: "select i, e, e + 0 from t;",
76637666
Expected: []sql.Row{
7667+
{0, "", float64(0)},
76647668
{1, "a", float64(2)},
76657669
{2, "b", float64(3)},
76667670
{3, "c", float64(4)},
@@ -7675,14 +7679,30 @@ where
76757679
{
76767680
Query: "select i, e, e + 0 from t;",
76777681
Expected: []sql.Row{
7682+
{0, "", float64(0)},
76787683
{1, "a", float64(2)},
76797684
{2, "b", float64(3)},
76807685
{3, "c", float64(4)},
76817686
},
76827687
},
7688+
{
7689+
Query: "alter table t modify column e enum('a', 'b', 'c');",
7690+
Expected: []sql.Row{
7691+
{types.NewOkResult(0)},
7692+
},
7693+
},
7694+
{
7695+
Query: "select i, e, e + 0 from t;",
7696+
Expected: []sql.Row{
7697+
{0, "", float64(0)},
7698+
{1, "a", float64(1)},
7699+
{2, "b", float64(2)},
7700+
{3, "c", float64(3)},
7701+
},
7702+
},
76837703
{
76847704
Query: "alter table t modify column e enum('abc');",
7685-
ExpectedErr: types.ErrConvertingToEnum,
7705+
ExpectedErr: types.ErrDataTruncatedForColumn,
76867706
},
76877707
},
76887708
},

sql/rowexec/ddl_iters.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,6 @@ func (i *modifyColumnIter) rewriteTable(ctx *sql.Context, rwt sql.RewritableTabl
545545
return false, err
546546
}
547547

548-
newColIdx := newSch.IndexOf(newCol.Name, newCol.Source)
549548
rowIter := sql.NewTableRowIter(ctx, rwt, partitions)
550549
for {
551550
r, err := rowIter.Next(ctx)
@@ -557,24 +556,27 @@ func (i *modifyColumnIter) rewriteTable(ctx *sql.Context, rwt sql.RewritableTabl
557556
return false, err
558557
}
559558

559+
// remap old enum values to new enum values
560+
if isOldEnum && isNewEnum && r[oldColIdx] != nil {
561+
oldIdx := int(r[oldColIdx].(uint16))
562+
// 0 values in enums are error values. They are preserved during remapping.
563+
if oldIdx != 0 {
564+
oldStr, _ := oldEnum.At(oldIdx)
565+
newIdx := newEnum.IndexOf(oldStr)
566+
if newIdx == -1 {
567+
return false, types.ErrDataTruncatedForColumn.New(newCol.Name)
568+
}
569+
r[oldColIdx] = uint16(newIdx)
570+
}
571+
}
572+
560573
newRow, err := projectRowWithTypes(ctx, newSch, projections, r)
561574
if err != nil {
562575
_ = inserter.DiscardChanges(ctx, err)
563576
_ = inserter.Close(ctx)
564577
return false, err
565578
}
566579

567-
// remap old enum values to new enum values
568-
if isOldEnum && isNewEnum && newRow[newColIdx] != nil {
569-
oldIdx := int(newRow[newColIdx].(uint16))
570-
oldStr, _ := oldEnum.At(oldIdx)
571-
newIdx := newEnum.IndexOf(oldStr)
572-
if newIdx == -1 {
573-
return false, types.ErrDataTruncatedForColumn.New(newCol.Name)
574-
}
575-
newRow[newColIdx] = uint16(newIdx)
576-
}
577-
578580
err = i.validateNullability(ctx, newSch, newRow)
579581
if err != nil {
580582
_ = inserter.DiscardChanges(ctx, err)

0 commit comments

Comments
 (0)