Skip to content

Commit a69741d

Browse files
authored
Merge pull request #2927 from dolthub/nicktobey/binary
Converting a nil value should return nil, not panic.
2 parents e7b7ae1 + 84bbb7b commit a69741d

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

enginetest/queries/queries.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5206,6 +5206,17 @@ SELECT * FROM cte WHERE d = 2;`,
52065206
Query: "SELECT CAST(-3 AS DOUBLE) FROM dual",
52075207
Expected: []sql.Row{{-3.0}},
52085208
},
5209+
{
5210+
Query: "SELECT BINARY c, BINARY vc, BINARY t, BINARY b, BINARY vb, BINARY bl FROM niltexttable",
5211+
Expected: []sql.Row{
5212+
{nil, nil, nil, nil, nil, nil},
5213+
{[]byte("2"), nil, []byte("2"), nil, []byte("2"), nil},
5214+
{nil, []byte("3"), []byte("3"), nil, nil, []byte("3")},
5215+
{[]byte("4"), []byte("4"), nil, []byte("4\x00"), nil, nil},
5216+
{nil, nil, nil, []byte("5\x00"), []byte("5"), []byte("5")},
5217+
{[]byte("6"), []byte("6"), []byte("6"), []byte("6\x00"), []byte("6"), []byte("6")},
5218+
},
5219+
},
52095220
{
52105221
Query: `SELECT CONVERT("-3.9876", FLOAT) FROM dual`,
52115222
Expected: []sql.Row{{float32(-3.9876)}},

enginetest/scriptgen/setup/scripts/niltable

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,26 @@ insert into niltable values
2121
exec
2222
create index niltable_i2 on niltable (i2)
2323
----
24+
25+
exec
26+
CREATE TABLE `niltexttable` (
27+
`i` bigint NOT NULL,
28+
`c` char(2),
29+
`vc` varchar(2),
30+
`t` text,
31+
`b` binary(2),
32+
`vb` varbinary(2),
33+
`bl` blob,
34+
PRIMARY KEY (`i`)
35+
)
36+
----
37+
38+
exec
39+
insert into niltexttable values
40+
(1, null, null, null, null, null, null),
41+
(2, '2', null, '2', null, '2', null),
42+
(3, null, '3', '3', null, null, '3'),
43+
(4, '4', '4', null, '4', null, null),
44+
(5, null, null, null, '5', '5', '5'),
45+
(6, '6', '6', '6', '6', '6', '6')
46+
----

enginetest/scriptgen/setup/setup_data.sg.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sql/expression/convert.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ func (c *Convert) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
284284
// converted type where applicable (e.g. Char conversion supports only |typeLength|, Decimal conversion supports
285285
// |typeLength| and |typeScale|).
286286
func convertValue(ctx *sql.Context, val interface{}, castTo string, originType sql.Type, typeLength, typeScale int) (interface{}, error) {
287+
if val == nil {
288+
return nil, nil
289+
}
287290
switch strings.ToLower(castTo) {
288291
case ConvertToBinary:
289292
b, _, err := types.LongBlob.Convert(ctx, val)

0 commit comments

Comments
 (0)