Search in sources :

Example 1 with SubscriptExpression

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));
}
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 SubscriptExpression

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())"));
}
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 3 with SubscriptExpression

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))))"));
}
Also used : 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) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 4 with SubscriptExpression

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())"));
}
Also used : CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) 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 5 with SubscriptExpression

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."));
}
Also used : 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) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Test(org.junit.Test)

Aggregations

ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)11 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)11 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)11 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)11 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)11 Expression (io.confluent.ksql.execution.expression.tree.Expression)11 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)11 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)11 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)11 Test (org.junit.Test)11 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)10 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)7 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)7 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)6 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)4 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)3 SqlStruct (io.confluent.ksql.schema.ksql.types.SqlStruct)3 DoubleLiteral (io.confluent.ksql.execution.expression.tree.DoubleLiteral)2