Search in sources :

Example 11 with CreateMapExpression

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))"));
}
Also used : CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 12 with CreateMapExpression

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))));
}
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) 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 13 with CreateMapExpression

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);
}
Also used : LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) CreateMapTerm(io.confluent.ksql.execution.interpreter.terms.CreateMapTerm) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Term(io.confluent.ksql.execution.interpreter.terms.Term) LambdaFunction3Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction3Term) ColumnReferenceTerm(io.confluent.ksql.execution.interpreter.terms.ColumnReferenceTerm) LambdaFunction1Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction1Term) LikeTerm(io.confluent.ksql.execution.interpreter.terms.LikeTerm) FunctionCallTerm(io.confluent.ksql.execution.interpreter.terms.FunctionCallTerm) SearchedCaseTerm(io.confluent.ksql.execution.interpreter.terms.SearchedCaseTerm) StructTerm(io.confluent.ksql.execution.interpreter.terms.StructTerm) LambdaVariableTerm(io.confluent.ksql.execution.interpreter.terms.LambdaVariableTerm) CreateMapTerm(io.confluent.ksql.execution.interpreter.terms.CreateMapTerm) LambdaFunction2Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction2Term) SubscriptTerm(io.confluent.ksql.execution.interpreter.terms.SubscriptTerm) NotTerm(io.confluent.ksql.execution.interpreter.terms.NotTerm) IsNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNullTerm) CreateArrayTerm(io.confluent.ksql.execution.interpreter.terms.CreateArrayTerm) DereferenceTerm(io.confluent.ksql.execution.interpreter.terms.DereferenceTerm) IsNotNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNotNullTerm) InPredicateTerm(io.confluent.ksql.execution.interpreter.terms.InPredicateTerm) ImmutableMap(com.google.common.collect.ImmutableMap) Pair(io.confluent.ksql.util.Pair)

Aggregations

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