Search in sources :

Example 16 with DecimalLiteral

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

the class InterpretedExpressionTest method shouldEvaluateUnaryArithmetic.

@Test
public void shouldEvaluateUnaryArithmetic() {
    // Given:
    final Expression expression1 = new ArithmeticUnaryExpression(Optional.empty(), Sign.PLUS, new IntegerLiteral(1));
    final Expression expression2 = new ArithmeticUnaryExpression(Optional.empty(), Sign.MINUS, new IntegerLiteral(1));
    final Expression expression3 = new ArithmeticUnaryExpression(Optional.empty(), Sign.MINUS, new DecimalLiteral(new BigDecimal("345.5")));
    final Expression expression4 = new ArithmeticUnaryExpression(Optional.empty(), Sign.MINUS, new DoubleLiteral(45.5d));
    // When:
    InterpretedExpression interpreter1 = interpreter(expression1);
    InterpretedExpression interpreter2 = interpreter(expression2);
    InterpretedExpression interpreter3 = interpreter(expression3);
    InterpretedExpression interpreter4 = interpreter(expression4);
    // Then:
    assertThat(interpreter1.evaluate(ROW), is(1));
    assertThat(interpreter2.evaluate(ROW), is(-1));
    assertThat(interpreter3.evaluate(ROW), is(new BigDecimal("-345.5")));
    assertThat(interpreter4.evaluate(ROW), is(-45.5d));
}
Also used : 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) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) ArithmeticUnaryExpression(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression) 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 17 with DecimalLiteral

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

the class ImplicitlyCastResolver method resolveToDecimal.

@SuppressWarnings("CyclomaticComplexiIntegerLiteralty")
private static Expression resolveToDecimal(final Expression expression, final SqlDecimal toDecimalType) {
    final BigDecimal literalValue;
    if (expression instanceof IntegerLiteral) {
        literalValue = BigDecimal.valueOf(((IntegerLiteral) expression).getValue());
    } else if (expression instanceof LongLiteral) {
        literalValue = BigDecimal.valueOf(((LongLiteral) expression).getValue());
    } else if (expression instanceof DoubleLiteral) {
        literalValue = BigDecimal.valueOf(((DoubleLiteral) expression).getValue());
    } else if (expression instanceof DecimalLiteral) {
        literalValue = ((DecimalLiteral) expression).getValue();
    } else {
        return expression;
    }
    final SqlDecimal fromDecimalType = (SqlDecimal) DecimalUtil.fromValue(literalValue);
    if (DecimalUtil.canImplicitlyCast(fromDecimalType, toDecimalType)) {
        return new DecimalLiteral(expression.getLocation(), DecimalUtil.cast(literalValue, toDecimalType.getPrecision(), toDecimalType.getScale()));
    }
    return expression;
}
Also used : LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) BigDecimal(java.math.BigDecimal) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral)

Example 18 with DecimalLiteral

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

the class ImplicitlyCastResolverTest method shouldNotCastToDecimal.

@Test
public void shouldNotCastToDecimal() {
    // Given
    final List<Literal> fromLiterals = Arrays.asList(new BooleanLiteral("true"), new StringLiteral("10.2"), new DecimalLiteral(BigDecimal.valueOf(10.133)));
    for (final Literal literal : fromLiterals) {
        // When
        final Expression expression = ImplicitlyCastResolver.resolve(literal, DECIMAL_5_2);
        // Then
        assertThat("Should not cast " + literal.getClass().getSimpleName() + " to " + DECIMAL_5_2, expression, instanceOf(literal.getClass()));
        assertThat("Should not cast " + literal.getClass().getSimpleName() + " to " + DECIMAL_5_2, expression.equals(literal), is(true));
    }
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) Expression(io.confluent.ksql.execution.expression.tree.Expression) BooleanLiteral(io.confluent.ksql.execution.expression.tree.BooleanLiteral) 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) 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