Search in sources :

Example 21 with DoubleLiteral

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

Aggregations

DoubleLiteral (io.confluent.ksql.execution.expression.tree.DoubleLiteral)21 Test (org.junit.Test)20 Expression (io.confluent.ksql.execution.expression.tree.Expression)16 ArithmeticBinaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticBinaryExpression)15 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)15 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)15 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)15 ArithmeticUnaryExpression (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression)14 ComparisonExpression (io.confluent.ksql.execution.expression.tree.ComparisonExpression)14 InListExpression (io.confluent.ksql.execution.expression.tree.InListExpression)14 SearchedCaseExpression (io.confluent.ksql.execution.expression.tree.SearchedCaseExpression)14 SubscriptExpression (io.confluent.ksql.execution.expression.tree.SubscriptExpression)14 DereferenceExpression (io.confluent.ksql.execution.expression.tree.DereferenceExpression)11 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)11 LogicalBinaryExpression (io.confluent.ksql.execution.expression.tree.LogicalBinaryExpression)11 LongLiteral (io.confluent.ksql.execution.expression.tree.LongLiteral)10 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)10 DecimalLiteral (io.confluent.ksql.execution.expression.tree.DecimalLiteral)9 BigDecimal (java.math.BigDecimal)9 Cast (io.confluent.ksql.execution.expression.tree.Cast)6