Search in sources :

Example 1 with UdfMetadata

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

the class InterpretedExpressionTest method givenUdf.

private void givenUdf(final String name, 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(SqlTypes.INTEGER);
    final UdfMetadata metadata = mock(UdfMetadata.class);
    when(factory.getMetadata()).thenReturn(metadata);
}
Also used : UdfMetadata(io.confluent.ksql.function.udf.UdfMetadata)

Example 2 with UdfMetadata

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

the class UdfLoader method loadUdfFromClass.

@VisibleForTesting
public void loadUdfFromClass(final Class<?> theClass, final String path) {
    final UdfDescription udfDescriptionAnnotation = theClass.getAnnotation(UdfDescription.class);
    if (udfDescriptionAnnotation == null) {
        throw new KsqlException(String.format("Cannot load class %s. Classes containing UDFs must" + "be annotated with @UdfDescription.", theClass.getName()));
    }
    final String functionName = udfDescriptionAnnotation.name();
    final String sensorName = "ksql-udf-" + functionName;
    @SuppressWarnings("unchecked") final Class<? extends Kudf> udfClass = metrics.map(m -> (Class) UdfMetricProducer.class).orElse(PluggableUdf.class);
    FunctionMetrics.initInvocationSensor(metrics, sensorName, "ksql-udf", functionName + " udf");
    final UdfFactory factory = new UdfFactory(udfClass, new UdfMetadata(udfDescriptionAnnotation.name(), udfDescriptionAnnotation.description(), udfDescriptionAnnotation.author(), udfDescriptionAnnotation.version(), udfDescriptionAnnotation.category(), path));
    functionRegistry.ensureFunctionFactory(factory);
    for (final Method method : theClass.getMethods()) {
        final Udf udfAnnotation = method.getAnnotation(Udf.class);
        if (udfAnnotation != null) {
            final KsqlScalarFunction function;
            try {
                function = createFunction(theClass, udfDescriptionAnnotation, udfAnnotation, method, path, sensorName, udfClass);
            } catch (final KsqlException e) {
                if (throwExceptionOnLoadFailure) {
                    throw e;
                } else {
                    LOGGER.warn("Failed to add UDF to the MetaStore. name={} method={}", udfDescriptionAnnotation.name(), method, e);
                    continue;
                }
            }
            factory.addFunction(function);
        }
    }
}
Also used : UdfDescription(io.confluent.ksql.function.udf.UdfDescription) Logger(org.slf4j.Logger) Kudf(io.confluent.ksql.function.udf.Kudf) Udf(io.confluent.ksql.function.udf.Udf) Configurable(org.apache.kafka.common.Configurable) Time(org.apache.kafka.common.utils.Time) FunctionName(io.confluent.ksql.name.FunctionName) ParamType(io.confluent.ksql.function.types.ParamType) LoggerFactory(org.slf4j.LoggerFactory) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Function(java.util.function.Function) Objects(java.util.Objects) UdfMetadata(io.confluent.ksql.function.udf.UdfMetadata) List(java.util.List) PluggableUdf(io.confluent.ksql.function.udf.PluggableUdf) Metrics(org.apache.kafka.common.metrics.Metrics) SqlTypeParser(io.confluent.ksql.schema.ksql.SqlTypeParser) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Method(java.lang.reflect.Method) Udf(io.confluent.ksql.function.udf.Udf) PluggableUdf(io.confluent.ksql.function.udf.PluggableUdf) Method(java.lang.reflect.Method) UdfDescription(io.confluent.ksql.function.udf.UdfDescription) KsqlException(io.confluent.ksql.util.KsqlException) UdfMetadata(io.confluent.ksql.function.udf.UdfMetadata) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with UdfMetadata

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

the class UdafLoader method loadUdafFromClass.

void loadUdafFromClass(final Class<?> theClass, final String path) {
    final UdafDescription udafAnnotation = theClass.getAnnotation(UdafDescription.class);
    final List<UdafFactoryInvoker> invokers = new ArrayList<>();
    for (final Method method : theClass.getMethods()) {
        if (method.getAnnotation(UdafFactory.class) == null) {
            continue;
        }
        if (!Modifier.isStatic(method.getModifiers())) {
            LOGGER.warn("Trying to create a UDAF from a non-static factory method. Udaf factory" + " methods must be static. class={}, method={}, name={}", method.getDeclaringClass(), method.getName(), udafAnnotation.name());
            continue;
        }
        final UdafFactory annotation = method.getAnnotation(UdafFactory.class);
        try {
            LOGGER.info("Adding UDAF name={} from path={} class={}", udafAnnotation.name(), path, method.getDeclaringClass());
            final UdafFactoryInvoker invoker = createUdafFactoryInvoker(method, FunctionName.of(udafAnnotation.name()), annotation.description(), annotation.paramSchema(), annotation.aggregateSchema(), annotation.returnSchema());
            invokers.add(invoker);
        } catch (final Exception e) {
            LOGGER.warn("Failed to create UDAF name={}, method={}, class={}, path={}", udafAnnotation.name(), method.getName(), method.getDeclaringClass(), path, e);
        }
    }
    functionRegistry.addAggregateFunctionFactory(new UdafAggregateFunctionFactory(new UdfMetadata(udafAnnotation.name(), udafAnnotation.description(), udafAnnotation.author(), udafAnnotation.version(), udafAnnotation.category(), path), invokers));
}
Also used : UdafDescription(io.confluent.ksql.function.udaf.UdafDescription) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) UdfMetadata(io.confluent.ksql.function.udf.UdfMetadata) UdafFactory(io.confluent.ksql.function.udaf.UdafFactory)

Example 4 with UdfMetadata

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

the class SqlToJavaVisitorTest method givenUdf.

private void givenUdf(final String name, final UdfFactory factory, final KsqlScalarFunction function, final SqlType returnType) {
    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)

Example 5 with UdfMetadata

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

the class ExpressionTypeManagerTest method init.

@Before
public void init() {
    expressionTypeManager = new ExpressionTypeManager(SCHEMA, functionRegistry);
    final UdfFactory internalFactory = mock(UdfFactory.class);
    final UdfMetadata metadata = mock(UdfMetadata.class);
    when(internalFactory.getMetadata()).thenReturn(metadata);
    when(functionRegistry.getUdfFactory(any())).thenReturn(internalFactory);
}
Also used : UdfFactory(io.confluent.ksql.function.UdfFactory) UdfMetadata(io.confluent.ksql.function.udf.UdfMetadata) Before(org.junit.Before)

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