Skip to content

Commit

Permalink
enhance: [2.4]Update the template expression proto to improve transmi…
Browse files Browse the repository at this point in the history
…ssion efficiency (#37485)

issue: #36672 

master pr: #37484

Signed-off-by: Cai Zhang <[email protected]>
  • Loading branch information
xiaocai2333 authored Nov 7, 2024
1 parent cea8c75 commit 651a56e
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 314 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/klauspost/compress v1.17.9
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
github.com/milvus-io/milvus-proto/go-api/v2 v2.4.15
github.com/milvus-io/milvus-proto/go-api/v2 v2.4.16-0.20241106083218-955997f1a757
github.com/minio/minio-go/v7 v7.0.73
github.com/pingcap/log v1.1.1-0.20221015072633-39906604fb81
github.com/prometheus/client_golang v1.14.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,8 @@ github.com/milvus-io/cgosymbolizer v0.0.0-20240722103217-b7dee0e50119 h1:9VXijWu
github.com/milvus-io/cgosymbolizer v0.0.0-20240722103217-b7dee0e50119/go.mod h1:DvXTE/K/RtHehxU8/GtDs4vFtfw64jJ3PaCnFri8CRg=
github.com/milvus-io/gorocksdb v0.0.0-20220624081344-8c5f4212846b h1:TfeY0NxYxZzUfIfYe5qYDBzt4ZYRqzUjTR6CvUzjat8=
github.com/milvus-io/gorocksdb v0.0.0-20220624081344-8c5f4212846b/go.mod h1:iwW+9cWfIzzDseEBCCeDSN5SD16Tidvy8cwQ7ZY8Qj4=
github.com/milvus-io/milvus-proto/go-api/v2 v2.4.15 h1:1y+hkeGh7zaD5ZasWjfKNZYWdH8VlLKcjoeyFiSh/I8=
github.com/milvus-io/milvus-proto/go-api/v2 v2.4.15/go.mod h1:/6UT4zZl6awVeXLeE7UGDWZvXj3IWkRsh3mqsn0DiAs=
github.com/milvus-io/milvus-proto/go-api/v2 v2.4.16-0.20241106083218-955997f1a757 h1:t7B2lyq//BG8S+azUNEfohYxRtU5V9NAy8z0G+QAPo4=
github.com/milvus-io/milvus-proto/go-api/v2 v2.4.16-0.20241106083218-955997f1a757/go.mod h1:/6UT4zZl6awVeXLeE7UGDWZvXj3IWkRsh3mqsn0DiAs=
github.com/milvus-io/milvus-storage/go v0.0.0-20231227072638-ebd0b8e56d70 h1:Z+sp64fmAOxAG7mU0dfVOXvAXlwRB0c8a96rIM5HevI=
github.com/milvus-io/milvus-storage/go v0.0.0-20231227072638-ebd0b8e56d70/go.mod h1:GPETMcTZq1gLY1WA6Na5kiNAKnq8SEMMiVKUZrM3sho=
github.com/milvus-io/pulsar-client-go v0.6.10 h1:eqpJjU+/QX0iIhEo3nhOqMNXL+TyInAs1IAHZCrCM/A=
Expand Down
128 changes: 104 additions & 24 deletions internal/parser/planparserv2/convert_field_data_to_generic_value.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,140 @@
package planparserv2

import (
"bytes"
"encoding/json"
"fmt"

"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
"github.com/milvus-io/milvus/internal/proto/planpb"
)

func convertArrayValue(templateName string, templateValue *schemapb.TemplateArrayValue) (*planpb.GenericValue, error) {
var arrayValues []*planpb.GenericValue
var elementType schemapb.DataType
switch templateValue.GetData().(type) {
case *schemapb.TemplateArrayValue_BoolData:
elements := templateValue.GetBoolData().GetData()
arrayValues = make([]*planpb.GenericValue, len(elements))
for i, element := range elements {
arrayValues[i] = &planpb.GenericValue{
Val: &planpb.GenericValue_BoolVal{
BoolVal: element,
},
}
}
elementType = schemapb.DataType_Bool
case *schemapb.TemplateArrayValue_LongData:
elements := templateValue.GetLongData().GetData()
arrayValues = make([]*planpb.GenericValue, len(elements))
for i, element := range elements {
arrayValues[i] = &planpb.GenericValue{
Val: &planpb.GenericValue_Int64Val{
Int64Val: element,
},
}
}
elementType = schemapb.DataType_Int64
case *schemapb.TemplateArrayValue_DoubleData:
elements := templateValue.GetDoubleData().GetData()
arrayValues = make([]*planpb.GenericValue, len(elements))
for i, element := range elements {
arrayValues[i] = &planpb.GenericValue{
Val: &planpb.GenericValue_FloatVal{
FloatVal: element,
},
}
}
elementType = schemapb.DataType_Double
case *schemapb.TemplateArrayValue_StringData:
elements := templateValue.GetStringData().GetData()
arrayValues = make([]*planpb.GenericValue, len(elements))
for i, element := range elements {
arrayValues[i] = &planpb.GenericValue{
Val: &planpb.GenericValue_StringVal{
StringVal: element,
},
}
}
elementType = schemapb.DataType_VarChar
case *schemapb.TemplateArrayValue_ArrayData:
elements := templateValue.GetArrayData().GetData()
arrayValues = make([]*planpb.GenericValue, len(elements))
for i, element := range elements {
targetValue, err := convertArrayValue(templateName, element)
if err != nil {
return nil, err
}
arrayValues[i] = targetValue
}
elementType = schemapb.DataType_Array
case *schemapb.TemplateArrayValue_JsonData:
elements := templateValue.GetJsonData().GetData()
arrayValues = make([]*planpb.GenericValue, len(elements))
for i, element := range elements {
var jsonElement interface{}
err := json.Unmarshal(element, &jsonElement)
if err != nil {
return nil, err
}
decoder := json.NewDecoder(bytes.NewBuffer(element))
decoder.UseNumber()
var value interface{}
if err = decoder.Decode(&value); err != nil {
return nil, err
}
parsedValue, _, err := parseJSONValue(value)
if err != nil {
return nil, err
}
arrayValues[i] = parsedValue
}
elementType = schemapb.DataType_JSON
default:
return nil, fmt.Errorf("unknown template variable value type: %v", templateValue.GetData())
}
return &planpb.GenericValue{
Val: &planpb.GenericValue_ArrayVal{
ArrayVal: &planpb.Array{
Array: arrayValues,
SameType: elementType != schemapb.DataType_JSON,
ElementType: elementType,
},
},
}, nil
}

func ConvertToGenericValue(templateName string, templateValue *schemapb.TemplateValue) (*planpb.GenericValue, error) {
if templateValue == nil {
return nil, fmt.Errorf("expression template variable value is nil, template name: {%s}", templateName)
}
switch templateValue.GetType() {
case schemapb.DataType_Bool:
switch templateValue.GetVal().(type) {
case *schemapb.TemplateValue_BoolVal:
return &planpb.GenericValue{
Val: &planpb.GenericValue_BoolVal{
BoolVal: templateValue.GetBoolVal(),
},
}, nil
case schemapb.DataType_Int8, schemapb.DataType_Int16, schemapb.DataType_Int32, schemapb.DataType_Int64:
case *schemapb.TemplateValue_Int64Val:
return &planpb.GenericValue{
Val: &planpb.GenericValue_Int64Val{
Int64Val: templateValue.GetInt64Val(),
},
}, nil
case schemapb.DataType_Float, schemapb.DataType_Double:
case *schemapb.TemplateValue_FloatVal:
return &planpb.GenericValue{
Val: &planpb.GenericValue_FloatVal{
FloatVal: templateValue.GetFloatVal(),
},
}, nil
case schemapb.DataType_String, schemapb.DataType_VarChar:
case *schemapb.TemplateValue_StringVal:
return &planpb.GenericValue{
Val: &planpb.GenericValue_StringVal{
StringVal: templateValue.GetStringVal(),
},
}, nil
case schemapb.DataType_Array:
elements := templateValue.GetArrayVal().GetArray()
arrayValues := make([]*planpb.GenericValue, len(elements))
for i, element := range elements {
arrayElement, err := ConvertToGenericValue(templateName, element)
if err != nil {
return nil, err
}
arrayValues[i] = arrayElement
}
return &planpb.GenericValue{
Val: &planpb.GenericValue_ArrayVal{
ArrayVal: &planpb.Array{
Array: arrayValues,
SameType: templateValue.GetArrayVal().GetSameType(),
ElementType: templateValue.GetArrayVal().GetElementType(),
},
},
}, nil
case *schemapb.TemplateValue_ArrayVal:
return convertArrayValue(templateName, templateValue.GetArrayVal())
default:
return nil, fmt.Errorf("expression elements can only be scalars")
}
Expand Down
Loading

0 comments on commit 651a56e

Please sign in to comment.