Search in sources :

Example 1 with ArithmeticBinaryExpression

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

the class InterpretedExpressionTest method shouldEvaluateArithmetic.

@Test
public void shouldEvaluateArithmetic() {
    // Given:
    final Expression expression1 = new ArithmeticBinaryExpression(Operator.ADD, new IntegerLiteral(1), new IntegerLiteral(2));
    final Expression expression2 = new ArithmeticBinaryExpression(Operator.ADD, new IntegerLiteral(1), new LongLiteral(4));
    final Expression expression3 = new ArithmeticBinaryExpression(Operator.ADD, new DoubleLiteral(5.5), new LongLiteral(4));
    final Expression expression4 = new ArithmeticBinaryExpression(Operator.MULTIPLY, new IntegerLiteral(5), new LongLiteral(4));
    final Expression expression5 = new ArithmeticBinaryExpression(Operator.DIVIDE, new LongLiteral(18), new LongLiteral(3));
    final Expression expression6 = new ArithmeticBinaryExpression(Operator.MODULUS, new LongLiteral(20), new LongLiteral(3));
    final Expression expression7 = new ArithmeticBinaryExpression(Operator.ADD, new DecimalLiteral(new BigDecimal("12.5").setScale(2)), new DecimalLiteral(new BigDecimal("1.25").setScale(2)));
    final Expression expression8 = new ArithmeticBinaryExpression(Operator.ADD, new DecimalLiteral(new BigDecimal("12.5").setScale(2)), new DoubleLiteral(2.0d));
    // When:
    InterpretedExpression interpreter1 = interpreter(expression1);
    InterpretedExpression interpreter2 = interpreter(expression2);
    InterpretedExpression interpreter3 = interpreter(expression3);
    InterpretedExpression interpreter4 = interpreter(expression4);
    InterpretedExpression interpreter5 = interpreter(expression5);
    InterpretedExpression interpreter6 = interpreter(expression6);
    InterpretedExpression interpreter7 = interpreter(expression7);
    InterpretedExpression interpreter8 = interpreter(expression8);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(3));
    assertThat(interpreter2.evaluate(ROW), is(5L));
    assertThat(interpreter3.evaluate(ROW), is(9.5d));
    assertThat(interpreter4.evaluate(ROW), is(20L));
    assertThat(interpreter5.evaluate(ROW), is(6L));
    assertThat(interpreter6.evaluate(ROW), is(2L));
    assertThat(interpreter7.evaluate(ROW), is(BigDecimal.valueOf(13.75).setScale(2)));
    assertThat(interpreter8.evaluate(ROW), is(14.5d));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) 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) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 2 with ArithmeticBinaryExpression

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

the class InterpretedExpressionTest method shouldEvaluateLambda_functionCall.

@Test
public void shouldEvaluateLambda_functionCall() {
    // Given:
    final UdfFactory udfFactory = mock(UdfFactory.class);
    final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
    when(udf.newInstance(any())).thenReturn(new TransFormUdf());
    givenUdf("TRANSFORM", udfFactory, udf);
    when(udf.parameters()).thenReturn(ImmutableList.of(ArrayType.of(IntegerType.INSTANCE), LambdaType.of(ImmutableList.of(IntegerType.INSTANCE), IntegerType.INSTANCE)));
    when(udf.getReturnType(any())).thenReturn(SqlTypes.array(SqlTypes.INTEGER));
    // When:
    InterpretedExpression interpreter1 = interpreter(new FunctionCall(FunctionName.of("TRANSFORM"), ImmutableList.of(new CreateArrayExpression(ImmutableList.of(new IntegerLiteral(1), new IntegerLiteral(2))), new LambdaFunctionCall(ImmutableList.of("X"), new ArithmeticBinaryExpression(Operator.ADD, new IntegerLiteral(1), new LambdaVariable("X"))))));
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(ImmutableList.of(2, 3)));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) UdfFactory(io.confluent.ksql.function.UdfFactory) 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 3 with ArithmeticBinaryExpression

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

the class AstBuilderTest method shouldBuildLambdaFunctionWithMultipleLambdas.

@Test
public void shouldBuildLambdaFunctionWithMultipleLambdas() {
    // Given:
    final SingleStatementContext stmt = givenQuery("SELECT TRANSFORM_ARRAY(Col4, X => X + 5, (X,Y) => X + Y) FROM TEST1;");
    // When:
    final Query result = (Query) builder.buildStatement(stmt);
    // Then:
    assertThat(result.getSelect(), is(new Select(ImmutableList.of(new SingleColumn(new FunctionCall(FunctionName.of("TRANSFORM_ARRAY"), ImmutableList.of(column("COL4"), new LambdaFunctionCall(ImmutableList.of("X"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("X"), new IntegerLiteral(5))), new LambdaFunctionCall(ImmutableList.of("X", "Y"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("X"), new LambdaVariable("Y"))))), Optional.empty())))));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Query(io.confluent.ksql.parser.tree.Query) SingleStatementContext(io.confluent.ksql.parser.SqlBaseParser.SingleStatementContext) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) Select(io.confluent.ksql.parser.tree.Select) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) 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 4 with ArithmeticBinaryExpression

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

the class ExpressionParserTest method shouldParseSelectExpression.

@Test
public void shouldParseSelectExpression() {
    // When:
    final SelectExpression parsed = ExpressionParser.parseSelectExpression("1 + 2 AS `three`");
    // Then:
    assertThat(parsed, equalTo(SelectExpression.of(ColumnName.of("three"), new ArithmeticBinaryExpression(parsed.getExpression().getLocation(), Operator.ADD, ONE, TWO))));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) SelectExpression(io.confluent.ksql.execution.plan.SelectExpression) ExpressionParser.parseSelectExpression(io.confluent.ksql.parser.ExpressionParser.parseSelectExpression) Test(org.junit.Test)

Example 5 with ArithmeticBinaryExpression

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

the class AstBuilderTest method shouldBuildLambdaFunction.

@Test
public void shouldBuildLambdaFunction() {
    // Given:
    final SingleStatementContext stmt = givenQuery("SELECT TRANSFORM_ARRAY(Col4, X => X + 5) FROM TEST1;");
    // When:
    final Query result = (Query) builder.buildStatement(stmt);
    // Then:
    assertThat(result.getSelect(), is(new Select(ImmutableList.of(new SingleColumn(new FunctionCall(FunctionName.of("TRANSFORM_ARRAY"), ImmutableList.of(column("COL4"), new LambdaFunctionCall(ImmutableList.of("X"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("X"), new IntegerLiteral(5))))), Optional.empty())))));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Query(io.confluent.ksql.parser.tree.Query) SingleStatementContext(io.confluent.ksql.parser.SqlBaseParser.SingleStatementContext) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) Select(io.confluent.ksql.parser.tree.Select) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) 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)

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