use of io.confluent.ksql.schema.ksql.types.SqlDecimal in project ksql by confluentinc.
the class DecimalUtilTest method shouldWidenIntAndDecimal.
@Test
public void shouldWidenIntAndDecimal() {
// Given:
final SqlDecimal smallerPrecision = SqlTypes.decimal(4, 3);
final SqlDecimal largerPrecision = SqlTypes.decimal(11, 0);
// Then:
assertThat(DecimalUtil.widen(smallerPrecision, SqlTypes.INTEGER), is(SqlTypes.decimal(13, 3)));
assertThat(DecimalUtil.widen(SqlTypes.INTEGER, largerPrecision), is(SqlTypes.decimal(11, 0)));
}
use of io.confluent.ksql.schema.ksql.types.SqlDecimal in project ksql by confluentinc.
the class DecimalUtilTest method shouldAllowImplicitlyCastOnHigherScale.
@Test
public void shouldAllowImplicitlyCastOnHigherScale() {
// Given:
final SqlDecimal s1 = SqlTypes.decimal(2, 1);
final SqlDecimal s2 = SqlTypes.decimal(2, 2);
// When:
final boolean compatible = DecimalUtil.canImplicitlyCast(s1, s2);
// Then:
assertThat(compatible, is(false));
}
use of io.confluent.ksql.schema.ksql.types.SqlDecimal in project ksql by confluentinc.
the class DecimalUtilTest method shouldAllowImplicitlyCastOnHigherPrecisionAndScale.
@Test
public void shouldAllowImplicitlyCastOnHigherPrecisionAndScale() {
// Given:
final SqlDecimal s1 = SqlTypes.decimal(5, 2);
final SqlDecimal s2 = SqlTypes.decimal(6, 3);
// When:
final boolean compatible = DecimalUtil.canImplicitlyCast(s1, s2);
// Then:
assertThat(compatible, is(true));
}
use of io.confluent.ksql.schema.ksql.types.SqlDecimal in project ksql by confluentinc.
the class SqlTypeWalkerTest method shouldVisitDecimal.
@Test
public void shouldVisitDecimal() {
// Given:
final SqlDecimal type = SqlTypes.decimal(10, 2);
when(visitor.visitDecimal(any())).thenReturn("Expected");
// When:
final String result = SqlTypeWalker.visit(type, visitor);
// Then:
verify(visitor).visitDecimal(same(type));
assertThat(result, is("Expected"));
}
use of io.confluent.ksql.schema.ksql.types.SqlDecimal in project ksql by confluentinc.
the class DecimalUtil method widen.
/**
* Return a {@link SqlDecimal} wide enough to hold either the {@code t0} or {@code t1} types.
*
* <p>Both sides must support implicit casting to {@link SqlDecimal}.
*
* @param t0 the first type
* @param t1 the second type
* @return a type wide enough to hold either type.
*/
public static SqlDecimal widen(final SqlType t0, final SqlType t1) {
final SqlDecimal lDecimal = DecimalUtil.toSqlDecimal(t0);
final SqlDecimal rDecimal = DecimalUtil.toSqlDecimal(t1);
final int wholePrecision = Math.max(lDecimal.getPrecision() - lDecimal.getScale(), rDecimal.getPrecision() - rDecimal.getScale());
final int scale = Math.max(lDecimal.getScale(), rDecimal.getScale());
return SqlTypes.decimal(wholePrecision + scale, scale);
}
Aggregations