Search in sources :

Example 1 with QueryDataType

use of com.hazelcast.sql.impl.type.QueryDataType in project hazelcast by hazelcast.

the class MetadataCompactResolver method resolveMetadata.

@Override
public KvMetadata resolveMetadata(boolean isKey, List<MappingField> resolvedFields, Map<String, String> options, InternalSerializationService serializationService) {
    Map<QueryPath, MappingField> fieldsByPath = extractFields(resolvedFields, isKey);
    String typeNameProperty = isKey ? OPTION_KEY_COMPACT_TYPE_NAME : OPTION_VALUE_COMPACT_TYPE_NAME;
    String typeName = options.get(typeNameProperty);
    List<TableField> fields = new ArrayList<>(fieldsByPath.size());
    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);
    Schema schema = resolveSchema(typeName, fieldsByPath);
    return new KvMetadata(fields, GenericQueryTargetDescriptor.DEFAULT, new CompactUpsertTargetDescriptor(schema));
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) Schema(com.hazelcast.internal.serialization.impl.compact.Schema) ArrayList(java.util.ArrayList) CompactUpsertTargetDescriptor(com.hazelcast.jet.sql.impl.inject.CompactUpsertTargetDescriptor) 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) QueryPath(com.hazelcast.sql.impl.extract.QueryPath)

Example 2 with QueryDataType

use of com.hazelcast.sql.impl.type.QueryDataType in project hazelcast by hazelcast.

the class MetadataCompactResolver method resolveSchema.

private Schema resolveSchema(String typeName, Map<QueryPath, MappingField> fields) {
    SchemaWriter schemaWriter = new SchemaWriter(typeName);
    for (Entry<QueryPath, MappingField> entry : fields.entrySet()) {
        String name = entry.getKey().getPath();
        QueryDataType type = entry.getValue().type();
        schemaWriter.addField(new FieldDescriptor(name, resolveToCompactKind(type.getTypeFamily())));
    }
    return schemaWriter.build();
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) MappingField(com.hazelcast.sql.impl.schema.MappingField) SchemaWriter(com.hazelcast.internal.serialization.impl.compact.SchemaWriter) FieldDescriptor(com.hazelcast.internal.serialization.impl.compact.FieldDescriptor)

Example 3 with QueryDataType

use of com.hazelcast.sql.impl.type.QueryDataType in project hazelcast by hazelcast.

the class MetadataPortableResolver method resolveFields.

Stream<MappingField> resolveFields(boolean isKey, @Nullable ClassDefinition clazz) {
    if (clazz == null || clazz.getFieldCount() == 0) {
        // ClassDefinition does not exist, or it is empty, map the whole value
        String name = isKey ? KEY : VALUE;
        return Stream.of(new MappingField(name, QueryDataType.OBJECT, name));
    }
    return clazz.getFieldNames().stream().map(name -> {
        QueryPath path = new QueryPath(name, isKey);
        QueryDataType type = resolvePortableType(clazz.getFieldType(name));
        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)

Example 4 with QueryDataType

use of com.hazelcast.sql.impl.type.QueryDataType in project hazelcast by hazelcast.

the class MetadataPortableResolver method resolveClassDefinition.

@Nonnull
private static ClassDefinition resolveClassDefinition(boolean isKey, Map<String, String> options, Collection<MappingField> fields, InternalSerializationService serializationService) {
    Tuple3<Integer, Integer, Integer> settings = settings(isKey, options);
    // noinspection ConstantConditions
    ClassDefinition classDefinition = serializationService.getPortableContext().lookupClassDefinition(settings.f0(), settings.f1(), settings.f2());
    if (classDefinition != null) {
        return classDefinition;
    }
    ClassDefinitionBuilder classDefinitionBuilder = new ClassDefinitionBuilder(settings.f0(), settings.f1(), settings.f2());
    for (MappingField field : fields) {
        String name = field.name();
        QueryDataType type = field.type();
        switch(type.getTypeFamily()) {
            case BOOLEAN:
                classDefinitionBuilder.addBooleanField(name);
                break;
            case TINYINT:
                classDefinitionBuilder.addByteField(name);
                break;
            case SMALLINT:
                classDefinitionBuilder.addShortField(name);
                break;
            case INTEGER:
                classDefinitionBuilder.addIntField(name);
                break;
            case BIGINT:
                classDefinitionBuilder.addLongField(name);
                break;
            case REAL:
                classDefinitionBuilder.addFloatField(name);
                break;
            case DOUBLE:
                classDefinitionBuilder.addDoubleField(name);
                break;
            case DECIMAL:
                classDefinitionBuilder.addDecimalField(name);
                break;
            case VARCHAR:
                classDefinitionBuilder.addStringField(name);
                break;
            case TIME:
                classDefinitionBuilder.addTimeField(name);
                break;
            case DATE:
                classDefinitionBuilder.addDateField(name);
                break;
            case TIMESTAMP:
                classDefinitionBuilder.addTimestampField(name);
                break;
            case TIMESTAMP_WITH_TIME_ZONE:
                classDefinitionBuilder.addTimestampWithTimezoneField(name);
                break;
            default:
        }
    }
    return classDefinitionBuilder.build();
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) MappingField(com.hazelcast.sql.impl.schema.MappingField) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Nonnull(javax.annotation.Nonnull)

Example 5 with QueryDataType

use of com.hazelcast.sql.impl.type.QueryDataType 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)

Aggregations

QueryDataType (com.hazelcast.sql.impl.type.QueryDataType)61 QueryPath (com.hazelcast.sql.impl.extract.QueryPath)23 MappingField (com.hazelcast.sql.impl.schema.MappingField)17 ArrayList (java.util.ArrayList)16 TableField (com.hazelcast.sql.impl.schema.TableField)14 MapTableField (com.hazelcast.sql.impl.schema.map.MapTableField)13 HazelcastTable (com.hazelcast.jet.sql.impl.schema.HazelcastTable)10 RelDataType (org.apache.calcite.rel.type.RelDataType)7 OptimizerTestSupport (com.hazelcast.jet.sql.impl.opt.OptimizerTestSupport)6 IndexScanMapPhysicalRel (com.hazelcast.jet.sql.impl.opt.physical.IndexScanMapPhysicalRel)6 FullScanPhysicalRel (com.hazelcast.jet.sql.impl.opt.physical.FullScanPhysicalRel)5 Nonnull (javax.annotation.Nonnull)5 IndexEqualsFilter (com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter)4 Test (org.junit.Test)4 IndexType (com.hazelcast.config.IndexType)3 KvMetadata (com.hazelcast.jet.sql.impl.connector.keyvalue.KvMetadata)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 HashMap (java.util.HashMap)3 List (java.util.List)3