Search in sources :

Example 1 with SqlDecimal

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)));
}
Also used : SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) Test(org.junit.Test)

Example 2 with SqlDecimal

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));
}
Also used : SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) Test(org.junit.Test)

Example 3 with SqlDecimal

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));
}
Also used : SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) Test(org.junit.Test)

Example 4 with SqlDecimal

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"));
}
Also used : SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 5 with SqlDecimal

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);
}
Also used : SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal)

Aggregations

SqlDecimal (io.confluent.ksql.schema.ksql.types.SqlDecimal)20 Test (org.junit.Test)16 BigDecimal (java.math.BigDecimal)4 DecimalLiteral (io.confluent.ksql.execution.expression.tree.DecimalLiteral)3 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)3 CreateArrayExpression (io.confluent.ksql.execution.expression.tree.CreateArrayExpression)2 CreateMapExpression (io.confluent.ksql.execution.expression.tree.CreateMapExpression)2 CreateStructExpression (io.confluent.ksql.execution.expression.tree.CreateStructExpression)2 Expression (io.confluent.ksql.execution.expression.tree.Expression)2 LongLiteral (io.confluent.ksql.execution.expression.tree.LongLiteral)2 Result (io.confluent.ksql.execution.util.CoercionUtil.Result)2 SqlArgument (io.confluent.ksql.schema.ksql.SqlArgument)2 KsqlException (io.confluent.ksql.util.KsqlException)2 DoubleLiteral (io.confluent.ksql.execution.expression.tree.DoubleLiteral)1 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)1 ArithmeticBinaryTerm (io.confluent.ksql.execution.interpreter.terms.ArithmeticBinaryTerm)1 ArithmeticUnaryTerm (io.confluent.ksql.execution.interpreter.terms.ArithmeticUnaryTerm)1 CastTerm (io.confluent.ksql.execution.interpreter.terms.CastTerm)1 Term (io.confluent.ksql.execution.interpreter.terms.Term)1 UdfSchemaProvider (io.confluent.ksql.function.udf.UdfSchemaProvider)1