Search in sources :

Example 31 with QueryPath

use of com.hazelcast.sql.impl.extract.QueryPath 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 32 with QueryPath

use of com.hazelcast.sql.impl.extract.QueryPath 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 33 with QueryPath

use of com.hazelcast.sql.impl.extract.QueryPath 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 34 with QueryPath

use of com.hazelcast.sql.impl.extract.QueryPath in project hazelcast by hazelcast.

the class KvMetadataAvroResolver method schema.

private Schema schema(List<TableField> fields) {
    QueryPath[] paths = paths(fields);
    QueryDataType[] types = types(fields);
    FieldAssembler<Schema> schema = SchemaBuilder.record("jet.sql").fields();
    for (int i = 0; i < fields.size(); i++) {
        String path = paths[i].getPath();
        if (path == null) {
            continue;
        }
        QueryDataType type = types[i];
        switch(type.getTypeFamily()) {
            case BOOLEAN:
                schema = schema.name(path).type().unionOf().nullType().and().booleanType().endUnion().nullDefault();
                break;
            case TINYINT:
            case SMALLINT:
            case INTEGER:
                schema = schema.name(path).type().unionOf().nullType().and().intType().endUnion().nullDefault();
                break;
            case BIGINT:
                schema = schema.name(path).type().unionOf().nullType().and().longType().endUnion().nullDefault();
                break;
            case REAL:
                schema = schema.name(path).type().unionOf().nullType().and().floatType().endUnion().nullDefault();
                break;
            case DOUBLE:
                schema = schema.name(path).type().unionOf().nullType().and().doubleType().endUnion().nullDefault();
                break;
            case DECIMAL:
            case TIME:
            case DATE:
            case TIMESTAMP:
            case TIMESTAMP_WITH_TIME_ZONE:
            case VARCHAR:
                schema = schema.name(path).type().unionOf().nullType().and().stringType().endUnion().nullDefault();
                break;
            case OBJECT:
                schema = schema.name(path).type().unionOf().nullType().and().booleanType().and().intType().and().longType().and().floatType().and().doubleType().and().stringType().endUnion().nullDefault();
                break;
            default:
                throw new IllegalArgumentException("Unknown type: " + type.getTypeFamily());
        }
    }
    return schema.endRecord();
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) Schema(org.apache.avro.Schema)

Example 35 with QueryPath

use of com.hazelcast.sql.impl.extract.QueryPath in project hazelcast by hazelcast.

the class KvMetadataJsonResolver 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, JsonQueryTargetDescriptor.INSTANCE, JsonUpsertTargetDescriptor.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) MapTableField(com.hazelcast.sql.impl.schema.map.MapTableField)

Aggregations

QueryPath (com.hazelcast.sql.impl.extract.QueryPath)35 QueryDataType (com.hazelcast.sql.impl.type.QueryDataType)22 MappingField (com.hazelcast.sql.impl.schema.MappingField)19 MapTableField (com.hazelcast.sql.impl.schema.map.MapTableField)17 TableField (com.hazelcast.sql.impl.schema.TableField)13 ArrayList (java.util.ArrayList)10 HazelcastTable (com.hazelcast.jet.sql.impl.schema.HazelcastTable)6 KvMetadata (com.hazelcast.jet.sql.impl.connector.keyvalue.KvMetadata)4 OptimizerTestSupport (com.hazelcast.jet.sql.impl.opt.OptimizerTestSupport)3 Parameters (junitparams.Parameters)3 Test (org.junit.Test)3 FullScanPhysicalRel (com.hazelcast.jet.sql.impl.opt.physical.FullScanPhysicalRel)2 IndexScanMapPhysicalRel (com.hazelcast.jet.sql.impl.opt.physical.IndexScanMapPhysicalRel)2 ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)2 ImmutableList (com.google.common.collect.ImmutableList)1 IndexConfig (com.hazelcast.config.IndexConfig)1 ArrayDataSerializableFactory (com.hazelcast.internal.serialization.impl.ArrayDataSerializableFactory)1 FieldDescriptor (com.hazelcast.internal.serialization.impl.compact.FieldDescriptor)1 Schema (com.hazelcast.internal.serialization.impl.compact.Schema)1 SchemaWriter (com.hazelcast.internal.serialization.impl.compact.SchemaWriter)1