Search in sources :

Example 1 with WhenClause

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

the class ExpressionFormatterTest method shouldFormatSearchedCaseExpression.

@Test
public void shouldFormatSearchedCaseExpression() {
    final SearchedCaseExpression expression = new SearchedCaseExpression(Collections.singletonList(new WhenClause(new StringLiteral("foo"), new LongLiteral(1))), Optional.empty());
    assertThat(ExpressionFormatter.formatExpression(expression), equalTo("(CASE WHEN 'foo' THEN 1 END)"));
}
Also used : WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) Test(org.junit.Test)

Example 2 with WhenClause

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

the class ExpressionFormatterTest method shouldFormatSimpleCaseExpressionWithDefaultValue.

@Test
public void shouldFormatSimpleCaseExpressionWithDefaultValue() {
    final SimpleCaseExpression expression = new SimpleCaseExpression(new StringLiteral("operand"), Collections.singletonList(new WhenClause(new StringLiteral("foo"), new LongLiteral(1))), Optional.of(new LongLiteral(2)));
    assertThat(ExpressionFormatter.formatExpression(expression), equalTo("(CASE 'operand' WHEN 'foo' THEN 1 ELSE 2 END)"));
}
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) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) Test(org.junit.Test)

Example 3 with WhenClause

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

the class ExpressionFormatterTest method shouldFormatSearchedCaseExpressionWithDefaultValue.

@Test
public void shouldFormatSearchedCaseExpressionWithDefaultValue() {
    final SearchedCaseExpression expression = new SearchedCaseExpression(Collections.singletonList(new WhenClause(new StringLiteral("foo"), new LongLiteral(1))), Optional.of(new LongLiteral(2)));
    assertThat(ExpressionFormatter.formatExpression(expression), equalTo("(CASE WHEN 'foo' THEN 1 ELSE 2 END)"));
}
Also used : WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) Test(org.junit.Test)

Example 4 with WhenClause

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

the class ExpressionFormatterTest method shouldFormatSimpleCaseExpression.

@Test
public void shouldFormatSimpleCaseExpression() {
    final SimpleCaseExpression expression = new SimpleCaseExpression(new StringLiteral("operand"), Collections.singletonList(new WhenClause(new StringLiteral("foo"), new LongLiteral(1))), Optional.empty());
    assertThat(ExpressionFormatter.formatExpression(expression), equalTo("(CASE 'operand' WHEN 'foo' THEN 1 END)"));
}
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) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) Test(org.junit.Test)

Example 5 with WhenClause

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForCaseStatementWith13Conditions.

