Search in sources :

Example 26 with ArithmeticBinaryExpression

use of io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression in project ksql by confluentinc.

the class SqlToJavaVisitorTest method shouldGenerateCastDecimalToDoubleInBinaryExpression.

@Test
public void shouldGenerateCastDecimalToDoubleInBinaryExpression() {
    // Given:
    final ArithmeticBinaryExpression binExp = new ArithmeticBinaryExpression(Operator.ADD, new UnqualifiedColumnReferenceExp(ColumnName.of("COL8")), new UnqualifiedColumnReferenceExp(ColumnName.of("COL3")));
    // When:
    final String java = sqlToJavaVisitor.process(binExp);
    // Then:
    final String doubleCast = CastEvaluator.generateCode("COL8", SqlTypes.decimal(2, 1), SqlTypes.DOUBLE, ksqlConfig);
    assertThat(java, containsString(doubleCast));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Test(org.junit.Test)

Example 27 with ArithmeticBinaryExpression

use of io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression in project ksql by confluentinc.

the class FunctionArgumentsUtilTest method shouldResolveLambdaWithoutGenerics.

@Test
public void shouldResolveLambdaWithoutGenerics() {
    // Given:
    givenUdfWithNameAndReturnType("SmallLambda", SqlTypes.DOUBLE);
    when(function.parameters()).thenReturn(ImmutableList.of(ArrayType.of(ParamTypes.DOUBLE), LambdaType.of(ImmutableList.of(ParamTypes.INTEGER), ParamTypes.INTEGER)));
    final FunctionCall expression = new FunctionCall(FunctionName.of("SmallLambda"), ImmutableList.of(ARRAYCOL, new LambdaFunctionCall(ImmutableList.of("2.3"), new ArithmeticBinaryExpression(Operator.ADD, new DoubleLiteral(2.3), new DoubleLiteral(2.3)))));
    // When:
    final FunctionTypeInfo argumentsAndContexts = FunctionArgumentsUtil.getFunctionTypeInfo(expressionTypeManager, expression, udfFactory, Collections.emptyMap());
    // Then:
    assertThat(argumentsAndContexts.getReturnType(), is(SqlTypes.DOUBLE));
    assertThat(argumentsAndContexts.getArgumentInfos().size(), is(2));
    verify(udfFactory).getFunction(ImmutableList.of(SqlArgument.of(SqlTypes.array(SqlTypes.DOUBLE)), SqlArgument.of(SqlLambda.of(1))));
    verify(function).getReturnType(ImmutableList.of(SqlArgument.of(SqlTypes.array(SqlTypes.DOUBLE)), SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.INTEGER), SqlTypes.DOUBLE))));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) FunctionTypeInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.FunctionTypeInfo) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) Test(org.junit.Test)

Example 28 with ArithmeticBinaryExpression

use of io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression in project ksql by confluentinc.

the class ExpressionTypeManagerTest method shouldEvaluateLambdaInUDFWithArray.

@Test
public void shouldEvaluateLambdaInUDFWithArray() {
    // Given:
    givenUdfWithNameAndReturnType("TRANSFORM", SqlTypes.DOUBLE);
    when(function.parameters()).thenReturn(ImmutableList.of(ArrayType.of(DoubleType.INSTANCE), LambdaType.of(ImmutableList.of(DoubleType.INSTANCE), DoubleType.INSTANCE)));
    final Expression expression = new FunctionCall(FunctionName.of("TRANSFORM"), ImmutableList.of(ARRAYCOL, new LambdaFunctionCall(ImmutableList.of("X"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("X"), new IntegerLiteral(5)))));
    // When:
    final SqlType exprType = expressionTypeManager.getExpressionSqlType(expression);
    // Then:
    assertThat(exprType, is(SqlTypes.DOUBLE));
    verify(udfFactory).getFunction(ImmutableList.of(SqlArgument.of(SqlTypes.array(SqlTypes.DOUBLE)), SqlArgument.of(SqlLambda.of(1))));
    verify(function).getReturnType(ImmutableList.of(SqlArgument.of(SqlTypes.array(SqlTypes.DOUBLE)), SqlArgument.of(SqlLambdaResolved.of(ImmutableList.of(SqlTypes.DOUBLE), SqlTypes.DOUBLE))));
}
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 29 with ArithmeticBinaryExpression

use of io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression in project ksql by confluentinc.

the class ExpressionTypeManagerTest method shouldResolveTypeForAddDoubleIntegerLiteral.

@Test
public void shouldResolveTypeForAddDoubleIntegerLiteral() {
    final Expression expression = new ArithmeticBinaryExpression(Operator.ADD, COL3, literal(10));
    final SqlType type = expressionTypeManager.getExpressionSqlType(expression);
    assertThat(type, is(SqlTypes.DOUBLE));
}
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) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Test(org.junit.Test)

Example 30 with ArithmeticBinaryExpression

use of io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression in project ksql by confluentinc.

the class ExpressionTypeManagerTest method shouldFailIfWhenIsNotBoolean.

@Test
public void shouldFailIfWhenIsNotBoolean() {
    // Given:
    final Expression expression = new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ArithmeticBinaryExpression(Operator.ADD, TestExpressions.COL0, new IntegerLiteral(10)), new StringLiteral("foo"))), Optional.empty());
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> expressionTypeManager.getExpressionSqlType(expression));
    // Then:
    assertThat(e.getMessage(), containsString("WHEN operand type should be boolean." + System.lineSeparator() + "Type for '(COL0 + 10)' is BIGINT"));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) 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) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Aggregations

ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)37 Test (org.junit.Test)37 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)20 Expression (io.confluent.ksql.execution.expression.tree.Expression)20 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)19 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)19 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)19 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)19 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)19 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)19 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)17 FunctionCall (io.confluent.ksql.execution.expression.tree.FunctionCall)16 LambdaFunctionCall (io.confluent.ksql.execution.expression.tree.LambdaFunctionCall)16 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)15 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)15 LambdaVariable (io.confluent.ksql.execution.expression.tree.LambdaVariable)15 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)13 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)11 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)11 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)9