Skip to content

Commit c1be102

Browse files
authored
feat: [2.5]The expression supports filling elements through templates (#37280)
issue: #36672 master pr: #37033 milvus-proto pr: milvus-io/milvus-proto#332 Signed-off-by: Cai Zhang <[email protected]>
1 parent aa2878b commit c1be102

32 files changed

+2636
-1155
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require (
2323
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
2424
github.com/klauspost/compress v1.17.9
2525
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
26-
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.4-0.20240930043709-0c23514e4c34
26+
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.4-0.20241025031121-4d5c88b00cf7
2727
github.com/minio/minio-go/v7 v7.0.73
2828
github.com/pingcap/log v1.1.1-0.20221015072633-39906604fb81
2929
github.com/prometheus/client_golang v1.14.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,8 @@ github.com/milvus-io/gorocksdb v0.0.0-20220624081344-8c5f4212846b h1:TfeY0NxYxZz
629629
github.com/milvus-io/gorocksdb v0.0.0-20220624081344-8c5f4212846b/go.mod h1:iwW+9cWfIzzDseEBCCeDSN5SD16Tidvy8cwQ7ZY8Qj4=
630630
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.4-0.20240930043709-0c23514e4c34 h1:Fwxpg98128gfWRbQ1A3PMP9o2IfYZk7RSEy8rcoCWDA=
631631
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.4-0.20240930043709-0c23514e4c34/go.mod h1:/6UT4zZl6awVeXLeE7UGDWZvXj3IWkRsh3mqsn0DiAs=
632+
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.4-0.20241025031121-4d5c88b00cf7 h1:HwAitQk+V59QdYUwwVVYHTujd4QZrebg2Cc2hmcjhAg=
633+
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.4-0.20241025031121-4d5c88b00cf7/go.mod h1:/6UT4zZl6awVeXLeE7UGDWZvXj3IWkRsh3mqsn0DiAs=
632634
github.com/milvus-io/pulsar-client-go v0.12.1 h1:O2JZp1tsYiO7C0MQ4hrUY/aJXnn2Gry6hpm7UodghmE=
633635
github.com/milvus-io/pulsar-client-go v0.12.1/go.mod h1:dkutuH4oS2pXiGm+Ti7fQZ4MRjrMPZ8IJeEGAWMeckk=
634636
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 h1:AMFGa4R4MiIpspGNG7Z948v4n35fFGB3RR3G/ry4FWs=

internal/parser/planparserv2/Plan.g4

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ expr:
55
| FloatingConstant # Floating
66
| BooleanConstant # Boolean
77
| StringLiteral # String
8-
| Identifier # Identifier
8+
| (Identifier|Meta) # Identifier
99
| JSONIdentifier # JSONIdentifier
10+
| LBRACE Identifier RBRACE # TemplateVariable
1011
| '(' expr ')' # Parens
1112
| '[' expr (',' expr)* ','? ']' # Array
1213
| EmptyArray # EmptyArray
@@ -19,7 +20,6 @@ expr:
1920
| expr op = (ADD | SUB) expr # AddSub
2021
| expr op = (SHL | SHR) expr # Shift
2122
| expr op = NOT? IN expr # Term
22-
// | EmptyTerm # EmptyTerm
2323
| (JSONContains | ArrayContains)'('expr',' expr')' # JSONContains
2424
| (JSONContainsAll | ArrayContainsAll)'('expr',' expr')' # JSONContainsAll
2525
| (JSONContainsAny | ArrayContainsAny)'('expr',' expr')' # JSONContainsAny
@@ -45,6 +45,8 @@ expr:
4545
// INT64: 'int64';
4646
// FLOAT: 'float';
4747
// DOUBLE: 'double';
48+
LBRACE: '{';
49+
RBRACE: '}';
4850

4951
LT: '<';
5052
LE: '<=';
@@ -99,10 +101,11 @@ FloatingConstant:
99101
DecimalFloatingConstant
100102
| HexadecimalFloatingConstant;
101103

102-
Identifier: Nondigit (Nondigit | Digit)* | '$meta';
104+
Identifier: Nondigit (Nondigit | Digit)*;
105+
Meta: '$meta';
103106

104107
StringLiteral: EncodingPrefix? ('"' DoubleSCharSequence? '"' | '\'' SingleSCharSequence? '\'');
105-
JSONIdentifier: Identifier('[' (StringLiteral | DecimalConstant) ']')+;
108+
JSONIdentifier: (Identifier | Meta)('[' (StringLiteral | DecimalConstant) ']')+;
106109

107110
fragment EncodingPrefix: 'u8' | 'u' | 'U' | 'L';
108111

internal/parser/planparserv2/check_identical_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ func TestCheckIdentical(t *testing.T) {
2626
exprStr1 := exprStr1Arr[i]
2727
exprStr2 := exprStr2Arr[i]
2828

29-
expr1, err := ParseExpr(helper, exprStr1)
29+
expr1, err := ParseExpr(helper, exprStr1, nil)
3030
assert.NoError(t, err)
31-
expr2, err := ParseExpr(helper, exprStr2)
31+
expr2, err := ParseExpr(helper, exprStr2, nil)
3232
assert.NoError(t, err)
3333

3434
assert.True(t, CheckPredicatesIdentical(expr1, expr1))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package planparserv2
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
7+
"github.com/milvus-io/milvus/internal/proto/planpb"
8+
)
9+
10+
func ConvertToGenericValue(templateName string, templateValue *schemapb.TemplateValue) (*planpb.GenericValue, error) {
11+
if templateValue == nil {
12+
return nil, fmt.Errorf("expression template variable value is nil, template name: {%s}", templateName)
13+
}
14+
switch templateValue.GetType() {
15+
case schemapb.DataType_Bool:
16+
return &planpb.GenericValue{
17+
Val: &planpb.GenericValue_BoolVal{
18+
BoolVal: templateValue.GetBoolVal(),
19+
},
20+
}, nil
21+
case schemapb.DataType_Int8, schemapb.DataType_Int16, schemapb.DataType_Int32, schemapb.DataType_Int64:
22+
return &planpb.GenericValue{
23+
Val: &planpb.GenericValue_Int64Val{
24+
Int64Val: templateValue.GetInt64Val(),
25+
},
26+
}, nil
27+
case schemapb.DataType_Float, schemapb.DataType_Double:
28+
return &planpb.GenericValue{
29+
Val: &planpb.GenericValue_FloatVal{
30+
FloatVal: templateValue.GetFloatVal(),
31+
},
32+
}, nil
33+
case schemapb.DataType_String, schemapb.DataType_VarChar:
34+
return &planpb.GenericValue{
35+
Val: &planpb.GenericValue_StringVal{
36+
StringVal: templateValue.GetStringVal(),
37+
},
38+
}, nil
39+
case schemapb.DataType_Array:
40+
elements := templateValue.GetArrayVal().GetArray()
41+
arrayValues := make([]*planpb.GenericValue, len(elements))
42+
for i, element := range elements {
43+
arrayElement, err := ConvertToGenericValue(templateName, element)
44+
if err != nil {
45+
return nil, err
46+
}
47+
arrayValues[i] = arrayElement
48+
}
49+
return &planpb.GenericValue{
50+
Val: &planpb.GenericValue_ArrayVal{
51+
ArrayVal: &planpb.Array{
52+
Array: arrayValues,
53+
SameType: templateValue.GetArrayVal().GetSameType(),
54+
ElementType: templateValue.GetArrayVal().GetElementType(),
55+
},
56+
},
57+
}, nil
58+
default:
59+
return nil, fmt.Errorf("expression elements can only be scalars")
60+
}
61+
}
62+
63+
func UnmarshalExpressionValues(input map[string]*schemapb.TemplateValue) (map[string]*planpb.GenericValue, error) {
64+
result := make(map[string]*planpb.GenericValue, len(input))
65+
for name, value := range input {
66+
rv, err := ConvertToGenericValue(name, value)
67+
if err != nil {
68+
return nil, err
69+
}
70+
result[name] = rv
71+
}
72+
return result, nil
73+
}

0 commit comments

Comments
 (0)