Search in sources :

Example 1 with MapType

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

the class GenericsUtilTest method shouldNotIdentifyInstanceOfTypeMismatch.

@Test
public void shouldNotIdentifyInstanceOfTypeMismatch() {
    // Given:
    final MapType map = MapType.of(GenericType.of("A"), GenericType.of("B"));
    final SqlArgument instance = SqlArgument.of(SqlTypes.array(SqlTypes.STRING));
    // When:
    final boolean isInstance = GenericsUtil.reserveGenerics(map, instance, new HashMap<>()).getLeft();
    // Then:
    assertThat("expected not instance of", !isInstance);
}
Also used : SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) HashMap(java.util.HashMap) MapType(io.confluent.ksql.function.types.MapType) Test(org.junit.Test)

Example 2 with MapType

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

the class GenericsUtilTest method shouldIdentifyMapGeneric.

@Test
public void shouldIdentifyMapGeneric() {
    // Given:
    final MapType a = MapType.of(GenericType.of("A"), GenericType.of("B"));
    final SqlArgument instance = SqlArgument.of(SqlTypes.map(SqlTypes.DOUBLE, SqlTypes.BIGINT));
    // When:
    final Map<GenericType, SqlType> mapping = GenericsUtil.reserveGenerics(a, instance);
    // Then:
    assertThat(mapping, hasEntry(a.key(), SqlTypes.DOUBLE));
    assertThat(mapping, hasEntry(a.value(), SqlTypes.BIGINT));
}
Also used : SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) GenericType(io.confluent.ksql.function.types.GenericType) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) MapType(io.confluent.ksql.function.types.MapType) Test(org.junit.Test)

Example 3 with MapType

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

the class GenericsUtilTest method shouldNotIdentifyInstanceOfTypeMismatchLambda.

@Test
public void shouldNotIdentifyInstanceOfTypeMismatchLambda() {
    // Given:
    final MapType map = MapType.of(GenericType.of("A"), GenericType.of("B"));
    final SqlArgument lambdaInstance = SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.INTEGER), SqlTypes.BIGINT));
    final LambdaType lambda = LambdaType.of(ImmutableList.of(GenericType.of("A")), GenericType.of("B"));
    final SqlArgument mapInstance = SqlArgument.of(SqlTypes.map(SqlTypes.STRING, SqlTypes.BOOLEAN));
    // When:
    final boolean isInstance1 = GenericsUtil.reserveGenerics(map, lambdaInstance, new HashMap<>()).getLeft();
    final boolean isInstance2 = GenericsUtil.reserveGenerics(lambda, mapInstance, new HashMap<>()).getLeft();
    // Then:
    assertThat("expected not instance of", !isInstance1);
    assertThat("expected not instance of", !isInstance2);
}
Also used : LambdaType(io.confluent.ksql.function.types.LambdaType) SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) HashMap(java.util.HashMap) MapType(io.confluent.ksql.function.types.MapType) Test(org.junit.Test)

Example 4 with MapType

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

the class UdfUtilTest method shouldGetMapSchemaFromMapClass.

@Test
public void shouldGetMapSchemaFromMapClass() throws NoSuchMethodException {
    final Type type = getClass().getDeclaredMethod("mapType", Map.class).getGenericParameterTypes()[0];
    final ParamType schema = UdfUtil.getSchemaFromType(type);
    assertThat(schema, instanceOf(MapType.class));
    assertThat(((MapType) schema).value(), equalTo(ParamTypes.INTEGER));
}
Also used : ParamType(io.confluent.ksql.function.types.ParamType) MapType(io.confluent.ksql.function.types.MapType) LambdaType(io.confluent.ksql.function.types.LambdaType) StructType(io.confluent.ksql.function.types.StructType) Type(java.lang.reflect.Type) ArrayType(io.confluent.ksql.function.types.ArrayType) GenericType(io.confluent.ksql.function.types.GenericType) ParamType(io.confluent.ksql.function.types.ParamType) MapType(io.confluent.ksql.function.types.MapType) Test(org.junit.Test)

Example 5 with MapType

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

MapType (io.confluent.ksql.function.types.MapType)7 Test (org.junit.Test)5 GenericType (io.confluent.ksql.function.types.GenericType)4 SqlArgument (io.confluent.ksql.schema.ksql.SqlArgument)4 ArrayType (io.confluent.ksql.function.types.ArrayType)3 LambdaType (io.confluent.ksql.function.types.LambdaType)3 StructType (io.confluent.ksql.function.types.StructType)3 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)3 HashMap (java.util.HashMap)3 ParamType (io.confluent.ksql.function.types.ParamType)2 KsqlException (io.confluent.ksql.util.KsqlException)2 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 Type (java.lang.reflect.Type)1