Search in sources :

Example 11 with MappingField

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

the class KvMetadataJavaResolver method resolvePrimitiveField.

private Stream<MappingField> resolvePrimitiveField(boolean isKey, QueryDataType type) {
    QueryPath path = isKey ? QueryPath.KEY_PATH : QueryPath.VALUE_PATH;
    String name = isKey ? KEY : VALUE;
    String externalName = path.toString();
    return Stream.of(new MappingField(name, type, externalName));
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) MappingField(com.hazelcast.sql.impl.schema.MappingField)

Example 12 with MappingField

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

the class KvMetadataJavaResolver method resolvePrimitiveMetadata.

private KvMetadata resolvePrimitiveMetadata(boolean isKey, List<MappingField> resolvedFields, Map<QueryPath, MappingField> fieldsByPath, QueryDataType type) {
    List<TableField> fields = new ArrayList<>();
    QueryPath path = isKey ? QueryPath.KEY_PATH : QueryPath.VALUE_PATH;
    MappingField field = fieldsByPath.get(path);
    if (field != null) {
        fields.add(new MapTableField(field.name(), field.type(), false, path));
    }
    maybeAddDefaultField(isKey, resolvedFields, fields, type);
    return new KvMetadata(fields, GenericQueryTargetDescriptor.DEFAULT, PrimitiveUpsertTargetDescriptor.INSTANCE);
}
Also used : QueryPath(com.hazelcast.sql.impl.extract.QueryPath) ArrayList(java.util.ArrayList) 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)

Example 13 with MappingField

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

the class JsonResolver method resolveFields.

static List<MappingField> resolveFields(Map<String, Object> json) {
    Map<String, MappingField> fields = new LinkedHashMap<>();
    for (Entry<String, Object> entry : json.entrySet()) {
        String name = entry.getKey();
        QueryDataType type = resolveType(entry.getValue());
        MappingField field = new MappingField(name, type);
        fields.putIfAbsent(field.name(), field);
    }
    return new ArrayList<>(fields.values());
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) ArrayList(java.util.ArrayList) MappingField(com.hazelcast.sql.impl.schema.MappingField) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with MappingField

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

the class SqlCreateMapping method unparse.

public static String unparse(Mapping mapping) {
    SqlPrettyWriter writer = new SqlPrettyWriter(SqlPrettyWriter.config());
    writer.keyword("CREATE MAPPING");
    writer.identifier(mapping.name(), true);
    // external name defaults to mapping name - omit it if it's equal
    if (mapping.externalName() != null && !mapping.externalName().equals(mapping.name())) {
        writer.keyword("EXTERNAL NAME");
        writer.identifier(mapping.externalName(), true);
    }
    List<MappingField> fields = mapping.fields();
    if (fields.size() > 0) {
        SqlWriter.Frame frame = writer.startList("(", ")");
        for (MappingField field : fields) {
            printIndent(writer);
            writer.identifier(field.name(), true);
            writer.print(field.type().getTypeFamily().toString());
            if (field.externalName() != null) {
                writer.print(" ");
                writer.keyword("EXTERNAL NAME");
                writer.identifier(field.externalName(), true);
            }
        }
        writer.newlineAndIndent();
        writer.endList(frame);
    }
    writer.newlineAndIndent();
    writer.keyword("TYPE");
    writer.print(mapping.type());
    Map<String, String> options = mapping.options();
    if (options.size() > 0) {
        writer.newlineAndIndent();
        writer.keyword("OPTIONS");
        SqlWriter.Frame withFrame = writer.startList("(", ")");
        for (Map.Entry<String, String> option : options.entrySet()) {
            printIndent(writer);
            writer.literal(writer.getDialect().quoteStringLiteral(option.getKey()));
            writer.print("= ");
            writer.literal(writer.getDialect().quoteStringLiteral(option.getValue()));
        }
        writer.newlineAndIndent();
        writer.endList(withFrame);
    }
    return writer.toString();
}
Also used : SqlWriter(org.apache.calcite.sql.SqlWriter) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) MappingField(com.hazelcast.sql.impl.schema.MappingField) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 15 with MappingField

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

the class MetadataCompactResolverTest method test_objectIsForbiddenForCompact.

@Test
@Parameters({ "true, __key", "false, this" })
public void test_objectIsForbiddenForCompact(boolean key, String prefix) {
    InternalSerializationService ss = createSerializationService();
    Map<String, String> options = ImmutableMap.of((key ? OPTION_KEY_COMPACT_TYPE_NAME : OPTION_VALUE_COMPACT_TYPE_NAME), "testAll");
    List<MappingField> fields = asList(field("object", QueryDataType.OBJECT, prefix + ".object"));
    assertThatThrownBy(() -> INSTANCE.resolveAndValidateFields(key, fields, options, ss).collect(Collectors.toList())).isInstanceOf(QueryException.class).hasMessageContaining("Cannot derive Compact type for '" + QueryDataTypeFamily.OBJECT + "'");
}
Also used : QueryException(com.hazelcast.sql.impl.QueryException) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) MappingField(com.hazelcast.sql.impl.schema.MappingField) Parameters(junitparams.Parameters) Test(org.junit.Test)

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