2021-04-30 15:19:52 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
|
|
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
|
|
// or implied. See the License for the specific language governing permissions and limitations under the License.
|
|
|
|
|
2021-06-22 14:40:07 +08:00
|
|
|
package proxy
|
2021-04-29 16:48:06 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-07-13 14:37:03 +08:00
|
|
|
"math"
|
2021-09-14 16:15:47 +08:00
|
|
|
"strings"
|
2021-04-29 16:48:06 +08:00
|
|
|
|
|
|
|
ant_ast "github.com/antonmedv/expr/ast"
|
|
|
|
ant_parser "github.com/antonmedv/expr/parser"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/planpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/schemapb"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
|
|
|
)
|
|
|
|
|
2021-06-05 14:57:34 +08:00
|
|
|
func parseQueryExpr(schema *typeutil.SchemaHelper, exprStr string) (*planpb.Expr, error) {
|
|
|
|
if exprStr == "" {
|
2021-04-29 16:48:06 +08:00
|
|
|
return nil, nil
|
|
|
|
}
|
2021-06-05 14:57:34 +08:00
|
|
|
|
2021-04-29 16:48:06 +08:00
|
|
|
return parseQueryExprAdvanced(schema, exprStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ParserContext struct {
|
|
|
|
schema *typeutil.SchemaHelper
|
|
|
|
}
|
|
|
|
|
2021-07-13 14:37:03 +08:00
|
|
|
type optimizer struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*optimizer) Enter(*ant_ast.Node) {}
|
|
|
|
|
|
|
|
func (optimizer *optimizer) Exit(node *ant_ast.Node) {
|
|
|
|
patch := func(newNode ant_ast.Node) {
|
|
|
|
ant_ast.Patch(node, newNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch node := (*node).(type) {
|
|
|
|
case *ant_ast.UnaryNode:
|
|
|
|
switch node.Operator {
|
|
|
|
case "-":
|
|
|
|
if i, ok := node.Node.(*ant_ast.IntegerNode); ok {
|
|
|
|
patch(&ant_ast.IntegerNode{Value: -i.Value})
|
|
|
|
} else if i, ok := node.Node.(*ant_ast.FloatNode); ok {
|
|
|
|
patch(&ant_ast.FloatNode{Value: -i.Value})
|
|
|
|
} else {
|
|
|
|
optimizer.err = fmt.Errorf("can only make a number negative")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "+":
|
|
|
|
if i, ok := node.Node.(*ant_ast.IntegerNode); ok {
|
|
|
|
patch(&ant_ast.IntegerNode{Value: i.Value})
|
|
|
|
} else if i, ok := node.Node.(*ant_ast.FloatNode); ok {
|
|
|
|
patch(&ant_ast.FloatNode{Value: i.Value})
|
|
|
|
} else {
|
|
|
|
optimizer.err = fmt.Errorf("can only make a number positive")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *ant_ast.BinaryNode:
|
|
|
|
floatNodeLeft, leftFloat := node.Left.(*ant_ast.FloatNode)
|
|
|
|
integerNodeLeft, leftInteger := node.Left.(*ant_ast.IntegerNode)
|
|
|
|
floatNodeRight, rightFloat := node.Right.(*ant_ast.FloatNode)
|
|
|
|
integerNodeRight, rightInteger := node.Right.(*ant_ast.IntegerNode)
|
|
|
|
|
|
|
|
switch node.Operator {
|
|
|
|
case "+":
|
|
|
|
if leftFloat && rightFloat {
|
|
|
|
patch(&ant_ast.FloatNode{Value: floatNodeLeft.Value + floatNodeRight.Value})
|
|
|
|
} else if leftFloat && rightInteger {
|
|
|
|
patch(&ant_ast.FloatNode{Value: floatNodeLeft.Value + float64(integerNodeRight.Value)})
|
|
|
|
} else if leftInteger && rightFloat {
|
|
|
|
patch(&ant_ast.FloatNode{Value: float64(integerNodeLeft.Value) + floatNodeRight.Value})
|
|
|
|
} else if leftInteger && rightInteger {
|
|
|
|
patch(&ant_ast.IntegerNode{Value: integerNodeLeft.Value + integerNodeRight.Value})
|
|
|
|
} else {
|
|
|
|
optimizer.err = fmt.Errorf("can only add two number")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "-":
|
|
|
|
if leftFloat && rightFloat {
|
|
|
|
patch(&ant_ast.FloatNode{Value: floatNodeLeft.Value - floatNodeRight.Value})
|
|
|
|
} else if leftFloat && rightInteger {
|
|
|
|
patch(&ant_ast.FloatNode{Value: floatNodeLeft.Value - float64(integerNodeRight.Value)})
|
|
|
|
} else if leftInteger && rightFloat {
|
|
|
|
patch(&ant_ast.FloatNode{Value: float64(integerNodeLeft.Value) - floatNodeRight.Value})
|
|
|
|
} else if leftInteger && rightInteger {
|
|
|
|
patch(&ant_ast.IntegerNode{Value: integerNodeLeft.Value - integerNodeRight.Value})
|
|
|
|
} else {
|
|
|
|
optimizer.err = fmt.Errorf("can only subtract two number")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "*":
|
|
|
|
if leftFloat && rightFloat {
|
|
|
|
patch(&ant_ast.FloatNode{Value: floatNodeLeft.Value * floatNodeRight.Value})
|
|
|
|
} else if leftFloat && rightInteger {
|
|
|
|
patch(&ant_ast.FloatNode{Value: floatNodeLeft.Value * float64(integerNodeRight.Value)})
|
|
|
|
} else if leftInteger && rightFloat {
|
|
|
|
patch(&ant_ast.FloatNode{Value: float64(integerNodeLeft.Value) * floatNodeRight.Value})
|
|
|
|
} else if leftInteger && rightInteger {
|
|
|
|
patch(&ant_ast.IntegerNode{Value: integerNodeLeft.Value * integerNodeRight.Value})
|
|
|
|
} else {
|
|
|
|
optimizer.err = fmt.Errorf("can only multiply two number")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "/":
|
|
|
|
if leftFloat && rightFloat {
|
|
|
|
if floatNodeRight.Value == 0 {
|
|
|
|
optimizer.err = fmt.Errorf("number divide by zero")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
patch(&ant_ast.FloatNode{Value: floatNodeLeft.Value / floatNodeRight.Value})
|
|
|
|
} else if leftFloat && rightInteger {
|
|
|
|
if integerNodeRight.Value == 0 {
|
|
|
|
optimizer.err = fmt.Errorf("number divide by zero")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
patch(&ant_ast.FloatNode{Value: floatNodeLeft.Value / float64(integerNodeRight.Value)})
|
|
|
|
} else if leftInteger && rightFloat {
|
|
|
|
if floatNodeRight.Value == 0 {
|
|
|
|
optimizer.err = fmt.Errorf("number divide by zero")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
patch(&ant_ast.FloatNode{Value: float64(integerNodeLeft.Value) / floatNodeRight.Value})
|
|
|
|
} else if leftInteger && rightInteger {
|
|
|
|
if integerNodeRight.Value == 0 {
|
|
|
|
optimizer.err = fmt.Errorf("number divide by zero")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
patch(&ant_ast.IntegerNode{Value: integerNodeLeft.Value / integerNodeRight.Value})
|
|
|
|
} else {
|
|
|
|
optimizer.err = fmt.Errorf("can only divide two number")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "%":
|
|
|
|
if leftInteger && rightInteger {
|
|
|
|
patch(&ant_ast.IntegerNode{Value: integerNodeLeft.Value % integerNodeRight.Value})
|
|
|
|
} else {
|
|
|
|
optimizer.err = fmt.Errorf("can only modulus two integer")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case "**":
|
|
|
|
if leftFloat && rightFloat {
|
|
|
|
patch(&ant_ast.FloatNode{Value: math.Pow(floatNodeLeft.Value, floatNodeRight.Value)})
|
|
|
|
} else if leftFloat && rightInteger {
|
|
|
|
patch(&ant_ast.FloatNode{Value: math.Pow(floatNodeLeft.Value, float64(integerNodeRight.Value))})
|
|
|
|
} else if leftInteger && rightFloat {
|
|
|
|
patch(&ant_ast.FloatNode{Value: math.Pow(float64(integerNodeLeft.Value), floatNodeRight.Value)})
|
|
|
|
} else if leftInteger && rightInteger {
|
|
|
|
patch(&ant_ast.IntegerNode{Value: int(math.Pow(float64(integerNodeLeft.Value), float64(integerNodeRight.Value)))})
|
|
|
|
} else {
|
|
|
|
optimizer.err = fmt.Errorf("can only pow two number")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 16:48:06 +08:00
|
|
|
func parseQueryExprAdvanced(schema *typeutil.SchemaHelper, exprStr string) (*planpb.Expr, error) {
|
|
|
|
ast, err := ant_parser.Parse(exprStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-13 14:37:03 +08:00
|
|
|
|
|
|
|
optimizer := &optimizer{}
|
|
|
|
ant_ast.Walk(&ast.Node, optimizer)
|
|
|
|
if optimizer.err != nil {
|
|
|
|
return nil, optimizer.err
|
|
|
|
}
|
|
|
|
|
2021-04-29 16:48:06 +08:00
|
|
|
context := ParserContext{schema}
|
|
|
|
|
2021-07-13 14:37:03 +08:00
|
|
|
expr, err := context.handleExpr(&ast.Node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return expr, nil
|
2021-04-29 16:48:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (context *ParserContext) createColumnInfo(field *schemapb.FieldSchema) *planpb.ColumnInfo {
|
|
|
|
return &planpb.ColumnInfo{
|
2021-06-07 17:21:36 +08:00
|
|
|
FieldId: field.FieldID,
|
|
|
|
DataType: field.DataType,
|
|
|
|
IsPrimaryKey: field.IsPrimaryKey,
|
2021-04-29 16:48:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-25 10:23:19 +08:00
|
|
|
func isSameOrder(opStr1, opStr2 string) bool {
|
|
|
|
isLess1 := opStr1 == "<" || opStr1 == "<="
|
|
|
|
isLess2 := opStr2 == "<" || opStr2 == "<="
|
|
|
|
return isLess1 == isLess2
|
2021-07-13 14:37:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func getCompareOpType(opStr string, reverse bool) planpb.OpType {
|
|
|
|
type OpType = planpb.OpType
|
|
|
|
var op planpb.OpType
|
2021-04-30 15:19:52 +08:00
|
|
|
|
2021-04-29 16:48:06 +08:00
|
|
|
if !reverse {
|
|
|
|
switch opStr {
|
|
|
|
case "<":
|
2021-07-13 14:37:03 +08:00
|
|
|
op = planpb.OpType_LessThan
|
2021-04-29 16:48:06 +08:00
|
|
|
case ">":
|
2021-07-13 14:37:03 +08:00
|
|
|
op = planpb.OpType_GreaterThan
|
2021-04-29 16:48:06 +08:00
|
|
|
case "<=":
|
2021-07-13 14:37:03 +08:00
|
|
|
op = planpb.OpType_LessEqual
|
2021-04-29 16:48:06 +08:00
|
|
|
case ">=":
|
2021-07-13 14:37:03 +08:00
|
|
|
op = planpb.OpType_GreaterEqual
|
2021-04-29 16:48:06 +08:00
|
|
|
case "==":
|
2021-07-13 14:37:03 +08:00
|
|
|
op = planpb.OpType_Equal
|
2021-04-29 16:48:06 +08:00
|
|
|
case "!=":
|
2021-07-13 14:37:03 +08:00
|
|
|
op = planpb.OpType_NotEqual
|
2021-04-29 16:48:06 +08:00
|
|
|
default:
|
2021-07-13 14:37:03 +08:00
|
|
|
op = planpb.OpType_Invalid
|
2021-04-29 16:48:06 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch opStr {
|
|
|
|
case ">":
|
2021-07-13 14:37:03 +08:00
|
|
|
op = planpb.OpType_LessThan
|
2021-04-29 16:48:06 +08:00
|
|
|
case "<":
|
2021-07-13 14:37:03 +08:00
|
|
|
op = planpb.OpType_GreaterThan
|
2021-04-29 16:48:06 +08:00
|
|
|
case ">=":
|
2021-07-13 14:37:03 +08:00
|
|
|
op = planpb.OpType_LessEqual
|
2021-04-29 16:48:06 +08:00
|
|
|
case "<=":
|
2021-07-13 14:37:03 +08:00
|
|
|
op = planpb.OpType_GreaterEqual
|
2021-04-29 16:48:06 +08:00
|
|
|
case "==":
|
2021-07-13 14:37:03 +08:00
|
|
|
op = planpb.OpType_Equal
|
2021-04-29 16:48:06 +08:00
|
|
|
case "!=":
|
2021-07-13 14:37:03 +08:00
|
|
|
op = planpb.OpType_NotEqual
|
2021-04-29 16:48:06 +08:00
|
|
|
default:
|
2021-07-13 14:37:03 +08:00
|
|
|
op = planpb.OpType_Invalid
|
2021-04-29 16:48:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return op
|
|
|
|
}
|
|
|
|
|
2021-04-30 15:19:52 +08:00
|
|
|
func getLogicalOpType(opStr string) planpb.BinaryExpr_BinaryOp {
|
|
|
|
switch opStr {
|
|
|
|
case "&&", "and":
|
|
|
|
return planpb.BinaryExpr_LogicalAnd
|
|
|
|
case "||", "or":
|
|
|
|
return planpb.BinaryExpr_LogicalOr
|
|
|
|
default:
|
|
|
|
return planpb.BinaryExpr_Invalid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 16:15:47 +08:00
|
|
|
func parseBoolNode(nodeRaw *ant_ast.Node) *ant_ast.BoolNode {
|
|
|
|
switch node := (*nodeRaw).(type) {
|
|
|
|
case *ant_ast.IdentifierNode:
|
|
|
|
val := strings.ToLower(node.Value)
|
|
|
|
if val == "true" {
|
|
|
|
return &ant_ast.BoolNode{
|
|
|
|
Value: true,
|
|
|
|
}
|
|
|
|
} else if val == "false" {
|
|
|
|
return &ant_ast.BoolNode{
|
|
|
|
Value: false,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 14:37:03 +08:00
|
|
|
func (context *ParserContext) createCmpExpr(left, right ant_ast.Node, operator string) (*planpb.Expr, error) {
|
2021-09-14 16:15:47 +08:00
|
|
|
if boolNode := parseBoolNode(&left); boolNode != nil {
|
|
|
|
left = boolNode
|
|
|
|
}
|
|
|
|
if boolNode := parseBoolNode(&right); boolNode != nil {
|
|
|
|
right = boolNode
|
|
|
|
}
|
2021-07-13 14:37:03 +08:00
|
|
|
idNodeLeft, leftIDNode := left.(*ant_ast.IdentifierNode)
|
|
|
|
idNodeRight, rightIDNode := right.(*ant_ast.IdentifierNode)
|
|
|
|
|
|
|
|
if leftIDNode && rightIDNode {
|
|
|
|
leftField, err := context.handleIdentifier(idNodeLeft)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rightField, err := context.handleIdentifier(idNodeRight)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
op := getCompareOpType(operator, false)
|
|
|
|
if op == planpb.OpType_Invalid {
|
2021-07-25 10:23:19 +08:00
|
|
|
return nil, fmt.Errorf("invalid binary operator(%s)", operator)
|
2021-07-13 14:37:03 +08:00
|
|
|
}
|
|
|
|
expr := &planpb.Expr{
|
|
|
|
Expr: &planpb.Expr_CompareExpr{
|
|
|
|
CompareExpr: &planpb.CompareExpr{
|
2021-07-25 10:23:19 +08:00
|
|
|
LeftColumnInfo: context.createColumnInfo(leftField),
|
|
|
|
RightColumnInfo: context.createColumnInfo(rightField),
|
|
|
|
Op: op,
|
2021-07-13 14:37:03 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return expr, nil
|
|
|
|
}
|
|
|
|
|
2021-04-30 15:19:52 +08:00
|
|
|
var idNode *ant_ast.IdentifierNode
|
|
|
|
var isReversed bool
|
|
|
|
var valueNode *ant_ast.Node
|
2021-07-13 14:37:03 +08:00
|
|
|
if leftIDNode {
|
2021-04-30 15:19:52 +08:00
|
|
|
idNode = idNodeLeft
|
|
|
|
isReversed = false
|
2021-07-13 14:37:03 +08:00
|
|
|
valueNode = &right
|
|
|
|
} else if rightIDNode {
|
2021-04-30 15:19:52 +08:00
|
|
|
idNode = idNodeRight
|
|
|
|
isReversed = true
|
2021-07-13 14:37:03 +08:00
|
|
|
valueNode = &left
|
2021-04-30 15:19:52 +08:00
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("compare expr has no identifier")
|
2021-04-29 16:48:06 +08:00
|
|
|
}
|
2021-04-30 15:19:52 +08:00
|
|
|
|
2021-04-29 16:48:06 +08:00
|
|
|
field, err := context.handleIdentifier(idNode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-30 15:19:52 +08:00
|
|
|
val, err := context.handleLeafValue(valueNode, field.DataType)
|
2021-04-29 16:48:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-13 14:37:03 +08:00
|
|
|
op := getCompareOpType(operator, isReversed)
|
|
|
|
if op == planpb.OpType_Invalid {
|
2021-07-25 10:23:19 +08:00
|
|
|
return nil, fmt.Errorf("invalid binary operator(%s)", operator)
|
2021-04-29 16:48:06 +08:00
|
|
|
}
|
|
|
|
|
2021-04-30 15:19:52 +08:00
|
|
|
expr := &planpb.Expr{
|
2021-07-25 10:23:19 +08:00
|
|
|
Expr: &planpb.Expr_UnaryRangeExpr{
|
|
|
|
UnaryRangeExpr: &planpb.UnaryRangeExpr{
|
2021-04-29 16:48:06 +08:00
|
|
|
ColumnInfo: context.createColumnInfo(field),
|
2021-07-25 10:23:19 +08:00
|
|
|
Op: op,
|
|
|
|
Value: val,
|
2021-04-29 16:48:06 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-04-30 15:19:52 +08:00
|
|
|
return expr, nil
|
2021-04-29 16:48:06 +08:00
|
|
|
}
|
|
|
|
|
2021-07-13 14:37:03 +08:00
|
|
|
func (context *ParserContext) handleCmpExpr(node *ant_ast.BinaryNode) (*planpb.Expr, error) {
|
|
|
|
return context.createCmpExpr(node.Left, node.Right, node.Operator)
|
|
|
|
}
|
|
|
|
|
2021-04-29 16:48:06 +08:00
|
|
|
func (context *ParserContext) handleLogicalExpr(node *ant_ast.BinaryNode) (*planpb.Expr, error) {
|
2021-04-30 15:19:52 +08:00
|
|
|
op := getLogicalOpType(node.Operator)
|
|
|
|
if op == planpb.BinaryExpr_Invalid {
|
2021-07-25 10:23:19 +08:00
|
|
|
return nil, fmt.Errorf("invalid logical operator(%s)", node.Operator)
|
2021-04-30 15:19:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
leftExpr, err := context.handleExpr(&node.Left)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rightExpr, err := context.handleExpr(&node.Right)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
expr := &planpb.Expr{
|
|
|
|
Expr: &planpb.Expr_BinaryExpr{
|
|
|
|
BinaryExpr: &planpb.BinaryExpr{
|
|
|
|
Op: op,
|
|
|
|
Left: leftExpr,
|
|
|
|
Right: rightExpr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return expr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (context *ParserContext) handleArrayExpr(node *ant_ast.Node, dataType schemapb.DataType) ([]*planpb.GenericValue, error) {
|
|
|
|
arrayNode, ok2 := (*node).(*ant_ast.ArrayNode)
|
|
|
|
if !ok2 {
|
|
|
|
return nil, fmt.Errorf("right operand of the InExpr must be array")
|
|
|
|
}
|
|
|
|
var arr []*planpb.GenericValue
|
|
|
|
for _, element := range arrayNode.Nodes {
|
2021-09-18 14:45:50 +08:00
|
|
|
// use value inside
|
|
|
|
// #nosec G601
|
2021-04-30 15:19:52 +08:00
|
|
|
val, err := context.handleLeafValue(&element, dataType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
arr = append(arr, val)
|
|
|
|
}
|
|
|
|
return arr, nil
|
2021-04-29 16:48:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (context *ParserContext) handleInExpr(node *ant_ast.BinaryNode) (*planpb.Expr, error) {
|
2021-06-11 17:08:25 +08:00
|
|
|
if node.Operator != "in" && node.Operator != "not in" {
|
2021-07-25 10:23:19 +08:00
|
|
|
return nil, fmt.Errorf("invalid operator(%s)", node.Operator)
|
2021-06-11 17:08:25 +08:00
|
|
|
}
|
2021-04-30 15:19:52 +08:00
|
|
|
idNode, ok := node.Left.(*ant_ast.IdentifierNode)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("left operand of the InExpr must be identifier")
|
|
|
|
}
|
|
|
|
field, err := context.handleIdentifier(idNode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
arrayData, err := context.handleArrayExpr(&node.Right, field.DataType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
expr := &planpb.Expr{
|
|
|
|
Expr: &planpb.Expr_TermExpr{
|
|
|
|
TermExpr: &planpb.TermExpr{
|
|
|
|
ColumnInfo: context.createColumnInfo(field),
|
|
|
|
Values: arrayData,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-06-11 17:08:25 +08:00
|
|
|
|
|
|
|
if node.Operator == "not in" {
|
|
|
|
return context.createNotExpr(expr)
|
|
|
|
}
|
2021-04-30 15:19:52 +08:00
|
|
|
return expr, nil
|
2021-04-29 16:48:06 +08:00
|
|
|
}
|
|
|
|
|
2021-07-25 10:23:19 +08:00
|
|
|
func (context *ParserContext) combineUnaryRangeExpr(a, b *planpb.UnaryRangeExpr) *planpb.Expr {
|
|
|
|
if a.Op == planpb.OpType_LessEqual || a.Op == planpb.OpType_LessThan {
|
|
|
|
a, b = b, a
|
|
|
|
}
|
|
|
|
|
|
|
|
lowerInclusive := (a.Op == planpb.OpType_GreaterEqual)
|
|
|
|
upperInclusive := (b.Op == planpb.OpType_LessEqual)
|
|
|
|
|
|
|
|
expr := &planpb.Expr{
|
|
|
|
Expr: &planpb.Expr_BinaryRangeExpr{
|
|
|
|
BinaryRangeExpr: &planpb.BinaryRangeExpr{
|
|
|
|
ColumnInfo: a.ColumnInfo,
|
|
|
|
LowerInclusive: lowerInclusive,
|
|
|
|
UpperInclusive: upperInclusive,
|
|
|
|
LowerValue: a.Value,
|
|
|
|
UpperValue: b.Value,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return expr
|
|
|
|
}
|
|
|
|
|
2021-07-13 14:37:03 +08:00
|
|
|
func (context *ParserContext) handleMultiCmpExpr(node *ant_ast.BinaryNode) (*planpb.Expr, error) {
|
|
|
|
exprs := []*planpb.Expr{}
|
|
|
|
curNode := node
|
|
|
|
|
2021-07-25 10:23:19 +08:00
|
|
|
// handle multiple relational operator
|
2021-07-13 14:37:03 +08:00
|
|
|
for {
|
|
|
|
binNodeLeft, LeftOk := curNode.Left.(*ant_ast.BinaryNode)
|
|
|
|
if !LeftOk {
|
|
|
|
expr, err := context.handleCmpExpr(curNode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
exprs = append(exprs, expr)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if isSameOrder(node.Operator, binNodeLeft.Operator) {
|
|
|
|
expr, err := context.createCmpExpr(binNodeLeft.Right, curNode.Right, curNode.Operator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
exprs = append(exprs, expr)
|
|
|
|
curNode = binNodeLeft
|
2021-07-25 10:23:19 +08:00
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("illegal multi-range expr")
|
2021-07-13 14:37:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-25 10:23:19 +08:00
|
|
|
// combine UnaryRangeExpr to BinaryRangeExpr
|
|
|
|
var lastExpr *planpb.UnaryRangeExpr
|
2021-07-13 14:37:03 +08:00
|
|
|
for i := len(exprs) - 1; i >= 0; i-- {
|
2021-07-25 10:23:19 +08:00
|
|
|
if expr, ok := exprs[i].Expr.(*planpb.Expr_UnaryRangeExpr); ok {
|
|
|
|
if lastExpr != nil && expr.UnaryRangeExpr.ColumnInfo.FieldId == lastExpr.ColumnInfo.FieldId {
|
|
|
|
binaryRangeExpr := context.combineUnaryRangeExpr(expr.UnaryRangeExpr, lastExpr)
|
|
|
|
exprs = append(exprs[0:i], append([]*planpb.Expr{binaryRangeExpr}, exprs[i+2:]...)...)
|
|
|
|
lastExpr = nil
|
|
|
|
} else {
|
|
|
|
lastExpr = expr.UnaryRangeExpr
|
2021-07-13 14:37:03 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lastExpr = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-25 10:23:19 +08:00
|
|
|
// use `&&` to connect exprs
|
2021-07-13 14:37:03 +08:00
|
|
|
combinedExpr := exprs[len(exprs)-1]
|
|
|
|
for i := len(exprs) - 2; i >= 0; i-- {
|
|
|
|
expr := exprs[i]
|
|
|
|
combinedExpr = &planpb.Expr{
|
|
|
|
Expr: &planpb.Expr_BinaryExpr{
|
|
|
|
BinaryExpr: &planpb.BinaryExpr{
|
|
|
|
Op: planpb.BinaryExpr_LogicalAnd,
|
|
|
|
Left: combinedExpr,
|
|
|
|
Right: expr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return combinedExpr, nil
|
|
|
|
}
|
|
|
|
|
2021-04-29 16:48:06 +08:00
|
|
|
func (context *ParserContext) handleBinaryExpr(node *ant_ast.BinaryNode) (*planpb.Expr, error) {
|
|
|
|
switch node.Operator {
|
2021-07-13 14:37:03 +08:00
|
|
|
case "<", "<=", ">", ">=":
|
|
|
|
return context.handleMultiCmpExpr(node)
|
|
|
|
case "==", "!=":
|
2021-04-29 16:48:06 +08:00
|
|
|
return context.handleCmpExpr(node)
|
2021-04-30 15:19:52 +08:00
|
|
|
case "and", "or", "&&", "||":
|
2021-04-29 16:48:06 +08:00
|
|
|
return context.handleLogicalExpr(node)
|
|
|
|
case "in", "not in":
|
|
|
|
return context.handleInExpr(node)
|
|
|
|
}
|
2021-04-30 15:19:52 +08:00
|
|
|
return nil, fmt.Errorf("unsupported binary operator %s", node.Operator)
|
2021-04-29 16:48:06 +08:00
|
|
|
}
|
|
|
|
|
2021-04-30 15:19:52 +08:00
|
|
|
func (context *ParserContext) createNotExpr(childExpr *planpb.Expr) (*planpb.Expr, error) {
|
|
|
|
expr := &planpb.Expr{
|
|
|
|
Expr: &planpb.Expr_UnaryExpr{
|
|
|
|
UnaryExpr: &planpb.UnaryExpr{
|
|
|
|
Op: planpb.UnaryExpr_Not,
|
|
|
|
Child: childExpr,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return expr, nil
|
2021-04-29 16:48:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (context *ParserContext) handleLeafValue(nodeRaw *ant_ast.Node, dataType schemapb.DataType) (gv *planpb.GenericValue, err error) {
|
|
|
|
switch node := (*nodeRaw).(type) {
|
|
|
|
case *ant_ast.FloatNode:
|
|
|
|
if typeutil.IsFloatingType(dataType) {
|
|
|
|
gv = &planpb.GenericValue{
|
|
|
|
Val: &planpb.GenericValue_FloatVal{
|
|
|
|
FloatVal: node.Value,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("type mismatch")
|
|
|
|
}
|
|
|
|
case *ant_ast.IntegerNode:
|
|
|
|
if typeutil.IsFloatingType(dataType) {
|
|
|
|
gv = &planpb.GenericValue{
|
|
|
|
Val: &planpb.GenericValue_FloatVal{
|
|
|
|
FloatVal: float64(node.Value),
|
|
|
|
},
|
|
|
|
}
|
2021-09-02 22:18:10 +08:00
|
|
|
} else if typeutil.IsIntegerType(dataType) {
|
2021-04-29 16:48:06 +08:00
|
|
|
gv = &planpb.GenericValue{
|
|
|
|
Val: &planpb.GenericValue_Int64Val{
|
|
|
|
Int64Val: int64(node.Value),
|
|
|
|
},
|
|
|
|
}
|
2021-09-14 16:15:47 +08:00
|
|
|
} else if dataType == schemapb.DataType_Bool {
|
|
|
|
gv = &planpb.GenericValue{
|
|
|
|
Val: &planpb.GenericValue_BoolVal{},
|
|
|
|
}
|
|
|
|
if node.Value == 1 {
|
|
|
|
gv.Val.(*planpb.GenericValue_BoolVal).BoolVal = true
|
|
|
|
} else {
|
|
|
|
gv.Val.(*planpb.GenericValue_BoolVal).BoolVal = false
|
|
|
|
}
|
2021-04-29 16:48:06 +08:00
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("type mismatch")
|
|
|
|
}
|
|
|
|
case *ant_ast.BoolNode:
|
2021-09-14 16:15:47 +08:00
|
|
|
if typeutil.IsBoolType(dataType) {
|
2021-04-29 16:48:06 +08:00
|
|
|
gv = &planpb.GenericValue{
|
|
|
|
Val: &planpb.GenericValue_BoolVal{
|
|
|
|
BoolVal: node.Value,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("type mismatch")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported leaf node")
|
|
|
|
}
|
|
|
|
|
|
|
|
return gv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (context *ParserContext) handleIdentifier(node *ant_ast.IdentifierNode) (*schemapb.FieldSchema, error) {
|
|
|
|
fieldName := node.Value
|
|
|
|
field, err := context.schema.GetFieldFromName(fieldName)
|
|
|
|
return field, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (context *ParserContext) handleUnaryExpr(node *ant_ast.UnaryNode) (*planpb.Expr, error) {
|
|
|
|
switch node.Operator {
|
|
|
|
case "!", "not":
|
2021-04-30 15:19:52 +08:00
|
|
|
subExpr, err := context.handleExpr(&node.Node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return context.createNotExpr(subExpr)
|
2021-04-29 16:48:06 +08:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid unary operator(%s)", node.Operator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 15:19:52 +08:00
|
|
|
func (context *ParserContext) handleExpr(nodeRaw *ant_ast.Node) (*planpb.Expr, error) {
|
2021-04-29 16:48:06 +08:00
|
|
|
switch node := (*nodeRaw).(type) {
|
|
|
|
case *ant_ast.IdentifierNode,
|
|
|
|
*ant_ast.FloatNode,
|
|
|
|
*ant_ast.IntegerNode,
|
|
|
|
*ant_ast.BoolNode:
|
|
|
|
return nil, fmt.Errorf("scalar expr is not supported yet")
|
|
|
|
case *ant_ast.UnaryNode:
|
|
|
|
expr, err := context.handleUnaryExpr(node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return expr, nil
|
|
|
|
case *ant_ast.BinaryNode:
|
|
|
|
return context.handleBinaryExpr(node)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported node (%s)", node.Type().String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 14:57:34 +08:00
|
|
|
func CreateQueryPlan(schemaPb *schemapb.CollectionSchema, exprStr string, vectorFieldName string, queryInfo *planpb.QueryInfo) (*planpb.PlanNode, error) {
|
2021-04-29 16:48:06 +08:00
|
|
|
schema, err := typeutil.CreateSchemaHelper(schemaPb)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-05 14:57:34 +08:00
|
|
|
expr, err := parseQueryExpr(schema, exprStr)
|
2021-04-29 16:48:06 +08:00
|
|
|
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
|
|
|
|
}
|
2021-09-03 17:12:55 +08:00
|
|
|
|
|
|
|
func CreateExprQueryPlan(schemaPb *schemapb.CollectionSchema, exprStr string) (*planpb.PlanNode, error) {
|
|
|
|
schema, err := typeutil.CreateSchemaHelper(schemaPb)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
expr, err := parseQueryExpr(schema, exprStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
planNode := &planpb.PlanNode{
|
|
|
|
Node: &planpb.PlanNode_Predicates{
|
|
|
|
Predicates: expr,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return planNode, nil
|
|
|
|
}
|