Search in sources :

Example 1 with SqlLambdaResolved

use of io.confluent.ksql.schema.ksql.types.SqlLambdaResolved in project ksql by confluentinc.

the class UdfLoaderTest method shouldLoadLambdaReduceUdfs.

@Test
public void shouldLoadLambdaReduceUdfs() {
    // Given:
    final SqlLambdaResolved lambda = SqlLambdaResolved.of(ImmutableList.of(SqlTypes.INTEGER, SqlTypes.INTEGER, SqlTypes.INTEGER), SqlTypes.INTEGER);
    // When:
    final KsqlScalarFunction fun = FUNC_REG.getUdfFactory(FunctionName.of("reduce")).getFunction(ImmutableList.of(SqlArgument.of(SqlMap.of(SqlTypes.INTEGER, SqlTypes.INTEGER)), SqlArgument.of(SqlTypes.INTEGER), SqlArgument.of(lambda)));
    // Then:
    assertThat(fun.name().text(), equalToIgnoringCase("reduce"));
}
Also used : SqlLambdaResolved(io.confluent.ksql.schema.ksql.types.SqlLambdaResolved) Test(org.junit.Test)

Example 2 with SqlLambdaResolved

use of io.confluent.ksql.schema.ksql.types.SqlLambdaResolved in project ksql by confluentinc.

the class UdfLoaderTest method shouldLoadLambdaTransformUdfs.

@Test
public void shouldLoadLambdaTransformUdfs() {
    // Given:
    final SqlLambdaResolved lambda = SqlLambdaResolved.of(ImmutableList.of(SqlTypes.INTEGER), SqlTypes.INTEGER);
    // When:
    final KsqlScalarFunction fun = FUNC_REG.getUdfFactory(FunctionName.of("transform")).getFunction(ImmutableList.of(SqlArgument.of(SqlArray.of(SqlTypes.INTEGER)), SqlArgument.of(lambda)));
    // Then:
    assertThat(fun.name().text(), equalToIgnoringCase("transform"));
}
Also used : SqlLambdaResolved(io.confluent.ksql.schema.ksql.types.SqlLambdaResolved) Test(org.junit.Test)

Example 3 with SqlLambdaResolved

use of io.confluent.ksql.schema.ksql.types.SqlLambdaResolved in project ksql by confluentinc.

the class GenericsUtil method resolveGenerics.

/**
 * Identifies a mapping from generic type to concrete type based on a {@code schema} and
 * an {@code instance}, where the {@code instance} schema is expected to have no generic
 * types and have the same nested structure as {@code schema}. Any Generic type mapping
 * identified is added to the list passed in.
 *
 * @param mapping   a list of GenericType to SqlType mappings
 * @param schema    the schema that may contain generics
 * @param instance  a schema with the same structure as {@code schema} but with no generics
 *
 * @return whether we were able to resolve generics in the instance and schema
 */
