From 92a2d608acbc6e7c3bd85edb450d0dc5ae8aa64c Mon Sep 17 00:00:00 2001 From: smellthemoon <64083300+smellthemoon@users.noreply.github.com> Date: Thu, 9 Jan 2025 19:27:03 +0800 Subject: [PATCH] fix: Bulk insert failed when the nullable/default_value field is not exist (#39063) #39036 Signed-off-by: lixinguo Co-authored-by: lixinguo --- internal/util/importutilv2/json/row_parser.go | 12 ++++++++++++ .../util/importutilv2/json/row_parser_test.go | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/internal/util/importutilv2/json/row_parser.go b/internal/util/importutilv2/json/row_parser.go index 391427efbd0f2..326a5270abb55 100644 --- a/internal/util/importutilv2/json/row_parser.go +++ b/internal/util/importutilv2/json/row_parser.go @@ -126,6 +126,18 @@ func (r *rowParser) Parse(raw any) (Row, error) { } } for fieldName, fieldID := range r.name2FieldID { + if _, ok = row[fieldID]; !ok { + if r.id2Field[fieldID].GetNullable() { + row[fieldID] = nil + } + if r.id2Field[fieldID].GetDefaultValue() != nil { + data, err := nullutil.GetDefaultValue(r.id2Field[fieldID]) + if err != nil { + return nil, err + } + row[fieldID] = data + } + } if _, ok = row[fieldID]; !ok { return nil, merr.WrapErrImportFailed(fmt.Sprintf("value of field '%s' is missed", fieldName)) } diff --git a/internal/util/importutilv2/json/row_parser_test.go b/internal/util/importutilv2/json/row_parser_test.go index 96fa48beccec5..89b934224cdc5 100644 --- a/internal/util/importutilv2/json/row_parser_test.go +++ b/internal/util/importutilv2/json/row_parser_test.go @@ -72,6 +72,23 @@ func TestRowParser_Parse_Valid(t *testing.T) { }, }, }, + { + FieldID: 6, + Name: "null_fid", + DataType: schemapb.DataType_VarChar, + Nullable: true, + DefaultValue: &schemapb.ValueField{ + Data: &schemapb.ValueField_StringData{ + StringData: "a", + }, + }, + TypeParams: []*commonpb.KeyValuePair{ + { + Key: "max_length", + Value: "256", + }, + }, + }, }, } r, err := NewRowParser(schema) @@ -185,6 +202,7 @@ func TestRowParser_Parse_Invalid(t *testing.T) { {name: `{"id": 1, "vector": [], "arrayField": [1, 2, 3, 4], "x": 6, "$meta": [], "name": "test"}`, expectErr: "not a JSON object"}, {name: `{"id": 1, "vector": [], "arrayField": [1, 2, 3, 4], "x": 8, "$meta": "{\"y\": 8}", "name": "testName"}`, expectErr: "value length 8 exceeds max_length 4"}, {name: `{"id": 1, "vector": [], "arrayField": [1, 2, 3, 4, 5], "x": 8, "$meta": "{\"z\": 9}", "name": "test"}`, expectErr: "array capacity 5 exceeds max_capacity 4"}, + {name: `{"id": 1, "vector": [], "x": 8, "$meta": "{\"z\": 9}", "name": "test"}`, expectErr: "value of field 'arrayField' is missed"}, } for _, c := range cases {