Search in sources :

Example 31 with SqlType

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

the class UdfIndex method getCandidates.

private void getCandidates(final List<SqlArgument> arguments, final int argIndex, final Node current, final List<Node> candidates, final Map<GenericType, SqlType> reservedGenerics, final boolean allowCasts) {
    if (argIndex == arguments.size()) {
        if (current.value != null) {
            candidates.add(current);
        }
        return;
    }
    final SqlArgument arg = arguments.get(argIndex);
    for (final Entry<Parameter, Node> candidate : current.children.entrySet()) {
        final Map<GenericType, SqlType> reservedCopy = new HashMap<>(reservedGenerics);
        if (candidate.getKey().accepts(arg, reservedCopy, allowCasts)) {
            final Node node = candidate.getValue();
            getCandidates(arguments, argIndex + 1, node, candidates, reservedCopy, allowCasts);
        }
    }
}
Also used : SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) GenericType(io.confluent.ksql.function.types.GenericType) HashMap(java.util.HashMap) SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

Example 32 with SqlType

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

the class ExpressionTypeManagerTest method shouldEvaluateAnyNumberOfArgumentLambda.

@Test
public void shouldEvaluateAnyNumberOfArgumentLambda() {
    // Given:
    givenUdfWithNameAndReturnType("TRANSFORM", SqlTypes.STRING);
    when(function.parameters()).thenReturn(ImmutableList.of(ArrayType.of(DoubleType.INSTANCE), StringType.INSTANCE, MapType.of(LongType.INSTANCE, DoubleType.INSTANCE), LambdaType.of(ImmutableList.of(DoubleType.INSTANCE, StringType.INSTANCE, LongType.INSTANCE, DoubleType.INSTANCE), StringType.INSTANCE)));
    final Expression expression = new FunctionCall(FunctionName.of("TRANSFORM"), ImmutableList.of(ARRAYCOL, new StringLiteral("Q"), MAPCOL, new LambdaFunctionCall(ImmutableList.of("A", "B", "C", "D"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("C"), new IntegerLiteral(5)))));
    // When:
    final SqlType exprType = expressionTypeManager.getExpressionSqlType(expression);
    // Then:
    assertThat(exprType, is(SqlTypes.STRING));
    verify(udfFactory).getFunction(ImmutableList.of(SqlArgument.of(SqlTypes.array(SqlTypes.DOUBLE)), SqlArgument.of(SqlTypes.STRING), SqlArgument.of(SqlTypes.map(SqlTypes.BIGINT, SqlTypes.DOUBLE)), SqlArgument.of(SqlLambda.of(4))));
    verify(function).getReturnType(ImmutableList.of(SqlArgument.of(SqlTypes.array(SqlTypes.DOUBLE)), SqlArgument.of(SqlTypes.STRING), SqlArgument.of(SqlTypes.map(SqlTypes.BIGINT, SqlTypes.DOUBLE)), SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.DOUBLE, SqlTypes.STRING, SqlTypes.BIGINT, SqlTypes.DOUBLE), SqlTypes.BIGINT))));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 33 with SqlType

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

the class ExpressionTypeManagerTest method shouldHandleMultipleLambdasInSameFunctionCallWithDifferentVariableNames.

@Test
public void shouldHandleMultipleLambdasInSameFunctionCallWithDifferentVariableNames() {
    // Given:
    givenUdfWithNameAndReturnType("TRANSFORM", SqlTypes.INTEGER);
    when(function.parameters()).thenReturn(ImmutableList.of(MapType.of(LongType.INSTANCE, DoubleType.INSTANCE), IntegerType.INSTANCE, LambdaType.of(ImmutableList.of(DoubleType.INSTANCE, DoubleType.INSTANCE), StringType.INSTANCE), LambdaType.of(ImmutableList.of(DoubleType.INSTANCE, DoubleType.INSTANCE), StringType.INSTANCE)));
    final Expression expression = new ArithmeticBinaryExpression(Operator.ADD, new FunctionCall(FunctionName.of("TRANSFORM"), ImmutableList.of(MAPCOL, new IntegerLiteral(0), new LambdaFunctionCall(ImmutableList.of("A", "B"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("A"), new LambdaVariable("B"))), new LambdaFunctionCall(ImmutableList.of("K", "V"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("K"), new LambdaVariable("V"))))), new IntegerLiteral(5));
    // When:
    final SqlType result = expressionTypeManager.getExpressionSqlType(expression);
    assertThat(result, is(SqlTypes.INTEGER));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 34 with SqlType

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

the class ExpressionTypeManagerTest method shouldEvaluateBooleanSchemaForNotLikeExpression.

@Test
public void shouldEvaluateBooleanSchemaForNotLikeExpression() {
    final Expression expression = new NotExpression(new LikePredicate(COL1, new StringLiteral("%foo"), Optional.empty()));
    final SqlType exprType0 = expressionTypeManager.getExpressionSqlType(expression);
    assertThat(exprType0, is(SqlTypes.BOOLEAN));
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) LikePredicate(io.confluent.ksql.execution.expression.tree.LikePredicate) Test(org.junit.Test)

Example 35 with SqlType

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

the class ExpressionTypeManagerTest method testComparisonExpr.

@Test
public void testComparisonExpr() {
    final Expression expression = new ComparisonExpression(Type.GREATER_THAN, TestExpressions.COL0, COL3);
    final SqlType exprType = expressionTypeManager.getExpressionSqlType(expression);
    assertThat(exprType, is(SqlTypes.BOOLEAN));
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Test(org.junit.Test)

Aggregations

SqlType (io.confluent.ksql.schema.ksql.types.SqlType)140 Test (org.junit.Test)80 Expression (io.confluent.ksql.execution.expression.tree.Expression)47 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)38 KsqlException (io.confluent.ksql.util.KsqlException)33 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)30 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)29 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)29 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)29 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)29 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)29 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)29 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)29 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)29 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)29 Optional (java.util.Optional)20 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)15 Result (io.confluent.ksql.schema.ksql.SqlValueCoercer.Result)14 Struct (org.apache.kafka.connect.data.Struct)14 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)13