milvus/internal/util/indexparamcheck/inverted_checker.go
Bingyi Sun a7cff3873b
fix: Verify json_cast_type (#40604)
issue: https://github.com/milvus-io/milvus/issues/40420

---------

Signed-off-by: sunby <sunbingyi1992@gmail.com>
2025-03-14 16:48:26 +08:00

56 lines
2.0 KiB
Go

package indexparamcheck
import (
"fmt"
"strconv"
"github.com/samber/lo"
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
"github.com/milvus-io/milvus/pkg/v2/common"
"github.com/milvus-io/milvus/pkg/v2/util/merr"
"github.com/milvus-io/milvus/pkg/v2/util/typeutil"
)
// INVERTEDChecker checks if a INVERTED index can be built.
type INVERTEDChecker struct {
scalarIndexChecker
}
var validJSONCastTypes = []int{int(schemapb.DataType_Bool), int(schemapb.DataType_Int8), int(schemapb.DataType_Int16), int(schemapb.DataType_Int32), int(schemapb.DataType_Int64), int(schemapb.DataType_Float), int(schemapb.DataType_Double), int(schemapb.DataType_String), int(schemapb.DataType_VarChar)}
func (c *INVERTEDChecker) CheckTrain(dataType schemapb.DataType, params map[string]string) error {
// check json index params
isJSONIndex := typeutil.IsJSONType(dataType)
if isJSONIndex {
castType, exist := params[common.JSONCastTypeKey]
if !exist {
return merr.WrapErrParameterMissing(common.JSONCastTypeKey, "json index must specify cast type")
}
castTypeInt, err := strconv.Atoi(castType)
if err != nil {
return merr.WrapErrParameterInvalid(common.JSONCastTypeKey, "json_cast_type must be DataType")
}
if !lo.Contains(validJSONCastTypes, castTypeInt) {
return merr.WrapErrParameterInvalidMsg("json_cast_type %v is not supported", castType)
}
if _, exist := params[common.JSONPathKey]; !exist {
return merr.WrapErrParameterMissing(common.JSONPathKey, "json index must specify json path")
}
}
return c.scalarIndexChecker.CheckTrain(dataType, params)
}
func (c *INVERTEDChecker) CheckValidDataType(indexType IndexType, field *schemapb.FieldSchema) error {
dType := field.GetDataType()
if !typeutil.IsBoolType(dType) && !typeutil.IsArithmetic(dType) && !typeutil.IsStringType(dType) &&
!typeutil.IsArrayType(dType) && !typeutil.IsJSONType(dType) {
return fmt.Errorf("INVERTED are not supported on %s field", dType.String())
}
return nil
}
func newINVERTEDChecker() *INVERTEDChecker {
return &INVERTEDChecker{}
}