Search in sources :

Example 26 with ParamType

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);
}
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 27 with ParamType

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());
}
Also used : ParamType(io.confluent.ksql.function.types.ParamType)

Example 28 with ParamType

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"))));
}
Also used : ParamType(io.confluent.ksql.function.types.ParamType) MapType(io.confluent.ksql.function.types.MapType) LambdaType(io.confluent.ksql.function.types.LambdaType) StructType(io.confluent.ksql.function.types.StructType) Type(java.lang.reflect.Type) ArrayType(io.confluent.ksql.function.types.ArrayType) GenericType(io.confluent.ksql.function.types.GenericType) ParamType(io.confluent.ksql.function.types.ParamType) Test(org.junit.Test)

Example 29 with ParamType

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));
}
Also used : ArrayType(io.confluent.ksql.function.types.ArrayType) ParamType(io.confluent.ksql.function.types.ParamType) MapType(io.confluent.ksql.function.types.MapType) LambdaType(io.confluent.ksql.function.types.LambdaType) StructType(io.confluent.ksql.function.types.StructType) Type(java.lang.reflect.Type) ArrayType(io.confluent.ksql.function.types.ArrayType) GenericType(io.confluent.ksql.function.types.GenericType) ParamType(io.confluent.ksql.function.types.ParamType) Test(org.junit.Test)

Example 30 with ParamType

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"))));
}
Also used : ParamType(io.confluent.ksql.function.types.ParamType) MapType(io.confluent.ksql.function.types.MapType) LambdaType(io.confluent.ksql.function.types.LambdaType) StructType(io.confluent.ksql.function.types.StructType) Type(java.lang.reflect.Type) ArrayType(io.confluent.ksql.function.types.ArrayType) GenericType(io.confluent.ksql.function.types.GenericType) ParamType(io.confluent.ksql.function.types.ParamType) Test(org.junit.Test)

Aggregations

ParamType (io.confluent.ksql.function.types.ParamType)35 GenericType (io.confluent.ksql.function.types.GenericType)26 Test (org.junit.Test)22 LambdaType (io.confluent.ksql.function.types.LambdaType)20 StructType (io.confluent.ksql.function.types.StructType)20 ArrayType (io.confluent.ksql.function.types.ArrayType)19 Type (java.lang.reflect.Type)19 MapType (io.confluent.ksql.function.types.MapType)18 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)11 HashMap (java.util.HashMap)5 KsqlException (io.confluent.ksql.util.KsqlException)4 ParameterizedType (java.lang.reflect.ParameterizedType)4 ArrayList (java.util.ArrayList)4 GenericArrayType (java.lang.reflect.GenericArrayType)3 TypeVariable (java.lang.reflect.TypeVariable)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Expression (io.confluent.ksql.execution.expression.tree.Expression)2 KsqlScalarFunction (io.confluent.ksql.function.KsqlScalarFunction)2 ParamTypes (io.confluent.ksql.function.types.ParamTypes)2 Kudf (io.confluent.ksql.function.udf.Kudf)2