Search in sources :

Example 6 with SqlDecimal

use of io.confluent.ksql.schema.ksql.types.SqlDecimal in project ksql by confluentinc.

the class CoercionUtilTest method shouldCoerceToDecimals.

@Test
public void shouldCoerceToDecimals() {
    // Given:
    final ImmutableList<Expression> expressions = ImmutableList.of(new IntegerLiteral(10), new LongLiteral(1234567890), new StringLiteral("\t -100.010 \t"), BIGINT_EXPRESSION, DECIMAL_EXPRESSION, INT_EXPRESSION);
    // When:
    final Result result = CoercionUtil.coerceUserList(expressions, typeManager);
    // Then:
    final SqlDecimal decimalType = SqlTypes.decimal(22, 3);
    assertThat(result.commonType(), is(Optional.of(decimalType)));
    assertThat(result.expressions(), is(ImmutableList.of(new DecimalLiteral(new BigDecimal("10.000")), new DecimalLiteral(new BigDecimal("1234567890.000")), new DecimalLiteral(new BigDecimal("-100.010")), cast(BIGINT_EXPRESSION, decimalType), cast(DECIMAL_EXPRESSION, decimalType), cast(INT_EXPRESSION, decimalType))));
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) BigDecimal(java.math.BigDecimal) Result(io.confluent.ksql.execution.util.CoercionUtil.Result) Test(org.junit.Test)

Example 7 with SqlDecimal

use of io.confluent.ksql.schema.ksql.types.SqlDecimal in project ksql by confluentinc.

the class UdfLoaderTest method shouldLoadFunctionWithSchemaProvider.

@Test
public void shouldLoadFunctionWithSchemaProvider() {
    // Given:
    final UdfFactory returnDecimal = FUNC_REG.getUdfFactory(FunctionName.of("returndecimal"));
    // When:
    final SqlDecimal decimal = SqlTypes.decimal(2, 1);
    final List<SqlArgument> args = Collections.singletonList(SqlArgument.of(decimal));
    final KsqlScalarFunction function = returnDecimal.getFunction(args);
    // Then:
    assertThat(function.getReturnType(args), equalTo(decimal));
}
Also used : SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) Test(org.junit.Test)

Example 8 with SqlDecimal

use of io.confluent.ksql.schema.ksql.types.SqlDecimal in project ksql by confluentinc.

the class UdfLoaderTest method shouldThrowOnReturnTypeMismatch.

@Test
public void shouldThrowOnReturnTypeMismatch() {
    // Given:
    final UdfFactory returnIncompatible = FUNC_REG.getUdfFactory(of("returnincompatible"));
    final SqlDecimal decimal = decimal(2, 1);
    final List<SqlArgument> args = singletonList(SqlArgument.of(decimal));
    final KsqlScalarFunction function = returnIncompatible.getFunction(args);
    // When:
    final Exception e = assertThrows(KsqlException.class, () -> function.getReturnType(args));
    // Then:
    assertThat(e.getMessage(), containsString("Return type DECIMAL(2, 1) of UDF RETURNINCOMPATIBLE does not " + "match the declared return type STRING."));
}
Also used : SqlArgument(io.confluent.ksql.schema.ksql.SqlArgument) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) KsqlException(io.confluent.ksql.util.KsqlException) Test(org.junit.Test)

Example 9 with SqlDecimal

use of io.confluent.ksql.schema.ksql.types.SqlDecimal in project ksql by confluentinc.

the class UdfLoaderTest method shouldLoadDecimalUdfs.

@Test
public void shouldLoadDecimalUdfs() {
    // Given:
    final SqlDecimal schema = SqlTypes.decimal(2, 1);
    // When:
    final KsqlScalarFunction fun = FUNC_REG.getUdfFactory(FunctionName.of("floor")).getFunction(ImmutableList.of(SqlArgument.of(schema)));
    // Then:
    assertThat(fun.name().text(), equalToIgnoringCase("floor"));
}
Also used : SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) Test(org.junit.Test)

Example 10 with SqlDecimal

use of io.confluent.ksql.schema.ksql.types.SqlDecimal in project ksql by confluentinc.

the class CoercionUtilTest method shouldCoerceToIntsToDecimals.

@Test
public void shouldCoerceToIntsToDecimals() {
    // Given:
    final ImmutableList<Expression> expressions = ImmutableList.of(new DecimalLiteral(new BigDecimal("1.1")), new IntegerLiteral(10), INT_EXPRESSION);
    // When:
    final Result result = CoercionUtil.coerceUserList(expressions, typeManager);
    // Then:
    final SqlDecimal decimalType = SqlTypes.decimal(11, 1);
    assertThat(result.commonType(), is(Optional.of(decimalType)));
    assertThat(result.expressions(), is(ImmutableList.of(new DecimalLiteral(new BigDecimal("1.1")), new DecimalLiteral(new BigDecimal("10.0")), cast(INT_EXPRESSION, decimalType))));
}
Also used : CreateArrayExpression(io.confluent.ksql.execution.expression.tree.CreateArrayExpression) CreateStructExpression(io.confluent.ksql.execution.expression.tree.CreateStructExpression) Expression(io.confluent.ksql.execution.expression.tree.Expression) CreateMapExpression(io.confluent.ksql.execution.expression.tree.CreateMapExpression) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) BigDecimal(java.math.BigDecimal) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Result(io.confluent.ksql.execution.util.CoercionUtil.Result) Test(org.junit.Test)

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