use of io.confluent.ksql.execution.expression.tree.DecimalLiteral in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldImplicitlyCastFunctionCallParameters.
@Test
public void shouldImplicitlyCastFunctionCallParameters() {
// Given:
final UdfFactory udfFactory = mock(UdfFactory.class);
final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
givenUdf("FOO", udfFactory, udf, SqlTypes.STRING);
when(udf.parameters()).thenReturn(ImmutableList.of(ParamTypes.DOUBLE, ParamTypes.LONG));
// When:
final String javaExpression = sqlToJavaVisitor.process(new FunctionCall(FunctionName.of("FOO"), ImmutableList.of(new DecimalLiteral(new BigDecimal("1.2")), new IntegerLiteral(1))));
// Then:
final String doubleCast = CastEvaluator.generateCode("new BigDecimal(\"1.2\")", SqlTypes.decimal(2, 1), SqlTypes.DOUBLE, ksqlConfig);
final String longCast = CastEvaluator.generateCode("1", SqlTypes.INTEGER, SqlTypes.BIGINT, ksqlConfig);
assertThat(javaExpression, is("((String) FOO_0.evaluate(" + doubleCast + ", " + longCast + "))"));
}
use of io.confluent.ksql.execution.expression.tree.DecimalLiteral in project ksql by confluentinc.
the class CoercionUtilTest method shouldCoerceStringNumericWithDecimalPointToDecimals.
@Test
public void shouldCoerceStringNumericWithDecimalPointToDecimals() {
// Given:
final ImmutableList<Expression> expressions = ImmutableList.of(new IntegerLiteral(10), new StringLiteral("1.0"));
// When:
final Result result = CoercionUtil.coerceUserList(expressions, typeManager);
// Then:
assertThat(result.commonType(), is(Optional.of(SqlTypes.decimal(11, 1))));
assertThat(result.expressions(), is(ImmutableList.of(new DecimalLiteral(new BigDecimal("10.0")), new DecimalLiteral(new BigDecimal("1.0")))));
}
use of io.confluent.ksql.execution.expression.tree.DecimalLiteral 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))));
}
use of io.confluent.ksql.execution.expression.tree.DecimalLiteral in project ksql by confluentinc.
the class KsqlParserTest method shouldParseDecimals.
@Test
public void shouldParseDecimals() {
assertThat(parseDouble("0.1"), is(new DecimalLiteral(new BigDecimal("0.1"))));
assertThat(parseDouble("0.123"), is(new DecimalLiteral(new BigDecimal("0.123"))));
assertThat(parseDouble("00123.000"), is(new DecimalLiteral(new BigDecimal("123.000"))));
}
use of io.confluent.ksql.execution.expression.tree.DecimalLiteral in project ksql by confluentinc.
the class SqlToJavaVisitorTest method shouldImplicitlyCastFunctionCallParametersVariadic.
@Test
public void shouldImplicitlyCastFunctionCallParametersVariadic() {
// Given:
final UdfFactory udfFactory = mock(UdfFactory.class);
final KsqlScalarFunction udf = mock(KsqlScalarFunction.class);
givenUdf("FOO", udfFactory, udf, SqlTypes.STRING);
when(udf.parameters()).thenReturn(ImmutableList.of(ParamTypes.DOUBLE, ArrayType.of(ParamTypes.LONG)));
when(udf.isVariadic()).thenReturn(true);
// When:
final String javaExpression = sqlToJavaVisitor.process(new FunctionCall(FunctionName.of("FOO"), ImmutableList.of(new DecimalLiteral(new BigDecimal("1.2")), new IntegerLiteral(1), new IntegerLiteral(1))));
// Then:
final String doubleCast = CastEvaluator.generateCode("new BigDecimal(\"1.2\")", SqlTypes.decimal(2, 1), SqlTypes.DOUBLE, ksqlConfig);
final String longCast = CastEvaluator.generateCode("1", SqlTypes.INTEGER, SqlTypes.BIGINT, ksqlConfig);
assertThat(javaExpression, is("((String) FOO_0.evaluate(" + doubleCast + ", " + longCast + ", " + longCast + "))"));
}
Aggregations