use of io.confluent.ksql.execution.expression.tree.WhenClause in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForFunctionWithMultipleLambdas.
@Test
public void shouldGenerateCorrectCodeForFunctionWithMultipleLambdas() {
// Given:
final UdfFactory udfFactory = mock(UdfFactory.class);
final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
givenUdf("function", udfFactory, udf, SqlTypes.STRING);
when(udf.parameters()).thenReturn(ImmutableList.of(ArrayType.of(ParamTypes.DOUBLE), ParamTypes.STRING, LambdaType.of(ImmutableList.of(ParamTypes.DOUBLE, ParamTypes.STRING), ParamTypes.DOUBLE), LambdaType.of(ImmutableList.of(ParamTypes.DOUBLE, ParamTypes.STRING), ParamTypes.STRING)));
final Expression expression = new FunctionCall(FunctionName.of("function"), ImmutableList.of(ARRAYCOL, COL1, new LambdaFunctionCall(ImmutableList.of("X", "S"), new ArithmeticBinaryExpression(Operator.ADD, new LambdaVariable("X"), new LambdaVariable("X"))), new LambdaFunctionCall(ImmutableList.of("X", "S"), new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ComparisonExpression(ComparisonExpression.Type.LESS_THAN, new LambdaVariable("X"), new IntegerLiteral(10)), new StringLiteral("test")), new WhenClause(new ComparisonExpression(ComparisonExpression.Type.LESS_THAN, new LambdaVariable("X"), new IntegerLiteral(100)), new StringLiteral("test2"))), Optional.of(new LambdaVariable("S"))))));
// When:
final String javaExpression = sqlToJavaVisitor.process(expression);
// Then
assertThat(javaExpression, equalTo("((String) function_0.evaluate(COL4, COL1, new BiFunction() {\n" + " @Override\n" + " public Object apply(Object arg1, Object arg2) {\n" + " final Double X = (Double) arg1;\n" + " final String S = (String) arg2;\n" + " return (X + X);\n" + " }\n" + "}, new BiFunction() {\n" + " @Override\n" + " public Object apply(Object arg1, Object arg2) {\n" + " final Double X = (Double) arg1;\n" + " final String S = (String) arg2;\n" + " return ((java.lang.String)SearchedCaseFunction.searchedCaseFunction(ImmutableList.copyOf(Arrays.asList( SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(X)) == null || ((Object)(10)) == null) ? false : (X < 10)); }}, new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"test\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(X)) == null || ((Object)(100)) == null) ? false : (X < 100)); }}, new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"test2\"; }}))), new Supplier<java.lang.String>() { @Override public java.lang.String get() { return S; }}));\n" + " }\n" + "}))"));
}
use of io.confluent.ksql.execution.expression.tree.WhenClause in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForCaseStatement.
@Test
public void shouldGenerateCorrectCodeForCaseStatement() {
// Given:
final Expression expression = new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ComparisonExpression(ComparisonExpression.Type.LESS_THAN, COL7, new IntegerLiteral(10)), new StringLiteral("small")), new WhenClause(new ComparisonExpression(ComparisonExpression.Type.LESS_THAN, COL7, new IntegerLiteral(100)), new StringLiteral("medium"))), Optional.of(new StringLiteral("large")));
// When:
final String javaExpression = sqlToJavaVisitor.process(expression);
// ThenL
assertThat(javaExpression, equalTo("((java.lang.String)SearchedCaseFunction.searchedCaseFunction(ImmutableList.copyOf(Arrays.asList( SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(10)) == null) ? false : (COL7 < 10)); }}, new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"small\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(100)) == null) ? false : (COL7 < 100)); }}, new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"medium\"; }}))), new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"large\"; }}))"));
}
use of io.confluent.ksql.execution.expression.tree.WhenClause in project ksql by confluentinc.
the class ExpressionTypeManagerTest method shouldGetCorrectSchemaForSearchedCase.
@Test
public void shouldGetCorrectSchemaForSearchedCase() {
// Given:
final Expression expression = new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ComparisonExpression(Type.LESS_THAN, COL7, new IntegerLiteral(10)), new StringLiteral("small")), new WhenClause(new ComparisonExpression(Type.LESS_THAN, COL7, new IntegerLiteral(100)), new StringLiteral("medium"))), Optional.of(new StringLiteral("large")));
// When:
final SqlType result = expressionTypeManager.getExpressionSqlType(expression);
// Then:
assertThat(result, is(SqlTypes.STRING));
}
use of io.confluent.ksql.execution.expression.tree.WhenClause in project ksql by confluentinc.
the class ExpressionTypeManagerTest method shouldThrowOnSimpleCase.
@Test
public void shouldThrowOnSimpleCase() {
final Expression expression = new SimpleCaseExpression(TestExpressions.COL0, ImmutableList.of(new WhenClause(new IntegerLiteral(10), new StringLiteral("ten"))), Optional.empty());
// When:
assertThrows(UnsupportedOperationException.class, () -> expressionTypeManager.getExpressionSqlType(expression));
}
use of io.confluent.ksql.execution.expression.tree.WhenClause in project ksql by confluentinc.
the class ExpressionTypeManagerTest method shouldFailIfDefaultHasDifferentTypeToWhen.
@Test
public void shouldFailIfDefaultHasDifferentTypeToWhen() {
// Given:
final Expression expression = new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ComparisonExpression(Type.EQUAL, TestExpressions.COL0, new IntegerLiteral(10)), new StringLiteral("good"))), Optional.of(new BooleanLiteral("true")));
// When:
final Exception e = assertThrows(KsqlException.class, () -> expressionTypeManager.getExpressionSqlType(expression));
// Then:
assertThat(e.getMessage(), containsString("Invalid Case expression. Type for the default clause should be the same as for 'THEN' clauses." + System.lineSeparator() + "THEN type: STRING." + System.lineSeparator() + "DEFAULT type: BOOLEAN."));
}
Aggregations