Skip to content

Commit

Permalink
Fix err msg of invalid expression (milvus-io#17296)
Browse files Browse the repository at this point in the history
Signed-off-by: longjiquan <[email protected]>
  • Loading branch information
longjiquan authored Jun 1, 2022
1 parent 04011a7 commit b4f7975
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/parser/planparserv2/parser_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (v *ParserVisitor) VisitLike(ctx *parser.LikeContext) interface{} {
}

if !typeutil.IsStringType(leftExpr.dataType) {
return fmt.Errorf("like operation on non-text field is unsupported")
return fmt.Errorf("like operation on non-string field is unsupported")
}

column := toColumnInfo(leftExpr)
Expand Down
4 changes: 4 additions & 0 deletions internal/parser/planparserv2/plan_parser_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,15 @@ func TestExpr_Invalid(t *testing.T) {
`1 == not_in_schema`,
`true == "str"`,
`"str" != false`,
`VarCharField != FloatField`,
`FloatField == VarCharField`,
// ---------------------- relational --------------------
`not_in_schema < 1`,
`1 <= not_in_schema`,
`true <= "str"`,
`"str" >= false`,
`VarCharField < FloatField`,
`FloatField > VarCharField`,
// ------------------------ like ------------------------
`(VarCharField % 2) like "prefix%"`,
`FloatField like "prefix%"`,
Expand Down
10 changes: 10 additions & 0 deletions internal/parser/planparserv2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,17 @@ func handleCompare(op planpb.OpType, left *ExprWithType, right *ExprWithType) (*
}
}

func relationalCompatible(t1, t2 schemapb.DataType) bool {
both := typeutil.IsStringType(t1) && typeutil.IsStringType(t2)
neither := !typeutil.IsStringType(t1) && !typeutil.IsStringType(t2)
return both || neither
}

func HandleCompare(op int, left, right *ExprWithType) (*planpb.Expr, error) {
if !relationalCompatible(left.dataType, right.dataType) {
return nil, fmt.Errorf("comparisons between string and non-string are not supported")
}

cmpOp := cmpOpMap[op]
if valueExpr := left.expr.GetValueExpr(); valueExpr != nil {
op, err := reverseOrder(cmpOp)
Expand Down
59 changes: 59 additions & 0 deletions internal/parser/planparserv2/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package planparserv2

import (
"testing"

"github.com/milvus-io/milvus/internal/proto/schemapb"
)

func Test_relationalCompatible(t *testing.T) {
type args struct {
t1 schemapb.DataType
t2 schemapb.DataType
}
tests := []struct {
name string
args args
want bool
}{
{
// both.
args: args{
t1: schemapb.DataType_VarChar,
t2: schemapb.DataType_VarChar,
},
want: true,
},
{
// neither.
args: args{
t1: schemapb.DataType_Float,
t2: schemapb.DataType_Float,
},
want: true,
},
{
// in-compatible.
args: args{
t1: schemapb.DataType_Float,
t2: schemapb.DataType_VarChar,
},
want: false,
},
{
// in-compatible.
args: args{
t1: schemapb.DataType_VarChar,
t2: schemapb.DataType_Float,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := relationalCompatible(tt.args.t1, tt.args.t2); got != tt.want {
t.Errorf("relationalCompatible() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion tests/python_client/testcases/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1302,5 +1302,5 @@ def test_query_string_with_invaild_prefix_expr(self):
collection_w = self.init_collection_general(prefix, insert_data=True)[0]
expression = 'float like "0%"'
collection_w.query(expression, check_task=CheckTasks.err_res,
check_items={ct.err_code: 1, ct.err_msg: "like operation on non-text field is unsupported"}
check_items={ct.err_code: 1, ct.err_msg: "like operation on non-string field is unsupported"}
)

0 comments on commit b4f7975

Please sign in to comment.