Skip to content

Commit

Permalink
fix: not check json data when set nullable or default_value (#36575)
Browse files Browse the repository at this point in the history
#36354

Signed-off-by: lixinguo <[email protected]>
Co-authored-by: lixinguo <[email protected]>
  • Loading branch information
smellthemoon and lixinguo authored Sep 29, 2024
1 parent 9c1772f commit a78a6b3
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/proxy/validate_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ func (v *validateUtil) checkVarCharFieldData(field *schemapb.FieldData, fieldSch

func (v *validateUtil) checkJSONFieldData(field *schemapb.FieldData, fieldSchema *schemapb.FieldSchema) error {
jsonArray := field.GetScalars().GetJsonData().GetData()
if jsonArray == nil {
if jsonArray == nil && fieldSchema.GetDefaultValue() == nil && !fieldSchema.GetNullable() {
msg := fmt.Sprintf("json field '%v' is illegal, array type mismatch", field.GetFieldName())
return merr.WrapErrParameterInvalid("need json array", "got nil", msg)
}
Expand Down
211 changes: 211 additions & 0 deletions internal/proxy/validate_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6112,6 +6112,50 @@ func Test_validateUtil_checkIntegerFieldData(t *testing.T) {
}}, nil))
})

t.Run("no int data but set nullable==true or set default_value", func(t *testing.T) {
v := newValidateUtil(withOverflowCheck())

f := &schemapb.FieldSchema{
DataType: schemapb.DataType_Int8,
Nullable: true,
}

data := &schemapb.FieldData{
Field: &schemapb.FieldData_Scalars{
Scalars: &schemapb.ScalarField{
Data: &schemapb.ScalarField_IntData{
IntData: &schemapb.IntArray{
Data: nil,
},
},
},
},
}

err := v.checkIntegerFieldData(data, f)
assert.NoError(t, err)

f = &schemapb.FieldSchema{
DataType: schemapb.DataType_Int8,
DefaultValue: &schemapb.ValueField{},
}

data = &schemapb.FieldData{
Field: &schemapb.FieldData_Scalars{
Scalars: &schemapb.ScalarField{
Data: &schemapb.ScalarField_IntData{
IntData: &schemapb.IntArray{
Data: nil,
},
},
},
},
}

err = v.checkIntegerFieldData(data, f)
assert.NoError(t, err)
})

t.Run("tiny int, type mismatch", func(t *testing.T) {
v := newValidateUtil(withOverflowCheck())

Expand Down Expand Up @@ -6242,6 +6286,48 @@ func Test_validateUtil_checkJSONData(t *testing.T) {
assert.Error(t, err)
})

t.Run("no json data but set nullable==true or set default_value", func(t *testing.T) {
v := newValidateUtil(withOverflowCheck())

f := &schemapb.FieldSchema{
DataType: schemapb.DataType_JSON,
Nullable: true,
}
data := &schemapb.FieldData{
Field: &schemapb.FieldData_Scalars{
Scalars: &schemapb.ScalarField{
Data: &schemapb.ScalarField_JsonData{
JsonData: &schemapb.JSONArray{
Data: nil,
},
},
},
},
}

err := v.checkJSONFieldData(data, f)
assert.NoError(t, err)

f = &schemapb.FieldSchema{
DataType: schemapb.DataType_JSON,
DefaultValue: &schemapb.ValueField{},
}
data = &schemapb.FieldData{
Field: &schemapb.FieldData_Scalars{
Scalars: &schemapb.ScalarField{
Data: &schemapb.ScalarField_JsonData{
JsonData: &schemapb.JSONArray{
Data: nil,
},
},
},
},
}

err = v.checkJSONFieldData(data, f)
assert.NoError(t, err)
})

t.Run("json string exceed max length", func(t *testing.T) {
v := newValidateUtil(withOverflowCheck(), withMaxLenCheck())
jsonString := ""
Expand Down Expand Up @@ -6338,6 +6424,49 @@ func Test_validateUtil_checkLongFieldData(t *testing.T) {
},
},
}, nil))

