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