Search in sources :

Example 1 with CreateArrayExpression

use of io.confluent.ksql.execution.expression.tree.CreateArrayExpression 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 CreateArrayExpression

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

the class InterpretedExpressionTest method shouldEvaluateCastToArray.

@Test
public void shouldEvaluateCastToArray() {
    // Given:
    final Expression cast1 = new Cast(new CreateArrayExpression(ImmutableList.of(new StringLiteral("1"), new StringLiteral("2"))), new Type(SqlTypes.array(SqlTypes.INTEGER)));
    final Expression cast2 = new Cast(new CreateArrayExpression(ImmutableList.of(new DoubleLiteral(2.5), new DoubleLiteral(3.6))), new Type(SqlTypes.array(SqlTypes.INTEGER)));
    final Expression cast3 = new Cast(new CreateArrayExpression(ImmutableList.of(new DoubleLiteral(2.5), new DoubleLiteral(3.6))), new Type(SqlTypes.array(SqlTypes.STRING)));
    // When:
    InterpretedExpression interpreter1 = interpreter(cast1);
    InterpretedExpression interpreter2 = interpreter(cast2);
    InterpretedExpression interpreter3 = interpreter(cast3);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(ImmutableList.of(1, 2)));
    assertThat(interpreter2.evaluate(ROW), is(ImmutableList.of(2, 3)));
    assertThat(interpreter3.evaluate(ROW), is(ImmutableList.of("2.5", "3.6")));
}
Also used : Cast(io.confluent.ksql.execution.expression.tree.Cast) 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) 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) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) Test(org.junit.Test)

Example 3 with CreateArrayExpression

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

the class InterpretedExpressionTest method shouldEvaluateLambda_functionCall.

@Test
public void shouldEvaluateLambda_functionCall() {
    // Given:
    final UdfFactory udfFactory = mock(UdfFactory.class);
    final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
    when(udf.newInstance(any())).thenReturn(new TransFormUdf());
    givenUdf("TRANSFORM", udfFactory, udf);
    when(udf.parameters()).thenReturn(ImmutableList.of(ArrayType.of(IntegerType.INSTANCE), LambdaType.of(ImmutableList.of(IntegerType.INSTANCE), IntegerType.INSTANCE)));
    when(udf.getReturnType(any())).thenReturn(SqlTypes.array(SqlTypes.INTEGER));
    // When:
    InterpretedExpression interpreter1 = interpreter(new FunctionCall(FunctionName.of("TRANSFORM"), ImmutableList.of(new CreateArrayExpression(ImmutableList.of(new IntegerLiteral(1), new IntegerLiteral(2))), new LambdaFunctionCall(ImmutableList.of("X"), new ArithmeticBinaryExpression(Operator.ADD, new IntegerLiteral(1), new LambdaVariable("X"))))));
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(ImmutableList.of(2, 3)));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) UdfFactory(io.confluent.ksql.function.UdfFactory) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 4 with CreateArrayExpression

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

the class ExpressionTreeRewriterTest method shouldRewriteCreateArrayExpression.

@Test
public void shouldRewriteCreateArrayExpression() {
    // Given:
    final CreateArrayExpression parsed = parseExpression("ARRAY['foo', col4[1]]");
    final Expression firstVal = parsed.getValues().get(0);
    final Expression secondVal = parsed.getValues().get(1);
    when(processor.apply(firstVal, context)).thenReturn(expr1);
    when(processor.apply(secondVal, context)).thenReturn(expr2);
    // When:
    final Expression rewritten = expressionRewriter.rewrite(parsed, context);
    // Then:
    assertThat(rewritten, equalTo(new CreateArrayExpression(ImmutableList.of(expr1, expr2))));
}
Also used : CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) 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) 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) Test(org.junit.Test)

Example 5 with CreateArrayExpression

use of io.confluent.ksql.execution.expression.tree.CreateArrayExpression 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)

Aggregations

CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)13 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)12 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)12 Expression (io.confluent.ksql.execution.expression.tree.Expression)12 Test (org.junit.Test)12 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)10 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)9 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)9 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)9 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)9 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)8 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)7 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)6 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)6 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)6 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)5 KsqlException (io.confluent.ksql.util.KsqlException)5 LogicalBinaryExpression (io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression)4 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)4 DoubleLiteral (io.confluent.ksql.execution.expression.tree.DoubleLiteral)3