Search in sources :

Example 1 with MAP

use of io.confluent.ksql.schema.ksql.types.SqlBaseType.MAP in project ksql by confluentinc.

the class CastEvaluator method castStructToStruct.

@SuppressWarnings("OptionalGetWithoutIsPresent")
private static String castStructToStruct(final String innerCode, final SqlType from, final SqlType to, final KsqlConfig config) {
    final SqlStruct fromStruct = (SqlStruct) from;
    final SqlStruct toStruct = (SqlStruct) to;
    try {
        final String mappers = fromStruct.fields().stream().filter(fromField -> toStruct.field(fromField.name()).isPresent()).map(fromField -> castFieldToField(fromField, toStruct.field(fromField.name()).get(), config)).collect(Collectors.joining(System.lineSeparator(), "ImmutableMap.builder()\n\t\t", "\n\t\t.build()"));
        // Inefficient, but only way to pass type until SqlToJavaVisitor supports passing
        // additional parameters to the generated code. Hopefully, JVM optimises this away.
        final String schemaCode = "SchemaConverters.sqlToConnectConverter()" + ".toConnectSchema(" + SqlTypeCodeGen.generateCode(toStruct) + ")";
        return "CastEvaluator.castStruct(" + innerCode + ", " + mappers + "," + schemaCode + ")";
    } catch (final UnsupportedCastException e) {
        throw new UnsupportedCastException(from, to, e);
    }
}
Also used : Field(io.confluent.ksql.schema.ksql.types.SqlStruct.Field) ARRAY(io.confluent.ksql.schema.ksql.types.SqlBaseType.ARRAY) HashMap(java.util.HashMap) STRING(io.confluent.ksql.schema.ksql.types.SqlBaseType.STRING) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) Function(java.util.function.Function) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) BIGINT(io.confluent.ksql.schema.ksql.types.SqlBaseType.BIGINT) ArrayList(java.util.ArrayList) Schema(org.apache.kafka.connect.data.Schema) MAP(io.confluent.ksql.schema.ksql.types.SqlBaseType.MAP) INTEGER(io.confluent.ksql.schema.ksql.types.SqlBaseType.INTEGER) BOOLEAN(io.confluent.ksql.schema.ksql.types.SqlBaseType.BOOLEAN) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) STRUCT(io.confluent.ksql.schema.ksql.types.SqlBaseType.STRUCT) SchemaConverters(io.confluent.ksql.schema.ksql.SchemaConverters) DECIMAL(io.confluent.ksql.schema.ksql.types.SqlBaseType.DECIMAL) TIME(io.confluent.ksql.schema.ksql.types.SqlBaseType.TIME) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct) DATE(io.confluent.ksql.schema.ksql.types.SqlBaseType.DATE) ImmutableMap(com.google.common.collect.ImmutableMap) DOUBLE(io.confluent.ksql.schema.ksql.types.SqlBaseType.DOUBLE) KsqlConfig(io.confluent.ksql.util.KsqlConfig) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) TIMESTAMP(io.confluent.ksql.schema.ksql.types.SqlBaseType.TIMESTAMP) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) Struct(org.apache.kafka.connect.data.Struct) KsqlException(io.confluent.ksql.util.KsqlException) SqlStruct(io.confluent.ksql.schema.ksql.types.SqlStruct)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 SchemaConverters (io.confluent.ksql.schema.ksql.SchemaConverters)1 SqlArray (io.confluent.ksql.schema.ksql.types.SqlArray)1 SqlBaseType (io.confluent.ksql.schema.ksql.types.SqlBaseType)1 ARRAY (io.confluent.ksql.schema.ksql.types.SqlBaseType.ARRAY)1 BIGINT (io.confluent.ksql.schema.ksql.types.SqlBaseType.BIGINT)1 BOOLEAN (io.confluent.ksql.schema.ksql.types.SqlBaseType.BOOLEAN)1 DATE (io.confluent.ksql.schema.ksql.types.SqlBaseType.DATE)1 DECIMAL (io.confluent.ksql.schema.ksql.types.SqlBaseType.DECIMAL)1 DOUBLE (io.confluent.ksql.schema.ksql.types.SqlBaseType.DOUBLE)1 INTEGER (io.confluent.ksql.schema.ksql.types.SqlBaseType.INTEGER)1 MAP (io.confluent.ksql.schema.ksql.types.SqlBaseType.MAP)1 STRING (io.confluent.ksql.schema.ksql.types.SqlBaseType.STRING)1 STRUCT (io.confluent.ksql.schema.ksql.types.SqlBaseType.STRUCT)1 TIME (io.confluent.ksql.schema.ksql.types.SqlBaseType.TIME)1 TIMESTAMP (io.confluent.ksql.schema.ksql.types.SqlBaseType.TIMESTAMP)1 SqlDecimal (io.confluent.ksql.schema.ksql.types.SqlDecimal)1 SqlMap (io.confluent.ksql.schema.ksql.types.SqlMap)1 SqlStruct (io.confluent.ksql.schema.ksql.types.SqlStruct)1 Field (io.confluent.ksql.schema.ksql.types.SqlStruct.Field)1