summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/processing/ValidateFieldTypes.java
blob: d9b93d11a527f7100ae341bcd0c20ef3003e5868 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.processing;

import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.document.DataType;
import com.yahoo.document.TensorDataType;
import com.yahoo.searchdefinition.RankProfileRegistry;
import com.yahoo.searchdefinition.Search;
import com.yahoo.searchdefinition.document.Attribute;
import com.yahoo.vespa.documentmodel.DocumentSummary;
import com.yahoo.vespa.documentmodel.SummaryField;
import com.yahoo.vespa.model.container.search.QueryProfiles;

import java.util.HashMap;
import java.util.Map;

/**
 * This Processor checks to make sure all fields with the same name have the same {@link DataType}. This check
 * explicitly disregards whether a field is an index field, an attribute or a summary field. This is a requirement if we
 * hope to move to a model where index fields, attributes and summary fields share a common field class.
 *
 * @author Simon Thoresen
 */
public class ValidateFieldTypes extends Processor {

    public ValidateFieldTypes(Search search, DeployLogger deployLogger, RankProfileRegistry rankProfileRegistry, QueryProfiles queryProfiles) {
        super(search, deployLogger, rankProfileRegistry, queryProfiles);
    }

    @Override
    public void process() {
        String searchName = search.getName();
        Map<String, DataType> seenFields = new HashMap<>();
        search.allFields().forEach(field -> {
            checkFieldType(searchName, "index field", field.getName(), field.getDataType(), seenFields);
            for (Map.Entry<String, Attribute> entry : field.getAttributes().entrySet()) {
                checkFieldType(searchName, "attribute", entry.getKey(), entry.getValue().getDataType(), seenFields);
            }
        });
        for (DocumentSummary summary : search.getSummaries().values()) {
            for (SummaryField field : summary.getSummaryFields()) {
                checkFieldType(searchName, "summary field", field.getName(), field.getDataType(), seenFields);
            }
        }
    }

    private void checkFieldType(String searchName, String fieldDesc, String fieldName, DataType fieldType,
                                Map<String, DataType> seenFields) {
        DataType seenType = seenFields.get(fieldName);
        if (seenType == null) {
            seenFields.put(fieldName, fieldType);
        } else if ( ! compatibleTypes(seenType, fieldType)) {
            throw newProcessException(searchName, fieldName, "Incompatible types. Expected " + 
                                                             seenType.getName() + " for " + fieldDesc +
                                                             " '" + fieldName + "', got " + fieldType.getName() + ".");
        }
    }

    private static boolean compatibleTypes(DataType seenType, DataType fieldType) {
        // legacy tag field type compatibility; probably not needed any more (Oct 2016)
        if ("tag".equals(seenType.getName())) {
            return "tag".equals(fieldType.getName()) || "WeightedSet<string>".equals(fieldType.getName());
        }
        if ("tag".equals(fieldType.getName())) {
            return "tag".equals(seenType.getName()) || "WeightedSet<string>".equals(seenType.getName());
        }
        if (seenType instanceof TensorDataType && fieldType instanceof TensorDataType) {
            return fieldType.isAssignableFrom(seenType); // TODO: Just do this for all types
        }
        return seenType.equals(fieldType);
    }
    
}