Skip to content

Commit

Permalink
fix(valuer): support uint comparison
Browse files Browse the repository at this point in the history
Signed-off-by: Jiyong Huang <huangjy@emqx.io>
  • Loading branch information
ngjaying committed Oct 10, 2023
1 parent d4e8793 commit c87dc48
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 39 deletions.
43 changes: 5 additions & 38 deletions internal/xsql/valuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1188,59 +1188,26 @@ func invalidOpError(lhs interface{}, op ast.Token, rhs interface{}) error {

func convertNum(para interface{}) interface{} {
if isInt(para) {
para = toInt64(para)
// Already check type of para so that there will be no error, just ignore error
para, _ = cast.ToInt64(para, cast.CONVERT_SAMEKIND)
} else if isFloat(para) {
para = toFloat64(para)
para, _ = cast.ToFloat64(para, cast.CONVERT_SAMEKIND)
}
return para
}

func isInt(para interface{}) bool {
switch para.(type) {
case int:
return true
case int8:
return true
case int16:
return true
case int32:
return true
case int64:
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return true
}
return false
}

func toInt64(para interface{}) int64 {
if v, ok := para.(int); ok {
return int64(v)
} else if v, ok := para.(int8); ok {
return int64(v)
} else if v, ok := para.(int16); ok {
return int64(v)
} else if v, ok := para.(int32); ok {
return int64(v)
} else if v, ok := para.(int64); ok {
return v
}
return 0
}

func isFloat(para interface{}) bool {
switch para.(type) {
case float32:
return true
case float64:
case float32, float64:
return true
}
return false
}

func toFloat64(para interface{}) float64 {
if v, ok := para.(float32); ok {
return float64(v)
} else if v, ok := para.(float64); ok {
return v
}
return 0
}
11 changes: 10 additions & 1 deletion internal/xsql/valuer_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2022 EMQ Technologies Co., Ltd.
// Copyright 2021-2023 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 @@ -139,6 +139,15 @@ func TestComparison(t *testing.T) {
false, true, errors.New("invalid operation int64(12) = string(string literal)"),
false, false, false, true, false, true,
},
}, { // 12
m: map[string]interface{}{
"a": uint32(32),
"b": int64(72),
},
r: []interface{}{
false, true, errors.New("invalid operation int64(32) = string(string literal)"),
false, true, false, true, true, false,
},
},
}
sqls := []string{
Expand Down

0 comments on commit c87dc48

Please sign in to comment.