Search in sources :

Example 6 with MappingField

use of com.hazelcast.sql.impl.schema.MappingField in project hazelcast by hazelcast.

the class MetadataPortableResolver method resolveAndValidateFields.

private static Stream<MappingField> resolveAndValidateFields(boolean isKey, Map<QueryPath, MappingField> userFieldsByPath, @Nullable ClassDefinition clazz) {
    if (clazz == null) {
        // CLassDefinition does not exist, make sure there are no OBJECT fields
        return userFieldsByPath.values().stream().peek(mappingField -> {
            QueryDataType type = mappingField.type();
            if (type == QueryDataType.OBJECT) {
                throw QueryException.error("Cannot derive Portable type for '" + type.getTypeFamily() + "'");
            }
        });
    }
    for (String name : clazz.getFieldNames()) {
        QueryPath path = new QueryPath(name, isKey);
        QueryDataType type = resolvePortableType(clazz.getFieldType(name));
        MappingField userField = userFieldsByPath.get(path);
        if (userField != null && !type.getTypeFamily().equals(userField.type().getTypeFamily())) {
            throw QueryException.error("Mismatch between declared and resolved type: " + userField.name());
        }
    }
    return userFieldsByPath.values().stream();
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) MappingField(com.hazelcast.sql.impl.schema.MappingField)

Example 7 with MappingField

use of com.hazelcast.sql.impl.schema.MappingField in project hazelcast by hazelcast.

the class MappingColumnsTable method rows.

@Override
protected List<Object[]> rows() {
    List<Object[]> rows = new ArrayList<>(mappings.size());
    for (Mapping mapping : mappings) {
        List<MappingField> fields = mapping.fields();
        for (int i = 0; i < fields.size(); i++) {
            MappingField field = fields.get(i);
            Object[] row = new Object[] { catalog(), schema, mapping.name(), field.name(), field.externalName(), i + 1, String.valueOf(true), field.type().getTypeFamily().name() };
            rows.add(row);
        }
    }
    for (View view : views) {
        for (int i = 0; i < view.viewColumnNames().size(); i++) {
            Object[] row = new Object[] { catalog(), schema, view.name(), view.viewColumnNames().get(i), null, i + 1, String.valueOf(true), view.viewColumnTypes().get(i).getTypeFamily().name() };
            rows.add(row);
        }
    }
    return rows;
}
Also used : ArrayList(java.util.ArrayList) Mapping(com.hazelcast.sql.impl.schema.Mapping) MappingField(com.hazelcast.sql.impl.schema.MappingField) View(com.hazelcast.sql.impl.schema.view.View)

Example 8 with MappingField

use of com.hazelcast.sql.impl.schema.MappingField in project hazelcast by hazelcast.

the class KvMetadataAvroResolver method resolveMetadata.

@Override
public KvMetadata resolveMetadata(boolean isKey, List<MappingField> resolvedFields, Map<String, String> options, InternalSerializationService serializationService) {
    Map<QueryPath, MappingField> fieldsByPath = extractFields(resolvedFields, isKey);
    List<TableField> fields = new ArrayList<>();
    for (Entry<QueryPath, MappingField> entry : fieldsByPath.entrySet()) {
        QueryPath path = entry.getKey();
        QueryDataType type = entry.getValue().type();
        String name = entry.getValue().name();
        fields.add(new MapTableField(name, type, false, path));
    }
    maybeAddDefaultField(isKey, resolvedFields, fields, QueryDataType.OBJECT);
    return new KvMetadata(fields, AvroQueryTargetDescriptor.INSTANCE, new AvroUpsertTargetDescriptor(schema(fields).toString()));
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) ArrayList(java.util.ArrayList) AvroUpsertTargetDescriptor(com.hazelcast.jet.sql.impl.inject.AvroUpsertTargetDescriptor) MappingField(com.hazelcast.sql.impl.schema.MappingField) TableField(com.hazelcast.sql.impl.schema.TableField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField)

Example 9 with MappingField

use of com.hazelcast.sql.impl.schema.MappingField in project hazelcast by hazelcast.

the class KvMetadataJavaResolver method resolveAndValidateObjectFields.

private Stream<MappingField> resolveAndValidateObjectFields(boolean isKey, List<MappingField> userFields, Class<?> clazz) {
    Map<QueryPath, MappingField> userFieldsByPath = extractFields(userFields, isKey);
    for (Entry<String, Class<?>> classField : FieldsUtil.resolveClass(clazz).entrySet()) {
        QueryPath path = new QueryPath(classField.getKey(), isKey);
        QueryDataType type = QueryDataTypeUtils.resolveTypeForClass(classField.getValue());
        MappingField userField = userFieldsByPath.get(path);
        if (userField != null && !type.getTypeFamily().equals(userField.type().getTypeFamily())) {
            throw QueryException.error("Mismatch between declared and resolved type for field '" + userField.name() + "'. Declared: " + userField.type().getTypeFamily() + ", resolved: " + type.getTypeFamily());
        }
    }
    return userFieldsByPath.values().stream();
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) MappingField(com.hazelcast.sql.impl.schema.MappingField)

Example 10 with MappingField

use of com.hazelcast.sql.impl.schema.MappingField in project hazelcast by hazelcast.

the class KvMetadataJavaResolver method resolveObjectFields.

private Stream<MappingField> resolveObjectFields(boolean isKey, Class<?> clazz) {
    Map<String, Class<?>> fieldsInClass = FieldsUtil.resolveClass(clazz);
    if (fieldsInClass.isEmpty()) {
        // we didn't find any non-object fields in the class, map the whole value (e.g. in java.lang.Object)
        String name = isKey ? KEY : VALUE;
        return Stream.of(new MappingField(name, QueryDataType.OBJECT, name));
    }
    return fieldsInClass.entrySet().stream().map(classField -> {
        QueryPath path = new QueryPath(classField.getKey(), isKey);
        QueryDataType type = QueryDataTypeUtils.resolveTypeForClass(classField.getValue());
        String name = classField.getKey();
        return new MappingField(name, type, path.toString());
    });
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) MappingField(com.hazelcast.sql.impl.schema.MappingField)

Aggregations

MappingField (com.hazelcast.sql.impl.schema.MappingField)52 Test (org.junit.Test)23 QueryPath (com.hazelcast.sql.impl.extract.QueryPath)19 QueryDataType (com.hazelcast.sql.impl.type.QueryDataType)18 ArrayList (java.util.ArrayList)14 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)10 Mapping (com.hazelcast.sql.impl.schema.Mapping)10 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 Parameters (junitparams.Parameters)8 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)7 TableField (com.hazelcast.sql.impl.schema.TableField)7 MapTableField (com.hazelcast.sql.impl.schema.map.MapTableField)7 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)6 LinkedHashMap (java.util.LinkedHashMap)6 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)4 KvMetadata (com.hazelcast.jet.sql.impl.connector.keyvalue.KvMetadata)3 QueryException (com.hazelcast.sql.impl.QueryException)3 SerializationConfig (com.hazelcast.config.SerializationConfig)2 View (com.hazelcast.sql.impl.schema.view.View)2