Search in sources :

Example 31 with ComparisonExpression

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

the class SqlFormatterTest method setUp.

@Before
public void setUp() {
    final Table left = new Table(SourceName.of("left"));
    final Table right = new Table(SourceName.of("right"));
    final Table right2 = new Table(SourceName.of("right2"));
    leftAlias = new AliasedRelation(left, SourceName.of("L"));
    rightAlias = new AliasedRelation(right, SourceName.of("R"));
    right2Alias = new AliasedRelation(right2, SourceName.of("R2"));
    criteria = new JoinOn(new ComparisonExpression(ComparisonExpression.Type.EQUAL, new StringLiteral("left.col0"), new StringLiteral("right.col0")));
    criteria2 = new JoinOn(new ComparisonExpression(ComparisonExpression.Type.EQUAL, new StringLiteral("left.col0"), new StringLiteral("right2.col0")));
    metaStore = MetaStoreFixture.getNewMetaStore(mock(FunctionRegistry.class));
    final KsqlTopic ksqlTopicOrders = new KsqlTopic("orders_topic", KeyFormat.nonWindowed(FormatInfo.of(FormatFactory.KAFKA.name()), SerdeFeatures.of()), ValueFormat.of(FormatInfo.of(FormatFactory.JSON.name()), SerdeFeatures.of()));
    final KsqlStream<?> ksqlStreamOrders = new KsqlStream<>("sqlexpression", SourceName.of("ADDRESS"), ORDERS_SCHEMA, Optional.empty(), false, ksqlTopicOrders, false);
    metaStore.putSource(ksqlStreamOrders, false);
    final KsqlTopic ksqlTopicItems = new KsqlTopic("item_topic", KeyFormat.nonWindowed(FormatInfo.of(FormatFactory.KAFKA.name()), SerdeFeatures.of()), ValueFormat.of(FormatInfo.of(FormatFactory.JSON.name()), SerdeFeatures.of()));
    final KsqlTable<String> ksqlTableOrders = new KsqlTable<>("sqlexpression", SourceName.of("ITEMID"), ITEM_INFO_SCHEMA, Optional.empty(), false, ksqlTopicItems, false);
    metaStore.putSource(ksqlTableOrders, false);
    final KsqlTable<String> ksqlTableTable = new KsqlTable<>("sqlexpression", SourceName.of("TABLE"), TABLE_SCHEMA, Optional.empty(), false, ksqlTopicItems, false);
    metaStore.putSource(ksqlTableTable, false);
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) KsqlStream(io.confluent.ksql.metastore.model.KsqlStream) CreateTable(io.confluent.ksql.parser.tree.CreateTable) KsqlTable(io.confluent.ksql.metastore.model.KsqlTable) DropTable(io.confluent.ksql.parser.tree.DropTable) Table(io.confluent.ksql.parser.tree.Table) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) KsqlTable(io.confluent.ksql.metastore.model.KsqlTable) StringContains.containsString(org.hamcrest.core.StringContains.containsString) JoinOn(io.confluent.ksql.parser.tree.JoinOn) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation) KsqlTopic(io.confluent.ksql.execution.ddl.commands.KsqlTopic) Before(org.junit.Before)

Example 32 with ComparisonExpression

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

Example 33 with ComparisonExpression

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForDecimalDecimalGEQ.

@Test
public void shouldGenerateCorrectCodeForDecimalDecimalGEQ() {
    // Given:
    final ComparisonExpression compExp = new ComparisonExpression(ComparisonExpression.Type.GREATER_THAN_OR_EQUAL, new UnqualifiedColumnReferenceExp(ColumnName.of("COL8")), new UnqualifiedColumnReferenceExp(ColumnName.of("COL9")));
    // When:
    final String java = sqlToJavaVisitor.process(compExp);
    // Then:
    assertThat(java, containsString("(COL8.compareTo(COL9) >= 0))"));
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Test(org.junit.Test)

Example 34 with ComparisonExpression

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForDateDateLT.

@Test
public void shouldGenerateCorrectCodeForDateDateLT() {
    // Given:
    final ComparisonExpression compExp = new ComparisonExpression(Type.LESS_THAN, DATECOL, DATECOL);
    // When:
    final String java = sqlToJavaVisitor.process(compExp);
    // Then:
    assertThat(java, containsString("(COL13.compareTo(COL13) < 0)"));
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 35 with ComparisonExpression

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

the class SqlToJavaVisitorTest method shouldThrowOnTimeDateNEQ.

@Test
public void shouldThrowOnTimeDateNEQ() {
    // Given:
    final ComparisonExpression compExp = new ComparisonExpression(Type.NOT_EQUAL, TIMECOL, DATECOL);
    // Then:
    final Exception e = assertThrows(KsqlException.class, () -> sqlToJavaVisitor.process(compExp));
    assertThat(e.getMessage(), is("Unexpected comparison to TIME: DATE"));
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Aggregations

ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)102 Test (org.junit.Test)100 Expression (io.confluent.ksql.execution.expression.tree.Expression)72 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)67 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)65 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)60 LogicalBinaryExpression (io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression)60 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)55 KsqlException (io.confluent.ksql.util.KsqlException)26 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)25 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)23 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)19 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)19 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)19 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)19 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)19 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)19 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)14 SimpleCaseExpression (io.confluent.ksql.execution.expression.tree.SimpleCaseExpression)13 WindowBounds (io.confluent.ksql.planner.plan.QueryFilterNode.WindowBounds)12