Skip to content

Commit 2032865

Browse files
elianddbclaude
andcommitted
Fix enum error message to include row number to match MySQL exactly
- Added ErrDataTruncatedForColumnAtRow error type in types/enum.go - Added rowNumber field to insertIter struct to track current row - Modified enum error conversion in insert.go to use row-specific error - Updated test expectations in script_queries.go to match new format - MySQL returns: 'Data truncated for column 'e' at row 1' - Now matches MySQL behavior exactly 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 17e8eb1 commit 2032865

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

enginetest/queries/script_queries.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ CREATE TABLE tab3 (
12021202
{
12031203
// enum values must match EXACTLY for case-sensitive collations
12041204
Query: "INSERT INTO enumtest1 VALUES (10, 'ABC'), (11, 'aBc'), (12, 'xyz');",
1205-
ExpectedErrStr: "Data truncated for column 'e'",
1205+
ExpectedErrStr: "Data truncated for column 'e' at row 1",
12061206
},
12071207
{
12081208
Query: "SHOW CREATE TABLE enumtest1;",
@@ -8053,11 +8053,11 @@ where
80538053
Assertions: []ScriptTestAssertion{
80548054
{
80558055
Query: "insert into t values (1, 500)",
8056-
ExpectedErrStr: "Data truncated for column 'e'",
8056+
ExpectedErrStr: "Data truncated for column 'e' at row 1",
80578057
},
80588058
{
80598059
Query: "insert into t values (1, -1)",
8060-
ExpectedErrStr: "Data truncated for column 'e'",
8060+
ExpectedErrStr: "Data truncated for column 'e' at row 1",
80618061
},
80628062
},
80638063
},
@@ -9221,7 +9221,7 @@ where
92219221
},
92229222
{
92239223
Query: "insert into child1 values ('a');",
9224-
ExpectedErrStr: "Data truncated for column 'e'",
9224+
ExpectedErrStr: "Data truncated for column 'e' at row 1",
92259225
},
92269226
{
92279227
Query: "select * from child1 order by e;",
@@ -9322,7 +9322,7 @@ where
93229322
},
93239323
{
93249324
Query: "insert into child4 values (3);",
9325-
ExpectedErrStr: "Data truncated for column 'e'",
9325+
ExpectedErrStr: "Data truncated for column 'e' at row 1",
93269326
},
93279327
{
93289328
Query: "insert into child4 values ('q');",
@@ -9332,7 +9332,7 @@ where
93329332
},
93339333
{
93349334
Query: "insert into child4 values ('a');",
9335-
ExpectedErrStr: "Data truncated for column 'e'",
9335+
ExpectedErrStr: "Data truncated for column 'e' at row 1",
93369336
},
93379337
{
93389338
Query: "select * from child4 order by e;",

sql/rowexec/insert.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type insertIter struct {
4949
firstGeneratedAutoIncRowIdx int
5050

5151
deferredDefaults sql.FastIntSet
52+
rowNumber int64
5253
}
5354

5455
func getInsertExpressions(values sql.Node) []sql.Expression {
@@ -74,6 +75,9 @@ func (i *insertIter) Next(ctx *sql.Context) (returnRow sql.Row, returnErr error)
7475
return nil, i.ignoreOrClose(ctx, row, err)
7576
}
7677

78+
// Increment row number for error reporting (MySQL starts at 1)
79+
i.rowNumber++
80+
7781
// Prune the row down to the size of the schema. It can be larger in the case of running with an outer scope, in which
7882
// case the additional scope variables are prepended to the row.
7983
if len(row) > len(i.schema) {
@@ -141,7 +145,7 @@ func (i *insertIter) Next(ctx *sql.Context) (returnRow sql.Row, returnErr error)
141145
} else if sql.ErrNotMatchingSRID.Is(cErr) {
142146
cErr = sql.ErrNotMatchingSRIDWithColName.New(col.Name, cErr)
143147
} else if types.ErrConvertingToEnum.Is(cErr) {
144-
cErr = types.ErrDataTruncatedForColumn.New(col.Name)
148+
cErr = types.ErrDataTruncatedForColumnAtRow.New(col.Name, i.rowNumber)
145149
}
146150
return nil, sql.NewWrappedInsertError(origRow, cErr)
147151
}

sql/types/enum.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var (
4343
ErrConvertingToEnum = errors.NewKind("value %v is not valid for this Enum")
4444

4545
ErrDataTruncatedForColumn = errors.NewKind("Data truncated for column '%s'")
46+
ErrDataTruncatedForColumnAtRow = errors.NewKind("Data truncated for column '%s' at row %d")
4647

4748
enumValueType = reflect.TypeOf(uint16(0))
4849
)

0 commit comments

Comments
 (0)