Skip to content

Commit

Permalink
fix(valuer): nil handling (#2952)
Browse files Browse the repository at this point in the history
Signed-off-by: Jiyong Huang <[email protected]>
  • Loading branch information
ngjaying committed Jun 27, 2024
1 parent 72df28d commit add08de
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 38 deletions.
7 changes: 1 addition & 6 deletions internal/topo/operator/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ func TestFilterPlan_Apply(t *testing.T) {
"a": int64(6),
},
},
result: &xsql.Tuple{
Emitter: "tbl",
Message: xsql.Message{
"a": int64(6),
},
},
result: nil,
},
{
sql: "SELECT * FROM tbl WHERE abc > def and abc <= ghi",
Expand Down
15 changes: 4 additions & 11 deletions internal/topo/operator/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/lf-edge/ekuiper/internal/conf"
"github.com/lf-edge/ekuiper/internal/topo/context"
"github.com/lf-edge/ekuiper/internal/xsql"
Expand Down Expand Up @@ -433,13 +435,6 @@ func TestLeftJoinPlan_Apply(t *testing.T) {
{
Tuples: []xsql.TupleRow{
&xsql.Tuple{Emitter: "src1", Message: xsql.Message{"id1": 1, "f2": "w1"}},
&xsql.Tuple{Emitter: "src2", Message: xsql.Message{"id2": 2, "f2": "w1"}},
},
},
{
Tuples: []xsql.TupleRow{
&xsql.Tuple{Emitter: "src1", Message: xsql.Message{"id1": 1, "f2": "w1"}},
&xsql.Tuple{Emitter: "src2", Message: xsql.Message{"id2": 2, "f2": "w2"}},
},
},
},
Expand Down Expand Up @@ -762,7 +757,7 @@ func TestLeftJoinPlan_Apply(t *testing.T) {
fmt.Printf("The test bucket size is %d.\n\n", len(tests))
contextLogger := conf.Log.WithField("rule", "TestLeftJoinPlan_Apply")
ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
for i, tt := range tests {
for _, tt := range tests {
stmt, err := xsql.NewParser(strings.NewReader(tt.sql)).Parse()
if err != nil {
t.Errorf("statement parse error %s", err)
Expand All @@ -775,9 +770,7 @@ func TestLeftJoinPlan_Apply(t *testing.T) {
fv, afv := xsql.NewFunctionValuersForOp(nil)
pp := &JoinOp{Joins: stmt.Joins, From: table}
result := pp.Apply(ctx, tt.data, fv, afv)
if !reflect.DeepEqual(tt.result, result) {
t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.result, result)
}
assert.Equal(t, tt.result, result)
}
}
}
Expand Down
29 changes: 13 additions & 16 deletions internal/xsql/valuer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022-2023 EMQ Technologies Co., Ltd.
// Copyright 2022-2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -483,6 +483,11 @@ func (v *ValuerEval) Eval(expr ast.Expr) interface{} {
case *ast.ValueSetExpr:
return v.evalValueSet(expr)
case *ast.BetweenExpr:
lower := v.Eval(expr.Lower)
higher := v.Eval(expr.Higher)
if lower == nil || higher == nil {
return nil
}
return []interface{}{
v.Eval(expr.Lower), v.Eval(expr.Higher),
}
Expand Down Expand Up @@ -538,6 +543,9 @@ func (v *ValuerEval) evalBinaryExpr(expr *ast.BinaryExpr) interface{} {
}
switch expr.OP {
case ast.BETWEEN, ast.NOTBETWEEN:
if lhs == nil || rhs == nil {
return false
}
arr, ok := rhs.([]interface{})
if !ok {
return fmt.Errorf("between operator expects two arguments, but found %v", rhs)
Expand Down Expand Up @@ -656,7 +664,7 @@ func (v *ValuerEval) evalSetsExpr(lhs interface{}, op ast.Token, rhsSet interfac
}
}
if lhs == nil {
return nil
return false
}
rhsSetVals := reflect.ValueOf(rhsSet)
for i := 0; i < rhsSetVals.Len(); i++ {
Expand Down Expand Up @@ -751,22 +759,11 @@ func (v *ValuerEval) subset(result interface{}, expr ast.Expr) interface{} {
}

// lhs and rhs are non-nil
func (v *ValuerEval) simpleDataEval(lhs, rhs interface{}, op ast.Token) interface{} {
func (v *ValuerEval) simpleDataEval(lhs, rhs interface{}, op ast.Token) any {
if lhs == nil || rhs == nil {
// for relationship, return false
switch op {
case ast.EQ, ast.LTE, ast.GTE:
if lhs == nil && rhs == nil {
return true
} else {
return false
}
case ast.NEQ:
if lhs == nil && rhs == nil {
return false
} else {
return true
}
case ast.LT, ast.GT:
case ast.AND, ast.OR, ast.BITWISE_AND, ast.BITWISE_OR, ast.BITWISE_XOR, ast.EQ, ast.NEQ, ast.GT, ast.GTE, ast.LT, ast.LTE:
return false
default:
return nil
Expand Down
6 changes: 3 additions & 3 deletions internal/xsql/valuer_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 EMQ Technologies Co., Ltd.
// Copyright 2021-2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestComparison(t *testing.T) {
},
r: []interface{}{
false, false, false,
true, false, true, false, false, true,
false, false, false, false, false, false,
},
}, { // 11
m: map[string]interface{}{
Expand All @@ -137,7 +137,7 @@ func TestComparison(t *testing.T) {
},
r: []interface{}{
false, true, errors.New("invalid operation int64(12) = string(string literal)"),
false, false, false, true, false, true,
false, false, false, false, false, false,
},
}, { // 12
m: map[string]interface{}{
Expand Down
4 changes: 2 additions & 2 deletions pkg/ast/token.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 EMQ Technologies Co., Ltd.
// Copyright 2021-2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,7 +36,7 @@ const (
BADSTRING // "abc

operatorBeg
// ADD and the following are InfluxQL Operators
// ADD and the following
ADD // +
SUB // -
MUL // *
Expand Down

0 comments on commit add08de

Please sign in to comment.