Search in sources :

Example 11 with WhenClause

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

the class TermCompiler method visitSearchedCaseExpression.

@Override
public Term visitSearchedCaseExpression(final SearchedCaseExpression node, final Context context) {
    final SqlType resultSchema = expressionTypeManager.getExpressionSqlType(node, context.getLambdaSqlTypeMapping());
    final List<Pair<Term, Term>> operandResultTerms = node.getWhenClauses().stream().map(whenClause -> Pair.of(process(whenClause.getOperand(), context), process(whenClause.getResult(), context))).collect(ImmutableList.toImmutableList());
    final Optional<Term> defaultValueTerm = node.getDefaultValue().map(exp -> process(node.getDefaultValue().get(), context));
    return new SearchedCaseTerm(operandResultTerms, defaultValueTerm, resultSchema);
}
Also used : TimestampLiteral(io.confluent.ksql.execution.expression.tree.TimestampLiteral) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) GenericsUtil(io.confluent.ksql.function.GenericsUtil) Pair(io.confluent.ksql.util.Pair) Term(io.confluent.ksql.execution.interpreter.terms.Term) LambdaFunction3Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction3Term) LiteralTerms(io.confluent.ksql.execution.interpreter.terms.LiteralTerms) Map(java.util.Map) ColumnReferenceTerm(io.confluent.ksql.execution.interpreter.terms.ColumnReferenceTerm) ExpressionTypeManager(io.confluent.ksql.execution.util.ExpressionTypeManager) NullLiteral(io.confluent.ksql.execution.expression.tree.NullLiteral) SchemaConverters.sqlToFunctionConverter(io.confluent.ksql.schema.ksql.SchemaConverters.sqlToFunctionConverter) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) BytesLiteral(io.confluent.ksql.execution.expression.tree.BytesLiteral) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) Expression(io.confluent.ksql.execution.expression.tree.Expression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) LambdaFunction1Term(io.confluent.ksql.execution.interpreter.terms.LambdaFunctionTerms.LambdaFunction1Term) KsqlException(io.confluent.ksql.util.KsqlException) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) Iterables(com.google.common.collect.Iterables) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) ExpressionVisitor(io.confluent.ksql.execution.expression.tree.ExpressionVisitor) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) SqlMap(io.confluent.ksql.schema.ksql.types.SqlMap) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) ArrayList(java.util.ArrayList) InListEvaluator(io.confluent.ksql.execution.codegen.helpers.InListEvaluator) BetweenPredicate(io.confluent.ksql.execution.expression.tree.BetweenPredicate) Cast(io.confluent.ksql.execution.expression.tree.Cast) LikeTerm(io.confluent.ksql.execution.interpreter.terms.LikeTerm) CoercionUtil(io.confluent.ksql.execution.util.CoercionUtil) FunctionCallTerm(io.confluent.ksql.execution.interpreter.terms.FunctionCallTerm) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Context(io.confluent.ksql.execution.interpreter.TermCompiler.Context) LogicalBinaryTerms(io.confluent.ksql.execution.interpreter.terms.LogicalBinaryTerms) FunctionTypeInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.FunctionTypeInfo) Type(io.confluent.ksql.execution.expression.tree.Type) Kudf(io.confluent.ksql.function.udf.Kudf) ParamTypes(io.confluent.ksql.function.types.ParamTypes) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ArrayType(io.confluent.ksql.function.types.ArrayType) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) DecimalUtil(io.confluent.ksql.util.DecimalUtil) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) UdfFactory(io.confluent.ksql.function.UdfFactory) ArgumentInfo(io.confluent.ksql.execution.util.FunctionArgumentsUtil.ArgumentInfo) SearchedCaseTerm(io.confluent.ksql.execution.interpreter.terms.SearchedCaseTerm) StructTerm(io.confluent.ksql.execution.interpreter.terms.StructTerm) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) LambdaVariableTerm(io.confluent.ksql.execution.interpreter.terms.LambdaVariableTerm) DateLiteral(io.confluent.ksql.execution.expression.tree.DateLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) Immutable(com.google.errorprone.annotations.Immutable) IntervalUnit(io.confluent.ksql.execution.expression.tree.IntervalUnit) 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) ImmutableMap(com.google.common.collect.ImmutableMap) NotTerm(io.confluent.ksql.execution.interpreter.terms.NotTerm) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) IsNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNullTerm) IsNotNullPredicate(io.confluent.ksql.execution.expression.tree.IsNotNullPredicate) FunctionArgumentsUtil(io.confluent.ksql.execution.util.FunctionArgumentsUtil) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Streams(com.google.common.collect.Streams) SqlArray(io.confluent.ksql.schema.ksql.types.SqlArray) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) String.format(java.lang.String.format) CreateArrayTerm(io.confluent.ksql.execution.interpreter.terms.CreateArrayTerm) List(java.util.List) BooleanLiteral(io.confluent.ksql.execution.expression.tree.BooleanLiteral) Optional(java.util.Optional) Column(io.confluent.ksql.schema.ksql.Column) DereferenceTerm(io.confluent.ksql.execution.interpreter.terms.DereferenceTerm) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) ArrayAccess(io.confluent.ksql.execution.codegen.helpers.ArrayAccess) IsNullPredicate(io.confluent.ksql.execution.expression.tree.IsNullPredicate) ParamType(io.confluent.ksql.function.types.ParamType) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) HashMap(java.util.HashMap) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) ImmutableList(com.google.common.collect.ImmutableList) QualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp) SchemaConverters(io.confluent.ksql.schema.ksql.SchemaConverters) TimeLiteral(io.confluent.ksql.execution.expression.tree.TimeLiteral) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) IsNotNullTerm(io.confluent.ksql.execution.interpreter.terms.IsNotNullTerm) NotExpression(io.confluent.ksql.execution.expression.tree.NotExpression) InPredicateTerm(io.confluent.ksql.execution.interpreter.terms.InPredicateTerm) LikePredicate(io.confluent.ksql.execution.expression.tree.LikePredicate) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) 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) SearchedCaseTerm(io.confluent.ksql.execution.interpreter.terms.SearchedCaseTerm) Pair(io.confluent.ksql.util.Pair)

