Skip to content

Commit 803b097

Browse files
elianddbclaude
andcommitted
Enhance TypeAwareConversion to handle blob types for consistent SET/ENUM casting
- Extend TypeAwareConversion to handle IsBlobType in addition to IsTextOnly - Use TypeAwareConversion consistently for both CHAR and BINARY conversions - Remove direct ConvertToCollatedString call in binary conversion - Update function documentation to reflect blob type support - Simplify convertValue logic by using unified conversion approach This ensures SET/ENUM types are converted properly for all string and binary target types through a single, consistent code path. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 752fbd3 commit 803b097

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

sql/expression/convert.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,7 @@ func (c *Convert) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
280280

281281
// convertValue converts a value from its current type to the specified target type for CAST/CONVERT operations.
282282
// It handles type-specific conversion logic and applies length/scale constraints where applicable.
283-
// For SET/ENUM types converting to text types, uses TypeAwareConversion for proper string representation.
284-
// For SET/ENUM types converting to binary types, converts to string first before applying binary conversion.
283+
// Uses TypeAwareConversion for SET/ENUM types to ensure proper string representation when converting to text or binary types.
285284
// If |typeLength| and |typeScale| are 0, they are ignored, otherwise they are used as constraints on the
286285
// converted type where applicable (e.g. Char conversion supports only |typeLength|, Decimal conversion supports
287286
// |typeLength| and |typeScale|).
@@ -293,10 +292,7 @@ func convertValue(ctx *sql.Context, val interface{}, castTo string, originType s
293292
}
294293
switch strings.ToLower(castTo) {
295294
case ConvertToBinary:
296-
// For SET/ENUM types, convert to string first since TypeAwareConversion only handles text types
297-
if types.IsSet(originType) || types.IsEnum(originType) {
298-
val, _, _ = types.ConvertToCollatedString(ctx, val, originType)
299-
}
295+
val, _ = types.TypeAwareConversion(ctx, val, originType, types.LongBlob)
300296
b, _, err := types.LongBlob.Convert(ctx, val)
301297
if err != nil {
302298
return nil, nil

sql/types/conversion.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,14 +737,14 @@ func GeneralizeTypes(a, b sql.Type) sql.Type {
737737

738738
// TypeAwareConversion converts a value to a specified type, with awareness of the value's original type. This is
739739
// necessary because some types, such as EnumType and SetType, are stored as ints and require information from the
740-
// original type to properly convert to strings.
740+
// original type to properly convert to strings and binary types.
741741
func TypeAwareConversion(ctx *sql.Context, val interface{}, originalType sql.Type, convertedType sql.Type) (interface{}, error) {
742742
if val == nil {
743743
return nil, nil
744744
}
745745
var converted interface{}
746746
var err error
747-
if IsTextOnly(convertedType) {
747+
if IsTextOnly(convertedType) || IsBlobType(convertedType) {
748748
converted, _, err = ConvertToCollatedString(ctx, val, originalType)
749749
} else {
750750
converted, _, err = convertedType.Convert(ctx, val)

0 commit comments

Comments
 (0)