Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit ee455f0

Browse files
committed
sql: use custom boolean coercion logic in condition evaluation
Signed-off-by: Miguel Molina <miguel@erizocosmi.co>
1 parent bc23348 commit ee455f0

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

engine_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,10 @@ var queries = []struct {
12301230
`SELECT (NULL+1)`,
12311231
[]sql.Row{{nil}},
12321232
},
1233+
{
1234+
`SELECT * FROM mytable WHERE NULL AND i = 3`,
1235+
[]sql.Row{},
1236+
},
12331237
}
12341238

12351239
func TestQueries(t *testing.T) {

sql/core.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package sql // import "github.com/src-d/go-mysql-server/sql"
33
import (
44
"fmt"
55
"io"
6+
"math"
7+
"time"
68

79
"gopkg.in/src-d/go-errors.v1"
810
)
@@ -218,3 +220,39 @@ type Lockable interface {
218220
// available.
219221
Unlock(ctx *Context, id uint32) error
220222
}
223+
224+
// EvaluateCondition evaluates a condition, which is an expression whose value
225+
// will be coerced to boolean.
226+
func EvaluateCondition(ctx *Context, cond Expression, row Row) (bool, error) {
227+
v, err := cond.Eval(ctx, row)
228+
if err != nil {
229+
return false, err
230+
}
231+
232+
switch b := v.(type) {
233+
case bool:
234+
return b, nil
235+
case int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8:
236+
if b != 0 {
237+
return true, nil
238+
}
239+
return false, nil
240+
case time.Duration:
241+
if int64(b) != 0 {
242+
return true, nil
243+
}
244+
return false, nil
245+
case time.Time:
246+
if b.UnixNano() != 0 {
247+
return true, nil
248+
}
249+
return false, nil
250+
case float32, float64:
251+
if int(math.Round(v.(float64))) != 0 {
252+
return true, nil
253+
}
254+
return false, nil
255+
default:
256+
return false, nil
257+
}
258+
}

sql/plan/filter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ func (i *FilterIter) Next() (sql.Row, error) {
107107
return nil, err
108108
}
109109

110-
result, err := i.cond.Eval(i.ctx, row)
110+
ok, err := sql.EvaluateCondition(i.ctx, i.cond, row)
111111
if err != nil {
112112
return nil, err
113113
}
114114

115-
if result == true {
115+
if ok {
116116
return row, nil
117117
}
118118
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy