Search in sources :

Example 21 with ArithmeticBinaryExpression

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForDecimalMod.

@Test
public void shouldGenerateCorrectCodeForDecimalMod() {
    // Given:
    final ArithmeticBinaryExpression binExp = new ArithmeticBinaryExpression(Operator.MODULUS, new UnqualifiedColumnReferenceExp(ColumnName.of("COL8")), new UnqualifiedColumnReferenceExp(ColumnName.of("COL8")));
    // When:
    final String java = sqlToJavaVisitor.process(binExp);
    // Then:
    assertThat(java, is("(COL8.remainder(COL8, new MathContext(2, RoundingMode.UNNECESSARY)).setScale(1))"));
}
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 22 with ArithmeticBinaryExpression

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForDecimalSubtract.

@Test
public void shouldGenerateCorrectCodeForDecimalSubtract() {
    // Given:
    final ArithmeticBinaryExpression binExp = new ArithmeticBinaryExpression(Operator.SUBTRACT, new UnqualifiedColumnReferenceExp(ColumnName.of("COL8")), new UnqualifiedColumnReferenceExp(ColumnName.of("COL8")));
    // When:
    final String java = sqlToJavaVisitor.process(binExp);
    // Then:
    assertThat(java, is("(COL8.subtract(COL8, new MathContext(3, RoundingMode.UNNECESSARY)).setScale(1))"));
}
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 23 with ArithmeticBinaryExpression

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForNestedLambdas.

@Test
public void shouldGenerateCorrectCodeForNestedLambdas() {
    // Given:
    final UdfFactory udfFactory = mock(UdfFactory.class);
    final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
    givenUdf("nested", udfFactory, udf, SqlTypes.DOUBLE);
    when(udf.parameters()).thenReturn(ImmutableList.of(ArrayType.of(ParamTypes.DOUBLE), ParamTypes.DOUBLE, LambdaType.of(ImmutableList.of(ParamTypes.DOUBLE, ParamTypes.INTEGER), ParamTypes.INTEGER)));
    final Expression expression = new ArithmeticBinaryExpression(Operator.ADD, new FunctionCall(FunctionName.of("nested"), ImmutableList.of(ARRAYCOL, new IntegerLiteral(0), new LambdaFunctionCall(ImmutableList.of("A", "B"), new ArithmeticBinaryExpression(Operator.ADD, new FunctionCall(FunctionName.of("nested"), ImmutableList.of(ARRAYCOL, new IntegerLiteral(0), new LambdaFunctionCall(ImmutableList.of("Q", "V"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("Q"), new LambdaVariable("V"))))), new LambdaVariable("B"))))), new IntegerLiteral(5));
    // When:
    final String javaExpression = sqlToJavaVisitor.process(expression);
    // Then
    assertThat(javaExpression, equalTo("(((Double) nested_0.evaluate(COL4, (Double)NullSafe.apply(0,new Function() {\n" + " @Override\n" + " public Object apply(Object arg1) {\n" + "   final Integer val = (Integer) arg1;\n" + "   return val.doubleValue();\n" + " }\n" + "}), new BiFunction() {\n" + " @Override\n" + " public Object apply(Object arg1, Object arg2) {\n" + "   final Double A = (Double) arg1;\n" + "   final Integer B = (Integer) arg2;\n" + "   return (((Double) nested_1.evaluate(COL4, (Double)NullSafe.apply(0,new Function() {\n" + " @Override\n" + " public Object apply(Object arg1) {\n" + "   final Integer val = (Integer) arg1;\n" + "   return val.doubleValue();\n" + " }\n" + "}), new BiFunction() {\n" + " @Override\n" + " public Object apply(Object arg1, Object arg2) {\n" + "   final Double Q = (Double) arg1;\n" + "   final Integer V = (Integer) arg2;\n" + "   return (Q + V);\n" + " }\n" + "})) + B);\n" + " }\n" + "})) + 5)"));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) 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) UdfFactory(io.confluent.ksql.function.UdfFactory) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 24 with ArithmeticBinaryExpression

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForLambdaExpressionWithTwoArguments.

@Test
public void shouldGenerateCorrectCodeForLambdaExpressionWithTwoArguments() {
    // Given:
    final UdfFactory udfFactory = mock(UdfFactory.class);
    final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
    givenUdf("REDUCE", udfFactory, udf, SqlTypes.STRING);
    when(udf.parameters()).thenReturn(ImmutableList.of(ArrayType.of(ParamTypes.DOUBLE), ParamTypes.DOUBLE, LambdaType.of(ImmutableList.of(ParamTypes.DOUBLE, ParamTypes.DOUBLE), ParamTypes.DOUBLE)));
    final Expression expression = new FunctionCall(FunctionName.of("REDUCE"), ImmutableList.of(ARRAYCOL, COL3, new LambdaFunctionCall(ImmutableList.of("X", "S"), (new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("X"), new LambdaVariable("S"))))));
    // When:
    final String javaExpression = sqlToJavaVisitor.process(expression);
    // Then
    assertThat(javaExpression, equalTo("((String) REDUCE_0.evaluate(COL4, COL3, new BiFunction() {\n" + " @Override\n" + " public Object apply(Object arg1, Object arg2) {\n" + "   final Double X = (Double) arg1;\n" + "   final Double S = (Double) arg2;\n" + "   return (X + S);\n" + " }\n" + "}))"));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) 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) UdfFactory(io.confluent.ksql.function.UdfFactory) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 25 with ArithmeticBinaryExpression

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForDecimalAdd.

@Test
public void shouldGenerateCorrectCodeForDecimalAdd() {
    // Given:
    final ArithmeticBinaryExpression binExp = new ArithmeticBinaryExpression(Operator.ADD, new UnqualifiedColumnReferenceExp(ColumnName.of("COL8")), new UnqualifiedColumnReferenceExp(ColumnName.of("COL8")));
    // When:
    final String java = sqlToJavaVisitor.process(binExp);
    // Then:
    assertThat(java, is("(COL8.add(COL8, new MathContext(3, RoundingMode.UNNECESSARY)).setScale(1))"));
}
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)

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