use of io.confluent.ksql.execution.expression.tree.SearchedCaseExpression 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.SearchedCaseExpression 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."));
}
use of io.confluent.ksql.execution.expression.tree.SearchedCaseExpression in project ksql by confluentinc.
the class ExpressionTypeManagerTest method shouldGetCorrectSchemaForSearchedCaseWhenStruct.
@Test
public void shouldGetCorrectSchemaForSearchedCaseWhenStruct() {
// Given:
final Expression expression = new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ComparisonExpression(Type.EQUAL, TestExpressions.COL0, new IntegerLiteral(10)), ADDRESS)), Optional.empty());
// When:
final SqlType result = expressionTypeManager.getExpressionSqlType(expression);
// Then:
final SqlType sqlType = SCHEMA.findColumn(ADDRESS.getColumnName()).get().type();
assertThat(result, is(sqlType));
}
use of io.confluent.ksql.execution.expression.tree.SearchedCaseExpression in project ksql by confluentinc.
the class InterpretedExpressionTest method shouldEvaluateSearchedCase.
@Test
public void shouldEvaluateSearchedCase() {
// Given:
final Expression case1 = new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ComparisonExpression(ComparisonExpression.Type.GREATER_THAN, COL7, new IntegerLiteral(10)), new StringLiteral("Large")), new WhenClause(new ComparisonExpression(ComparisonExpression.Type.GREATER_THAN, COL7, new IntegerLiteral(5)), new StringLiteral("Medium")), new WhenClause(new ComparisonExpression(ComparisonExpression.Type.GREATER_THAN, COL7, new IntegerLiteral(2)), new StringLiteral("Small"))), Optional.of(new StringLiteral("Tiny")));
final Expression case2 = new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ComparisonExpression(ComparisonExpression.Type.LESS_THAN, COL7, new IntegerLiteral(6)), new StringLiteral("Blah"))), Optional.empty());
// When:
InterpretedExpression interpreter1 = interpreter(case1);
InterpretedExpression interpreter2 = interpreter(case2);
// Then:
assertThat(interpreter1.evaluate(make(7, 12)), is("Large"));
assertThat(interpreter1.evaluate(make(7, 9)), is("Medium"));
assertThat(interpreter1.evaluate(make(7, 3)), is("Small"));
assertThat(interpreter1.evaluate(make(7, 1)), is("Tiny"));
assertThat(interpreter2.evaluate(make(7, 1)), is("Blah"));
assertThat(interpreter2.evaluate(make(7, 10)), nullValue());
}
use of io.confluent.ksql.execution.expression.tree.SearchedCaseExpression in project ksql by confluentinc.
the class KsqlParserTest method shouldBuildSearchedCaseStatement.
@Test
public void shouldBuildSearchedCaseStatement() {
// Given:
final String statementString = "CREATE STREAM S AS SELECT CASE WHEN orderunits < 10 THEN 'small' WHEN orderunits < 100 THEN 'medium' ELSE 'large' END FROM orders;";
// When:
final Statement statement = KsqlParserTestUtil.buildSingleAst(statementString, metaStore).getStatement();
// Then:
final SearchedCaseExpression searchedCaseExpression = getSearchedCaseExpressionFromCsas(statement);
assertThat(searchedCaseExpression.getWhenClauses().size(), equalTo(2));
assertThat(searchedCaseExpression.getWhenClauses().get(0).getOperand().toString(), equalTo("(ORDERUNITS < 10)"));
assertThat(searchedCaseExpression.getWhenClauses().get(0).getResult().toString(), equalTo("'small'"));
assertThat(searchedCaseExpression.getWhenClauses().get(1).getOperand().toString(), equalTo("(ORDERUNITS < 100)"));
assertThat(searchedCaseExpression.getWhenClauses().get(1).getResult().toString(), equalTo("'medium'"));
assertTrue(searchedCaseExpression.getDefaultValue().isPresent());
assertThat(searchedCaseExpression.getDefaultValue().get().toString(), equalTo("'large'"));
}
Aggregations