Search in sources :

Example 6 with UdfMetadata

use of io.confluent.ksql.function.udf.UdfMetadata 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);
}
Also used : Udtf(io.confluent.ksql.function.udtf.Udtf) Method(java.lang.reflect.Method) KsqlException(io.confluent.ksql.util.KsqlException) UdfMetadata(io.confluent.ksql.function.udf.UdfMetadata) ParamType(io.confluent.ksql.function.types.ParamType) ParameterizedType(java.lang.reflect.ParameterizedType) ParamType(io.confluent.ksql.function.types.ParamType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) UdtfDescription(io.confluent.ksql.function.udtf.UdtfDescription)

Example 7 with UdfMetadata

use of io.confluent.ksql.function.udf.UdfMetadata in project ksql by confluentinc.

the class ExpressionTypeManagerTest method givenUdfWithNameAndReturnType.

private void givenUdfWithNameAndReturnType(final String name, final SqlType returnType, final UdfFactory factory, final KsqlScalarFunction function) {
    when(functionRegistry.isAggregate(FunctionName.of(name))).thenReturn(false);
    when(functionRegistry.getUdfFactory(FunctionName.of(name))).thenReturn(factory);
    when(factory.getFunction(anyList())).thenReturn(function);
    when(function.getReturnType(anyList())).thenReturn(returnType);
    final UdfMetadata metadata = mock(UdfMetadata.class);
    when(factory.getMetadata()).thenReturn(metadata);
}
Also used : UdfMetadata(io.confluent.ksql.function.udf.UdfMetadata)

Aggregations

UdfMetadata (io.confluent.ksql.function.udf.UdfMetadata)7 Method (java.lang.reflect.Method)3 ParamType (io.confluent.ksql.function.types.ParamType)2 KsqlException (io.confluent.ksql.util.KsqlException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 UdfFactory (io.confluent.ksql.function.UdfFactory)1 UdafDescription (io.confluent.ksql.function.udaf.UdafDescription)1 UdafFactory (io.confluent.ksql.function.udaf.UdafFactory)1 Kudf (io.confluent.ksql.function.udf.Kudf)1 PluggableUdf (io.confluent.ksql.function.udf.PluggableUdf)1 Udf (io.confluent.ksql.function.udf.Udf)1 UdfDescription (io.confluent.ksql.function.udf.UdfDescription)1 Udtf (io.confluent.ksql.function.udtf.Udtf)1 UdtfDescription (io.confluent.ksql.function.udtf.UdtfDescription)1 FunctionName (io.confluent.ksql.name.FunctionName)1 SqlTypeParser (io.confluent.ksql.schema.ksql.SqlTypeParser)1 KsqlConfig (io.confluent.ksql.util.KsqlConfig)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1