From 92d13d7a057e1975737c59ada6c5b29fa0d340c4 Mon Sep 17 00:00:00 2001 From: Cai Zhang Date: Sat, 2 Nov 2024 11:16:17 +0800 Subject: [PATCH] Refine error message for contains Signed-off-by: Cai Zhang --- internal/parser/planparserv2/plan_parser_v2_test.go | 9 +++++---- internal/parser/planparserv2/utils.go | 11 ++++++++--- tests/python_client/testcases/test_mix_scenes.py | 4 ++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/internal/parser/planparserv2/plan_parser_v2_test.go b/internal/parser/planparserv2/plan_parser_v2_test.go index d1e55baf17d37..febff4be83180 100644 --- a/internal/parser/planparserv2/plan_parser_v2_test.go +++ b/internal/parser/planparserv2/plan_parser_v2_test.go @@ -997,7 +997,6 @@ func Test_JSONContains(t *testing.T) { `JSON_contains(JSONField, 5)`, `json_contains(A, [1,2,3])`, `array_contains(A, [1,2,3])`, - `array_contains(ArrayField, [1,2,3])`, `array_contains(ArrayField, 1)`, `json_contains(JSONField, 5)`, `json_contains($meta, 1)`, @@ -1030,6 +1029,8 @@ func Test_InvalidJSONContains(t *testing.T) { `json_contains(A, StringField > 5)`, `json_contains(A)`, `json_contains(A, 5, C)`, + `json_contains(ArrayField, "abc")`, + `json_contains(ArrayField, [1,2])`, } for _, expr = range exprs { _, err = CreateSearchPlan(schema, expr, "FloatVectorField", &planpb.QueryInfo{ @@ -1135,7 +1136,7 @@ func Test_JSONContainsAll(t *testing.T) { `JSON_CONTAINS_ALL(A, [1,"2",3.0])`, `array_contains_all(ArrayField, [1,2,3])`, `array_contains_all(ArrayField, [1])`, - `json_contains_all(ArrayField, [1,2,3])`, + `array_contains_all(ArrayField, [1,2,3])`, } for _, expr = range exprs { plan, err = CreateSearchPlan(schema, expr, "FloatVectorField", &planpb.QueryInfo{ @@ -1157,6 +1158,7 @@ func Test_JSONContainsAll(t *testing.T) { `JSON_CONTAINS_ALL(A[""], [1,2,3])`, `JSON_CONTAINS_ALL(Int64Field, [1,2,3])`, `JSON_CONTAINS_ALL(A, B)`, + `JSON_CONTAINS_ALL(ArrayField, [[1,2,3]])`, } for _, expr = range invalidExprs { _, err = CreateSearchPlan(schema, expr, "FloatVectorField", &planpb.QueryInfo{ @@ -1180,8 +1182,6 @@ func Test_JSONContainsAny(t *testing.T) { `json_contains_any(A, [1,"2",3.0])`, `JSON_CONTAINS_ANY(A, [1,"2",3.0])`, `JSON_CONTAINS_ANY(ArrayField, [1,2,3])`, - `JSON_CONTAINS_ANY(ArrayField, [3,4,5])`, - `JSON_CONTAINS_ANY(ArrayField, [1,2,3])`, } for _, expr = range exprs { plan, err = CreateSearchPlan(schema, expr, "FloatVectorField", &planpb.QueryInfo{ @@ -1202,6 +1202,7 @@ func Test_JSONContainsAny(t *testing.T) { `JSON_CONTAINS_ANY(A, [2>>a])`, `JSON_CONTAINS_ANY(A[""], [1,2,3])`, `JSON_CONTAINS_ANY(Int64Field, [1,2,3])`, + `JSON_CONTAINS_ANY(ArrayField, [[1,2,3]])`, `JSON_CONTAINS_ANY(A, B)`, } for _, expr = range invalidExprs { diff --git a/internal/parser/planparserv2/utils.go b/internal/parser/planparserv2/utils.go index 4faef470dd7c1..61eeca1b86875 100644 --- a/internal/parser/planparserv2/utils.go +++ b/internal/parser/planparserv2/utils.go @@ -665,16 +665,21 @@ func checkContainsElement(columnExpr *ExprWithType, op planpb.JSONContainsExpr_J if typeutil.IsArrayType(columnExpr.expr.GetColumnExpr().GetInfo().GetDataType()) { var elements []*planpb.GenericValue if op == planpb.JSONContainsExpr_Contains { - elements = []*planpb.GenericValue{elementValue} + castedValue, err := castValue(columnExpr.expr.GetColumnExpr().GetInfo().GetElementType(), elementValue) + if err != nil { + return err + } + elements = []*planpb.GenericValue{castedValue} } else { elements = elementValue.GetArrayVal().GetArray() } + arrayElementType := columnExpr.expr.GetColumnExpr().GetInfo().GetElementType() for _, value := range elements { valExpr := toValueExpr(value) - if !canBeCompared(columnExpr, valExpr) { + if !canBeComparedDataType(arrayElementType, valExpr.dataType) { return fmt.Errorf("%s operation can't compare between array element type: %s and %s", op.String(), - columnExpr.expr.GetColumnExpr().GetInfo().GetElementType(), + arrayElementType, valExpr.dataType) } } diff --git a/tests/python_client/testcases/test_mix_scenes.py b/tests/python_client/testcases/test_mix_scenes.py index 7fa5b5770b5a0..fce7d816d8e5c 100644 --- a/tests/python_client/testcases/test_mix_scenes.py +++ b/tests/python_client/testcases/test_mix_scenes.py @@ -889,11 +889,11 @@ def test_bitmap_index_query_with_invalid_array_params(self): # query self.collection_wrap.query( expr=Expr.array_contains_any('ARRAY_VARCHAR', [['a', 'b']]).value, limit=1, check_task=CheckTasks.err_res, - check_items={ct.err_code: 65535, ct.err_msg: "fail to Query on QueryNode"}) + check_items={ct.err_code: 1100, ct.err_msg: qem.ParseExpressionFailed}) self.collection_wrap.query( expr=Expr.array_contains_all('ARRAY_VARCHAR', [['a', 'b']]).value, limit=1, check_task=CheckTasks.err_res, - check_items={ct.err_code: 65535, ct.err_msg: "fail to Query on QueryNode"}) + check_items={ct.err_code: 1100, ct.err_msg: qem.ParseExpressionFailed}) self.collection_wrap.query( expr=Expr.array_contains('ARRAY_VARCHAR', [['a', 'b']]).value, limit=1, check_task=CheckTasks.err_res,