Skip to content

Commit

Permalink
enhance: [2.4] Refine error message for contains array (#37443)
Browse files Browse the repository at this point in the history
issue: #36221 

master pr: #37383

Signed-off-by: Cai Zhang <[email protected]>
  • Loading branch information
xiaocai2333 authored Nov 7, 2024
1 parent 20534a3 commit 4ae5337
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 5 additions & 4 deletions internal/parser/planparserv2/plan_parser_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,6 @@ func Test_JSONContains(t *testing.T) {
`JSON_CONTAINS(JSONField["x"], 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)`,
Expand Down Expand Up @@ -973,6 +972,8 @@ func Test_InvalidJSONContains(t *testing.T) {
`json_contains(A, 5, C)`,
`json_Contains(JSONField, 5)`,
`JSON_contains(JSONField, 5)`,
`json_contains(ArrayField, "abc")`,
`json_contains(ArrayField, [1,2])`,
}
for _, expr = range exprs {
_, err = CreateSearchPlan(schema, expr, "FloatVectorField", &planpb.QueryInfo{
Expand Down Expand Up @@ -1078,7 +1079,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{
Expand All @@ -1100,6 +1101,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{
Expand All @@ -1123,8 +1125,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{
Expand All @@ -1145,6 +1145,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 {
Expand Down
11 changes: 8 additions & 3 deletions internal/parser/planparserv2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,16 +664,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)
}
}
Expand Down

0 comments on commit 4ae5337

Please sign in to comment.