Skip to content

Commit

Permalink
Refine varchar length exceeds max length error message
Browse files Browse the repository at this point in the history
Signed-off-by: aoiasd <[email protected]>
  • Loading branch information
aoiasd committed Mar 29, 2024
1 parent b99c462 commit 374b154
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
21 changes: 13 additions & 8 deletions internal/proxy/validate_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,12 @@ func (v *validateUtil) checkVarCharFieldData(field *schemapb.FieldData, fieldSch
if err != nil {
return err
}
return verifyLengthPerRow(strArr, maxLength)

if i, ok := verifyLengthPerRow(strArr, maxLength); !ok {
return merr.WrapErrParameterInvalidMsg("the length (%d) of %dth VarChar %s exceeds max length (%d)",
len(strArr[i]), i, fieldSchema.GetName(), i, maxLength)
}
return nil
}

return nil
Expand Down Expand Up @@ -538,24 +543,24 @@ func (v *validateUtil) checkArrayFieldData(field *schemapb.FieldData, fieldSchem
if err != nil {
return err
}
for _, row := range data.GetData() {
if err := verifyLengthPerRow(row.GetStringData().GetData(), maxLength); err != nil {
return err
for rowCnt, row := range data.GetData() {
if i, ok := verifyLengthPerRow(row.GetStringData().GetData(), maxLength); !ok {
return merr.WrapErrParameterInvalidMsg("the length (%d) of %dth %s %s[%d] exceeds max length (%d)",
len(row.GetStringData().GetData()[i]), rowCnt, fieldSchema.GetDataType().String(), fieldSchema.GetName(), i, maxLength)
}
}
}
return v.checkArrayElement(data, fieldSchema)
}

func verifyLengthPerRow[E interface{ ~string | ~[]byte }](strArr []E, maxLength int64) error {
func verifyLengthPerRow[E interface{ ~string | ~[]byte }](strArr []E, maxLength int64) (int, bool) {
for i, s := range strArr {
if int64(len(s)) > maxLength {
msg := fmt.Sprintf("the length (%d) of %dth string exceeds max length (%d)", len(s), i, maxLength)
return merr.WrapErrParameterInvalid("valid length string", "string length exceeds max length", msg)
return i, false
}
}

return nil
return 0, true
}

func verifyCapacityPerRow(arrayArray []*schemapb.ScalarField, maxCapacity int64, elementType schemapb.DataType) error {
Expand Down
18 changes: 13 additions & 5 deletions internal/proxy/validate_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ import (
func Test_verifyLengthPerRow(t *testing.T) {
maxLength := 16

assert.NoError(t, verifyLengthPerRow[string](nil, int64(maxLength)))
_, ok := verifyLengthPerRow[string](nil, int64(maxLength))
assert.True(t, ok)

assert.NoError(t, verifyLengthPerRow([]string{"111111", "22222"}, int64(maxLength)))
_, ok = verifyLengthPerRow[string]([]string{"111111", "22222"}, int64(maxLength))
assert.True(t, ok)

assert.Error(t, verifyLengthPerRow([]string{"11111111111111111"}, int64(maxLength)))
row, ok := verifyLengthPerRow[string]([]string{"11111111111111111"}, int64(maxLength))
assert.True(t, ok)
assert.Equal(t, 1, row)

assert.Error(t, verifyLengthPerRow([]string{"11111111111111111", "222"}, int64(maxLength)))
row, ok = verifyLengthPerRow[string]([]string{"11111111111111111", "222"}, int64(maxLength))
assert.True(t, ok)
assert.Equal(t, 1, row)

assert.Error(t, verifyLengthPerRow([]string{"11111", "22222222222222222"}, int64(maxLength)))
row, ok = verifyLengthPerRow[string]([]string{"11111", "22222222222222222"}, int64(maxLength))
assert.True(t, ok)
assert.Equal(t, 2, row)
}

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

0 comments on commit 374b154

Please sign in to comment.