Search in sources :

Example 1 with ArrayType

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

the class GenericsUtilTest method shouldIdentifyArrayGeneric.

@Test
public void shouldIdentifyArrayGeneric() {
    // Given:
    final ArrayType a = ArrayType.of(GenericType.of("A"));
    final SqlArgument instance = SqlArgument.of(SqlTypes.array(SqlTypes.STRING));
    // When:
    final Map<GenericType, SqlType> mapping = GenericsUtil.reserveGenerics(a, instance);
    // Then:
    assertThat(mapping, hasEntry(a.element(), SqlTypes.STRING));
}
Also used : ArrayType(io.confluent.ksql.function.types.ArrayType) SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) GenericType(io.confluent.ksql.function.types.GenericType) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Test(org.junit.Test)

Example 2 with ArrayType

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

the class DescribeFunctionExecutor method getFunctionInfo.

private static FunctionInfo getFunctionInfo(final List<ParameterInfo> argTypes, final ParamType returnTypeSchema, final String description, final boolean variadic) {
    final List<ArgumentInfo> args = new ArrayList<>();
    for (int i = 0; i < argTypes.size(); i++) {
        final ParameterInfo param = argTypes.get(i);
        final boolean isVariadic = variadic && i == (argTypes.size() - 1);
        final String type = isVariadic ? ((ArrayType) param.type()).element().toString() : param.type().toString();
        args.add(new ArgumentInfo(param.name(), type, param.description(), isVariadic));
    }
    return new FunctionInfo(args, returnTypeSchema.toString(), description);
}
Also used : ArrayType(io.confluent.ksql.function.types.ArrayType) ArrayList(java.util.ArrayList) FunctionInfo(io.confluent.ksql.rest.entity.FunctionInfo) ParameterInfo(io.confluent.ksql.function.ParameterInfo) ArgumentInfo(io.confluent.ksql.rest.entity.ArgumentInfo)

Example 3 with ArrayType

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

the class UdfIndexTest method shouldMatchNestedGenericMethodWithMultipleGenerics.

@Test
public void shouldMatchNestedGenericMethodWithMultipleGenerics() {
    // Given:
    final ArrayType generic = ArrayType.of(GenericType.of("A"));
    givenFunctions(function(EXPECTED, false, generic, generic));
    // When:
    final KsqlScalarFunction fun = udfIndex.getFunction(ImmutableList.of(SqlArgument.of(SqlArray.of(SqlTypes.INTEGER)), SqlArgument.of(SqlArray.of(SqlTypes.INTEGER))));
    // Then:
    assertThat(fun.name(), equalTo(EXPECTED));
}
Also used : ArrayType(io.confluent.ksql.function.types.ArrayType) Test(org.junit.Test)

Example 4 with ArrayType

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

the class UdfIndexTest method shouldNotMatchNestedGenericMethodWithAlreadyReservedTypes.

@Test
public void shouldNotMatchNestedGenericMethodWithAlreadyReservedTypes() {
    // Given:
    final ArrayType generic = ArrayType.of(GenericType.of("A"));
    givenFunctions(function(EXPECTED, false, generic, generic));
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> udfIndex.getFunction(ImmutableList.of(SqlArgument.of(SqlArray.of(SqlTypes.INTEGER)), SqlArgument.of(SqlArray.of(SqlTypes.STRING)))));
    // Then:
    assertThat(e.getMessage(), containsString("Function 'name' does not accept parameters " + "(ARRAY<INTEGER>, ARRAY<STRING>)"));
}
Also used : ArrayType(io.confluent.ksql.function.types.ArrayType) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 5 with ArrayType

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

the class GenericsUtil method applyResolved.

/**
 * Replaces all generics in a schema with concrete schemas defined in {@code resolved}
 *
 * @param schema    the schema which may contain generics
 * @param resolved  the mapping from generics to resolved types
 * @return a schema with the same structure as {@code schema} but with no generics
 *
 * @throws KsqlException if there is a generic in {@code schema} that is not present
 *                       in {@code mapping}
 */
public static SqlType applyResolved(final ParamType schema, final Map<GenericType, SqlType> resolved) {
    if (schema instanceof ArrayType) {
        return SqlTypes.array(applyResolved(((ArrayType) schema).element(), resolved));
    }
    if (schema instanceof MapType) {
        final MapType mapType = (MapType) schema;
        final SqlType keyType = applyResolved(mapType.key(), resolved);
        final SqlType valueType = applyResolved(mapType.value(), resolved);
        return SqlTypes.map(keyType, valueType);
    }
    if (schema instanceof StructType) {
        final Builder struct = SqlTypes.struct();
        ((StructType) schema).getSchema().forEach((fieldName, type) -> struct.field(fieldName, applyResolved(type, resolved)));
        return struct.build();
    }
    if (schema instanceof GenericType) {
        final SqlType instance = resolved.get(schema);
        if (instance == null) {
            throw new KsqlException("Could not find mapping for generic type: " + schema);
        }
        return instance;
    }
    return SchemaConverters.functionToSqlConverter().toSqlType(schema);
}
Also used : ArrayType(io.confluent.ksql.function.types.ArrayType) GenericType(io.confluent.ksql.function.types.GenericType) StructType(io.confluent.ksql.function.types.StructType) Builder(io.confluent.ksql.schema.ksql.types.SqlStruct.Builder) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) KsqlException(io.confluent.ksql.util.KsqlException) MapType(io.confluent.ksql.function.types.MapType)

Aggregations

ArrayType (io.confluent.ksql.function.types.ArrayType)8 Test (org.junit.Test)5 GenericType (io.confluent.ksql.function.types.GenericType)4 KsqlException (io.confluent.ksql.util.KsqlException)4 MapType (io.confluent.ksql.function.types.MapType)3 StructType (io.confluent.ksql.function.types.StructType)3 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)3 LambdaType (io.confluent.ksql.function.types.LambdaType)2 ParamType (io.confluent.ksql.function.types.ParamType)2 ParameterInfo (io.confluent.ksql.function.ParameterInfo)1 ArgumentInfo (io.confluent.ksql.rest.entity.ArgumentInfo)1 FunctionInfo (io.confluent.ksql.rest.entity.FunctionInfo)1 SqlArgument (io.confluent.ksql.schema.ksql.SqlArgument)1 SqlArray (io.confluent.ksql.schema.ksql.types.SqlArray)1 SqlLambda (io.confluent.ksql.schema.ksql.types.SqlLambda)1 SqlLambdaResolved (io.confluent.ksql.schema.ksql.types.SqlLambdaResolved)1 SqlMap (io.confluent.ksql.schema.ksql.types.SqlMap)1 Builder (io.confluent.ksql.schema.ksql.types.SqlStruct.Builder)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1