t.Run("no long data but set nullable==true or set default_value", func(t *testing.T) {
v := newValidateUtil(withOverflowCheck())

f := &schemapb.FieldSchema{
DataType: schemapb.DataType_Int64,
Nullable: true,
}

data := &schemapb.FieldData{
Field: &schemapb.FieldData_Scalars{
Scalars: &schemapb.ScalarField{
Data: &schemapb.ScalarField_LongData{
LongData: &schemapb.LongArray{
Data: nil,
},
},
},
},
}

err := v.checkLongFieldData(data, f)
assert.NoError(t, err)

f = &schemapb.FieldSchema{
DataType: schemapb.DataType_Int64,
DefaultValue: &schemapb.ValueField{},
}

data = &schemapb.FieldData{
Field: &schemapb.FieldData_Scalars{
Scalars: &schemapb.ScalarField{
Data: &schemapb.ScalarField_LongData{
LongData: &schemapb.LongArray{
Data: nil,
},
},
},
},
}
err = v.checkLongFieldData(data, f)
assert.NoError(t, err)
})
}

func Test_validateUtil_checkFloatFieldData(t *testing.T) {
Expand Down Expand Up @@ -6367,6 +6496,47 @@ func Test_validateUtil_checkFloatFieldData(t *testing.T) {
},
},
}, nil))

t.Run("no float data but set nullable==true or set default_value", func(t *testing.T) {
v := newValidateUtil(withOverflowCheck())

f := &schemapb.FieldSchema{
Nullable: true,
}

data := &schemapb.FieldData{
Field: &schemapb.FieldData_Scalars{
Scalars: &schemapb.ScalarField{
Data: &schemapb.ScalarField_FloatData{
FloatData: &schemapb.FloatArray{
Data: nil,
},
},
},
},
}

err := v.checkFloatFieldData(data, f)
assert.NoError(t, err)

f = &schemapb.FieldSchema{
DefaultValue: &schemapb.ValueField{},
}

data = &schemapb.FieldData{
Field: &schemapb.FieldData_Scalars{
Scalars: &schemapb.ScalarField{
Data: &schemapb.ScalarField_FloatData{
FloatData: &schemapb.FloatArray{
Data: nil,
},
},
},
},
}
err = v.checkFloatFieldData(data, f)
assert.NoError(t, err)
})
}

func Test_validateUtil_checkDoubleFieldData(t *testing.T) {
Expand Down Expand Up @@ -6396,6 +6566,47 @@ func Test_validateUtil_checkDoubleFieldData(t *testing.T) {
},
},
}, nil))

t.Run("no float data but set nullable==true or set default_value", func(t *testing.T) {
v := newValidateUtil(withOverflowCheck())

f := &schemapb.FieldSchema{
Nullable: true,
}

data := &schemapb.FieldData{
Field: &schemapb.FieldData_Scalars{
Scalars: &schemapb.ScalarField{
Data: &schemapb.ScalarField_DoubleData{
DoubleData: &schemapb.DoubleArray{
Data: nil,
},
},
},
},
}

err := v.checkDoubleFieldData(data, f)
assert.NoError(t, err)

f = &schemapb.FieldSchema{
DefaultValue: &schemapb.ValueField{},
}

data = &schemapb.FieldData{
Field: &schemapb.FieldData_Scalars{
Scalars: &schemapb.ScalarField{
Data: &schemapb.ScalarField_DoubleData{
DoubleData: &schemapb.DoubleArray{
Data: nil,
},
},
},
},
}
err = v.checkDoubleFieldData(data, f)
assert.NoError(t, err)
})
}

func TestCheckArrayElementNilData(t *testing.T) {
Expand Down

0 comments on commit a78a6b3

Please sign in to comment.