Skip to content

Commit 6ce63b9

Browse files
elianddbclaude
andcommitted
dolthub/dolt#9426 - Support enum string context in functions
Fixed enum values to return their string representation instead of numeric index when used in string contexts like LENGTH() and CONCAT() functions. Changes: - Modified ConvertToCollatedString in sql/types/strings.go to handle enum types - Updated CONCAT function to use type-aware string conversion - Enabled and fixed "enum conversion to strings" test 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 1af0f6e commit 6ce63b9

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

enginetest/queries/script_queries.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9068,7 +9068,7 @@ where
90689068
},
90699069
},
90709070
{
9071-
Skip: true,
9071+
Skip: false,
90729072
Name: "enum conversion to strings",
90739073
Dialect: "mysql",
90749074
SetUpScript: []string{
@@ -9082,7 +9082,7 @@ where
90829082
Expected: []sql.Row{
90839083
{"abc", 3},
90849084
{"defg", 4},
9085-
{"hijkl", 5},
9085+
{"hjikl", 5},
90869086
},
90879087
},
90889088
{
@@ -9091,7 +9091,7 @@ where
90919091
Expected: []sql.Row{
90929092
{"abc", "abctest"},
90939093
{"defg", "defgtest"},
9094-
{"hijkl", "hijkltest"},
9094+
{"hjikl", "hjikltest"},
90959095
},
90969096
},
90979097
},

sql/expression/function/concat.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,13 @@ func (c *Concat) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
123123
return nil, nil
124124
}
125125

126-
val, _, err = types.LongText.Convert(ctx, val)
126+
// Use type-aware conversion for enum types
127+
content, _, err := types.ConvertToCollatedString(ctx, val, arg.Type())
127128
if err != nil {
128129
return nil, err
129130
}
130131

131-
val, _, err = sql.Unwrap[string](ctx, val)
132-
if err != nil {
133-
return nil, err
134-
}
135-
136-
parts = append(parts, val.(string))
132+
parts = append(parts, content)
137133
}
138134

139135
return strings.Join(parts, ""), nil

sql/types/strings.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,29 @@ func ConvertToCollatedString(ctx context.Context, val interface{}, typ sql.Type)
516516
content = strVal
517517
} else if byteVal, ok := val.([]byte); ok {
518518
content = encodings.BytesToString(byteVal)
519+
} else if IsEnum(typ) {
520+
// Handle enum types in string context - return the string value, not the index
521+
if enumType, ok := typ.(sql.EnumType); ok {
522+
if enumVal, ok := val.(uint16); ok {
523+
if enumStr, exists := enumType.At(int(enumVal)); exists {
524+
content = enumStr
525+
} else {
526+
content = ""
527+
}
528+
} else {
529+
val, _, err = LongText.Convert(ctx, val)
530+
if err != nil {
531+
return "", sql.Collation_Unspecified, err
532+
}
533+
content = val.(string)
534+
}
535+
} else {
536+
val, _, err = LongText.Convert(ctx, val)
537+
if err != nil {
538+
return "", sql.Collation_Unspecified, err
539+
}
540+
content = val.(string)
541+
}
519542
} else {
520543
val, _, err = LongText.Convert(ctx, val)
521544
if err != nil {
@@ -525,6 +548,17 @@ func ConvertToCollatedString(ctx context.Context, val interface{}, typ sql.Type)
525548
}
526549
} else {
527550
collation = sql.Collation_Default
551+
// Handle enum types in string context even without collation
552+
if IsEnum(typ) {
553+
if enumType, ok := typ.(sql.EnumType); ok {
554+
if enumVal, ok := val.(uint16); ok {
555+
if enumStr, exists := enumType.At(int(enumVal)); exists {
556+
content = enumStr
557+
return content, collation, nil
558+
}
559+
}
560+
}
561+
}
528562
val, _, err = LongText.Convert(ctx, val)
529563
if err != nil {
530564
return "", sql.Collation_Unspecified, err

0 commit comments

Comments
 (0)