Search in sources :

Example 1 with InPredicate

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

the class ExpressionFormatterTest method shouldFormatInPredicate.

@Test
public void shouldFormatInPredicate() {
    final InPredicate predicate = new InPredicate(new StringLiteral("foo"), new InListExpression(ImmutableList.of(new StringLiteral("a"))));
    assertThat(ExpressionFormatter.formatExpression(predicate), equalTo("('foo' IN ('a'))"));
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) Test(org.junit.Test)

Example 2 with InPredicate

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

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

the class ExpressionTypeManagerTest method shouldEvaluateBooleanSchemaForInExpression.

@Test
public void shouldEvaluateBooleanSchemaForInExpression() {
    final Expression expression = new InPredicate(TestExpressions.COL0, new InListExpression(ImmutableList.of(new StringLiteral("key1"))));
    final SqlType exprType0 = expressionTypeManager.getExpressionSqlType(expression);
    assertThat(exprType0, is(SqlTypes.BOOLEAN));
}
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) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) 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) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) Test(org.junit.Test)

Example 4 with InPredicate

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

the class InListEvaluator method preprocess.

/**
 * Preprocess the list of possible expression, ensuring compatible types, performing literal
 * coercion and removing null literals (which can never match).
 *
 * @param predicate The predicate to process
 * @param typeManager the type manager for the predicate
 * @param lambdaTypeMapping mapping of lambda variables to type
 * @return {@code predicate} after processing.
 */
public static InPredicate preprocess(final InPredicate predicate, final ExpressionTypeManager typeManager, final Map<String, SqlType> lambdaTypeMapping) {
    final List<Expression> nonNull = ImmutableList.<Expression>builder().add(predicate.getValue()).addAll(predicate.getValueList().getValues().stream().filter(e -> !(e instanceof NullLiteral)).collect(Collectors.toList())).build();
    final List<Expression> coerced = CoercionUtil.coerceUserList(nonNull, typeManager, lambdaTypeMapping).expressions();
    return new InPredicate(predicate.getLocation(), coerced.get(0), new InListExpression(predicate.getValueList().getLocation(), coerced.subList(1, coerced.size())));
}
Also used : Iterator(java.util.Iterator) ImmutableMap(com.google.common.collect.ImmutableMap) Expression(io.confluent.ksql.execution.expression.tree.Expression) Field(org.apache.kafka.connect.data.Field) SqlBooleans(io.confluent.ksql.schema.ksql.SqlBooleans) BiFunction(java.util.function.BiFunction) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Schema(org.apache.kafka.connect.data.Schema) BigDecimal(java.math.BigDecimal) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) Map(java.util.Map) Struct(org.apache.kafka.connect.data.Struct) CoercionUtil(io.confluent.ksql.execution.util.CoercionUtil) ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) Entry(java.util.Map.Entry) NullLiteral(io.confluent.ksql.execution.expression.tree.NullLiteral) Optional(java.util.Optional) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) RoundingMode(java.math.RoundingMode) Expression(io.confluent.ksql.execution.expression.tree.Expression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) NullLiteral(io.confluent.ksql.execution.expression.tree.NullLiteral)

Example 5 with InPredicate

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForInPredicate.

@Test
public void shouldGenerateCorrectCodeForInPredicate() {
    // Given:
    final Expression expression = new InPredicate(COL0, new InListExpression(ImmutableList.of(new IntegerLiteral(1), new IntegerLiteral(2))));
    // When:
    final String java = sqlToJavaVisitor.process(expression);
    // Then:
    assertThat(java, is("InListEvaluator.matches(COL0,1L,2L)"));
}
Also used : 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) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Aggregations

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