Search in sources :

Example 1 with Builder

use of io.confluent.ksql.schema.ksql.types.SqlStruct.Builder in project ksql by confluentinc.

the class GenericsUtil method applyResolved.

/**
 * Replaces all generics in a schema with concrete schemas defined in {@code resolved}
 *
 * @param schema    the schema which may contain generics
 * @param resolved  the mapping from generics to resolved types
 * @return a schema with the same structure as {@code schema} but with no generics
 *
 * @throws KsqlException if there is a generic in {@code schema} that is not present
 *                       in {@code mapping}
 */
public static SqlType applyResolved(final ParamType schema, final Map<GenericType, SqlType> resolved) {
    if (schema instanceof ArrayType) {
        return SqlTypes.array(applyResolved(((ArrayType) schema).element(), resolved));
    }
    if (schema instanceof MapType) {
        final MapType mapType = (MapType) schema;
        final SqlType keyType = applyResolved(mapType.key(), resolved);
        final SqlType valueType = applyResolved(mapType.value(), resolved);
        return SqlTypes.map(keyType, valueType);
    }
    if (schema instanceof StructType) {
        final Builder struct = SqlTypes.struct();
        ((StructType) schema).getSchema().forEach((fieldName, type) -> struct.field(fieldName, applyResolved(type, resolved)));
        return struct.build();
    }
    if (schema instanceof GenericType) {
        final SqlType instance = resolved.get(schema);
        if (instance == null) {
            throw new KsqlException("Could not find mapping for generic type: " + schema);
        }
        return instance;
    }
    return SchemaConverters.functionToSqlConverter().toSqlType(schema);
}
Also used : ArrayType(io.confluent.ksql.function.types.ArrayType) GenericType(io.confluent.ksql.function.types.GenericType) StructType(io.confluent.ksql.function.types.StructType) Builder(io.confluent.ksql.schema.ksql.types.SqlStruct.Builder) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) KsqlException(io.confluent.ksql.util.KsqlException) MapType(io.confluent.ksql.function.types.MapType)

Aggregations

ArrayType (io.confluent.ksql.function.types.ArrayType)1 GenericType (io.confluent.ksql.function.types.GenericType)1 MapType (io.confluent.ksql.function.types.MapType)1 StructType (io.confluent.ksql.function.types.StructType)1 Builder (io.confluent.ksql.schema.ksql.types.SqlStruct.Builder)1 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)1 KsqlException (io.confluent.ksql.util.KsqlException)1