use of io.confluent.ksql.execution.expression.tree.CreateMapExpression in project ksql by confluentinc.
the class CoercionUtilTest method shouldNotCoerceMapWithDifferentValueExpression.
@Test
public void shouldNotCoerceMapWithDifferentValueExpression() {
// Given:
final ImmutableList<Expression> expressions = ImmutableList.of(new CreateMapExpression(ImmutableMap.of(new IntegerLiteral(10), new IntegerLiteral(10))), new CreateMapExpression(ImmutableMap.of(new IntegerLiteral(10), STRING_EXPRESSION)));
// When:
final Exception e = assertThrows(KsqlException.class, () -> CoercionUtil.coerceUserList(expressions, typeManager));
// Then:
assertThat(e.getMessage(), startsWith("operator does not exist: MAP<INTEGER, INTEGER> = MAP<INTEGER, STRING> (MAP(10:=STR))"));
}
use of io.confluent.ksql.execution.expression.tree.CreateMapExpression in project ksql by confluentinc.
the class ExpressionTreeRewriterTest method shouldRewriteCreateMapExpression.
@Test
public void shouldRewriteCreateMapExpression() {
// Given:
final CreateMapExpression parsed = parseExpression("MAP('foo' := SUBSTRING('foo',0), 'bar' := col4[1])");
final Expression firstVal = parsed.getMap().get(new StringLiteral("foo"));
final Expression secondVal = parsed.getMap().get(new StringLiteral("bar"));
when(processor.apply(firstVal, context)).thenReturn(expr1);
when(processor.apply(secondVal, context)).thenReturn(expr2);
when(processor.apply(new StringLiteral("foo"), context)).thenReturn(new StringLiteral("foo"));
when(processor.apply(new StringLiteral("bar"), context)).thenReturn(new StringLiteral("bar"));
// When:
final Expression rewritten = expressionRewriter.rewrite(parsed, context);
// Then:
assertThat(rewritten, equalTo(new CreateMapExpression(ImmutableMap.of(new StringLiteral("foo"), expr1, new StringLiteral("bar"), expr2))));
}
use of io.confluent.ksql.execution.expression.tree.CreateMapExpression in project ksql by confluentinc.
the class TermCompiler method visitCreateMapExpression.
@Override
public Term visitCreateMapExpression(final CreateMapExpression exp, final Context context) {
final ImmutableMap<Expression, Expression> map = exp.getMap();
final List<Expression> keys = CoercionUtil.coerceUserList(map.keySet(), expressionTypeManager, context.getLambdaSqlTypeMapping()).expressions();
final List<Expression> values = CoercionUtil.coerceUserList(map.values(), expressionTypeManager, context.getLambdaSqlTypeMapping()).expressions();
final Iterable<Pair<Expression, Expression>> pairs = () -> Streams.zip(keys.stream(), values.stream(), Pair::of).iterator();
final ImmutableMap.Builder<Term, Term> mapTerms = ImmutableMap.builder();
for (Pair<Expression, Expression> p : pairs) {
mapTerms.put(process(p.getLeft(), context), process(p.getRight(), context));
}
final SqlType resultType = expressionTypeManager.getExpressionSqlType(exp, context.getLambdaSqlTypeMapping());
return new CreateMapTerm(mapTerms.build(), resultType);
}
Aggregations