Search in sources :

Example 1 with LambdaType

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

the class GenericsUtilTest method shouldFailToIdentifyLambdasWithDifferentArgumentList.

@Test
public void shouldFailToIdentifyLambdasWithDifferentArgumentList() {
    // Given:
    final GenericType typeA = GenericType.of("A");
    final GenericType typeB = GenericType.of("B");
    final GenericType typeC = GenericType.of("C");
    final LambdaType a = LambdaType.of(ImmutableList.of(typeA, typeC), typeB);
    final SqlArgument instance = SqlArgument.of(SqlLambda.of(4));
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> GenericsUtil.reserveGenerics(a, instance));
    // Then:
    assertThat(e.getMessage(), containsString("Cannot infer generics for LAMBDA (A, C) => B from LAMBDA " + "because they do not have the same schema structure."));
}
Also used : LambdaType(io.confluent.ksql.function.types.LambdaType) GenericType(io.confluent.ksql.function.types.GenericType) SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) Test(org.junit.Test)

Example 2 with LambdaType

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

the class GenericsUtilTest method shouldFailToIdentifyMismatchedGenericsInLambda.

@Test
public void shouldFailToIdentifyMismatchedGenericsInLambda() {
    // Given:
    final GenericType typeA = GenericType.of("A");
    final LambdaType a = LambdaType.of(ImmutableList.of(typeA), typeA);
    final SqlArgument instance = SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.DOUBLE), SqlTypes.BOOLEAN));
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> GenericsUtil.reserveGenerics(a, instance));
    // Then:
    assertThat(e.getMessage(), containsString("Found invalid instance of generic schema when mapping LAMBDA (A) => A to LAMBDA (DOUBLE) => BOOLEAN. " + "Cannot map A to both DOUBLE and BOOLEAN"));
}
Also used : LambdaType(io.confluent.ksql.function.types.LambdaType) GenericType(io.confluent.ksql.function.types.GenericType) SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) Test(org.junit.Test)

Example 3 with LambdaType

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

the class GenericsUtilTest method shouldNotMapGenericsForNonSqlLambdaResolved.

@Test
public void shouldNotMapGenericsForNonSqlLambdaResolved() {
    // Given:
    final GenericType typeA = GenericType.of("A");
    final GenericType typeB = GenericType.of("B");
    final GenericType typeC = GenericType.of("C");
    final LambdaType a = LambdaType.of(ImmutableList.of(typeA, typeC), typeB);
    final SqlArgument instance = SqlArgument.of(SqlLambda.of(2));
    // When:
    final Map<GenericType, SqlType> mapping = GenericsUtil.reserveGenerics(a, instance);
    // Then:
    // the map should be empty since the instance type was a SqlLambda without any types resolved
    assertThat(mapping.size(), is(0));
}
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 4 with LambdaType

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

the class GenericsUtilTest method shouldIdentifyInstanceOfLambda.

@Test
public void shouldIdentifyInstanceOfLambda() {
    // Given:
    final LambdaType lambda = LambdaType.of(ImmutableList.of(GenericType.of("A")), GenericType.of("B"));
    final SqlArgument instance = SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.INTEGER), SqlTypes.BIGINT));
    // When:
    final boolean isInstance = GenericsUtil.reserveGenerics(lambda, instance, new HashMap<>()).getLeft();
    // Then:
    assertThat("expected instance of", isInstance);
}
Also used : LambdaType(io.confluent.ksql.function.types.LambdaType) SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 5 with LambdaType

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

the class GenericsUtilTest method shouldNotIdentifyInstanceOfTypeMismatchLambda.

@Test
public void shouldNotIdentifyInstanceOfTypeMismatchLambda() {
    // Given:
    final MapType map = MapType.of(GenericType.of("A"), GenericType.of("B"));
    final SqlArgument lambdaInstance = SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.INTEGER), SqlTypes.BIGINT));
    final LambdaType lambda = LambdaType.of(ImmutableList.of(GenericType.of("A")), GenericType.of("B"));
    final SqlArgument mapInstance = SqlArgument.of(SqlTypes.map(SqlTypes.STRING, SqlTypes.BOOLEAN));
    // When:
    final boolean isInstance1 = GenericsUtil.reserveGenerics(map, lambdaInstance, new HashMap<>()).getLeft();
    final boolean isInstance2 = GenericsUtil.reserveGenerics(lambda, mapInstance, new HashMap<>()).getLeft();
    // Then:
    assertThat("expected not instance of", !isInstance1);
    assertThat("expected not instance of", !isInstance2);
}
Also used : LambdaType(io.confluent.ksql.function.types.LambdaType) SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) HashMap(java.util.HashMap) MapType(io.confluent.ksql.function.types.MapType) 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