Search in sources :

Example 56 with ComparisonExpression

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

the class ExpressionTypeManagerTest method shouldFailIfDefaultHasDifferentTypeToWhen.

@Test
public void shouldFailIfDefaultHasDifferentTypeToWhen() {
    // Given:
    final Expression expression = new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ComparisonExpression(Type.EQUAL, TestExpressions.COL0, new IntegerLiteral(10)), new StringLiteral("good"))), Optional.of(new BooleanLiteral("true")));
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> expressionTypeManager.getExpressionSqlType(expression));
    // Then:
    assertThat(e.getMessage(), containsString("Invalid Case expression. Type for the default clause should be the same as for 'THEN' clauses." + System.lineSeparator() + "THEN type: STRING." + System.lineSeparator() + "DEFAULT type: BOOLEAN."));
}
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) BooleanLiteral(io.confluent.ksql.execution.expression.tree.BooleanLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 57 with ComparisonExpression

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

the class KsqlParserTest method testProjectFilter.

@Test
public void testProjectFilter() {
    final String queryStr = "SELECT col0, col2, col3 FROM test1 WHERE col0 > 100;";
    final Statement statement = KsqlParserTestUtil.buildSingleAst(queryStr, metaStore).getStatement();
    assertThat(statement, is(instanceOf(Query.class)));
    final Query query = (Query) statement;
    Assert.assertTrue(query.getWhere().get() instanceof ComparisonExpression);
    final ComparisonExpression comparisonExpression = (ComparisonExpression) query.getWhere().get();
    assertThat(comparisonExpression.toString(), is("(COL0 > 100)"));
    assertThat(query.getSelect().getSelectItems(), hasSize(3));
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) Query(io.confluent.ksql.parser.tree.Query) TerminateQuery(io.confluent.ksql.parser.tree.TerminateQuery) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 58 with ComparisonExpression

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

the class SchemaKStreamTest method shouldRewriteTimeComparisonInFilter.

@Test
@SuppressWarnings("rawtypes")
public void shouldRewriteTimeComparisonInFilter() {
    // Given:
    final PlanNode logicalPlan = givenInitialKStreamOf("SELECT col0, col2, col3 FROM test1 " + "WHERE ROWTIME = '1984-01-01T00:00:00+00:00' EMIT CHANGES;");
    final FilterNode filterNode = (FilterNode) logicalPlan.getSources().get(0).getSources().get(0);
    // When:
    final SchemaKStream<?> filteredSchemaKStream = initialSchemaKStream.filter(filterNode.getPredicate(), childContextStacker);
    // Then:
    final StreamFilter step = (StreamFilter) filteredSchemaKStream.getSourceStep();
    assertThat(step.getFilterExpression(), equalTo(new ComparisonExpression(ComparisonExpression.Type.EQUAL, new UnqualifiedColumnReferenceExp(ColumnName.of("ROWTIME")), new LongLiteral(441763200000L))));
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) PlanNode(io.confluent.ksql.planner.plan.PlanNode) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) FilterNode(io.confluent.ksql.planner.plan.FilterNode) StreamFilter(io.confluent.ksql.execution.plan.StreamFilter) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Test(org.junit.Test)

Example 59 with ComparisonExpression

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForDecimalDecimalLEQ.

@Test
public void shouldGenerateCorrectCodeForDecimalDecimalLEQ() {
    // Given:
    final ComparisonExpression compExp = new ComparisonExpression(ComparisonExpression.Type.LESS_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 60 with ComparisonExpression

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForTimestampStringEQ.

@Test
public void shouldGenerateCorrectCodeForTimestampStringEQ() {
    // Given:
    final ComparisonExpression compExp = new ComparisonExpression(Type.EQUAL, TIMESTAMPCOL, new StringLiteral("2020-01-01T00:00:00"));
    // When:
    final String java = sqlToJavaVisitor.process(compExp);
    // Then:
    assertThat(java, containsString("(COL10.compareTo(SqlTimeTypes.parseTimestamp(\"2020-01-01T00:00:00\")) == 0)"));
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) 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