use of io.confluent.ksql.schema.ksql.types.SqlDecimal in project ksql by confluentinc.
the class DecimalUtilTest method shouldConvertIntegerToSqlDecimal.
@Test
public void shouldConvertIntegerToSqlDecimal() {
// When:
final SqlDecimal decimal = DecimalUtil.toSqlDecimal(SqlTypes.INTEGER);
// Then:
assertThat(decimal, is(SqlTypes.decimal(10, 0)));
}
use of io.confluent.ksql.schema.ksql.types.SqlDecimal 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.schema.ksql.types.SqlDecimal in project ksql by confluentinc.
the class ArithmeticInterpreter method doBinaryArithmetic.
/**
* Creates a term representing binary arithmetic on the given input term.
* @param operator The operator in use
* @param left The left term
* @param right The right term
* @param resultType The type of the resulting operation
* @param ksqlConfig The ksqlconfig
* @return the resulting term
*/
public static Term doBinaryArithmetic(final Operator operator, final Term left, final Term right, final SqlType resultType, final KsqlConfig ksqlConfig) {
if (resultType.baseType() == SqlBaseType.DECIMAL) {
final SqlDecimal decimal = (SqlDecimal) resultType;
final CastTerm leftTerm = CastInterpreter.cast(left, left.getSqlType(), DecimalUtil.toSqlDecimal(left.getSqlType()), ksqlConfig);
final CastTerm rightTerm = CastInterpreter.cast(right, right.getSqlType(), DecimalUtil.toSqlDecimal(right.getSqlType()), ksqlConfig);
final TypedArithmeticBinaryFunction<BigDecimal> fn = getDecimalFunction(decimal, operator);
return new ArithmeticBinaryTerm(leftTerm, rightTerm, (o1, o2) -> fn.doFunction((BigDecimal) o1, (BigDecimal) o2), resultType);
} else {
final Term leftTerm = left.getSqlType().baseType() == SqlBaseType.DECIMAL ? CastInterpreter.cast(left, left.getSqlType(), SqlTypes.DOUBLE, ksqlConfig) : left;
final Term rightTerm = right.getSqlType().baseType() == SqlBaseType.DECIMAL ? CastInterpreter.cast(right, right.getSqlType(), SqlTypes.DOUBLE, ksqlConfig) : right;
return new ArithmeticBinaryTerm(leftTerm, rightTerm, getNonDecimalArithmeticFunction(operator, leftTerm.getSqlType(), rightTerm.getSqlType()), resultType);
}
}
use of io.confluent.ksql.schema.ksql.types.SqlDecimal in project ksql by confluentinc.
the class EntityUtilTest method shouldBuildCorrectDecimalField.
@Test
public void shouldBuildCorrectDecimalField() {
// Given:
final SqlDecimal decimal = SqlTypes.decimal(10, 9);
final LogicalSchema schema = LogicalSchema.builder().valueColumn(ColumnName.of("field"), decimal).build();
// When:
final List<FieldInfo> fields = EntityUtil.buildSourceSchemaEntity(schema);
// Then:
assertThat(fields, hasSize(1));
assertThat(fields.get(0).getName(), equalTo("field"));
assertThat(fields.get(0).getSchema().getTypeName(), equalTo("DECIMAL"));
assertThat(fields.get(0).getSchema().getFields(), equalTo(Optional.empty()));
assertThat(fields.get(0).getSchema().getParameters().get(SqlDecimal.SCALE), equalTo(decimal.getScale()));
assertThat(fields.get(0).getSchema().getParameters().get(SqlDecimal.PRECISION), equalTo(decimal.getPrecision()));
}
use of io.confluent.ksql.schema.ksql.types.SqlDecimal in project ksql by confluentinc.
the class Round method provideDecimalSchema.
// Invoked via reflection
@SuppressWarnings("unused")
@UdfSchemaProvider
public static SqlType provideDecimalSchema(final List<SqlArgument> params) {
final SqlType s0 = params.get(0).getSqlTypeOrThrow();
if (s0.baseType() != SqlBaseType.DECIMAL) {
throw new KsqlException("The schema provider method for round expects a BigDecimal parameter" + "type as a parameter.");
}
final SqlDecimal param = (SqlDecimal) s0;
return SqlDecimal.of(param.getPrecision() - param.getScale(), 0);
}
Aggregations