Search in sources :

Example 6 with ArrayType

use of io.confluent.ksql.function.types.ArrayType 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 7 with ArrayType

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

the class UdfIndexTest method shouldIncludeAvailableSignaturesIfNotMatchFound.

@Test
public void shouldIncludeAvailableSignaturesIfNotMatchFound() {
    // Given:
    final ArrayType generic = of(GenericType.of("A"));
    givenFunctions(function(OTHER, true, false, STRING, INT), function(OTHER, true, STRING_VARARGS), function(OTHER, false, generic));
    // When:
    final Exception e = assertThrows(Exception.class, () -> udfIndex.getFunction(ImmutableList.of(SqlArgument.of(SqlTypes.STRING), SqlArgument.of(INTEGER), SqlArgument.of(SqlTypes.STRING))));
    // Then:
    assertThat(e.getMessage(), containsString("Valid alternatives are:" + lineSeparator() + "other(VARCHAR...)" + lineSeparator() + "other(ARRAY<A>)" + lineSeparator() + "other(VARCHAR paramName, INT paramName)"));
}
Also used : ArrayType(io.confluent.ksql.function.types.ArrayType) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 8 with ArrayType

use of io.confluent.ksql.function.types.ArrayType 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)

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