Skip to content

Commit f3d4da6

Browse files
authored
fix char to bool conversion (#2334)
1 parent 63f9c46 commit f3d4da6

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

enginetest/queries/queries.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9174,6 +9174,70 @@ from typestable`,
91749174
{36},
91759175
},
91769176
},
9177+
{
9178+
Query: "select 1 where if('', 1, char(''));",
9179+
Expected: []sql.Row{},
9180+
},
9181+
{
9182+
Query: "select 1 where if('', char(''), 1);",
9183+
Expected: []sql.Row{
9184+
{1},
9185+
},
9186+
},
9187+
{
9188+
Query: "select 1 where if(char(''), 0, 1);",
9189+
Expected: []sql.Row{
9190+
{1},
9191+
},
9192+
},
9193+
{
9194+
Query: "select 1 where if(char('123'), 0, 1);",
9195+
Expected: []sql.Row{
9196+
{1},
9197+
},
9198+
},
9199+
{
9200+
Query: "select 1 where 0 = if('', 1, char(''));",
9201+
Expected: []sql.Row{
9202+
{1},
9203+
},
9204+
},
9205+
{
9206+
Query: "select if('', 1, char(''));",
9207+
Expected: []sql.Row{
9208+
{[]byte{0}},
9209+
},
9210+
},
9211+
{
9212+
Query: "select if(char(''), 'true', 'false');",
9213+
Expected: []sql.Row{
9214+
{"false"},
9215+
},
9216+
},
9217+
{
9218+
Query: "select if(char('0'), 'true', 'false');",
9219+
Expected: []sql.Row{
9220+
{"false"},
9221+
},
9222+
},
9223+
{
9224+
Query: "select if(char('1'), 'true', 'false');",
9225+
Expected: []sql.Row{
9226+
{"false"},
9227+
},
9228+
},
9229+
{
9230+
Query: "select if(cast(1 as binary), 'true', 'false');",
9231+
Expected: []sql.Row{
9232+
{"true"},
9233+
},
9234+
},
9235+
{
9236+
Query: "select if(cast(0 as binary), 'true', 'false');",
9237+
Expected: []sql.Row{
9238+
{"false"},
9239+
},
9240+
},
91779241
}
91789242

91799243
var KeylessQueries = []QueryTest{

sql/core.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ type Lockable interface {
252252
// ConvertToBool converts a value to a boolean. nil is considered false.
253253
func ConvertToBool(ctx *Context, v interface{}) (bool, error) {
254254
switch b := v.(type) {
255+
case []uint8:
256+
return ConvertToBool(ctx, string(b))
255257
case bool:
256258
return b, nil
257259
case int:

sql/types/typecheck.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import (
2222

2323
// IsBlobType checks if t is BLOB
2424
func IsBlobType(t sql.Type) bool {
25+
if t == nil {
26+
return false
27+
}
2528
switch t.Type() {
2629
case sqltypes.Blob:
2730
return true
@@ -113,6 +116,9 @@ func IsText(t sql.Type) bool {
113116

114117
// IsTextBlob checks if t is one of the TEXTs or BLOBs.
115118
func IsTextBlob(t sql.Type) bool {
119+
if t == nil {
120+
return false
121+
}
116122
switch t.Type() {
117123
case sqltypes.Text, sqltypes.Blob:
118124
return true

0 commit comments

Comments
 (0)