Skip to content

Commit 31e5182

Browse files
Mandukhai-Alimaanineinchnick
authored andcommitted
Handle NULL VARBINARY values correctly in ConvertValue
1 parent d8a3792 commit 31e5182

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

trino/trino.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,7 +2533,11 @@ func (c *typeConverter) ConvertValue(v interface{}) (driver.Value, error) {
25332533
}
25342534
return vv.String, err
25352535
case "varbinary":
2536-
return scanNullBytes(v)
2536+
vv, err := scanNullBytes(v)
2537+
if !vv.Valid {
2538+
return nil, err
2539+
}
2540+
return vv.Bytes, err
25372541
case "tinyint", "smallint", "integer", "bigint":
25382542
vv, err := scanNullInt64(v)
25392543
if !vv.Valid {
@@ -2703,24 +2707,31 @@ func scanNullString(v interface{}) (sql.NullString, error) {
27032707
return sql.NullString{Valid: true, String: vv}, nil
27042708
}
27052709

2706-
func scanNullBytes(v interface{}) ([]byte, error) {
2710+
// NullBinary represents a []byte that may be null.
2711+
// This follows the same pattern as sql.NullString, sql.NullInt64, etc.
2712+
type NullBinary struct {
2713+
Bytes []byte
2714+
Valid bool // Valid is true if Bytes is not NULL
2715+
}
2716+
2717+
func scanNullBytes(v interface{}) (NullBinary, error) {
27072718
if v == nil {
2708-
return nil, nil
2719+
return NullBinary{}, nil // Valid: false, Bytes: nil
27092720
}
27102721

27112722
// VARBINARY values come back as a base64 encoded string.
27122723
vv, ok := v.(string)
27132724
if !ok {
2714-
return nil, fmt.Errorf("cannot convert %v (%T) to []byte", v, v)
2725+
return NullBinary{}, fmt.Errorf("cannot convert %v (%T) to []byte", v, v)
27152726
}
27162727

27172728
// Decode the base64 encoded string into a []byte.
27182729
decoded, err := base64.StdEncoding.DecodeString(vv)
27192730
if err != nil {
2720-
return nil, fmt.Errorf("cannot decode base64 string into []byte: %w", err)
2731+
return NullBinary{}, fmt.Errorf("cannot decode base64 string into []byte: %w", err)
27212732
}
27222733

2723-
return decoded, nil
2734+
return NullBinary{Bytes: decoded, Valid: true}, nil
27242735
}
27252736

27262737
// NullSliceString represents a slice of string that may be null.

0 commit comments

Comments
 (0)