Skip to content

Commit 07e7247

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 de1be10 commit 07e7247

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
@@ -9085,7 +9085,7 @@ where
90859085
},
90869086
},
90879087
{
9088-
Skip: true,
9088+
Skip: false,
90899089
Name: "enum conversion to strings",
90909090
Dialect: "mysql",
90919091
SetUpScript: []string{
@@ -9099,7 +9099,7 @@ where
90999099
Expected: []sql.Row{
91009100
{"abc", 3},
91019101
{"defg", 4},
9102-
{"hijkl", 5},
9102+
{"hjikl", 5},
91039103
},
91049104
},
91059105
{
@@ -9108,7 +9108,7 @@ where
91089108
Expected: []sql.Row{
91099109
{"abc", "abctest"},
91109110
{"defg", "defgtest"},
9111-
{"hijkl", "hijkltest"},
9111+
{"hjikl", "hjikltest"},
91129112
},
91139113
},
91149114
},

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)