Skip to content

Commit f569818

Browse files
committed
insert ignore to enum column truncates data
1 parent 6efed41 commit f569818

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

enginetest/queries/insert_queries.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2702,6 +2702,28 @@ var InsertIgnoreScripts = []ScriptTest{
27022702
},
27032703
},
27042704
},
2705+
{
2706+
// https://github.com/dolthub/dolt/issues/8611
2707+
Name: "issue 8611: insert ignore on enum type column",
2708+
SetUpScript: []string{
2709+
"create table test_table (x int auto_increment primary key, y enum('hello','bye'))",
2710+
},
2711+
Assertions: []ScriptTestAssertion{
2712+
{
2713+
Query: "insert into test_table values (1, 'invalid'), (2, 'comparative politics'), (3, null)",
2714+
ExpectedErr: types.ErrConvertingToEnum, // TODO: should be ErrDataTruncatedForColumn
2715+
},
2716+
{
2717+
Query: "insert ignore into test_table values (1, 'invalid'), (2, 'bye'), (3, null)",
2718+
Expected: []sql.Row{{types.OkResult{RowsAffected: 3}}},
2719+
//ExpectedWarning: mysql.ERWarnDataTruncated, // TODO: incorrect code
2720+
},
2721+
{
2722+
Query: "select * from test_table",
2723+
Expected: []sql.Row{{1, ""}, {2, "bye"}, {3, nil}},
2724+
},
2725+
},
2726+
},
27052727
}
27062728

27072729
var IgnoreWithDuplicateUniqueKeyKeylessScripts = []ScriptTest{
@@ -2958,4 +2980,21 @@ var InsertBrokenScripts = []ScriptTest{
29582980
},
29592981
},
29602982
},
2983+
{
2984+
// https://github.com/dolthub/dolt/issues/8617
2985+
Name: "INSERT INTO with ENUM NOT NULL",
2986+
SetUpScript: []string{
2987+
"CREATE TABLE test (pk BIGINT PRIMARY KEY NOT NULL, v1 ENUM('a','b','c') NOT NULL);",
2988+
},
2989+
Assertions: []ScriptTestAssertion{
2990+
{
2991+
Query: "INSERT INTO test (pk) VALUES (1);",
2992+
Expected: []sql.Row{{types.NewOkResult(1)}},
2993+
},
2994+
{
2995+
Query: "select * from t2;",
2996+
Expected: []sql.Row{{1, "a"}},
2997+
},
2998+
},
2999+
},
29613000
}

enginetest/queries/script_queries.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4368,7 +4368,7 @@ CREATE TABLE tab3 (
43684368
time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC),
43694369
time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC),
43704370
0,
4371-
"first",
4371+
"",
43724372
"",
43734373
},
43744374
},

sql/rowexec/ddl_iters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ func (i *modifyColumnIter) rewriteTable(ctx *sql.Context, rwt sql.RewritableTabl
570570
oldStr, _ := oldEnum.At(oldIdx)
571571
newIdx := newEnum.IndexOf(oldStr)
572572
if newIdx == -1 {
573-
return false, fmt.Errorf("data truncated for column %s", newCol.Name)
573+
return false, types.ErrDataTruncatedForColumn.New(newCol.Name)
574574
}
575575
newRow[newColIdx] = uint16(newIdx)
576576
}

sql/types/enum.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const (
4141
var (
4242
ErrConvertingToEnum = errors.NewKind("value %v is not valid for this Enum")
4343

44+
ErrDataTruncatedForColumn = errors.NewKind("Data truncated for column '%s'")
45+
4446
enumValueType = reflect.TypeOf(uint16(0))
4547
)
4648

@@ -278,15 +280,17 @@ func (t EnumType) CollationCoercibility(ctx *sql.Context) (collation sql.Collati
278280

279281
// Zero implements Type interface.
280282
func (t EnumType) Zero() interface{} {
281-
// / If an ENUM column is declared NOT NULL, its default value is the first element of the list of permitted values.
282-
return uint16(1)
283+
// If an ENUM column is declared NOT NULL, its default value is the first element of the list of permitted values.
284+
return uint16(0)
283285
}
284286

285287
// At implements EnumType interface.
286288
func (t EnumType) At(index int) (string, bool) {
287-
// / The elements listed in the column specification are assigned index numbers, beginning with 1.
289+
// The elements listed in the column specification are assigned index numbers, beginning with 1.
288290
index -= 1
289-
if index < 0 || index >= len(t.indexToVal) {
291+
if index == -1 {
292+
return "", true
293+
} else if index >= len(t.indexToVal) {
290294
return "", false
291295
}
292296
return t.indexToVal[index], true

sql/types/enum_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func TestEnumZero(t *testing.T) {
199199
typ := MustCreateEnumType(test.vals, sql.Collation_Default)
200200
v, ok := typ.Zero().(uint16)
201201
assert.True(t, ok)
202-
assert.Equal(t, uint16(1), v)
202+
assert.Equal(t, uint16(0), v)
203203
})
204204
}
205205
}

0 commit comments

Comments
 (0)