use of io.confluent.ksql.execution.expression.tree.SubscriptExpression in project ksql by confluentinc.
the class ExpressionTypeManagerTest method shouldThrowGoodErrorMessageForSubscriptOnStruct.
@Test
public void shouldThrowGoodErrorMessageForSubscriptOnStruct() {
// Given:
final SqlStruct structType = SqlTypes.struct().field("IN0", SqlTypes.INTEGER).build();
final LogicalSchema schema = LogicalSchema.builder().keyColumn(SystemColumns.ROWKEY_NAME, SqlTypes.STRING).valueColumn(COL0, structType).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 STRUCT<`IN0` INTEGER>. " + "Use the dereference operator for STRUCTS: COL0->'IN0'"));
}
use of io.confluent.ksql.execution.expression.tree.SubscriptExpression in project ksql by confluentinc.
the class ExpressionTypeManagerTest method shouldEvaluateTypeForArrayReferenceInStruct.
@Test
public void shouldEvaluateTypeForArrayReferenceInStruct() {
// Given:
final SqlStruct inner = SqlTypes.struct().field("IN0", SqlTypes.array(SqlTypes.INTEGER)).build();
final LogicalSchema schema = LogicalSchema.builder().keyColumn(SystemColumns.ROWKEY_NAME, SqlTypes.STRING).valueColumn(COL0, inner).build();
expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
final Expression structRef = new DereferenceExpression(Optional.empty(), new UnqualifiedColumnReferenceExp(COL0), "IN0");
final Expression expression = new SubscriptExpression(structRef, new IntegerLiteral(1));
// When:
final SqlType result = expressionTypeManager.getExpressionSqlType(expression);
// Then:
assertThat(result, is(SqlTypes.INTEGER));
}
use of io.confluent.ksql.execution.expression.tree.SubscriptExpression in project ksql by confluentinc.
the class ExpressionTreeRewriterTest method shouldRewriteSubscriptExpression.
@Test
public void shouldRewriteSubscriptExpression() {
// Given:
final SubscriptExpression parsed = parseExpression("col4[1]");
when(processor.apply(parsed.getBase(), context)).thenReturn(expr1);
when(processor.apply(parsed.getIndex(), context)).thenReturn(expr2);
// When:
final Expression rewritten = expressionRewriter.rewrite(parsed, context);
// Then:
assertThat(rewritten, equalTo(new SubscriptExpression(parsed.getLocation(), expr1, expr2)));
}
use of io.confluent.ksql.execution.expression.tree.SubscriptExpression in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldProcessMapExpressionCorrectly.
@Test
public void shouldProcessMapExpressionCorrectly() {
// Given:
final Expression expression = new SubscriptExpression(MAPCOL, new StringLiteral("key1"));
// When:
final String javaExpression = sqlToJavaVisitor.process(expression);
// Then:
assertThat(javaExpression, equalTo("((Double) ((java.util.Map)COL5).get(\"key1\"))"));
}
use of io.confluent.ksql.execution.expression.tree.SubscriptExpression in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldProcessStructExpressionCorrectly.
@Test
public void shouldProcessStructExpressionCorrectly() {
// Given:
final Expression expression = new CreateStructExpression(ImmutableList.of(new Field("col1", new StringLiteral("foo")), new Field("col2", new SubscriptExpression(MAPCOL, new StringLiteral("key1")))));
// When:
final String javaExpression = sqlToJavaVisitor.process(expression);
// Then:
assertThat(javaExpression, equalTo("((Struct)new Struct(schema0)" + ".put(\"col1\", (new Supplier<Object>() {@Override public Object get() { try { return \"foo\"; } catch (Exception e) { " + onException("struct field") + " }}}).get())" + ".put(\"col2\", (new Supplier<Object>() {@Override public Object get() { try { return ((Double) ((java.util.Map)COL5).get(\"key1\")); } catch (Exception e) { " + onException("struct field") + " }}}).get()))"));
}
Aggregations