aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/application/validation/ComplexFieldsWithStructFieldAttributesValidator.java
blob: c7ae8f4f4a3c780511c0952d7bbaa091ddebe040 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.application.validation;

import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.schema.Schema;
import com.yahoo.schema.derived.SchemaInfo;
import com.yahoo.schema.document.ComplexAttributeFieldUtils;
import com.yahoo.schema.document.GeoPos;
import com.yahoo.schema.document.ImmutableSDField;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.search.SearchCluster;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.stream.Collectors;

/**
 * Validates that complex fields (of type struct or map) that have struct field attributes are supported.
 *
 * Only applies for indexed search clusters.
 *
 * @author geirst
 */
public class ComplexFieldsWithStructFieldAttributesValidator extends Validator {

    @Override
    public void validate(VespaModel model, DeployState deployState) {
        List<SearchCluster> searchClusters = model.getSearchClusters();
        for (SearchCluster cluster : searchClusters) {
            if (cluster.isStreaming()) continue;

            for (SchemaInfo spec : cluster.schemas().values()) {
                validateComplexFields(cluster.getClusterName(), spec.fullSchema(), deployState.getDeployLogger());
            }
        }
    }

    private static void validateComplexFields(String clusterName, Schema schema, DeployLogger logger) {
        String unsupportedFields = validateComplexFields(schema);
        if (!unsupportedFields.isEmpty()) {
            throw new IllegalArgumentException(getErrorMessage(clusterName, schema, unsupportedFields));
        }
    }

    private static String validateComplexFields(Schema schema) {
        return schema.allFields()
                .filter(field -> isUnsupportedComplexField(field))
                .map(ComplexFieldsWithStructFieldAttributesValidator::toString)
                .collect(Collectors.joining(", "));
    }

    private static String getErrorMessage(String clusterName, Schema schema, String unsupportedFields) {
        return String.format("For cluster '%s', search '%s': The following complex fields do not support using struct field attributes: %s. " +
                             "Only supported for the following complex field types: array or map of struct with primitive types, map of primitive types. " +
                             "The supported primitive types are: byte, int, long, float, double and string",
                             clusterName, schema.getName(), unsupportedFields);
    }

    private static boolean isUnsupportedComplexField(ImmutableSDField field) {
        return (field.usesStructOrMap() &&
                !isSupportedComplexField(field) &&
                hasStructFieldAttributes(field.getStructFields()));
    }

    private static boolean isSupportedComplexField(ImmutableSDField field) {
        return (ComplexAttributeFieldUtils.isSupportedComplexField(field) ||
                GeoPos.isAnyPos(field));
    }

    private static String toString(ImmutableSDField field) {
        return field.getName() + " (" + String.join(", ", getStructFieldAttributes(field.getStructFields(), false)) + ")";
    }

    private static boolean hasStructFieldAttributes(Collection<? extends ImmutableSDField> structFields) {
        return !getStructFieldAttributes(structFields, true).isEmpty();
    }

    private static List<String> getStructFieldAttributes(Collection<? extends ImmutableSDField> structFields,
                                                         boolean returnAllTypes) {
        var result = new ArrayList<String>();
        for (var structField : structFields) {
            for (var attr : structField.getAttributes().values()) {
                if (returnAllTypes || !ComplexAttributeFieldUtils.isPrimitiveType(attr)) {
                    result.add(attr.getName());
                }
            }
            if (structField.usesStructOrMap() && structField.wasConfiguredToDoAttributing()) {
                result.add(structField.getName());
            }
            // If we encounter struct field attributes underneath this level, those are not supported and should be returned.
            result.addAll(getStructFieldAttributes(structField.getStructFields(), true));
        }
        return result;
    }
}