enhance: Refine varchar length exceeds max length error message (#31521)

Signed-off-by: aoiasd <zhicheng.yue@zilliz.com>
pull/32166/head^2
aoiasd 2024-04-12 15:01:19 +08:00 committed by GitHub
parent 4617d22482
commit fb376fd1e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 13 deletions

View File

@ -399,7 +399,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
@ -552,24 +557,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 {

View File

@ -3,6 +3,7 @@ package proxy
import (
"fmt"
"math"
"strings"
"testing"
"github.com/stretchr/testify/assert"
@ -17,15 +18,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{strings.Repeat("1", 20)}, int64(maxLength))
assert.False(t, ok)
assert.Equal(t, 0, row)
assert.Error(t, verifyLengthPerRow([]string{"11111111111111111", "222"}, int64(maxLength)))
row, ok = verifyLengthPerRow[string]([]string{strings.Repeat("1", 20), "222"}, int64(maxLength))
assert.False(t, ok)
assert.Equal(t, 0, row)
assert.Error(t, verifyLengthPerRow([]string{"11111", "22222222222222222"}, int64(maxLength)))
row, ok = verifyLengthPerRow[string]([]string{"11111", strings.Repeat("2", 20)}, int64(maxLength))
assert.False(t, ok)
assert.Equal(t, 1, row)
}
func Test_validateUtil_checkVarCharFieldData(t *testing.T) {