Search in sources :

Example 71 with ComparisonExpression

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

the class FilterTypeValidatorTest method shouldThrowOnBadTypeComparison_twoVars.

@Test
public void shouldThrowOnBadTypeComparison_twoVars() {
    // Given:
    final Expression left = new UnqualifiedColumnReferenceExp(COLUMN1);
    final Expression right = new UnqualifiedColumnReferenceExp(COLUMN2);
    final Expression comparision = new ComparisonExpression(Type.EQUAL, left, right);
    when(schema.findValueColumn(COLUMN1)).thenReturn(Optional.of(Column.of(COLUMN1, STRING, VALUE, 10)));
    when(schema.findValueColumn(COLUMN2)).thenReturn(Optional.of(Column.of(COLUMN2, INTEGER, VALUE, 10)));
    // When:
    assertThrows("Error in WHERE expression: " + "Cannot compare col1 (STRING) to col2 (INTEGER) with EQUAL.", KsqlException.class, () -> validator.validateFilterExpression(comparision));
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) Test(org.junit.Test)

Example 72 with ComparisonExpression

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

the class FilterTypeValidatorTest method shouldNotThrowOnGoodTypeComparison.

@Test
public void shouldNotThrowOnGoodTypeComparison() {
    // Given:
    final Expression left = new UnqualifiedColumnReferenceExp(COLUMN1);
    final Expression right = new IntegerLiteral(10);
    final Expression comparision = new ComparisonExpression(Type.EQUAL, left, right);
    when(schema.findValueColumn(any())).thenReturn(Optional.of(Column.of(COLUMN1, INTEGER, VALUE, 10)));
    // When:
    validator.validateFilterExpression(comparision);
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 73 with ComparisonExpression

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

the class FilterTypeValidatorTest method shouldThrowOnBadTypeCompoundComparison_rightError.

@Test
public void shouldThrowOnBadTypeCompoundComparison_rightError() {
    // Given:
    final Expression left1 = new UnqualifiedColumnReferenceExp(COLUMN2);
    final Expression right1 = new IntegerLiteral(10);
    final Expression comparision1 = new ComparisonExpression(Type.EQUAL, left1, right1);
    final Expression left2 = new UnqualifiedColumnReferenceExp(COLUMN1);
    final Expression right2 = new UnqualifiedColumnReferenceExp(COLUMN2);
    final Expression comparision2 = new ComparisonExpression(Type.EQUAL, left2, right2);
    final Expression expression = new LogicalBinaryExpression(LogicalBinaryExpression.Type.AND, comparision1, comparision2);
    when(schema.findValueColumn(COLUMN1)).thenReturn(Optional.of(Column.of(COLUMN1, STRING, VALUE, 10)));
    when(schema.findValueColumn(COLUMN2)).thenReturn(Optional.of(Column.of(COLUMN2, INTEGER, VALUE, 10)));
    // When:
    assertThrows("Error in WHERE expression: " + "Cannot compare col1 (STRING) to col2 (INTEGER) with EQUAL.", KsqlException.class, () -> validator.validateFilterExpression(expression));
}
Also used : LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) UnqualifiedColumnReferenceExp(io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 74 with ComparisonExpression

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

the class InterpretedExpressionTest method shouldEvaluateSearchedCase.

@Test
public void shouldEvaluateSearchedCase() {
    // Given:
    final Expression case1 = new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ComparisonExpression(ComparisonExpression.Type.GREATER_THAN, COL7, new IntegerLiteral(10)), new StringLiteral("Large")), new WhenClause(new ComparisonExpression(ComparisonExpression.Type.GREATER_THAN, COL7, new IntegerLiteral(5)), new StringLiteral("Medium")), new WhenClause(new ComparisonExpression(ComparisonExpression.Type.GREATER_THAN, COL7, new IntegerLiteral(2)), new StringLiteral("Small"))), Optional.of(new StringLiteral("Tiny")));
    final Expression case2 = new SearchedCaseExpression(ImmutableList.of(new WhenClause(new ComparisonExpression(ComparisonExpression.Type.LESS_THAN, COL7, new IntegerLiteral(6)), new StringLiteral("Blah"))), Optional.empty());
    // When:
    InterpretedExpression interpreter1 = interpreter(case1);
    InterpretedExpression interpreter2 = interpreter(case2);
    // Then:
    assertThat(interpreter1.evaluate(make(7, 12)), is("Large"));
    assertThat(interpreter1.evaluate(make(7, 9)), is("Medium"));
    assertThat(interpreter1.evaluate(make(7, 3)), is("Small"));
    assertThat(interpreter1.evaluate(make(7, 1)), is("Tiny"));
    assertThat(interpreter2.evaluate(make(7, 1)), is("Blah"));
    assertThat(interpreter2.evaluate(make(7, 10)), nullValue());
}
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) LogicalBinaryExpression(io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DereferenceExpression(io.confluent.ksql.execution.expression.tree.DereferenceExpression) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) 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 75 with ComparisonExpression

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

the class SqlToJavaVisitorTest method shouldGenerateCorrectCodeForDoubleDecimalEQ.

@Test
public void shouldGenerateCorrectCodeForDoubleDecimalEQ() {
    // Given:
    final ComparisonExpression compExp = new ComparisonExpression(ComparisonExpression.Type.EQUAL, new UnqualifiedColumnReferenceExp(ColumnName.of("COL3")), new UnqualifiedColumnReferenceExp(ColumnName.of("COL8")));
    // When:
    final String java = sqlToJavaVisitor.process(compExp);
    // Then:
    assertThat(java, containsString("(BigDecimal.valueOf(COL3).compareTo(COL8) == 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)

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