2024-10-31 06:20:22 +00:00
|
|
|
package planparserv2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FillExpressionValueSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFillExpressionValue(t *testing.T) {
|
|
|
|
suite.Run(t, new(FillExpressionValueSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type testcase struct {
|
|
|
|
expr string
|
|
|
|
values map[string]*schemapb.TemplateValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FillExpressionValueSuite) assertValidExpr(helper *typeutil.SchemaHelper, exprStr string, templateValues map[string]*schemapb.TemplateValue) {
|
|
|
|
expr, err := ParseExpr(helper, exprStr, templateValues)
|
|
|
|
s.NoError(err, exprStr)
|
|
|
|
s.NotNil(expr, exprStr)
|
|
|
|
ShowExpr(expr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FillExpressionValueSuite) assertInvalidExpr(helper *typeutil.SchemaHelper, exprStr string, templateValues map[string]*schemapb.TemplateValue) {
|
|
|
|
expr, err := ParseExpr(helper, exprStr, templateValues)
|
|
|
|
s.Error(err, exprStr)
|
|
|
|
s.Nil(expr, exprStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FillExpressionValueSuite) TestTermExpr() {
|
|
|
|
s.Run("normal case", func() {
|
|
|
|
testcases := []testcase{
|
|
|
|
{`Int64Field in {age}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"age": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Int64, []int64{int64(1), int64(2), int64(3), int64(4)})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`FloatField in {age}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"age": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Double, []float64{1.1, 2.2, 3.3, 4.4})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`A in {list}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"list": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_JSON, [][]byte{
|
|
|
|
generateJSONData(int64(1)),
|
|
|
|
generateJSONData(2.2),
|
|
|
|
generateJSONData("abc"),
|
|
|
|
generateJSONData(false),
|
|
|
|
})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
2024-11-07 08:28:25 +00:00
|
|
|
|
2024-10-31 06:20:22 +00:00
|
|
|
{`ArrayField in {list}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"list": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Array,
|
|
|
|
[]*schemapb.TemplateArrayValue{
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Int64, []int64{int64(1), int64(2), int64(3)}),
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Int64, []int64{int64(4), int64(5), int64(6)}),
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Int64, []int64{int64(7), int64(8), int64(9)}),
|
|
|
|
})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`ArrayField[0] in {list}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"list": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Int64, []int64{int64(1), int64(2), int64(3)})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
schemaH := newTestSchemaHelper(s.T())
|
|
|
|
for _, c := range testcases {
|
|
|
|
s.assertValidExpr(schemaH, c.expr, c.values)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Run("failed case", func() {
|
|
|
|
testcases := []testcase{
|
|
|
|
{`Int64Field in {age}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"age": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_String, []string{"abc", "def"})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`StringField in {list}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"list": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_JSON, [][]byte{
|
|
|
|
generateJSONData(int64(1)),
|
|
|
|
generateJSONData("abc"),
|
|
|
|
generateJSONData(2.2),
|
|
|
|
generateJSONData(false),
|
|
|
|
})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{"ArrayField[0] in {list}", map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"list": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_JSON, [][]byte{
|
|
|
|
generateJSONData(int64(1)),
|
|
|
|
generateJSONData("abc"),
|
|
|
|
generateJSONData(3.2),
|
|
|
|
})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{"Int64Field not in {not_list}", map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"not_list": generateTemplateValue(schemapb.DataType_Int64, int64(33)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{"Int64Field not in {not_list}", map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"age": generateTemplateValue(schemapb.DataType_Int64, int64(33)),
|
|
|
|
}},
|
|
|
|
{`Int64Field in {empty_list}`, map[string]*schemapb.TemplateValue{
|
|
|
|
"empty_list": generateTemplateValue(schemapb.DataType_Array, &schemapb.TemplateArrayValue{}),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
}
|
2024-11-07 08:28:25 +00:00
|
|
|
|
2024-10-31 06:20:22 +00:00
|
|
|
schemaH := newTestSchemaHelper(s.T())
|
|
|
|
for _, c := range testcases {
|
|
|
|
s.assertInvalidExpr(schemaH, c.expr, c.values)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FillExpressionValueSuite) TestUnaryRange() {
|
|
|
|
s.Run("normal case", func() {
|
|
|
|
testcases := []testcase{
|
|
|
|
{`Int64Field == 10`, nil},
|
|
|
|
{`Int64Field > {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"target": generateTemplateValue(schemapb.DataType_Int64, int64(11)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`FloatField < {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"target": generateTemplateValue(schemapb.DataType_Double, float64(12.3)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`DoubleField != {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"target": generateTemplateValue(schemapb.DataType_Double, float64(3.5)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`ArrayField[0] >= {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"target": generateTemplateValue(schemapb.DataType_Int64, int64(3)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`BoolField == {bool}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"bool": generateTemplateValue(schemapb.DataType_Bool, false),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{str} != StringField`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"str": generateTemplateValue(schemapb.DataType_String, "abc"),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{target} > Int64Field`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"target": generateTemplateValue(schemapb.DataType_Int64, int64(11)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
}
|
2024-11-07 08:28:25 +00:00
|
|
|
|
2024-10-31 06:20:22 +00:00
|
|
|
schemaH := newTestSchemaHelper(s.T())
|
|
|
|
for _, c := range testcases {
|
|
|
|
s.assertValidExpr(schemaH, c.expr, c.values)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Run("failed case", func() {
|
|
|
|
testcases := []testcase{
|
|
|
|
{`Int64Field == 10.5`, nil},
|
|
|
|
{`Int64Field > {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"target": generateTemplateValue(schemapb.DataType_Double, 11.2),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`FloatField < {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"target": generateTemplateValue(schemapb.DataType_String, "abc"),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`DoubleField != {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"target": generateTemplateValue(schemapb.DataType_Bool, false),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`ArrayField[0] >= {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"target": generateTemplateValue(schemapb.DataType_Double, 3.5),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`BoolField == {bool}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"bool": generateTemplateValue(schemapb.DataType_String, "abc"),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{str} != StringField`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"str": generateTemplateValue(schemapb.DataType_Int64, int64(5)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{int} != StringField`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"int": generateTemplateValue(schemapb.DataType_Int64, int64(5)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{str} != StringField`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"int": generateTemplateValue(schemapb.DataType_Int64, int64(5)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
schemaH := newTestSchemaHelper(s.T())
|
|
|
|
for _, c := range testcases {
|
|
|
|
s.assertInvalidExpr(schemaH, c.expr, c.values)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FillExpressionValueSuite) TestBinaryRange() {
|
|
|
|
s.Run("normal case", func() {
|
|
|
|
testcases := []testcase{
|
|
|
|
{`10 < Int64Field < 20`, nil},
|
|
|
|
{`{max} > Int64Field > {min}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"min": generateTemplateValue(schemapb.DataType_Int64, int64(11)),
|
|
|
|
"max": generateTemplateValue(schemapb.DataType_Int64, int64(22)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{min} <= FloatField <= {max}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"min": generateTemplateValue(schemapb.DataType_Float, float64(11)),
|
|
|
|
"max": generateTemplateValue(schemapb.DataType_Float, float64(22)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{min} < DoubleField < {max}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"min": generateTemplateValue(schemapb.DataType_Double, float64(11)),
|
|
|
|
"max": generateTemplateValue(schemapb.DataType_Double, float64(22)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{max} >= ArrayField[0] >= {min}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"min": generateTemplateValue(schemapb.DataType_Int64, int64(11)),
|
|
|
|
"max": generateTemplateValue(schemapb.DataType_Int64, int64(22)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{max} > Int64Field >= 10`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"max": generateTemplateValue(schemapb.DataType_Int64, int64(22)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`30 >= Int64Field > {min}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"min": generateTemplateValue(schemapb.DataType_Int64, int64(11)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`10 < Int64Field <= {max}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"max": generateTemplateValue(schemapb.DataType_Int64, int64(22)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{min} <= Int64Field < 20`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"min": generateTemplateValue(schemapb.DataType_Int64, int64(11)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaH := newTestSchemaHelper(s.T())
|
|
|
|
for _, c := range testcases {
|
|
|
|
s.assertValidExpr(schemaH, c.expr, c.values)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Run("failed case", func() {
|
|
|
|
testcases := []testcase{
|
|
|
|
{`10 < Int64Field < 20.5`, nil},
|
|
|
|
{`{max} > Int64Field > {min}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"min": generateTemplateValue(schemapb.DataType_Int64, int64(11)),
|
|
|
|
"max": generateTemplateValue(schemapb.DataType_Double, 22.5),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{min} <= FloatField <= {max}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"min": generateTemplateValue(schemapb.DataType_String, "abc"),
|
|
|
|
"max": generateTemplateValue(schemapb.DataType_Int64, int64(11)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{min} < DoubleField < {max}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"min": generateTemplateValue(schemapb.DataType_Int64, int64(33)),
|
|
|
|
"max": generateTemplateValue(schemapb.DataType_Int64, int64(22)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{max} >= ArrayField[0] >= {min}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"min": generateTemplateValue(schemapb.DataType_Double, 11.5),
|
|
|
|
"max": generateTemplateValue(schemapb.DataType_Int64, int64(22)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{max} >= Int64Field >= {min}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"max": generateTemplateValue(schemapb.DataType_Int64, int64(22)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{max} > Int64Field`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"max": generateTemplateValue(schemapb.DataType_Bool, false),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{$meta} > Int64Field`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"$meta": generateTemplateValue(schemapb.DataType_Int64, int64(22)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaH := newTestSchemaHelper(s.T())
|
|
|
|
for _, c := range testcases {
|
|
|
|
s.assertInvalidExpr(schemaH, c.expr, c.values)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FillExpressionValueSuite) TestBinaryArithOpEvalRange() {
|
|
|
|
s.Run("normal case", func() {
|
|
|
|
testcases := []testcase{
|
|
|
|
{`Int64Field + 5.5 == 10.5`, nil},
|
|
|
|
{`Int64Field - {offset} >= {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"offset": generateTemplateValue(schemapb.DataType_Double, 3.5),
|
|
|
|
"target": generateTemplateValue(schemapb.DataType_Double, 11.5),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`Int64Field * 3.5 <= {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"target": generateTemplateValue(schemapb.DataType_Double, 11.5),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`Int64Field / {offset} > 11.5`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"offset": generateTemplateValue(schemapb.DataType_Double, 3.5),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`ArrayField[0] % {offset} < 11`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"offset": generateTemplateValue(schemapb.DataType_Int64, int64(3)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`array_length(ArrayField) == {length}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"length": generateTemplateValue(schemapb.DataType_Int64, int64(3)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`array_length(ArrayField) > {length}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"length": generateTemplateValue(schemapb.DataType_Int64, int64(3)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaH := newTestSchemaHelper(s.T())
|
|
|
|
for _, c := range testcases {
|
|
|
|
s.assertValidExpr(schemaH, c.expr, c.values)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Run("failed case", func() {
|
|
|
|
testcases := []testcase{
|
|
|
|
{`Int64Field + 6 == 12.5`, nil},
|
|
|
|
{`Int64Field - {offset} == {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"offset": generateTemplateValue(schemapb.DataType_Int64, int64(4)),
|
|
|
|
"target": generateTemplateValue(schemapb.DataType_Double, 13.5),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`Int64Field * 6 == {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"target": generateTemplateValue(schemapb.DataType_Double, 13.5),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`Int64Field / {offset} == 11.5`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"offset": generateTemplateValue(schemapb.DataType_Int64, int64(6)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`Int64Field % {offset} < 11`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"offset": generateTemplateValue(schemapb.DataType_Double, 3.5),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`Int64Field + {offset} < {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"target": generateTemplateValue(schemapb.DataType_Double, 3.5),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`Int64Field + {offset} < {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"offset": generateTemplateValue(schemapb.DataType_String, "abc"),
|
|
|
|
"target": generateTemplateValue(schemapb.DataType_Int64, int64(15)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`Int64Field + {offset} < {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"offset": generateTemplateValue(schemapb.DataType_Int64, int64(15)),
|
|
|
|
"target": generateTemplateValue(schemapb.DataType_String, "def"),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`ArrayField + {offset} < {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"offset": generateTemplateValue(schemapb.DataType_Double, 3.5),
|
|
|
|
"target": generateTemplateValue(schemapb.DataType_Int64, int64(5)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`ArrayField[0] + {offset} < {target}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"offset": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Int64, []int64{int64(1), int64(2), int64(3)})),
|
|
|
|
"target": generateTemplateValue(schemapb.DataType_Int64, int64(5)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`array_length(ArrayField) == {length}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"length": generateTemplateValue(schemapb.DataType_String, "abc"),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaH := newTestSchemaHelper(s.T())
|
|
|
|
for _, c := range testcases {
|
|
|
|
s.assertInvalidExpr(schemaH, c.expr, c.values)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FillExpressionValueSuite) TestJSONContainsExpression() {
|
|
|
|
s.Run("normal case", func() {
|
|
|
|
testcases := []testcase{
|
|
|
|
{`json_contains(A, 5)`, nil},
|
|
|
|
{`json_contains(A, {age})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"age": generateTemplateValue(schemapb.DataType_Int64, int64(18)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains(A, {str})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"str": generateTemplateValue(schemapb.DataType_String, "abc"),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains(A, {bool})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"bool": generateTemplateValue(schemapb.DataType_Bool, false),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains_any(JSONField, {array})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"array": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_JSON, [][]byte{
|
|
|
|
generateJSONData(int64(1)),
|
|
|
|
generateJSONData("abc"),
|
|
|
|
generateJSONData(2.2),
|
|
|
|
generateJSONData(false),
|
|
|
|
})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains_any(JSONField, {array})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"array": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Int64, []int64{int64(1), int64(2), int64(3), int64(4)})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains_any(JSONField["A"], {array})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"array": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Int64, []int64{int64(1), int64(2), int64(3), int64(4)})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains_all(JSONField["A"], {array})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"array": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_JSON, [][]byte{
|
|
|
|
generateJSONData(int64(1)),
|
|
|
|
generateJSONData("abc"),
|
|
|
|
generateJSONData(2.2),
|
|
|
|
generateJSONData(false),
|
|
|
|
})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains_all(JSONField["A"], {array})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"array": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Array, []*schemapb.TemplateArrayValue{
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Int64, []int64{int64(1), int64(2), int64(3)}),
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Int64, []int64{int64(4), int64(5), int64(6)}),
|
|
|
|
})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains(ArrayField, {int})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"int": generateTemplateValue(schemapb.DataType_Int64, int64(1)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains_any(ArrayField, {list})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"list": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Int64, []int64{int64(1), int64(2), int64(3), int64(4)})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaH := newTestSchemaHelper(s.T())
|
|
|
|
|
|
|
|
for _, c := range testcases {
|
|
|
|
s.assertValidExpr(schemaH, c.expr, c.values)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Run("failed case", func() {
|
|
|
|
testcases := []testcase{
|
|
|
|
{`json_contains(ArrayField[0], {str})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"str": generateTemplateValue(schemapb.DataType_String, "abc"),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains_any(JSONField, {not_array})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"not_array": generateTemplateValue(schemapb.DataType_Int64, int64(1)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains_all(JSONField, {not_array})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"not_array": generateTemplateValue(schemapb.DataType_Int64, int64(1)),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains_all(JSONField, {not_array})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"array": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Int64, []int64{int64(1), int64(2), int64(3)})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains_all(ArrayField, {array})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"array": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_JSON, [][]byte{
|
|
|
|
generateJSONData(int64(1)),
|
|
|
|
generateJSONData("abc"),
|
|
|
|
generateJSONData(2.2),
|
|
|
|
generateJSONData(false),
|
|
|
|
})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains(ArrayField, {array})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"array": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_Int64, []int64{int64(1), int64(2), int64(3)})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`json_contains_any(ArrayField, {array})`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"array": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_JSON, [][]byte{
|
|
|
|
generateJSONData(int64(1)),
|
|
|
|
generateJSONData("abc"),
|
|
|
|
generateJSONData(2.2),
|
|
|
|
generateJSONData(false),
|
|
|
|
})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaH := newTestSchemaHelper(s.T())
|
|
|
|
|
|
|
|
for _, c := range testcases {
|
|
|
|
s.assertInvalidExpr(schemaH, c.expr, c.values)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FillExpressionValueSuite) TestBinaryExpression() {
|
|
|
|
s.Run("normal case", func() {
|
|
|
|
testcases := []testcase{
|
|
|
|
{`Int64Field > {int} && StringField in {list}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"int": generateTemplateValue(schemapb.DataType_Int64, int64(10)),
|
|
|
|
"list": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_String, []string{"abc", "def", "ghi"})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{max} > FloatField >= {min} or BoolField == {bool}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"min": generateTemplateValue(schemapb.DataType_Int64, int64(10)),
|
|
|
|
"max": generateTemplateValue(schemapb.DataType_Float, 22.22),
|
|
|
|
"bool": generateTemplateValue(schemapb.DataType_Bool, true),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaH := newTestSchemaHelper(s.T())
|
|
|
|
|
|
|
|
for _, c := range testcases {
|
|
|
|
s.assertValidExpr(schemaH, c.expr, c.values)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Run("failed case", func() {
|
|
|
|
testcases := []testcase{
|
|
|
|
{`Int64Field > {int} && StringField in {list}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"int": generateTemplateValue(schemapb.DataType_String, "abc"),
|
|
|
|
"list": generateTemplateValue(schemapb.DataType_Array,
|
|
|
|
generateTemplateArrayValue(schemapb.DataType_JSON, [][]byte{
|
|
|
|
generateJSONData("abc"),
|
|
|
|
generateJSONData(int64(10)),
|
|
|
|
generateJSONData("ghi"),
|
|
|
|
})),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
{`{max} > FloatField >= {min} or BoolField == {bool}`, map[string]*schemapb.TemplateValue{
|
2024-11-07 08:28:25 +00:00
|
|
|
"min": generateTemplateValue(schemapb.DataType_Int64, int64(10)),
|
|
|
|
"bool": generateTemplateValue(schemapb.DataType_Bool, true),
|
2024-10-31 06:20:22 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaH := newTestSchemaHelper(s.T())
|
|
|
|
|
|
|
|
for _, c := range testcases {
|
|
|
|
s.assertInvalidExpr(schemaH, c.expr, c.values)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|