Search in sources :

Example 61 with Expression

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

the class InterpretedExpressionTest method shouldEvaluateCastToMap.

@Test
public void shouldEvaluateCastToMap() {
    // Given:
    final Expression cast1 = new Cast(new CreateMapExpression(ImmutableMap.of(new StringLiteral("1"), new StringLiteral("2"), new StringLiteral("3"), new StringLiteral("4"))), new Type(SqlTypes.map(SqlTypes.INTEGER, SqlTypes.INTEGER)));
    final Expression cast2 = new Cast(new CreateMapExpression(ImmutableMap.of(new DoubleLiteral(2.5), new StringLiteral("2"), new DoubleLiteral(3.5), new StringLiteral("4"))), new Type(SqlTypes.map(SqlTypes.STRING, SqlTypes.BIGINT)));
    // When:
    InterpretedExpression interpreter1 = interpreter(cast1);
    InterpretedExpression interpreter2 = interpreter(cast2);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(ImmutableMap.of(1, 2, 3, 4)));
    assertThat(interpreter2.evaluate(ROW), is(ImmutableMap.of("2.5", 2L, "3.5", 4L)));
}
Also used : Cast(io.confluent.ksql.execution.expression.tree.Cast) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) 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) 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 62 with Expression

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForLikePatternWithEscape.

@Test
public void shouldGenerateCorrectCodeForLikePatternWithEscape() {
    // Given:
    final Expression expression = new LikePredicate(COL1, new StringLiteral("%foo"), Optional.of('!'));
    // When:
    final String javaExpression = sqlToJavaVisitor.process(expression);
    // Then:
    assertThat(javaExpression, equalTo("LikeEvaluator.matches(COL1, \"%foo\", '!')"));
}
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) 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) LikePredicate(io.confluent.ksql.execution.expression.tree.LikePredicate) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 63 with Expression

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

the class GenericRecordFactory method resolveValues.

private static Map<ColumnName, Object> resolveValues(final List<ColumnName> columns, final List<Expression> expressions, final LogicalSchema schema, final FunctionRegistry functionRegistry, final KsqlConfig config) {
    final Map<ColumnName, Object> values = new HashMap<>();
    for (int i = 0; i < columns.size(); i++) {
        final ColumnName column = columns.get(i);
        final SqlType columnType = columnType(column, schema);
        final Expression valueExp = expressions.get(i);
        final Object value = new GenericExpressionResolver(columnType, column, functionRegistry, config, "insert value", false).resolve(valueExp);
        values.put(column, value);
    }
    return values;
}
Also used : ColumnName(io.confluent.ksql.name.ColumnName) HashMap(java.util.HashMap) Expression(io.confluent.ksql.execution.expression.tree.Expression) SqlType(io.confluent.ksql.schema.ksql.types.SqlType)

Example 64 with Expression

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

the class ExpressionTreeRewriterTest method shouldRewriteInPredicate.

@Test
public void shouldRewriteInPredicate() {
    // Given:
    final InPredicate parsed = parseExpression("1 IN (1, 2, 3)");
    when(processor.apply(parsed.getValue(), context)).thenReturn(expr1);
    when(processor.apply(parsed.getValueList(), context)).thenReturn(inList);
    // When:
    final Expression rewritten = expressionRewriter.rewrite(parsed, context);
    // Then:
    assertThat(rewritten, equalTo(new InPredicate(parsed.getLocation(), expr1, inList)));
}
Also used : 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) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) Test(org.junit.Test)

Example 65 with Expression

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

the class ExpressionTreeRewriterTest method shouldRewriteDereferenceExpression.

@Test
public void shouldRewriteDereferenceExpression() {
    // Given:
    final DereferenceExpression parsed = parseExpression("col0->foo");
    when(processor.apply(parsed.getBase(), context)).thenReturn(expr1);
    // When:
    final Expression rewritten = expressionRewriter.rewrite(parsed, context);
    // Then:
    assertThat(rewritten, equalTo(new DereferenceExpression(parsed.getLocation(), expr1, parsed.getFieldName())));
}
Also used : DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) 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)

Aggregations

Expression (io.confluent.ksql.execution.expression.tree.Expression)343 Test (org.junit.Test)297 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)213 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)195 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)179 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)170 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)170 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)159 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)150 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)143 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)142 LogicalBinaryExpression (io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression)138 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)125 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)114 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)112 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)99 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)94 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)89 KsqlException (io.confluent.ksql.util.KsqlException)72 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)53