Search in sources :

Example 16 with SqlDecimal

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

Example 17 with SqlDecimal

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;
}
Also used : LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) BigDecimal(java.math.BigDecimal) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral)

Example 18 with SqlDecimal

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);
    }
}
Also used : CastTerm(io.confluent.ksql.execution.interpreter.terms.CastTerm) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) CastTerm(io.confluent.ksql.execution.interpreter.terms.CastTerm) ArithmeticBinaryTerm(io.confluent.ksql.execution.interpreter.terms.ArithmeticBinaryTerm) ArithmeticUnaryTerm(io.confluent.ksql.execution.interpreter.terms.ArithmeticUnaryTerm) Term(io.confluent.ksql.execution.interpreter.terms.Term) BigDecimal(java.math.BigDecimal) ArithmeticBinaryTerm(io.confluent.ksql.execution.interpreter.terms.ArithmeticBinaryTerm)

Example 19 with SqlDecimal

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

Example 20 with SqlDecimal

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);
}
Also used : SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) KsqlException(io.confluent.ksql.util.KsqlException) UdfSchemaProvider(io.confluent.ksql.function.udf.UdfSchemaProvider)

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