Spaces:
Running
Running
File size: 10,129 Bytes
b110593 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package schema
import (
"context"
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/schema"
"github.com/weaviate/weaviate/usecases/config"
)
func (m *Manager) validateClassNameUniqueness(name string) error {
pred := func(c *models.Class) bool {
return strings.EqualFold(name, c.Class)
}
existingName := ""
m.schemaCache.RLockGuard(func() error {
if cls := m.schemaCache.unsafeFindClassIf(pred); cls != nil {
existingName = cls.Class
}
return nil
})
if existingName == "" {
return nil
}
if name != existingName {
// It's a permutation
return fmt.Errorf(
"class name %q already exists as a permutation of: %q. class names must be unique when lowercased",
name, existingName)
}
return fmt.Errorf("class name %q already exists", name)
}
// Check that the format of the name is correct
func (m *Manager) validateClassName(ctx context.Context, className string) error {
_, err := schema.ValidateClassName(className)
return err
}
func (m *Manager) validatePropertyTokenization(tokenization string, propertyDataType schema.PropertyDataType) error {
if propertyDataType.IsPrimitive() {
primitiveDataType := propertyDataType.AsPrimitive()
switch primitiveDataType {
case schema.DataTypeString, schema.DataTypeStringArray:
// deprecated as of v1.19, will be migrated to DataTypeText/DataTypeTextArray
switch tokenization {
case models.PropertyTokenizationField, models.PropertyTokenizationWord:
return nil
}
case schema.DataTypeText, schema.DataTypeTextArray:
switch tokenization {
case models.PropertyTokenizationField, models.PropertyTokenizationWord,
models.PropertyTokenizationWhitespace, models.PropertyTokenizationLowercase:
return nil
}
default:
if tokenization == "" {
return nil
}
return fmt.Errorf("Tokenization is not allowed for data type '%s'", primitiveDataType)
}
return fmt.Errorf("Tokenization '%s' is not allowed for data type '%s'", tokenization, primitiveDataType)
}
if tokenization == "" {
return nil
}
if propertyDataType.IsNested() {
return fmt.Errorf("Tokenization is not allowed for object/object[] data types")
}
return fmt.Errorf("Tokenization is not allowed for reference data type")
}
func (m *Manager) validatePropertyIndexing(prop *models.Property) error {
if prop.IndexInverted != nil {
if prop.IndexFilterable != nil || prop.IndexSearchable != nil {
return fmt.Errorf("`indexInverted` is deprecated and can not be set together with `indexFilterable` or `indexSearchable`")
}
}
primitiveDataType, isPrimitive := schema.AsPrimitive(prop.DataType)
// TODO nested - should not be allowed for blobs (verify backward compat)
// if prop.IndexFilterable != nil {
// if isPrimitive && primitiveDataType == schema.DataTypeBlob {
// return fmt.Errorf("`indexFilterable` is not allowed for blob data type")
// }
// }
if prop.IndexSearchable != nil {
validateSet := true
if isPrimitive {
switch primitiveDataType {
case schema.DataTypeString, schema.DataTypeStringArray:
// string/string[] are migrated to text/text[] later,
// at this point they are still valid data types, therefore should be handled here
// true or false allowed
validateSet = false
case schema.DataTypeText, schema.DataTypeTextArray:
// true or false allowed
validateSet = false
default:
// do nothing
}
}
if validateSet && *prop.IndexSearchable {
return fmt.Errorf("`indexSearchable` is not allowed for other than text/text[] data types")
}
}
return nil
}
type validatorNestedProperty func(property *models.NestedProperty,
primitiveDataType, nestedDataType schema.DataType,
isPrimitive, isNested bool, propNamePrefix string) error
var validatorsNestedProperty = []validatorNestedProperty{
validateNestedPropertyName,
validateNestedPropertyDataType,
validateNestedPropertyTokenization,
validateNestedPropertyIndexFilterable,
validateNestedPropertyIndexSearchable,
}
func validateNestedProperties(properties []*models.NestedProperty, propNamePrefix string) error {
if len(properties) == 0 {
return fmt.Errorf("Property '%s': At least one nested property is required for data type object/object[]",
propNamePrefix)
}
for _, property := range properties {
primitiveDataType, isPrimitive := schema.AsPrimitive(property.DataType)
nestedDataType, isNested := schema.AsNested(property.DataType)
for _, validator := range validatorsNestedProperty {
if err := validator(property, primitiveDataType, nestedDataType, isPrimitive, isNested, propNamePrefix); err != nil {
return err
}
}
if isNested {
if err := validateNestedProperties(property.NestedProperties, propNamePrefix+"."+property.Name); err != nil {
return err
}
}
}
return nil
}
func validateNestedPropertyName(property *models.NestedProperty,
_, _ schema.DataType,
_, _ bool, propNamePrefix string,
) error {
return schema.ValidateNestedPropertyName(property.Name, propNamePrefix)
}
func validateNestedPropertyDataType(property *models.NestedProperty,
primitiveDataType, _ schema.DataType,
isPrimitive, isNested bool, propNamePrefix string,
) error {
propName := propNamePrefix + "." + property.Name
if isPrimitive {
// DataTypeString and DataTypeStringArray as deprecated since 1.19 are not allowed
switch primitiveDataType {
case schema.DataTypeString, schema.DataTypeStringArray:
return fmt.Errorf("Property '%s': data type '%s' is deprecated and not allowed as nested property", propName, primitiveDataType)
case schema.DataTypeGeoCoordinates, schema.DataTypePhoneNumber:
return fmt.Errorf("Property '%s': data type '%s' not allowed as nested property", propName, primitiveDataType)
default:
// do nothing
}
return nil
}
if isNested {
return nil
}
return fmt.Errorf("Property '%s': reference data type not allowed", propName)
}
// Tokenization allowed only for text/text[] data types
func validateNestedPropertyTokenization(property *models.NestedProperty,
primitiveDataType, _ schema.DataType,
isPrimitive, isNested bool, propNamePrefix string,
) error {
propName := propNamePrefix + "." + property.Name
if isPrimitive {
switch primitiveDataType {
case schema.DataTypeText, schema.DataTypeTextArray:
switch property.Tokenization {
case models.PropertyTokenizationField, models.PropertyTokenizationWord,
models.PropertyTokenizationWhitespace, models.PropertyTokenizationLowercase:
return nil
}
return fmt.Errorf("Property '%s': Tokenization '%s' is not allowed for data type '%s'",
propName, property.Tokenization, primitiveDataType)
default:
if property.Tokenization == "" {
return nil
}
return fmt.Errorf("Property '%s': Tokenization is not allowed for data type '%s'",
propName, primitiveDataType)
}
}
if property.Tokenization == "" {
return nil
}
if isNested {
return fmt.Errorf("Property '%s': Tokenization is not allowed for object/object[] data types", propName)
}
return fmt.Errorf("Property '%s': Tokenization is not allowed for reference data type", propName)
}
// indexFilterable allowed for primitive & ref data types
func validateNestedPropertyIndexFilterable(property *models.NestedProperty,
primitiveDataType, _ schema.DataType,
isPrimitive, _ bool, propNamePrefix string,
) error {
propName := propNamePrefix + "." + property.Name
// at this point indexSearchable should be set (either by user or by defaults)
if property.IndexFilterable == nil {
return fmt.Errorf("Property '%s': `indexFilterable` not set", propName)
}
if isPrimitive && primitiveDataType == schema.DataTypeBlob {
if *property.IndexFilterable {
return fmt.Errorf("Property: '%s': indexFilterable is not allowed for blob data type",
propName)
}
}
return nil
}
// indexSearchable allowed for text/text[] data types
func validateNestedPropertyIndexSearchable(property *models.NestedProperty,
primitiveDataType, _ schema.DataType,
isPrimitive, _ bool, propNamePrefix string,
) error {
propName := propNamePrefix + "." + property.Name
// at this point indexSearchable should be set (either by user or by defaults)
if property.IndexSearchable == nil {
return fmt.Errorf("Property '%s': `indexSearchable` not set", propName)
}
if isPrimitive {
switch primitiveDataType {
case schema.DataTypeText, schema.DataTypeTextArray:
return nil
default:
// do nothing
}
}
if *property.IndexSearchable {
return fmt.Errorf("Property '%s': `indexSearchable` is not allowed for other than text/text[] data types",
propName)
}
return nil
}
func (m *Manager) validateVectorSettings(ctx context.Context, class *models.Class) error {
if err := m.validateVectorizer(ctx, class); err != nil {
return err
}
if err := m.validateVectorIndex(ctx, class); err != nil {
return err
}
return nil
}
func (m *Manager) validateVectorizer(ctx context.Context, class *models.Class) error {
if class.Vectorizer == config.VectorizerModuleNone {
return nil
}
if err := m.vectorizerValidator.ValidateVectorizer(class.Vectorizer); err != nil {
return errors.Wrap(err, "vectorizer")
}
return nil
}
func (m *Manager) validateVectorIndex(ctx context.Context, class *models.Class) error {
switch class.VectorIndexType {
case "hnsw":
return nil
case "flat":
return nil
default:
return errors.Errorf("unrecognized or unsupported vectorIndexType %q",
class.VectorIndexType)
}
}
|