Fix err msg of invalid expression (#17296)

Signed-off-by: longjiquan <jiquan.long@zilliz.com>
pull/17310/head
Jiquan Long 2022-06-01 10:00:07 +08:00 committed by GitHub
parent 04011a7ecd
commit b4f797506d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 2 deletions

View File

@ -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)

View File

@ -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%"`,

View File

@ -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)

View File

@ -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)
}
})
}
}

View File

@ -1302,5 +1302,5 @@ class TestqueryString(TestcaseBase):
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"}
)