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