Search in sources :

Example 1 with DecimalLiteral

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

the class ImplicitlyCastResolverTest method shouldCastToDecimal.

@Test
public void shouldCastToDecimal() {
    // Given
    final Map<Literal, BigDecimal> fromLiterals = ImmutableMap.of(new IntegerLiteral(5), new BigDecimal("5.00"), new LongLiteral(5), new BigDecimal("5.00"), new DoubleLiteral(5), new BigDecimal("5.00"), new DecimalLiteral(BigDecimal.TEN), new BigDecimal("10.00"), new DecimalLiteral(new BigDecimal("10.1")), new BigDecimal("10.10"));
    for (final Map.Entry<Literal, BigDecimal> entry : fromLiterals.entrySet()) {
        final Literal literal = entry.getKey();
        final BigDecimal expected = entry.getValue();
        // When
        final Expression expression = ImplicitlyCastResolver.resolve(literal, DECIMAL_5_2);
        // Then
        assertThat("Should cast " + literal.getClass().getSimpleName() + " to " + DECIMAL_5_2, expression, instanceOf(DecimalLiteral.class));
        assertThat("Should cast " + literal.getClass().getSimpleName() + " to " + DECIMAL_5_2, ((DecimalLiteral) expression).getValue(), is(expected));
    }
}
Also used : LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) Expression(io.confluent.ksql.execution.expression.tree.Expression) Literal(io.confluent.ksql.execution.expression.tree.Literal) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) BooleanLiteral(io.confluent.ksql.execution.expression.tree.BooleanLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) BigDecimal(java.math.BigDecimal) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 2 with DecimalLiteral

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

the class InterpretedExpressionTest method shouldEvaluateCastToString.

@Test
public void shouldEvaluateCastToString() {
    // Given:
    final Expression cast1 = new Cast(new IntegerLiteral(10), new Type(SqlPrimitiveType.of("STRING")));
    final Expression cast2 = new Cast(new LongLiteral(1234L), new Type(SqlPrimitiveType.of("STRING")));
    final Expression cast3 = new Cast(new DoubleLiteral(12.5), new Type(SqlPrimitiveType.of("STRING")));
    final Expression cast4 = new Cast(new DecimalLiteral(new BigDecimal("4567.5")), new Type(SqlPrimitiveType.of("STRING")));
    // When:
    InterpretedExpression interpreter1 = interpreter(cast1);
    InterpretedExpression interpreter2 = interpreter(cast2);
    InterpretedExpression interpreter3 = interpreter(cast3);
    InterpretedExpression interpreter4 = interpreter(cast4);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is("10"));
    assertThat(interpreter2.evaluate(ROW), is("1234"));
    assertThat(interpreter3.evaluate(ROW), is("12.5"));
    assertThat(interpreter4.evaluate(ROW), is("4567.5"));
}
Also used : Cast(io.confluent.ksql.execution.expression.tree.Cast) LambdaType(io.confluent.ksql.function.types.LambdaType) IntegerType(io.confluent.ksql.function.types.IntegerType) GenericType(io.confluent.ksql.function.types.GenericType) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) IntervalUnitType(io.confluent.ksql.function.types.IntervalUnitType) Type(io.confluent.ksql.execution.expression.tree.Type) ArrayType(io.confluent.ksql.function.types.ArrayType) 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) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 3 with DecimalLiteral

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

the class InterpretedExpressionTest method shouldEvaluateCastToDouble.

@Test
public void shouldEvaluateCastToDouble() {
    // Given:
    final Expression cast1 = new Cast(new LongLiteral(10L), new Type(SqlPrimitiveType.of("DOUBLE")));
    final Expression cast2 = new Cast(new StringLiteral("1234.5"), new Type(SqlPrimitiveType.of("DOUBLE")));
    final Expression cast3 = new Cast(new IntegerLiteral(12), new Type(SqlPrimitiveType.of("DOUBLE")));
    final Expression cast4 = new Cast(new DecimalLiteral(new BigDecimal("4567.5")), new Type(SqlPrimitiveType.of("DOUBLE")));
    // When:
    InterpretedExpression interpreter1 = interpreter(cast1);
    InterpretedExpression interpreter2 = interpreter(cast2);
    InterpretedExpression interpreter3 = interpreter(cast3);
    InterpretedExpression interpreter4 = interpreter(cast4);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(10d));
    assertThat(interpreter2.evaluate(ROW), is(1234.5d));
    assertThat(interpreter3.evaluate(ROW), is(12d));
    assertThat(interpreter4.evaluate(ROW), is(4567.5d));
}
Also used : Cast(io.confluent.ksql.execution.expression.tree.Cast) LambdaType(io.confluent.ksql.function.types.LambdaType) IntegerType(io.confluent.ksql.function.types.IntegerType) GenericType(io.confluent.ksql.function.types.GenericType) SqlPrimitiveType(io.confluent.ksql.schema.ksql.types.SqlPrimitiveType) IntervalUnitType(io.confluent.ksql.function.types.IntervalUnitType) Type(io.confluent.ksql.execution.expression.tree.Type) ArrayType(io.confluent.ksql.function.types.ArrayType) 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) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 4 with DecimalLiteral

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

the class InterpretedExpressionTest method shouldEvaluateComparisons_decimal.

