aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/schema/derived/AttributeListTestCase.java
blob: f21999df94c370c10548cce9d7abd424592abc7c (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
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.derived;

import com.yahoo.schema.Schema;
import com.yahoo.schema.ApplicationBuilder;
import com.yahoo.schema.AbstractSchemaTestCase;
import com.yahoo.schema.document.Attribute;
import com.yahoo.schema.parser.ParseException;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Iterator;

import static com.yahoo.config.model.test.TestUtil.joinLines;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
 * Tests attribute deriving
 *
 * @author bratseth
 */
public class AttributeListTestCase extends AbstractSchemaTestCase {

    @Test
    void testDeriving() throws IOException, ParseException {
        // Test attribute importing
        Schema schema = ApplicationBuilder.buildFromFile("src/test/examples/simple.sd");

        // Test attribute deriving
        AttributeFields attributeFields = new AttributeFields(schema);
        Iterator attributes = attributeFields.attributeIterator();
        Attribute attribute;
        attribute = (Attribute) attributes.next();
        assertEquals("popularity", attribute.getName());
        assertEquals(Attribute.Type.INTEGER, attribute.getType());
        assertEquals(Attribute.CollectionType.SINGLE, attribute.getCollectionType());

        attribute = (Attribute) attributes.next();
        assertEquals("measurement", attribute.getName());
        assertEquals(Attribute.Type.INTEGER, attribute.getType());
        assertEquals(Attribute.CollectionType.SINGLE, attribute.getCollectionType());

        attribute = (Attribute) attributes.next();
        assertEquals("smallattribute", attribute.getName());
        assertEquals(Attribute.Type.BYTE, attribute.getType());
        assertEquals(Attribute.CollectionType.ARRAY, attribute.getCollectionType());

        attribute = (Attribute) attributes.next();
        assertEquals("access", attribute.getName());
        assertEquals(Attribute.Type.BYTE, attribute.getType());
        assertEquals(Attribute.CollectionType.SINGLE, attribute.getCollectionType());

        attribute = (Attribute) attributes.next();
        assertEquals("category_arr", attribute.getName());
        assertEquals(Attribute.Type.STRING, attribute.getType());
        assertEquals(Attribute.CollectionType.ARRAY, attribute.getCollectionType());

        attribute = (Attribute) attributes.next();
        assertEquals("measurement_arr", attribute.getName());
        assertEquals(Attribute.Type.INTEGER, attribute.getType());
        assertEquals(Attribute.CollectionType.ARRAY, attribute.getCollectionType());

        attribute = (Attribute) attributes.next();
        assertEquals("popsiness", attribute.getName());
        assertEquals(Attribute.Type.INTEGER, attribute.getType());
        assertEquals(Attribute.CollectionType.SINGLE, attribute.getCollectionType());

        assertFalse(attributes.hasNext());
    }

    @Test
    void fields_in_array_of_struct_are_derived_into_array_attributes() throws IOException, ParseException {
        Schema schema = ApplicationBuilder.buildFromFile("src/test/derived/array_of_struct_attribute/test.sd");
        Iterator<Attribute> attributes = new AttributeFields(schema).attributeIterator();

        assertAttribute("elem_array.name", Attribute.Type.STRING, Attribute.CollectionType.ARRAY, true, attributes.next());
        assertAttribute("elem_array.weight", Attribute.Type.INTEGER, Attribute.CollectionType.ARRAY, false, attributes.next());
        assertFalse(attributes.hasNext());
    }

    @Test
    void fields_in_map_of_struct_are_derived_into_array_attributes() throws IOException, ParseException {
        Schema schema = ApplicationBuilder.buildFromFile("src/test/derived/map_of_struct_attribute/test.sd");
        Iterator<Attribute> attributes = new AttributeFields(schema).attributeIterator();

        assertAttribute("str_elem_map.key", Attribute.Type.STRING, Attribute.CollectionType.ARRAY, true, attributes.next());
        assertAttribute("str_elem_map.value.name", Attribute.Type.STRING, Attribute.CollectionType.ARRAY, false, attributes.next());
        assertAttribute("str_elem_map.value.weight", Attribute.Type.INTEGER, Attribute.CollectionType.ARRAY, false, attributes.next());
        assertAttribute("int_elem_map.key", Attribute.Type.INTEGER, Attribute.CollectionType.ARRAY, false, attributes.next());
        assertAttribute("int_elem_map.value.name", Attribute.Type.STRING, Attribute.CollectionType.ARRAY, true, attributes.next());
        assertFalse(attributes.hasNext());
    }

    private static void assertAttribute(String name, Attribute.Type type, Attribute.CollectionType collection, boolean isFastSearch, Attribute attr) {
        assertEquals(name, attr.getName());
        assertEquals(type, attr.getType());
        assertEquals(collection, attr.getCollectionType());
        assertEquals(isFastSearch, attr.isFastSearch());
    }

    @Test
    void only_zcurve_attribute_is_derived_from_array_of_position_field() throws ParseException {
        Schema schema = ApplicationBuilder.createFromString(
                joinLines("search test {",
                        "  document test {",
                        "    field pos_array type array<position> {",
                        "      indexing: attribute",
                        "    }",
                        "  }",
                        "}")).getSchema();
        Iterator<Attribute> attributes = new AttributeFields(schema).attributeIterator();

        assertAttribute("pos_array_zcurve", Attribute.Type.LONG, Attribute.CollectionType.ARRAY, true, attributes.next());
        assertFalse(attributes.hasNext());
    }

    @Test
    void fields_in_map_of_primitive_are_derived_into_array_attributes() throws IOException, ParseException {
        Schema schema = ApplicationBuilder.buildFromFile("src/test/derived/map_attribute/test.sd");
        Iterator<Attribute> attributes = new AttributeFields(schema).attributeIterator();

        assertAttribute("str_map.key", Attribute.Type.STRING, Attribute.CollectionType.ARRAY, true, attributes.next());
        assertAttribute("str_map.value", Attribute.Type.STRING, Attribute.CollectionType.ARRAY, false, attributes.next());
        assertAttribute("int_map.key", Attribute.Type.INTEGER, Attribute.CollectionType.ARRAY, false, attributes.next());
        assertFalse(attributes.hasNext());
    }

    @Test
    void bad_map_attribute() throws ParseException {
        run_bad_struct_or_map_attribute("map<string,string>");
    }

    @Test
    void bad_array_of_struct_attribute() throws ParseException {
        run_bad_struct_or_map_attribute("array<s>");
    }

    private void run_bad_struct_or_map_attribute(String type) throws ParseException {
        run_bad_struct_or_map_attribute(type, false, true);
        run_bad_struct_or_map_attribute(type, true, false);
        run_bad_struct_or_map_attribute(type, true, true);
    }

    private void run_bad_struct_or_map_attribute(String type, boolean fs, boolean ilscript) throws ParseException {
        var exception = assertThrows(IllegalArgumentException.class, () -> ApplicationBuilder.createFromString(
                joinLines("search test {",
                        "  document test {",
                        "    struct s {",
                        "       field a type string { }",
                        "    }",
                        "    field metadata type " + type + " {",
                        "      indexing: summary" + (ilscript ? "| attribute" : ""),
                        "      " + (fs ? "attribute: fast-search" : ""),
                        "    }",
                        "  }",
                        "}")).getSchema());
        assertEquals("For schema 'test': Field 'metadata' of type '" + type + "' cannot be an attribute." +
                " Instead specify the struct fields to be searchable as attribute",
                exception.getMessage());
    }

}