Search in sources :

Example 1 with StructType

use of io.confluent.ksql.function.types.StructType in project ksql by confluentinc.

the class GenericsUtilTest method shouldFindAllConstituentGenerics.

@Test
public void shouldFindAllConstituentGenerics() {
    // Given:
    final GenericType a = GenericType.of("A");
    final GenericType b = GenericType.of("B");
    final GenericType c = GenericType.of("C");
    final GenericType d = GenericType.of("D");
    final ParamType map = MapType.of(GenericType.of("C"), GenericType.of("D"));
    final StructType complexSchema = StructType.builder().field("a", a).field("b", b).field("c", map).build();
    // When:
    final Set<ParamType> generics = GenericsUtil.constituentGenerics(complexSchema);
    // Then:
    assertThat(generics, containsInAnyOrder(a, b, c, d));
}
Also used : GenericType(io.confluent.ksql.function.types.GenericType) StructType(io.confluent.ksql.function.types.StructType) ParamType(io.confluent.ksql.function.types.ParamType) Test(org.junit.Test)

Example 2 with StructType

use of io.confluent.ksql.function.types.StructType 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)

Example 3 with StructType

use of io.confluent.ksql.function.types.StructType in project ksql by confluentinc.

the class GenericsUtil method resolveGenerics.

/**
 * Identifies a mapping from generic type to concrete type based on a {@code schema} and
 * an {@code instance}, where the {@code instance} schema is expected to have no generic
 * types and have the same nested structure as {@code schema}. Any Generic type mapping
 * identified is added to the list passed in.
 *
 * @param mapping   a list of GenericType to SqlType mappings
 * @param schema    the schema that may contain generics
 * @param instance  a schema with the same structure as {@code schema} but with no generics
 *
 * @return whether we were able to resolve generics in the instance and schema
 */
// CHECKSTYLE_RULES.OFF: NPathComplexity
// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
private static boolean resolveGenerics(final List<Entry<GenericType, SqlType>> mapping, final ParamType schema, final SqlArgument instance) {
    if (!isGeneric(schema) && !matches(schema, instance)) {
        // cannot identify from type mismatch
        return false;
    } else if (!hasGenerics(schema)) {
        // nothing left to identify
        return true;
    }
    KsqlPreconditions.checkArgument(isGeneric(schema) || (matches(schema, instance)), "Cannot resolve generics if the schema and instance have differing types: " + schema + " vs. " + instance);
    if (schema instanceof LambdaType) {
        final LambdaType lambdaType = (LambdaType) schema;
        final SqlLambda sqlLambda = instance.getSqlLambdaOrThrow();
        if (lambdaType.inputTypes().size() == sqlLambda.getNumInputs()) {
            if (sqlLambda instanceof SqlLambdaResolved) {
                final SqlLambdaResolved sqlLambdaResolved = (SqlLambdaResolved) sqlLambda;
                int i = 0;
                for (final ParamType paramType : lambdaType.inputTypes()) {
                    if (!resolveGenerics(mapping, paramType, SqlArgument.of(sqlLambdaResolved.getInputType().get(i)))) {
                        return false;
                    }
                    i++;
                }
                return resolveGenerics(mapping, lambdaType.returnType(), SqlArgument.of(sqlLambdaResolved.getReturnType()));
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
    final SqlType sqlType = instance.getSqlTypeOrThrow();
    if (isGeneric(schema)) {
        mapping.add(new HashMap.SimpleEntry<>((GenericType) schema, sqlType));
    }
    if (schema instanceof ArrayType) {
        final SqlArray sqlArray = (SqlArray) sqlType;
        return resolveGenerics(mapping, ((ArrayType) schema).element(), SqlArgument.of(sqlArray.getItemType()));
    }
    if (schema instanceof MapType) {
        final SqlMap sqlMap = (SqlMap) sqlType;
        final MapType mapType = (MapType) schema;
        return resolveGenerics(mapping, mapType.key(), SqlArgument.of(sqlMap.getKeyType())) && resolveGenerics(mapping, mapType.value(), SqlArgument.of(sqlMap.getValueType()));
    }
    if (schema instanceof StructType) {
        throw new KsqlException("Generic STRUCT is not yet supported");
    }
    return true;
}
Also used : LambdaType(io.confluent.ksql.function.types.LambdaType) GenericType(io.confluent.ksql.function.types.GenericType) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) StructType(io.confluent.ksql.function.types.StructType) SqlLambda(io.confluent.ksql.schema.ksql.types.SqlLambda) HashMap(java.util.HashMap) KsqlException(io.confluent.ksql.util.KsqlException) ParamType(io.confluent.ksql.function.types.ParamType) MapType(io.confluent.ksql.function.types.MapType) ArrayType(io.confluent.ksql.function.types.ArrayType) SqlLambdaResolved(io.confluent.ksql.schema.ksql.types.SqlLambdaResolved) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray)

Example 4 with StructType

use of io.confluent.ksql.function.types.StructType in project ksql by confluentinc.

the class GenericsUtilTest method shouldFindNoConstituentGenerics.

@Test
public void shouldFindNoConstituentGenerics() {
    // Given:
    final StructType struct = StructType.builder().field("a", ParamTypes.LONG).field("b", ParamTypes.DECIMAL).build();
    // When:
    final Set<ParamType> generics = GenericsUtil.constituentGenerics(struct);
    // Then:
    assertThat(generics, empty());
}
Also used : StructType(io.confluent.ksql.function.types.StructType) ParamType(io.confluent.ksql.function.types.ParamType) Test(org.junit.Test)

Aggregations

StructType (io.confluent.ksql.function.types.StructType)4 GenericType (io.confluent.ksql.function.types.GenericType)3 ParamType (io.confluent.ksql.function.types.ParamType)3 ArrayType (io.confluent.ksql.function.types.ArrayType)2 MapType (io.confluent.ksql.function.types.MapType)2 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)2 KsqlException (io.confluent.ksql.util.KsqlException)2 Test (org.junit.Test)2 LambdaType (io.confluent.ksql.function.types.LambdaType)1 SqlArray (io.confluent.ksql.schema.ksql.types.SqlArray)1 SqlLambda (io.confluent.ksql.schema.ksql.types.SqlLambda)1 SqlLambdaResolved (io.confluent.ksql.schema.ksql.types.SqlLambdaResolved)1 SqlMap (io.confluent.ksql.schema.ksql.types.SqlMap)1 Builder (io.confluent.ksql.schema.ksql.types.SqlStruct.Builder)1 HashMap (java.util.HashMap)1