Example 12 with WhenClause

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

the class SqlToJavaVisitorTest method shouldThrowOnSimpleCase.

@Test
public void shouldThrowOnSimpleCase() {
    // Given:
    final Expression expression = new SimpleCaseExpression(COL0, ImmutableList.of(new WhenClause(new IntegerLiteral(10), new StringLiteral("ten"))), empty());
    // When:
    assertThrows(UnsupportedOperationException.class, () -> sqlToJavaVisitor.process(expression));
}
Also used : SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) 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) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 13 with WhenClause

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForCaseStatementWithNoElse.

@Test
public void shouldGenerateCorrectCodeForCaseStatementWithNoElse() {
    // Given:
    final Expression expression = new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ComparisonExpression(ComparisonExpression.Type.LESS_THAN, COL7, new IntegerLiteral(10)), new StringLiteral("small")), new WhenClause(new ComparisonExpression(ComparisonExpression.Type.LESS_THAN, COL7, new IntegerLiteral(100)), new StringLiteral("medium"))), Optional.empty());
    // When:
    final String javaExpression = sqlToJavaVisitor.process(expression);
    // ThenL
    assertThat(javaExpression, equalTo("((java.lang.String)SearchedCaseFunction.searchedCaseFunction(ImmutableList.copyOf(Arrays.asList( SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(10)) == null) ? false : (COL7 < 10)); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"small\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(100)) == null) ? false : (COL7 < 100)); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"medium\"; }}))), new Supplier<java.lang.String>() { @Override public java.lang.String get() { return null; }}))"));
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) 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) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 14 with WhenClause

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

the class ExpressionTypeManagerTest method shouldFailIfWhenIsNotBoolean.

@Test
public void shouldFailIfWhenIsNotBoolean() {
    // Given:
    final Expression expression = new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ArithmeticBinaryExpression(Operator.ADD, TestExpressions.COL0, new IntegerLiteral(10)), new StringLiteral("foo"))), Optional.empty());
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> expressionTypeManager.getExpressionSqlType(expression));
    // Then:
    assertThat(e.getMessage(), containsString("WHEN operand type should be boolean." + System.lineSeparator() + "Type for '(COL0 + 10)' is BIGINT"));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) 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) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 15 with WhenClause

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

the class ExpressionTypeManagerTest method shouldFailOnInconsistentWhenResultType.

@Test
public void shouldFailOnInconsistentWhenResultType() {
    // Given:
    final Expression expression = new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ComparisonExpression(Type.EQUAL, TestExpressions.COL0, new IntegerLiteral(100)), new StringLiteral("one-hundred")), new WhenClause(new ComparisonExpression(Type.EQUAL, TestExpressions.COL0, new IntegerLiteral(10)), new IntegerLiteral(10))), Optional.empty());
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> expressionTypeManager.getExpressionSqlType(expression));
    // Then:
    assertThat(e.getMessage(), containsString("Invalid Case expression. Type for all 'THEN' clauses should be the same." + System.lineSeparator() + "THEN expression 'WHEN (COL0 = 10) THEN 10' has type: INTEGER." + System.lineSeparator() + "Previous THEN expression(s) type: STRING."));
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) 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) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Aggregations

WhenClause (io.confluent.ksql.execution.expression.tree.WhenClause)17 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)16 Test (org.junit.Test)16 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)15 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)14 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)13 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)13 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 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)13 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)13 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)13 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)8 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)7 NotExpression (io.confluent.ksql.execution.expression.tree.NotExpression)7 LongLiteral (io.confluent.ksql.execution.expression.tree.LongLiteral)5 KsqlException (io.confluent.ksql.util.KsqlException)5 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)4