2022-05-06 17:43:51 +08:00
|
|
|
package planparserv2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-05-10 10:19:19 +08:00
|
|
|
"github.com/antlr/antlr4/runtime/Go/antlr"
|
2022-10-16 20:49:27 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/schemapb"
|
2022-05-06 17:43:51 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/planpb"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
2022-05-06 17:43:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func handleExpr(schema *typeutil.SchemaHelper, exprStr string) interface{} {
|
|
|
|
if exprStr == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
inputStream := antlr.NewInputStream(exprStr)
|
|
|
|
errorListener := &errorListener{}
|
2022-05-20 18:47:57 +08:00
|
|
|
|
|
|
|
lexer := getLexer(inputStream, errorListener)
|
|
|
|
if errorListener.err != nil {
|
|
|
|
return errorListener.err
|
|
|
|
}
|
|
|
|
|
|
|
|
parser := getParser(lexer, errorListener)
|
|
|
|
if errorListener.err != nil {
|
|
|
|
return errorListener.err
|
|
|
|
}
|
2022-05-06 17:43:51 +08:00
|
|
|
|
|
|
|
ast := parser.Expr()
|
|
|
|
if errorListener.err != nil {
|
|
|
|
return errorListener.err
|
|
|
|
}
|
|
|
|
|
2022-05-20 18:47:57 +08:00
|
|
|
// lexer & parser won't be used by this thread, can be put into pool.
|
|
|
|
putLexer(lexer)
|
|
|
|
putParser(parser)
|
|
|
|
|
2022-05-06 17:43:51 +08:00
|
|
|
visitor := NewParserVisitor(schema)
|
|
|
|
return ast.Accept(visitor)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseExpr(schema *typeutil.SchemaHelper, exprStr string) (*planpb.Expr, error) {
|
|
|
|
if len(exprStr) <= 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := handleExpr(schema, exprStr)
|
|
|
|
|
|
|
|
if err := getError(ret); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse expression: %s, error: %s", exprStr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
predicate := getExpr(ret)
|
|
|
|
if predicate == nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse expression: %s", exprStr)
|
|
|
|
}
|
|
|
|
if !typeutil.IsBoolType(predicate.dataType) {
|
|
|
|
return nil, fmt.Errorf("predicate is not a boolean expression: %s, data type: %s", exprStr, predicate.dataType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return predicate.expr, nil
|
|
|
|
}
|
|
|
|
|
2023-05-23 10:19:26 +08:00
|
|
|
func ParseIdentifier(schema *typeutil.SchemaHelper, identifier string, checkFunc func(*planpb.Expr) error) error {
|
|
|
|
ret := handleExpr(schema, identifier)
|
|
|
|
|
|
|
|
if err := getError(ret); err != nil {
|
|
|
|
return fmt.Errorf("cannot parse identifier: %s, error: %s", identifier, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
predicate := getExpr(ret)
|
|
|
|
if predicate == nil {
|
|
|
|
return fmt.Errorf("cannot parse identifier: %s", identifier)
|
|
|
|
}
|
|
|
|
if predicate.expr.GetColumnExpr() == nil {
|
|
|
|
return fmt.Errorf("cannot parse identifier: %s", identifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
return checkFunc(predicate.expr)
|
|
|
|
}
|
|
|
|
|
2022-05-06 17:43:51 +08:00
|
|
|
func CreateRetrievePlan(schemaPb *schemapb.CollectionSchema, exprStr string) (*planpb.PlanNode, error) {
|
|
|
|
schema, err := typeutil.CreateSchemaHelper(schemaPb)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
expr, err := ParseExpr(schema, exprStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
planNode := &planpb.PlanNode{
|
2023-03-16 19:31:55 +08:00
|
|
|
Node: &planpb.PlanNode_Query{
|
|
|
|
Query: &planpb.QueryPlanNode{
|
|
|
|
Predicates: expr,
|
|
|
|
},
|
2022-05-06 17:43:51 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return planNode, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateSearchPlan(schemaPb *schemapb.CollectionSchema, exprStr string, vectorFieldName string, queryInfo *planpb.QueryInfo) (*planpb.PlanNode, error) {
|
|
|
|
schema, err := typeutil.CreateSchemaHelper(schemaPb)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
expr, err := ParseExpr(schema, exprStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vectorField, err := schema.GetFieldFromName(vectorFieldName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fieldID := vectorField.FieldID
|
|
|
|
dataType := vectorField.DataType
|
|
|
|
|
|
|
|
if !typeutil.IsVectorType(dataType) {
|
|
|
|
return nil, fmt.Errorf("field (%s) to search is not of vector data type", vectorFieldName)
|
|
|
|
}
|
|
|
|
|
|
|
|
planNode := &planpb.PlanNode{
|
|
|
|
Node: &planpb.PlanNode_VectorAnns{
|
|
|
|
VectorAnns: &planpb.VectorANNS{
|
|
|
|
IsBinary: dataType == schemapb.DataType_BinaryVector,
|
|
|
|
Predicates: expr,
|
|
|
|
QueryInfo: queryInfo,
|
|
|
|
PlaceholderTag: "$0",
|
|
|
|
FieldId: fieldID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return planNode, nil
|
|
|
|
}
|