Search in sources :

Example 16 with WhenClause

use of io.confluent.ksql.execution.expression.tree.WhenClause 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));
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) 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) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 17 with WhenClause

use of io.confluent.ksql.execution.expression.tree.WhenClause 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());
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) 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) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Aggregations

WhenClause (io.confluent.ksql.execution.expression.tree.WhenClause)17 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)16 Test (org.junit.Test)16 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)15 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)14 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)13 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)13 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)13 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)13 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)13 Expression (io.confluent.ksql.execution.expression.tree.Expression)13 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)13 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)13 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)13 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)8 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)7 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)7 LongLiteral (io.confluent.ksql.execution.expression.tree.LongLiteral)5 KsqlException (io.confluent.ksql.util.KsqlException)5 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)4