// CHECKSTYLE_RULES.OFF: NPathComplexity
// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
private static boolean resolveGenerics(final List<Entry<GenericType, SqlType>> mapping, final ParamType schema, final SqlArgument instance) {
    if (!isGeneric(schema) && !matches(schema, instance)) {
        // cannot identify from type mismatch
        return false;
    } else if (!hasGenerics(schema)) {
        // nothing left to identify
        return true;
    }
    KsqlPreconditions.checkArgument(isGeneric(schema) || (matches(schema, instance)), "Cannot resolve generics if the schema and instance have differing types: " + schema + " vs. " + instance);
    if (schema instanceof LambdaType) {
        final LambdaType lambdaType = (LambdaType) schema;
        final SqlLambda sqlLambda = instance.getSqlLambdaOrThrow();
        if (lambdaType.inputTypes().size() == sqlLambda.getNumInputs()) {
            if (sqlLambda instanceof SqlLambdaResolved) {
                final SqlLambdaResolved sqlLambdaResolved = (SqlLambdaResolved) sqlLambda;
                int i = 0;
                for (final ParamType paramType : lambdaType.inputTypes()) {
                    if (!resolveGenerics(mapping, paramType, SqlArgument.of(sqlLambdaResolved.getInputType().get(i)))) {
                        return false;
                    }
                    i++;
                }
                return resolveGenerics(mapping, lambdaType.returnType(), SqlArgument.of(sqlLambdaResolved.getReturnType()));
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
    final SqlType sqlType = instance.getSqlTypeOrThrow();
    if (isGeneric(schema)) {
        mapping.add(new HashMap.SimpleEntry<>((GenericType) schema, sqlType));
    }
    if (schema instanceof ArrayType) {
        final SqlArray sqlArray = (SqlArray) sqlType;
        return resolveGenerics(mapping, ((ArrayType) schema).element(), SqlArgument.of(sqlArray.getItemType()));
    }
    if (schema instanceof MapType) {
        final SqlMap sqlMap = (SqlMap) sqlType;
        final MapType mapType = (MapType) schema;
        return resolveGenerics(mapping, mapType.key(), SqlArgument.of(sqlMap.getKeyType())) && resolveGenerics(mapping, mapType.value(), SqlArgument.of(sqlMap.getValueType()));
    }
    if (schema instanceof StructType) {
        throw new KsqlException("Generic STRUCT is not yet supported");
    }
    return true;
}
Also used : LambdaType(io.confluent.ksql.function.types.LambdaType) GenericType(io.confluent.ksql.function.types.GenericType) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) StructType(io.confluent.ksql.function.types.StructType) SqlLambda(io.confluent.ksql.schema.ksql.types.SqlLambda) HashMap(java.util.HashMap) KsqlException(io.confluent.ksql.util.KsqlException) ParamType(io.confluent.ksql.function.types.ParamType) MapType(io.confluent.ksql.function.types.MapType) ArrayType(io.confluent.ksql.function.types.ArrayType) SqlLambdaResolved(io.confluent.ksql.schema.ksql.types.SqlLambdaResolved) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray)

Example 4 with SqlLambdaResolved

use of io.confluent.ksql.schema.ksql.types.SqlLambdaResolved in project ksql by confluentinc.

the class ParamTypes method areCompatible.

// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
// CHECKSTYLE_RULES.OFF: NPathComplexity
public static boolean areCompatible(final SqlArgument argument, final ParamType declared, final boolean allowCast) {
    // CHECKSTYLE_RULES.ON: CyclomaticComplexity
    // CHECKSTYLE_RULES.ON: NPathComplexity
    final Optional<SqlLambda> sqlLambdaOptional = argument.getSqlLambda();
    if (sqlLambdaOptional.isPresent() && declared instanceof LambdaType) {
        final LambdaType declaredLambda = (LambdaType) declared;
        final SqlLambda sqlLambda = sqlLambdaOptional.get();
        if (sqlLambda instanceof SqlLambdaResolved) {
            final SqlLambdaResolved sqlLambdaResolved = (SqlLambdaResolved) sqlLambda;
            if (sqlLambdaResolved.getInputType().size() != declaredLambda.inputTypes().size()) {
                return false;
            }
            int i = 0;
            for (final ParamType paramType : declaredLambda.inputTypes()) {
                if (!areCompatible(SqlArgument.of(sqlLambdaResolved.getInputType().get(i)), paramType, allowCast)) {
                    return false;
                }
                i++;
            }
            return areCompatible(SqlArgument.of(sqlLambdaResolved.getReturnType()), declaredLambda.returnType(), allowCast);
        } else {
            return sqlLambda.getNumInputs() == declaredLambda.inputTypes().size();
        }
    }
    if (argument.getSqlIntervalUnit().isPresent() && declared instanceof IntervalUnitType) {
        return true;
    } else if (argument.getSqlIntervalUnit().isPresent() || declared instanceof IntervalUnitType) {
        return false;
    }
    final SqlType argumentSqlType = argument.getSqlTypeOrThrow();
    if (argumentSqlType.baseType() == SqlBaseType.ARRAY && declared instanceof ArrayType) {
        return areCompatible(SqlArgument.of(((SqlArray) argumentSqlType).getItemType()), ((ArrayType) declared).element(), allowCast);
    }
    if (argumentSqlType.baseType() == SqlBaseType.MAP && declared instanceof MapType) {
        final SqlMap sqlType = (SqlMap) argumentSqlType;
        final MapType mapType = (MapType) declared;
        return areCompatible(SqlArgument.of(sqlType.getKeyType()), mapType.key(), allowCast) && areCompatible(SqlArgument.of(sqlType.getValueType()), mapType.value(), allowCast);
    }
    if (argumentSqlType.baseType() == SqlBaseType.STRUCT && declared instanceof StructType) {
        return isStructCompatible(argumentSqlType, declared);
    }
    return isPrimitiveMatch(argumentSqlType, declared, allowCast);
}
Also used : SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) SqlLambda(io.confluent.ksql.schema.ksql.types.SqlLambda) SqlLambdaResolved(io.confluent.ksql.schema.ksql.types.SqlLambdaResolved) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray)

Aggregations

SqlLambdaResolved (io.confluent.ksql.schema.ksql.types.SqlLambdaResolved)4 SqlArray (io.confluent.ksql.schema.ksql.types.SqlArray)2 SqlLambda (io.confluent.ksql.schema.ksql.types.SqlLambda)2 SqlMap (io.confluent.ksql.schema.ksql.types.SqlMap)2 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)2 Test (org.junit.Test)2 ArrayType (io.confluent.ksql.function.types.ArrayType)1 GenericType (io.confluent.ksql.function.types.GenericType)1 LambdaType (io.confluent.ksql.function.types.LambdaType)1 MapType (io.confluent.ksql.function.types.MapType)1 ParamType (io.confluent.ksql.function.types.ParamType)1 StructType (io.confluent.ksql.function.types.StructType)1 KsqlException (io.confluent.ksql.util.KsqlException)1 HashMap (java.util.HashMap)1