use of io.confluent.ksql.execution.expression.tree.SubscriptExpression 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.SubscriptExpression 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.SubscriptExpression in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldProcessArrayExpressionCorrectly.
@Test
public void shouldProcessArrayExpressionCorrectly() {
// Given:
final Expression expression = new SubscriptExpression(ARRAYCOL, literal(0));
// When:
final String javaExpression = sqlToJavaVisitor.process(expression);
// Then:
assertThat(javaExpression, equalTo("((Double) (ArrayAccess.arrayAccess((java.util.List) COL4, ((int) 0))))"));
}
use of io.confluent.ksql.execution.expression.tree.SubscriptExpression in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldProcessCreateArrayExpressionCorrectly.
@Test
public void shouldProcessCreateArrayExpressionCorrectly() {
// Given:
Expression expression = new CreateArrayExpression(ImmutableList.of(new SubscriptExpression(MAPCOL, new StringLiteral("key1")), new DoubleLiteral(1.0d)));
// When:
String java = sqlToJavaVisitor.process(expression);
// Then:
assertThat(java, equalTo("((List)new ArrayBuilder(2)" + ".add( (new Supplier<Object>() {@Override public Object get() { try { return ((Double) ((java.util.Map)COL5).get(\"key1\")); } catch (Exception e) { " + onException("array item") + " }}}).get())" + ".add( (new Supplier<Object>() {@Override public Object get() { try { return 1E0; } catch (Exception e) { " + onException("array item") + " }}}).get()).build())"));
}
use of io.confluent.ksql.execution.expression.tree.SubscriptExpression in project ksql by confluentinc.
the class ExpressionTypeManagerTest method shouldThrowGoodErrorMessageForSubscriptOnScalar.
@Test
public void shouldThrowGoodErrorMessageForSubscriptOnScalar() {
// Given:
final LogicalSchema schema = LogicalSchema.builder().keyColumn(SystemColumns.ROWKEY_NAME, SqlTypes.STRING).valueColumn(COL0, SqlTypes.INTEGER).build();
expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
final Expression expression = new SubscriptExpression(Optional.empty(), TestExpressions.COL0, new StringLiteral("IN0"));
// When:
final UnsupportedOperationException e = assertThrows(UnsupportedOperationException.class, () -> expressionTypeManager.getExpressionSqlType(expression));
// Then:
assertThat(e.getMessage(), is("Subscript expression (COL0['IN0']) do not apply to INTEGER."));
}
Aggregations