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));
}
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;
}
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));
}
}
Aggregations