Search in sources :

Example 46 with MappingField

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

the class MetadataJsonResolver method resolveMetadata.

@Override
public KvMetadata resolveMetadata(boolean isKey, List<MappingField> resolvedFields, Map<String, String> options, InternalSerializationService serializationService) {
    Map<QueryPath, MappingField> externalFieldsByPath = extractFields(resolvedFields, isKey);
    List<TableField> fields = new ArrayList<>();
    for (Entry<QueryPath, MappingField> entry : externalFieldsByPath.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, HazelcastJsonQueryTargetDescriptor.INSTANCE, HazelcastJsonUpsertTargetDescriptor.INSTANCE);
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) ArrayList(java.util.ArrayList) MappingField(com.hazelcast.sql.impl.schema.MappingField) TableField(com.hazelcast.sql.impl.schema.TableField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) KvMetadata(com.hazelcast.jet.sql.impl.connector.keyvalue.KvMetadata) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField)

Example 47 with MappingField

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

the class KvMetadataJavaResolver method resolveAndValidatePrimitiveField.

private Stream<MappingField> resolveAndValidatePrimitiveField(boolean isKey, List<MappingField> userFields, QueryDataType type) {
    Map<QueryPath, MappingField> userFieldsByPath = extractFields(userFields, isKey);
    QueryPath path = isKey ? QueryPath.KEY_PATH : QueryPath.VALUE_PATH;
    String name = isKey ? KEY : VALUE;
    String externalName = path.toString();
    MappingField userField = userFieldsByPath.get(path);
    if (userField != null && !userField.name().equals(name)) {
        throw QueryException.error("Cannot rename field: '" + name + '\'');
    }
    if (userField != null && !type.getTypeFamily().equals(userField.type().getTypeFamily())) {
        throw QueryException.error("Mismatch between declared and resolved type for field '" + userField.name() + "'");
    }
    for (MappingField field : userFieldsByPath.values()) {
        if (!externalName.equals(field.externalName())) {
            throw QueryException.error("The field '" + externalName + "' is of type " + type.getTypeFamily() + ", you can't map '" + field.externalName() + "' too");
        }
    }
    return userFieldsByPath.values().stream();
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) MappingField(com.hazelcast.sql.impl.schema.MappingField)

Example 48 with MappingField

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

the class KvMetadataJavaResolver method resolveObjectMetadata.

private KvMetadata resolveObjectMetadata(boolean isKey, List<MappingField> resolvedFields, Map<QueryPath, MappingField> fieldsByPath, Class<?> clazz) {
    Map<String, Class<?>> typesByNames = FieldsUtil.resolveClass(clazz);
    List<TableField> fields = new ArrayList<>();
    Map<String, String> typeNamesByPaths = new HashMap<>();
    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));
        if (path.getPath() != null && typesByNames.get(path.getPath()) != null) {
            typeNamesByPaths.put(path.getPath(), typesByNames.get(path.getPath()).getName());
        }
    }
    maybeAddDefaultField(isKey, resolvedFields, fields, QueryDataType.OBJECT);
    return new KvMetadata(fields, GenericQueryTargetDescriptor.DEFAULT, new PojoUpsertTargetDescriptor(clazz.getName(), typeNamesByPaths));
}
Also used : HashMap(java.util.HashMap) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) ArrayList(java.util.ArrayList) PojoUpsertTargetDescriptor(com.hazelcast.jet.sql.impl.inject.PojoUpsertTargetDescriptor) TableField(com.hazelcast.sql.impl.schema.TableField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) MappingField(com.hazelcast.sql.impl.schema.MappingField) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField) QueryPath(com.hazelcast.sql.impl.extract.QueryPath)

Example 49 with MappingField

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

the class KvMetadataJavaResolver method resolveMetadata.

public KvMetadata resolveMetadata(boolean isKey, List<MappingField> resolvedFields, Class<?> clazz) {
    QueryDataType type = QueryDataTypeUtils.resolveTypeForClass(clazz);
    Map<QueryPath, MappingField> fields = extractFields(resolvedFields, isKey);
    if (type != QueryDataType.OBJECT) {
        return resolvePrimitiveMetadata(isKey, resolvedFields, fields, type);
    } else {
        return resolveObjectMetadata(isKey, resolvedFields, fields, clazz);
    }
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) MappingField(com.hazelcast.sql.impl.schema.MappingField)

Example 50 with MappingField

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

the class KvMetadataResolvers method resolveAndValidateFields.

/**
 * A utility to implement {@link SqlConnector#resolveAndValidateFields} in
 * the connector.
 */
public List<MappingField> resolveAndValidateFields(List<MappingField> userFields, Map<String, String> options, NodeEngine nodeEngine) {
    InternalSerializationService ss = (InternalSerializationService) nodeEngine.getSerializationService();
    // normalize and validate the names and external names
    for (MappingField field : userFields) {
        String name = field.name();
        String externalName = field.externalName();
        if (externalName == null) {
            if (name.equals(KEY) || name.equals(VALUE)) {
                externalName = name;
            } else {
                externalName = VALUE_PREFIX + name;
            }
            field.setExternalName(name);
        }
        if ((name.equals(KEY) && !externalName.equals(KEY)) || (name.equals(VALUE) && !externalName.equals(VALUE))) {
            throw QueryException.error("Cannot rename field: '" + name + '\'');
        }
        if (!EXT_NAME_PATTERN.matcher(externalName).matches()) {
            throw QueryException.error("Invalid external name: " + externalName);
        }
    }
    Stream<MappingField> keyFields = findMetadataResolver(options, true).resolveAndValidateFields(true, userFields, options, ss).filter(field -> !field.name().equals(KEY) || field.externalName().equals(KEY));
    Stream<MappingField> valueFields = findMetadataResolver(options, false).resolveAndValidateFields(false, userFields, options, ss).filter(field -> !field.name().equals(VALUE) || field.externalName().equals(VALUE));
    Map<String, MappingField> fields = concat(keyFields, valueFields).collect(LinkedHashMap::new, (map, field) -> map.putIfAbsent(field.name(), field), Map::putAll);
    if (fields.isEmpty()) {
        throw QueryException.error("The resolved field list is empty");
    }
    return new ArrayList<>(fields.values());
}
Also used : ArrayList(java.util.ArrayList) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) MappingField(com.hazelcast.sql.impl.schema.MappingField) LinkedHashMap(java.util.LinkedHashMap) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

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