Search in sources :

Example 6 with ObjectType

use of io.crate.types.ObjectType in project crate by crate.

the class DataTypeTest method test_is_compatible_type_on_nested_types_with_different_names_same_inner_types.

@Test
public void test_is_compatible_type_on_nested_types_with_different_names_same_inner_types() {
    ObjectType obj1 = ObjectType.builder().setInnerType("obj1", ObjectType.builder().setInnerType("a", DataTypes.INTEGER).build()).build();
    ObjectType obj2 = ObjectType.builder().setInnerType("obj2", ObjectType.builder().setInnerType("a", DataTypes.INTEGER).build()).build();
    assertThat(DataTypes.isCompatibleType(obj1, obj2), is(true));
    ObjectType obj3 = ObjectType.builder().setInnerType("obj", ObjectType.builder().setInnerType("a", DataTypes.INTEGER).build()).build();
    ObjectType obj4 = ObjectType.builder().setInnerType("obj", ObjectType.builder().setInnerType("b", DataTypes.INTEGER).build()).build();
    assertThat(DataTypes.isCompatibleType(obj3, obj4), is(true));
}
Also used : ObjectType(io.crate.types.ObjectType) Test(org.junit.Test)

Example 7 with ObjectType

use of io.crate.types.ObjectType in project crate by crate.

the class DataTypeTest method test_is_compatible_type_with_different_names_same_inner_types.

@Test
public void test_is_compatible_type_with_different_names_same_inner_types() {
    ObjectType obj1 = ObjectType.builder().setInnerType("a", DataTypes.STRING).build();
    ObjectType obj2 = ObjectType.builder().setInnerType("b", DataTypes.STRING).build();
    assertThat(DataTypes.isCompatibleType(obj1, obj2), is(true));
}
Also used : ObjectType(io.crate.types.ObjectType) Test(org.junit.Test)

Example 8 with ObjectType

use of io.crate.types.ObjectType in project crate by crate.

the class Literal method typeMatchesValue.

private static <T> boolean typeMatchesValue(DataType<T> type, T value) {
    if (value == null) {
        return true;
    }
    if (type.id() == ObjectType.ID) {
        // noinspection unchecked
        Map<String, Object> mapValue = (Map<String, Object>) value;
        ObjectType objectType = ((ObjectType) type);
        for (String key : mapValue.keySet()) {
            DataType<?> innerType = objectType.innerType(key);
            // noinspection unchecked
            if (typeMatchesValue((DataType<Object>) innerType, mapValue.get(key)) == false) {
                return false;
            }
        }
        // lets do the expensive "deep" map value conversion only after everything else succeeded
        Map<String, Object> safeValue = objectType.sanitizeValue(value);
        return safeValue.size() == mapValue.size();
    }
    return Objects.equals(type.sanitizeValue(value), value);
}
Also used : ObjectType(io.crate.types.ObjectType) DataType(io.crate.types.DataType) Map(java.util.Map)

Example 9 with ObjectType

use of io.crate.types.ObjectType in project crate by crate.

the class DocIndexMetadataTest method test_resolve_inner_object_types.

@Test
public void test_resolve_inner_object_types() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject(Constants.DEFAULT_MAPPING_TYPE).startObject("properties").startObject("object").field("type", "object").startObject("properties").startObject("nestedObject").field("type", "object").startObject("properties").startObject("nestedNestedString").field("type", "string").endObject().endObject().endObject().startObject("nestedString").field("type", "string").endObject().endObject().endObject().endObject().endObject().endObject();
    IndexMetadata metadata = getIndexMetadata("test", builder);
    DocIndexMetadata md = newMeta(metadata, "test");
    ObjectType objectType = (ObjectType) md.references().get(new ColumnIdent("object")).valueType();
    assertThat(objectType.resolveInnerType(List.of("nestedString")), is(DataTypes.STRING));
    assertThat(objectType.resolveInnerType(List.of("nestedObject")).id(), is(ObjectType.ID));
    assertThat(objectType.resolveInnerType(List.of("nestedObject", "nestedNestedString")), is(DataTypes.STRING));
}
Also used : ObjectType(io.crate.types.ObjectType) ColumnIdent(io.crate.metadata.ColumnIdent) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

ObjectType (io.crate.types.ObjectType)9 Test (org.junit.Test)7 Function (io.crate.expression.symbol.Function)1 ColumnIdent (io.crate.metadata.ColumnIdent)1 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)1 DataType (io.crate.types.DataType)1 Map (java.util.Map)1 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1