use of io.confluent.ksql.function.types.LambdaType in project ksql by confluentinc.
the class GenericsUtilTest method shouldIdentifySqlLambdaResolvedGenerics.
@Test
public void shouldIdentifySqlLambdaResolvedGenerics() {
// Given:
final GenericType typeA = GenericType.of("A");
final GenericType typeB = GenericType.of("B");
final LambdaType a = LambdaType.of(ImmutableList.of(typeA, typeB), typeB);
final SqlArgument instance = SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.DOUBLE, SqlTypes.BIGINT), SqlTypes.BIGINT));
// When:
final Map<GenericType, SqlType> mapping = GenericsUtil.reserveGenerics(a, instance);
// Then:
assertThat(mapping, hasEntry(typeA, SqlTypes.DOUBLE));
assertThat(mapping, hasEntry(typeB, SqlTypes.BIGINT));
}
use of io.confluent.ksql.function.types.LambdaType in project ksql by confluentinc.
the class FunctionLoaderUtils method handleUdfReturnSchema.
// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
static SchemaProvider handleUdfReturnSchema(final Class theClass, final ParamType javaReturnSchema, final String annotationSchema, final SqlTypeParser parser, final String schemaProviderFunctionName, final String functionName, final boolean isVariadic) {
// CHECKSTYLE_RULES.ON: CyclomaticComplexity
final Function<List<SqlArgument>, SqlType> schemaProvider;
if (!Udf.NO_SCHEMA_PROVIDER.equals(schemaProviderFunctionName)) {
schemaProvider = handleUdfSchemaProviderAnnotation(schemaProviderFunctionName, theClass, functionName);
} else if (!Udf.NO_SCHEMA.equals(annotationSchema)) {
final SqlType sqlType = parser.parse(annotationSchema).getSqlType();
schemaProvider = args -> sqlType;
} else if (!GenericsUtil.hasGenerics(javaReturnSchema)) {
// it is important to do this eagerly and not in the lambda so that
// we can fail early (when loading the UDF) instead of when the user
// attempts to use the UDF
final SqlType sqlType = fromJavaType(javaReturnSchema, functionName);
schemaProvider = args -> sqlType;
} else {
schemaProvider = null;
}
return (parameters, arguments) -> {
if (schemaProvider != null) {
final SqlType returnType = schemaProvider.apply(arguments);
if (!(ParamTypes.areCompatible(SqlArgument.of(returnType), javaReturnSchema, false))) {
throw new KsqlException(String.format("Return type %s of UDF %s does not match the declared " + "return type %s.", returnType, functionName.toUpperCase(), SchemaConverters.functionToSqlConverter().toSqlType(javaReturnSchema)));
}
return returnType;
}
final Map<GenericType, SqlType> genericMapping = new HashMap<>();
for (int i = 0; i < Math.min(parameters.size(), arguments.size()); i++) {
final ParamType schema = parameters.get(i);
if (schema instanceof LambdaType) {
if (isVariadic && i == parameters.size() - 1) {
throw new KsqlException(String.format("Lambda function %s cannot be variadic.", arguments.get(i).toString()));
}
genericMapping.putAll(GenericsUtil.reserveGenerics(schema, arguments.get(i)));
} else {
// we resolve any variadic as if it were an array so that the type
// structure matches the input type
final SqlType instance = isVariadic && i == parameters.size() - 1 ? SqlTypes.array(arguments.get(i).getSqlTypeOrThrow()) : arguments.get(i).getSqlTypeOrThrow();
genericMapping.putAll(GenericsUtil.reserveGenerics(schema, SqlArgument.of(instance)));
}
}
return GenericsUtil.applyResolved(javaReturnSchema, genericMapping);
};
}
use of io.confluent.ksql.function.types.LambdaType in project ksql by confluentinc.
the class UdfUtilTest method shouldGetBiFunction.
@Test
public void shouldGetBiFunction() throws NoSuchMethodException {
final Type type = getClass().getDeclaredMethod("biFunctionType", BiFunction.class).getGenericParameterTypes()[0];
final ParamType schema = UdfUtil.getSchemaFromType(type);
assertThat(schema, instanceOf(LambdaType.class));
assertThat(((LambdaType) schema).inputTypes(), equalTo(ImmutableList.of(ParamTypes.LONG, ParamTypes.INTEGER)));
assertThat(((LambdaType) schema).returnType(), equalTo(ParamTypes.BOOLEAN));
}
Aggregations