Skip to content

Commit 121b5f3

Browse files
elianddbclaude
andcommitted
Fix enum literal default validation to match MySQL behavior
- Add validateEnumLiteralDefault method for stricter enum default validation - Reject numeric index references ('1', 1) for literal enum defaults - Allow only exact enum value matches for literal defaults - Preserve original ErrInvalidType format in enum expression - Fixes enums_with_default_values test failures 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent a1cecc8 commit 121b5f3

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

sql/columndefault.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ func (e *ColumnDefaultValue) CheckType(ctx *Context) error {
227227
if val == nil && !e.ReturnNil {
228228
return ErrIncompatibleDefaultType.New()
229229
}
230+
231+
// For enum literal defaults, use stricter validation than runtime conversion
232+
if enumType, isEnum := e.OutType.(EnumType); isEnum {
233+
return e.validateEnumLiteralDefault(enumType, val)
234+
}
235+
230236
_, inRange, err := e.OutType.Convert(ctx, val)
231237
if err != nil {
232238
return ErrIncompatibleDefaultType.Wrap(err)
@@ -238,6 +244,32 @@ func (e *ColumnDefaultValue) CheckType(ctx *Context) error {
238244
return nil
239245
}
240246

247+
// validateEnumLiteralDefault validates enum literal defaults more strictly than runtime conversions
248+
// MySQL doesn't allow numeric index references for literal enum defaults
249+
func (e *ColumnDefaultValue) validateEnumLiteralDefault(enumType EnumType, val interface{}) error {
250+
switch v := val.(type) {
251+
case string:
252+
// For string values, check if it's a direct enum value match
253+
enumValues := enumType.Values()
254+
for _, enumVal := range enumValues {
255+
if enumVal == v {
256+
return nil // Valid enum value
257+
}
258+
}
259+
// String doesn't match any enum value, return appropriate error
260+
if v == "" {
261+
return ErrIncompatibleDefaultType.New()
262+
}
263+
return ErrInvalidColumnDefaultValue.New("(unknown)")
264+
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
265+
// MySQL doesn't allow numeric enum indices as literal defaults
266+
return ErrInvalidColumnDefaultValue.New("(unknown)")
267+
default:
268+
// Other types not supported for enum defaults
269+
return ErrIncompatibleDefaultType.New()
270+
}
271+
}
272+
241273
type UnresolvedColumnDefault struct {
242274
ExprString string
243275
}

sql/expression/enum.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (e *EnumToString) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)
8080
case string:
8181
str = v
8282
default:
83-
return nil, sql.ErrInvalidType.New(e.Enum.Type().String())
83+
return nil, sql.ErrInvalidType.New(val, types.Text)
8484
}
8585
return str, nil
8686
}

0 commit comments

Comments
 (0)