use of io.confluent.ksql.execution.expression.tree.WhenClause in project ksql by confluentinc.
the class TermCompiler method visitSearchedCaseExpression.
@Override
public Term visitSearchedCaseExpression(final SearchedCaseExpression node, final Context context) {
final SqlType resultSchema = expressionTypeManager.getExpressionSqlType(node, context.getLambdaSqlTypeMapping());
final List<Pair<Term, Term>> operandResultTerms = node.getWhenClauses().stream().map(whenClause -> Pair.of(process(whenClause.getOperand(), context), process(whenClause.getResult(), context))).collect(ImmutableList.toImmutableList());
final Optional<Term> defaultValueTerm = node.getDefaultValue().map(exp -> process(node.getDefaultValue().get(), context));
return new SearchedCaseTerm(operandResultTerms, defaultValueTerm, resultSchema);
}
use of io.confluent.ksql.execution.expression.tree.WhenClause in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldThrowOnSimpleCase.
@Test
public void shouldThrowOnSimpleCase() {
// Given:
final Expression expression = new SimpleCaseExpression(COL0, ImmutableList.of(new WhenClause(new IntegerLiteral(10), new StringLiteral("ten"))), empty());
// When:
assertThrows(UnsupportedOperationException.class, () -> sqlToJavaVisitor.process(expression));
}
use of io.confluent.ksql.execution.expression.tree.WhenClause in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForCaseStatementWithNoElse.
@Test
public void shouldGenerateCorrectCodeForCaseStatementWithNoElse() {
// 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.empty());
// 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 null; }}))"));
}
use of io.confluent.ksql.execution.expression.tree.WhenClause 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"));
}
use of io.confluent.ksql.execution.expression.tree.WhenClause in project ksql by confluentinc.
the class ExpressionTypeManagerTest method shouldFailOnInconsistentWhenResultType.
@Test
public void shouldFailOnInconsistentWhenResultType() {
// Given:
final Expression expression = new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ComparisonExpression(Type.EQUAL, TestExpressions.COL0, new IntegerLiteral(100)), new StringLiteral("one-hundred")), new WhenClause(new ComparisonExpression(Type.EQUAL, TestExpressions.COL0, new IntegerLiteral(10)), new IntegerLiteral(10))), Optional.empty());
// When:
final Exception e = assertThrows(KsqlException.class, () -> expressionTypeManager.getExpressionSqlType(expression));
// Then:
assertThat(e.getMessage(), containsString("Invalid Case expression. Type for all 'THEN' clauses should be the same." + System.lineSeparator() + "THEN expression 'WHEN (COL0 = 10) THEN 10' has type: INTEGER." + System.lineSeparator() + "Previous THEN expression(s) type: STRING."));
}
Aggregations