Skip to content

Commit

Permalink
Fix logic with OR operation and add missing nil check to Like (#878)
Browse files Browse the repository at this point in the history
  • Loading branch information
VinaiRachakonda authored Mar 16, 2022
1 parent c50c471 commit 7804b74
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
12 changes: 12 additions & 0 deletions enginetest/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -6738,6 +6738,18 @@ var QueryTests = []QueryTest{
{1, 1, 0},
},
},
{
Query: `select c1 from jsontable where c1 LIKE (('%' OR 'dsads') OR '%')`,
Expected: []sql.Row{},
},
{
Query: `select c1 from jsontable where c1 LIKE ('%' OR NULL)`,
Expected: []sql.Row{},
},
{
Query: `select (('%' OR 'dsads') OR '%')`,
Expected: []sql.Row{{false}},
},
}

var KeylessQueries = []QueryTest{
Expand Down
4 changes: 4 additions & 0 deletions sql/expression/like.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (l *Like) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return nil, err
}

if likeMatcher == nil {
return false, nil
}

ok := likeMatcher.Match(left.(string))
if !l.cached {
likeMatcher.Dispose()
Expand Down
17 changes: 5 additions & 12 deletions sql/expression/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,31 +128,24 @@ func (o *Or) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return nil, err
}
if lval != nil {
lvalBool, err := sql.ConvertToBool(lval)
if err == nil && lvalBool {
lval, err = sql.ConvertToBool(lval)
if err == nil && lval.(bool) {
return true, nil
}
}

if lval == true {
return true, nil
}

rval, err := o.Right.Eval(ctx, row)
if err != nil {
return nil, err
}
if rval != nil {
rvalBool, err := sql.ConvertToBool(rval)
if err == nil && rvalBool {
rval, err = sql.ConvertToBool(rval)
if err == nil && rval.(bool) {
return true, nil
}
}

if rval == true {
return true, nil
}

// Can also be triggered by lval and rval not being bool types.
if lval == false && rval == false {
return false, nil
}
Expand Down
7 changes: 7 additions & 0 deletions sql/expression/logic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func TestAnd(t *testing.T) {
{"both true", true, true, true},
{"both false", false, false, false},
{"both nil", nil, nil, nil},
{"left is string, right is string", "dsdad", "dasa", false},
{"left is string, right is nil", "ads", nil, false},
{"left is nil, right is string", nil, "dada", false},
}

for _, tt := range testCases {
Expand Down Expand Up @@ -67,6 +70,10 @@ func TestOr(t *testing.T) {
{"both true", true, true, true},
{"both false", false, false, false},
{"both null", nil, nil, nil},
{"left is string, right is different string", "abc", "def", false},
{"left is string, right is nil", "abc", nil, nil},
{"left is nil, right is string", nil, "def", nil},
{"left is float, right is string", 2.0, "hello", true},
}

for _, tt := range testCases {
Expand Down

0 comments on commit 7804b74

Please sign in to comment.