Search in sources :

Example 1 with CreateMapExpression

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

the class InterpretedExpressionTest method shouldEvaluateSubscriptExpression.

@Test
public void shouldEvaluateSubscriptExpression() {
    // Given:
    final Expression expression1 = new SubscriptExpression(new CreateArrayExpression(ImmutableList.of(new StringLiteral("1"), new StringLiteral("2"))), new IntegerLiteral(1));
    final Expression expression2 = new SubscriptExpression(new CreateMapExpression(ImmutableMap.of(new StringLiteral("a"), new LongLiteral(123), new StringLiteral("b"), new LongLiteral(456))), new StringLiteral("a"));
    // When:
    InterpretedExpression interpreter1 = interpreter(expression1);
    InterpretedExpression interpreter2 = interpreter(expression2);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is("1"));
    assertThat(interpreter2.evaluate(ROW), is(123L));
}
Also used : CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) 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) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 2 with CreateMapExpression

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

the class InterpretedExpressionTest method shouldEvaluateCastToMap.

@Test
public void shouldEvaluateCastToMap() {
    // Given:
    final Expression cast1 = new Cast(new CreateMapExpression(ImmutableMap.of(new StringLiteral("1"), new StringLiteral("2"), new StringLiteral("3"), new StringLiteral("4"))), new Type(SqlTypes.map(SqlTypes.INTEGER, SqlTypes.INTEGER)));
    final Expression cast2 = new Cast(new CreateMapExpression(ImmutableMap.of(new DoubleLiteral(2.5), new StringLiteral("2"), new DoubleLiteral(3.5), new StringLiteral("4"))), new Type(SqlTypes.map(SqlTypes.STRING, SqlTypes.BIGINT)));
    // When:
    InterpretedExpression interpreter1 = interpreter(cast1);
    InterpretedExpression interpreter2 = interpreter(cast2);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(ImmutableMap.of(1, 2, 3, 4)));
    assertThat(interpreter2.evaluate(ROW), is(ImmutableMap.of("2.5", 2L, "3.5", 4L)));
}
Also used : Cast(io.confluent.ksql.execution.expression.tree.Cast) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) LambdaType(io.confluent.ksql.function.types.LambdaType) IntegerType(io.confluent.ksql.function.types.IntegerType) GenericType(io.confluent.ksql.function.types.GenericType) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) IntervalUnitType(io.confluent.ksql.function.types.IntervalUnitType) Type(io.confluent.ksql.execution.expression.tree.Type) ArrayType(io.confluent.ksql.function.types.ArrayType) 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) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) Test(org.junit.Test)

Example 3 with CreateMapExpression

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

the class SqlToJavaVisitorTest method shouldProcessCreateMapExpressionCorrectly.

@Test
public void shouldProcessCreateMapExpressionCorrectly() {
    // Given:
    Expression expression = new CreateMapExpression(ImmutableMap.of(new StringLiteral("foo"), new SubscriptExpression(MAPCOL, new StringLiteral("key1")), new StringLiteral("bar"), new DoubleLiteral(1.0d)));
    // When:
    String java = sqlToJavaVisitor.process(expression);
    // Then:
    assertThat(java, equalTo("((Map)new MapBuilder(2)" + ".put( (new Supplier<Object>() {@Override public Object get() { try {  return \"foo\"; } catch (Exception e) {  " + onException("map key") + " }}}).get(),  (new Supplier<Object>() {@Override public Object get() { try {  return ((Double) ((java.util.Map)COL5).get(\"key1\")); } catch (Exception e) {  " + onException("map value") + " }}}).get())" + ".put( (new Supplier<Object>() {@Override public Object get() { try {  return \"bar\"; } catch (Exception e) {  " + onException("map key") + " }}}).get(),  (new Supplier<Object>() {@Override public Object get() { try {  return 1E0; } catch (Exception e) {  " + onException("map value") + " }}}).get()).build())"));
}
Also used : CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) 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) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 4 with CreateMapExpression

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

the class ExpressionTypeManagerTest method shouldThrowOnMapOfNullValues.

@Test
public void shouldThrowOnMapOfNullValues() {
    // Given:
    Expression expression = new CreateMapExpression(ImmutableMap.of(new StringLiteral("foo"), new NullLiteral()));
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> expressionTypeManager.getExpressionSqlType(expression));
    // Then:
    assertThat(e.getMessage(), containsString("Cannot construct a map with all NULL values"));
}
Also used : CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) 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) NullLiteral(io.confluent.ksql.execution.expression.tree.NullLiteral) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 5 with CreateMapExpression

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

the class ExpressionTypeManagerTest method shouldEvaluateTypeForCreateMapExpression.

@Test
public void shouldEvaluateTypeForCreateMapExpression() {
    // Given:
    Expression expression = new CreateMapExpression(ImmutableMap.of(COL3, new UnqualifiedColumnReferenceExp(COL0)));
    // When:
    final SqlType type = expressionTypeManager.getExpressionSqlType(expression);
    // Then:
    assertThat(type, is(SqlTypes.map(SqlTypes.DOUBLE, SqlTypes.BIGINT)));
}
Also used : CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) 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) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Test(org.junit.Test)

Aggregations

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 Test (org.junit.Test)12 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)9 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)8 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)8 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)8 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)8 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)8 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)7 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)6 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)6 KsqlException (io.confluent.ksql.util.KsqlException)6 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)5 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)5 LogicalBinaryExpression (io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression)4 BooleanLiteral (io.confluent.ksql.execution.expression.tree.BooleanLiteral)2 DoubleLiteral (io.confluent.ksql.execution.expression.tree.DoubleLiteral)2