use of io.confluent.ksql.function.types.ParamType in project ksql by confluentinc.
the class UdtfLoader method loadUdtfFromClass.
public void loadUdtfFromClass(final Class<?> theClass, final String path) {
final UdtfDescription udtfDescriptionAnnotation = theClass.getAnnotation(UdtfDescription.class);
if (udtfDescriptionAnnotation == null) {
throw new KsqlException(String.format("Cannot load class %s. Classes containing UDTFs must" + "be annotated with @UdtfDescription.", theClass.getName()));
}
final String functionName = udtfDescriptionAnnotation.name();
final String sensorName = "ksql-udtf-" + functionName;
FunctionMetrics.initInvocationSensor(metrics, sensorName, "ksql-udtf", functionName + " udtf");
final UdfMetadata metadata = new UdfMetadata(udtfDescriptionAnnotation.name(), udtfDescriptionAnnotation.description(), udtfDescriptionAnnotation.author(), udtfDescriptionAnnotation.version(), udtfDescriptionAnnotation.category(), path);
final TableFunctionFactory factory = new TableFunctionFactory(metadata);
for (final Method method : theClass.getMethods()) {
if (method.getAnnotation(Udtf.class) != null) {
final Udtf annotation = method.getAnnotation(Udtf.class);
try {
if (method.getReturnType() != List.class) {
throw new KsqlException(String.format("UDTF functions must return a List. Class %s Method %s", theClass.getName(), method.getName()));
}
final Type ret = method.getGenericReturnType();
if (!(ret instanceof ParameterizedType)) {
throw new KsqlException(String.format("UDTF functions must return a parameterized List. Class %s Method %s", theClass.getName(), method.getName()));
}
final Type typeArg = ((ParameterizedType) ret).getActualTypeArguments()[0];
final ParamType returnType = FunctionLoaderUtils.getReturnType(method, typeArg, annotation.schema(), typeParser);
final List<ParameterInfo> parameters = FunctionLoaderUtils.createParameters(method, functionName, typeParser);
final KsqlTableFunction tableFunction = createTableFunction(method, FunctionName.of(functionName), returnType, parameters, annotation.description(), annotation);
factory.addFunction(tableFunction);
} catch (final KsqlException e) {
if (throwExceptionOnLoadFailure) {
throw e;
} else {
LOGGER.warn("Failed to add UDTF to the MetaStore. name={} method={}", udtfDescriptionAnnotation.name(), method, e);
}
}
}
}
functionRegistry.addTableFunctionFactory(factory);
}
use of io.confluent.ksql.function.types.ParamType in project ksql by confluentinc.
the class UdfLoader method createFunction.
private KsqlScalarFunction createFunction(final Class theClass, final UdfDescription udfDescriptionAnnotation, final Udf udfAnnotation, final Method method, final String path, final String sensorName, final Class<? extends Kudf> udfClass) {
// sanity check
FunctionLoaderUtils.instantiateFunctionInstance(method.getDeclaringClass(), udfDescriptionAnnotation.name());
final FunctionInvoker invoker = FunctionLoaderUtils.createFunctionInvoker(method);
final String functionName = udfDescriptionAnnotation.name();
LOGGER.info("Adding function " + functionName + " for method " + method);
final List<ParameterInfo> parameters = FunctionLoaderUtils.createParameters(method, functionName, typeParser);
final ParamType javaReturnSchema = FunctionLoaderUtils.getReturnType(method, udfAnnotation.schema(), typeParser);
final SchemaProvider schemaProviderFunction = FunctionLoaderUtils.handleUdfReturnSchema(theClass, javaReturnSchema, udfAnnotation.schema(), typeParser, udfAnnotation.schemaProvider(), udfDescriptionAnnotation.name(), method.isVarArgs());
return KsqlScalarFunction.create(schemaProviderFunction, javaReturnSchema, parameters, FunctionName.of(functionName.toUpperCase()), udfClass, getUdfFactory(method, udfDescriptionAnnotation, functionName, invoker, sensorName), udfAnnotation.description(), path, method.isVarArgs());
}
use of io.confluent.ksql.function.types.ParamType in project ksql by confluentinc.
the class UdfUtilTest method shouldGetPartialGenericBiFunction.
@Test
public void shouldGetPartialGenericBiFunction() throws NoSuchMethodException {
// Given:
final Type genericType = getClass().getMethod("partialGenericBiFunctionType").getGenericReturnType();
// When:
final ParamType returnType = UdfUtil.getSchemaFromType(genericType);
// Then:
assertThat(returnType, is(LambdaType.of(ImmutableList.of(GenericType.of("T"), ParamTypes.BOOLEAN), GenericType.of("U"))));
}
use of io.confluent.ksql.function.types.ParamType in project ksql by confluentinc.
the class UdfUtilTest method shouldGetArraySchemaFromListClass.
@Test
public void shouldGetArraySchemaFromListClass() throws NoSuchMethodException {
final Type type = getClass().getDeclaredMethod("listType", List.class).getGenericParameterTypes()[0];
final ParamType schema = UdfUtil.getSchemaFromType(type);
assertThat(schema, instanceOf(ArrayType.class));
assertThat(((ArrayType) schema).element(), equalTo(ParamTypes.DOUBLE));
}
use of io.confluent.ksql.function.types.ParamType in project ksql by confluentinc.
the class UdfUtilTest method shouldGetGenericSchemaFromParameterizedType.
@Test
public void shouldGetGenericSchemaFromParameterizedType() throws NoSuchMethodException {
// Given:
final Type genericType = getClass().getMethod("genericMapType").getGenericReturnType();
// When:
final ParamType returnType = UdfUtil.getSchemaFromType(genericType);
// Then:
assertThat(returnType, is(MapType.of(GenericType.of("K"), GenericType.of("V"))));
}
Aggregations