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);
}
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));
}
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);
}
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));
}
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);
}
Aggregations