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));
}
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)));
}
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())"));
}
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"));
}
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)));
}
Aggregations