Skip to content

Commit

Permalink
Support template to parse expression
Browse files Browse the repository at this point in the history
Signed-off-by: Cai Zhang <[email protected]>
  • Loading branch information
xiaocai2333 committed Oct 22, 2024
1 parent e6cb7d6 commit 8909454
Show file tree
Hide file tree
Showing 24 changed files with 2,597 additions and 1,007 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ replace (
github.com/milvus-io/milvus/pkg => ./pkg
github.com/streamnative/pulsarctl => github.com/xiaofan-luan/pulsarctl v0.5.1
github.com/tecbot/gorocksdb => github.com/milvus-io/gorocksdb v0.0.0-20220624081344-8c5f4212846b // indirect
github.com/milvus-io/milvus-proto/go-api/v2 => /home/zc/work/milvus-proto/go-api
)

exclude github.com/apache/pulsar-client-go/oauth2 v0.0.0-20211108044248-fe3b7c4e445b
3 changes: 3 additions & 0 deletions internal/parser/planparserv2/Plan.g4
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ expr:
| StringLiteral # String
| Identifier # Identifier
| JSONIdentifier # JSONIdentifier
| LBRACE Identifier RBRACE # Placeholder
| '(' expr ')' # Parens
| '[' expr (',' expr)* ','? ']' # Array
| EmptyArray # EmptyArray
Expand Down Expand Up @@ -44,6 +45,8 @@ expr:
// INT64: 'int64';
// FLOAT: 'float';
// DOUBLE: 'double';
LBRACE: '{';
RBRACE: '}';

LT: '<';
LE: '<=';
Expand Down
126 changes: 126 additions & 0 deletions internal/parser/planparserv2/convert_field_data_to_generic_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package planparserv2

import (
"encoding/json"
"fmt"
"strings"

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

func ConvertFieldDataToGenericValue(data *schemapb.ScalarField, dataType schemapb.DataType) ([]*planpb.GenericValue, error) {
if data == nil {
return nil, fmt.Errorf("convert field data is nil")
}
values := make([]*planpb.GenericValue, 0)
switch dataType {
case schemapb.DataType_Bool:
elements := data.GetBoolData().GetData()
for _, element := range elements {
values = append(values, &planpb.GenericValue{
Val: &planpb.GenericValue_BoolVal{
BoolVal: element,
},
})
}
case schemapb.DataType_Int8, schemapb.DataType_Int16, schemapb.DataType_Int32:
elements := data.GetIntData().GetData()
for _, element := range elements {
values = append(values, &planpb.GenericValue{
Val: &planpb.GenericValue_Int64Val{
Int64Val: int64(element),
},
})
}
case schemapb.DataType_Int64:
elements := data.GetLongData().GetData()
for _, element := range elements {
values = append(values, &planpb.GenericValue{
Val: &planpb.GenericValue_Int64Val{
Int64Val: element,
},
})
}
case schemapb.DataType_Float:
elements := data.GetFloatData().GetData()
for _, element := range elements {
values = append(values, &planpb.GenericValue{
Val: &planpb.GenericValue_FloatVal{
FloatVal: float64(element),
},
})
}
case schemapb.DataType_Double:
elements := data.GetDoubleData().GetData()
for _, element := range elements {
values = append(values, &planpb.GenericValue{
Val: &planpb.GenericValue_FloatVal{
FloatVal: element,
},
})
}
case schemapb.DataType_String, schemapb.DataType_VarChar:
elements := data.GetStringData().GetData()
for _, element := range elements {
values = append(values, &planpb.GenericValue{
Val: &planpb.GenericValue_StringVal{
StringVal: element,
},
})
}
case schemapb.DataType_Array:
elements := data.GetArrayData().GetData()
for _, element := range elements {
arrayElements, err := ConvertFieldDataToGenericValue(element, data.GetArrayData().GetElementType())
if err != nil {
return nil, err
}
values = append(values, &planpb.GenericValue{
Val: &planpb.GenericValue_ArrayVal{
ArrayVal: &planpb.Array{
Array: arrayElements,
SameType: data.GetArrayData().GetElementType() != schemapb.DataType_JSON,
ElementType: data.GetArrayData().GetElementType(),
},
},
})
}
case schemapb.DataType_JSON:
elements := data.GetJsonData().GetData()
for _, element := range elements {
var j interface{}
desc := json.NewDecoder(strings.NewReader(string(element)))
desc.UseNumber()
err := desc.Decode(&j)
if err != nil {
return nil, err
}
value, _, err := parseJSONValue(j)
if err != nil {
return nil, err
}
values = append(values, value)
}
default:
return nil, fmt.Errorf("expression elements can only be scalars")

}

return values, nil
}

func UnmarshalExpressionValues(input ...*schemapb.FieldData) (map[string]*planpb.GenericValue, error) {
result := make(map[string]*planpb.GenericValue, len(input))
for _, data := range input {
rv, err := ConvertFieldDataToGenericValue(data.GetScalars(), data.GetType())
if err != nil {
return nil, err
}
if len(rv) != 1 {
return nil, fmt.Errorf("invalue elements num for %s", data.GetFieldName())
}
result[data.GetFieldName()] = rv[0]
}
return result, nil
}
Loading

0 comments on commit 8909454

Please sign in to comment.