Search in sources :

Example 1 with InListExpression

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

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

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

use of io.confluent.ksql.execution.expression.tree.InListExpression 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)

Example 5 with InListExpression

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

the class ExpressionTypeManagerTest method shouldReturnBooleanForInPredicate.

@Test
public void shouldReturnBooleanForInPredicate() {
    // Given:
    final Expression expression = new InPredicate(TestExpressions.COL0, new InListExpression(ImmutableList.of(new IntegerLiteral(1), new IntegerLiteral(2))));
    // When:
    final SqlType result = expressionTypeManager.getExpressionSqlType(expression);
    // Then:
    assertThat(result, is(SqlTypes.BOOLEAN));
}
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) 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) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Aggregations

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