@Test
public void shouldGenerateCorrectCodeForCaseStatementWith13Conditions() {
    // Given:
    final ImmutableList<Integer> numbers = ImmutableList.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
    final ImmutableList<String> numberNames = ImmutableList.of("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve");
    final ImmutableList<WhenClause> arg = numbers.stream().map(n -> new WhenClause(new ComparisonExpression(ComparisonExpression.Type.EQUAL, COL7, new IntegerLiteral(n)), new StringLiteral(numberNames.get(n)))).collect(ImmutableList.toImmutableList());
    final Expression expression = new SearchedCaseExpression(arg, 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)(0)) == null) ? false : ((COL7 <= 0) && (COL7 >= 0))); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"zero\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(1)) == null) ? false : ((COL7 <= 1) && (COL7 >= 1))); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"one\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(2)) == null) ? false : ((COL7 <= 2) && (COL7 >= 2))); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"two\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(3)) == null) ? false : ((COL7 <= 3) && (COL7 >= 3))); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"three\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(4)) == null) ? false : ((COL7 <= 4) && (COL7 >= 4))); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"four\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(5)) == null) ? false : ((COL7 <= 5) && (COL7 >= 5))); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"five\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(6)) == null) ? false : ((COL7 <= 6) && (COL7 >= 6))); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"six\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(7)) == null) ? false : ((COL7 <= 7) && (COL7 >= 7))); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"seven\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(8)) == null) ? false : ((COL7 <= 8) && (COL7 >= 8))); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"eight\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(9)) == null) ? false : ((COL7 <= 9) && (COL7 >= 9))); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"nine\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(10)) == null) ? false : ((COL7 <= 10) && (COL7 >= 10))); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"ten\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(11)) == null) ? false : ((COL7 <= 11) && (COL7 >= 11))); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"eleven\"; }}), SearchedCaseFunction.whenClause( new Supplier<Boolean>() { @Override public Boolean get() { return ((((Object)(COL7)) == null || ((Object)(12)) == null) ? false : ((COL7 <= 12) && (COL7 >= 12))); }},  new Supplier<java.lang.String>() { @Override public java.lang.String get() { return \"twelve\"; }}))), new Supplier<java.lang.String>() { @Override public java.lang.String get() { return null; }}))"));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CoreMatchers.is(org.hamcrest.CoreMatchers.is) COL0(io.confluent.ksql.execution.testutil.TestExpressions.COL0) COL1(io.confluent.ksql.execution.testutil.TestExpressions.COL1) ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) UdfFactory(io.confluent.ksql.function.UdfFactory) Time(java.sql.Time) ColumnName(io.confluent.ksql.name.ColumnName) COL7(io.confluent.ksql.execution.testutil.TestExpressions.COL7) COL3(io.confluent.ksql.execution.testutil.TestExpressions.COL3) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) BigDecimal(java.math.BigDecimal) DateLiteral(io.confluent.ksql.execution.expression.tree.DateLiteral) UdfMetadata(io.confluent.ksql.function.udf.UdfMetadata) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) LambdaFunctionCall(io.confluent.ksql.execution.expression.tree.LambdaFunctionCall) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DATECOL(io.confluent.ksql.execution.testutil.TestExpressions.DATECOL) IntervalUnit(io.confluent.ksql.execution.expression.tree.IntervalUnit) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) SCHEMA(io.confluent.ksql.execution.testutil.TestExpressions.SCHEMA) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LambdaVariable(io.confluent.ksql.execution.expression.tree.LambdaVariable) ImmutableMap(com.google.common.collect.ImmutableMap) Expression(io.confluent.ksql.execution.expression.tree.Expression) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) KsqlConfig(io.confluent.ksql.util.KsqlConfig) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) TIMESTAMPCOL(io.confluent.ksql.execution.testutil.TestExpressions.TIMESTAMPCOL) LambdaType(io.confluent.ksql.function.types.LambdaType) CastEvaluator(io.confluent.ksql.execution.codegen.helpers.CastEvaluator) Sign(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression.Sign) Operator(io.confluent.ksql.schema.Operator) BYTESCOL(io.confluent.ksql.execution.testutil.TestExpressions.BYTESCOL) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) ARRAYCOL(io.confluent.ksql.execution.testutil.TestExpressions.ARRAYCOL) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) Mockito.mock(org.mockito.Mockito.mock) GenericType(io.confluent.ksql.function.types.GenericType) TestExpressions.literal(io.confluent.ksql.execution.testutil.TestExpressions.literal) Optional.empty(java.util.Optional.empty) KsqlScalarFunction(io.confluent.ksql.function.KsqlScalarFunction) MAPCOL(io.confluent.ksql.execution.testutil.TestExpressions.MAPCOL) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Mock(org.mockito.Mock) Assert.assertThrows(org.junit.Assert.assertThrows) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Field(io.confluent.ksql.execution.expression.tree.CreateStructExpression.Field) SourceName.of(io.confluent.ksql.name.SourceName.of) ImmutableList(com.google.common.collect.ImmutableList) Cast(io.confluent.ksql.execution.expression.tree.Cast) QualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.QualifiedColumnReferenceExp) MockitoJUnit(org.mockito.junit.MockitoJUnit) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) TimeLiteral(io.confluent.ksql.execution.expression.tree.TimeLiteral) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) Before(org.junit.Before) FunctionName(io.confluent.ksql.name.FunctionName) ParamTypes(io.confluent.ksql.function.types.ParamTypes) LikePredicate(io.confluent.ksql.execution.expression.tree.LikePredicate) InPredicate(io.confluent.ksql.execution.expression.tree.InPredicate) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Type(io.confluent.ksql.execution.expression.tree.ComparisonExpression.Type) TIMECOL(io.confluent.ksql.execution.testutil.TestExpressions.TIMECOL) Date(java.sql.Date) TimeUnit(java.util.concurrent.TimeUnit) FunctionCall(io.confluent.ksql.execution.expression.tree.FunctionCall) SimpleCaseExpression(io.confluent.ksql.execution.expression.tree.SimpleCaseExpression) SubscriptExpression(io.confluent.ksql.execution.expression.tree.SubscriptExpression) Rule(org.junit.Rule) InListExpression(io.confluent.ksql.execution.expression.tree.InListExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) ArrayType(io.confluent.ksql.function.types.ArrayType) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) MockitoRule(org.mockito.junit.MockitoRule) SqlTypes(io.confluent.ksql.schema.ksql.types.SqlTypes) Collections(java.util.Collections) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) WhenClause(io.confluent.ksql.execution.expression.tree.WhenClause) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) SearchedCaseExpression(io.confluent.ksql.execution.expression.tree.SearchedCaseExpression) 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)

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