@Test
public void shouldEvaluateComparisons_decimal() {
    // Given:
    final Expression expression1 = new ComparisonExpression(ComparisonExpression.Type.GREATER_THAN, COL8, new DecimalLiteral(new BigDecimal("1.2")));
    final Expression expression2 = new ComparisonExpression(ComparisonExpression.Type.LESS_THAN, COL8, new DecimalLiteral(new BigDecimal("5.1")));
    final Expression expression3 = new ComparisonExpression(ComparisonExpression.Type.EQUAL, COL8, new DecimalLiteral(new BigDecimal("10.4")));
    final Expression expression4 = new ComparisonExpression(ComparisonExpression.Type.GREATER_THAN, COL8, new IntegerLiteral(5));
    final Expression expression5 = new ComparisonExpression(ComparisonExpression.Type.LESS_THAN, COL8, new DoubleLiteral(6.5));
    final Expression expression6 = new ComparisonExpression(ComparisonExpression.Type.LESS_THAN, COL8, new LongLiteral(10L));
    // When:
    InterpretedExpression interpreter1 = interpreter(expression1);
    InterpretedExpression interpreter2 = interpreter(expression2);
    InterpretedExpression interpreter3 = interpreter(expression3);
    InterpretedExpression interpreter4 = interpreter(expression4);
    InterpretedExpression interpreter5 = interpreter(expression5);
    InterpretedExpression interpreter6 = interpreter(expression6);
    // Then:
    assertThat(interpreter1.evaluate(make(8, new BigDecimal("3.4"))), is(true));
    assertThat(interpreter1.evaluate(make(8, new BigDecimal("1.1"))), is(false));
    assertThat(interpreter2.evaluate(make(8, new BigDecimal("4.9"))), is(true));
    assertThat(interpreter2.evaluate(make(8, new BigDecimal("5.2"))), is(false));
    assertThat(interpreter3.evaluate(make(8, new BigDecimal("10.4"))), is(true));
    assertThat(interpreter3.evaluate(make(8, new BigDecimal("10.5"))), is(false));
    assertThat(interpreter4.evaluate(make(8, new BigDecimal("6.5"))), is(true));
    assertThat(interpreter4.evaluate(make(8, new BigDecimal("4.5"))), is(false));
    assertThat(interpreter5.evaluate(make(8, new BigDecimal("5.5"))), is(true));
    assertThat(interpreter5.evaluate(make(8, new BigDecimal("7.5"))), is(false));
    assertThat(interpreter6.evaluate(make(8, new BigDecimal("7"))), is(true));
    assertThat(interpreter6.evaluate(make(8, new BigDecimal("19.567"))), is(false));
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) 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) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) BigDecimal(java.math.BigDecimal) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 5 with DecimalLiteral

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

the class InterpretedExpressionTest method shouldEvaluateArithmetic.

@Test
public void shouldEvaluateArithmetic() {
    // Given:
    final Expression expression1 = new ArithmeticBinaryExpression(Operator.ADD, new IntegerLiteral(1), new IntegerLiteral(2));
    final Expression expression2 = new ArithmeticBinaryExpression(Operator.ADD, new IntegerLiteral(1), new LongLiteral(4));
    final Expression expression3 = new ArithmeticBinaryExpression(Operator.ADD, new DoubleLiteral(5.5), new LongLiteral(4));
    final Expression expression4 = new ArithmeticBinaryExpression(Operator.MULTIPLY, new IntegerLiteral(5), new LongLiteral(4));
    final Expression expression5 = new ArithmeticBinaryExpression(Operator.DIVIDE, new LongLiteral(18), new LongLiteral(3));
    final Expression expression6 = new ArithmeticBinaryExpression(Operator.MODULUS, new LongLiteral(20), new LongLiteral(3));
    final Expression expression7 = new ArithmeticBinaryExpression(Operator.ADD, new DecimalLiteral(new BigDecimal("12.5").setScale(2)), new DecimalLiteral(new BigDecimal("1.25").setScale(2)));
    final Expression expression8 = new ArithmeticBinaryExpression(Operator.ADD, new DecimalLiteral(new BigDecimal("12.5").setScale(2)), new DoubleLiteral(2.0d));
    // When:
    InterpretedExpression interpreter1 = interpreter(expression1);
    InterpretedExpression interpreter2 = interpreter(expression2);
    InterpretedExpression interpreter3 = interpreter(expression3);
    InterpretedExpression interpreter4 = interpreter(expression4);
    InterpretedExpression interpreter5 = interpreter(expression5);
    InterpretedExpression interpreter6 = interpreter(expression6);
    InterpretedExpression interpreter7 = interpreter(expression7);
    InterpretedExpression interpreter8 = interpreter(expression8);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(3));
    assertThat(interpreter2.evaluate(ROW), is(5L));
    assertThat(interpreter3.evaluate(ROW), is(9.5d));
    assertThat(interpreter4.evaluate(ROW), is(20L));
    assertThat(interpreter5.evaluate(ROW), is(6L));
    assertThat(interpreter6.evaluate(ROW), is(2L));
    assertThat(interpreter7.evaluate(ROW), is(BigDecimal.valueOf(13.75).setScale(2)));
    assertThat(interpreter8.evaluate(ROW), is(14.5d));
}
Also used : ArithmeticBinaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression) 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) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

DecimalLiteral (io.confluent.ksql.execution.expression.tree.DecimalLiteral)18 BigDecimal (java.math.BigDecimal)17 Test (org.junit.Test)17 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)16 Expression (io.confluent.ksql.execution.expression.tree.Expression)13 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)11 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)11 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)11 DoubleLiteral (io.confluent.ksql.execution.expression.tree.DoubleLiteral)10 LongLiteral (io.confluent.ksql.execution.expression.tree.LongLiteral)10 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)9 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)7 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)7 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)7 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)7 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)7 LogicalBinaryExpression (io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression)7 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)7 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)7 Cast (io.confluent.ksql.execution.expression.tree.Cast)4