Search in sources :

Example 11 with LambdaType

use of io.confluent.ksql.function.types.LambdaType in project ksql by confluentinc.

the class GenericsUtilTest method shouldIdentifySqlLambdaResolvedGenerics.

@Test
public void shouldIdentifySqlLambdaResolvedGenerics() {
    // Given:
    final GenericType typeA = GenericType.of("A");
    final GenericType typeB = GenericType.of("B");
    final LambdaType a = LambdaType.of(ImmutableList.of(typeA, typeB), typeB);
    final SqlArgument instance = SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.DOUBLE, SqlTypes.BIGINT), SqlTypes.BIGINT));
    // When:
    final Map<GenericType, SqlType> mapping = GenericsUtil.reserveGenerics(a, instance);
    // Then:
    assertThat(mapping, hasEntry(typeA, SqlTypes.DOUBLE));
    assertThat(mapping, hasEntry(typeB, SqlTypes.BIGINT));
}
Also used : LambdaType(io.confluent.ksql.function.types.LambdaType) GenericType(io.confluent.ksql.function.types.GenericType) SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Test(org.junit.Test)

Example 12 with LambdaType

use of io.confluent.ksql.function.types.LambdaType in project ksql by confluentinc.

the class FunctionLoaderUtils method handleUdfReturnSchema.

// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
static SchemaProvider handleUdfReturnSchema(final Class theClass, final ParamType javaReturnSchema, final String annotationSchema, final SqlTypeParser parser, final String schemaProviderFunctionName, final String functionName, final boolean isVariadic) {
    // CHECKSTYLE_RULES.ON: CyclomaticComplexity
    final Function<List<SqlArgument>, SqlType> schemaProvider;
    if (!Udf.NO_SCHEMA_PROVIDER.equals(schemaProviderFunctionName)) {
        schemaProvider = handleUdfSchemaProviderAnnotation(schemaProviderFunctionName, theClass, functionName);
    } else if (!Udf.NO_SCHEMA.equals(annotationSchema)) {
        final SqlType sqlType = parser.parse(annotationSchema).getSqlType();
        schemaProvider = args -> sqlType;
    } else if (!GenericsUtil.hasGenerics(javaReturnSchema)) {
        // it is important to do this eagerly and not in the lambda so that
        // we can fail early (when loading the UDF) instead of when the user
        // attempts to use the UDF
        final SqlType sqlType = fromJavaType(javaReturnSchema, functionName);
        schemaProvider = args -> sqlType;
    } else {
        schemaProvider = null;
    }
    return (parameters, arguments) -> {
        if (schemaProvider != null) {
            final SqlType returnType = schemaProvider.apply(arguments);
            if (!(ParamTypes.areCompatible(SqlArgument.of(returnType), javaReturnSchema, false))) {
                throw new KsqlException(String.format("Return type %s of UDF %s does not match the declared " + "return type %s.", returnType, functionName.toUpperCase(), SchemaConverters.functionToSqlConverter().toSqlType(javaReturnSchema)));
            }
            return returnType;
        }
        final Map<GenericType, SqlType> genericMapping = new HashMap<>();
        for (int i = 0; i < Math.min(parameters.size(), arguments.size()); i++) {
            final ParamType schema = parameters.get(i);
            if (schema instanceof LambdaType) {
                if (isVariadic && i == parameters.size() - 1) {
                    throw new KsqlException(String.format("Lambda function %s cannot be variadic.", arguments.get(i).toString()));
                }
                genericMapping.putAll(GenericsUtil.reserveGenerics(schema, arguments.get(i)));
            } else {
                // we resolve any variadic as if it were an array so that the type
                // structure matches the input type
                final SqlType instance = isVariadic && i == parameters.size() - 1 ? SqlTypes.array(arguments.get(i).getSqlTypeOrThrow()) : arguments.get(i).getSqlTypeOrThrow();
                genericMapping.putAll(GenericsUtil.reserveGenerics(schema, SqlArgument.of(instance)));
            }
        }
        return GenericsUtil.applyResolved(javaReturnSchema, genericMapping);
    };
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) UdfParameter(io.confluent.ksql.function.udf.UdfParameter) ParamType(io.confluent.ksql.function.types.ParamType) HashMap(java.util.HashMap) Function(java.util.function.Function) UdfUtil(io.confluent.ksql.execution.function.UdfUtil) Parameter(java.lang.reflect.Parameter) Map(java.util.Map) SqlTypeParser(io.confluent.ksql.schema.ksql.SqlTypeParser) SchemaConverters(io.confluent.ksql.schema.ksql.SchemaConverters) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Method(java.lang.reflect.Method) Udf(io.confluent.ksql.function.udf.Udf) SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) ParamTypes(io.confluent.ksql.function.types.ParamTypes) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) LambdaType(io.confluent.ksql.function.types.LambdaType) List(java.util.List) Type(java.lang.reflect.Type) UdfSchemaProvider(io.confluent.ksql.function.udf.UdfSchemaProvider) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) SqlTypes(io.confluent.ksql.schema.ksql.types.SqlTypes) GenericType(io.confluent.ksql.function.types.GenericType) LambdaType(io.confluent.ksql.function.types.LambdaType) List(java.util.List) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) KsqlException(io.confluent.ksql.util.KsqlException) HashMap(java.util.HashMap) Map(java.util.Map) ParamType(io.confluent.ksql.function.types.ParamType)

Example 13 with LambdaType

use of io.confluent.ksql.function.types.LambdaType in project ksql by confluentinc.

the class UdfUtilTest method shouldGetBiFunction.

@Test
public void shouldGetBiFunction() throws NoSuchMethodException {
    final Type type = getClass().getDeclaredMethod("biFunctionType", BiFunction.class).getGenericParameterTypes()[0];
    final ParamType schema = UdfUtil.getSchemaFromType(type);
    assertThat(schema, instanceOf(LambdaType.class));
    assertThat(((LambdaType) schema).inputTypes(), equalTo(ImmutableList.of(ParamTypes.LONG, ParamTypes.INTEGER)));
    assertThat(((LambdaType) schema).returnType(), equalTo(ParamTypes.BOOLEAN));
}
Also used : LambdaType(io.confluent.ksql.function.types.LambdaType) 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

LambdaType (io.confluent.ksql.function.types.LambdaType)13 GenericType (io.confluent.ksql.function.types.GenericType)11 Test (org.junit.Test)10 SqlArgument (io.confluent.ksql.schema.ksql.SqlArgument)9 ParamType (io.confluent.ksql.function.types.ParamType)6 MapType (io.confluent.ksql.function.types.MapType)5 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)5 HashMap (java.util.HashMap)5 ArrayType (io.confluent.ksql.function.types.ArrayType)4 StructType (io.confluent.ksql.function.types.StructType)4 Type (java.lang.reflect.Type)4 KsqlException (io.confluent.ksql.util.KsqlException)2 Optional (java.util.Optional)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Expression (io.confluent.ksql.execution.expression.tree.Expression)1 LambdaFunctionCall (io.confluent.ksql.execution.expression.tree.LambdaFunctionCall)1 UdfUtil (io.confluent.ksql.execution.function.UdfUtil)1 KsqlScalarFunction (io.confluent.ksql.function.KsqlScalarFunction)1 ParamTypes (io.confluent.ksql.function.types.ParamTypes)1 Udf (io.confluent.ksql.function.udf.Udf)1