From 98d86e2391fe631e8620af80106990eae50beccd Mon Sep 17 00:00:00 2001 From: "cai.zhang" Date: Fri, 19 May 2023 09:41:25 +0800 Subject: [PATCH] Return all dynamic field when retrieve json key (#24205) Signed-off-by: cai.zhang --- internal/proxy/task_test.go | 20 ++++++++++++++++++++ internal/proxy/util.go | 17 +++++++++++++---- tests/integration/json_expr_test.go | 6 +++--- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/internal/proxy/task_test.go b/internal/proxy/task_test.go index cb21c217cd..2ac8d506ed 100644 --- a/internal/proxy/task_test.go +++ b/internal/proxy/task_test.go @@ -432,6 +432,26 @@ func TestTranslateOutputFields(t *testing.T) { outputFields, err = translateOutputFields([]string{"*", floatVectorFieldName}, schema, true) assert.Equal(t, nil, err) assert.ElementsMatch(t, []string{idFieldName, tsFieldName, floatVectorFieldName, binaryVectorFieldName}, outputFields) + + t.Run("enable dynamic schema", func(t *testing.T) { + schema := &schemapb.CollectionSchema{ + Name: "TestTranslateOutputFields", + Description: "TestTranslateOutputFields", + AutoID: false, + EnableDynamicField: true, + Fields: []*schemapb.FieldSchema{ + {Name: idFieldName, DataType: schemapb.DataType_Int64, IsPrimaryKey: true}, + {Name: tsFieldName, DataType: schemapb.DataType_Int64}, + {Name: floatVectorFieldName, DataType: schemapb.DataType_FloatVector}, + {Name: binaryVectorFieldName, DataType: schemapb.DataType_BinaryVector}, + {Name: common.MetaFieldName, DataType: schemapb.DataType_JSON, IsDynamic: true}, + }, + } + + outputFields, err = translateOutputFields([]string{"A", idFieldName}, schema, true) + assert.Equal(t, nil, err) + assert.ElementsMatch(t, []string{common.MetaFieldName, idFieldName}, outputFields) + }) } func TestCreateCollectionTask(t *testing.T) { diff --git a/internal/proxy/util.go b/internal/proxy/util.go index e4ba6c90fa..6671ff2a79 100644 --- a/internal/proxy/util.go +++ b/internal/proxy/util.go @@ -800,7 +800,7 @@ func passwordVerify(ctx context.Context, username, rawPwd string, globalMetaCach // output_fields=["*",C] ==> [A,B,C,D] func translateOutputFields(outputFields []string, schema *schemapb.CollectionSchema, addPrimary bool) ([]string, error) { var primaryFieldName string - allFielNameMap := make(map[string]bool) + allFieldNameMap := make(map[string]bool) resultFieldNameMap := make(map[string]bool) resultFieldNames := make([]string, 0) @@ -808,17 +808,26 @@ func translateOutputFields(outputFields []string, schema *schemapb.CollectionSch if field.IsPrimaryKey { primaryFieldName = field.Name } - allFielNameMap[field.Name] = true + allFieldNameMap[field.Name] = true } for _, outputFieldName := range outputFields { outputFieldName = strings.TrimSpace(outputFieldName) if outputFieldName == "*" { - for fieldName := range allFielNameMap { + for fieldName := range allFieldNameMap { resultFieldNameMap[fieldName] = true } } else { - resultFieldNameMap[outputFieldName] = true + if _, ok := allFieldNameMap[outputFieldName]; ok { + resultFieldNameMap[outputFieldName] = true + } else { + if schema.EnableDynamicField { + resultFieldNameMap[common.MetaFieldName] = true + } else { + return nil, fmt.Errorf("field %s not exist", outputFieldName) + } + } + } } diff --git a/tests/integration/json_expr_test.go b/tests/integration/json_expr_test.go index c022ced62c..2eeef4df4a 100644 --- a/tests/integration/json_expr_test.go +++ b/tests/integration/json_expr_test.go @@ -134,7 +134,7 @@ func checkSearch(t *testing.T, c *MiniCluster, collectionName, fieldName string, assert.Equal(t, schemapb.DataType_JSON, result.Results.FieldsData[0].GetType()) assert.Equal(t, 5, len(result.Results.FieldsData[0].GetScalars().GetJsonData().GetData())) } - doSearch(c, collectionName, []string{fieldName}, expr, dim, t, checkFunc) + doSearch(c, collectionName, []string{"A"}, expr, dim, t, checkFunc) log.Info("GT expression run successfully") expr = `$meta["A"] < 10` @@ -144,7 +144,7 @@ func checkSearch(t *testing.T, c *MiniCluster, collectionName, fieldName string, assert.Equal(t, schemapb.DataType_JSON, result.Results.FieldsData[0].GetType()) assert.Equal(t, 5, len(result.Results.FieldsData[0].GetScalars().GetJsonData().GetData())) } - doSearch(c, collectionName, []string{fieldName}, expr, dim, t, checkFunc) + doSearch(c, collectionName, []string{"B"}, expr, dim, t, checkFunc) log.Info("LT expression run successfully") expr = `$meta["A"] <= 5` @@ -154,7 +154,7 @@ func checkSearch(t *testing.T, c *MiniCluster, collectionName, fieldName string, assert.Equal(t, schemapb.DataType_JSON, result.Results.FieldsData[0].GetType()) assert.Equal(t, 3, len(result.Results.FieldsData[0].GetScalars().GetJsonData().GetData())) } - doSearch(c, collectionName, []string{fieldName}, expr, dim, t, checkFunc) + doSearch(c, collectionName, []string{"C"}, expr, dim, t, checkFunc) log.Info("LE expression run successfully") expr = `A >= 95`