Search in sources :

Example 1 with CastTerm

use of io.confluent.ksql.execution.interpreter.terms.CastTerm 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)

Aggregations

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 SqlDecimal (io.confluent.ksql.schema.ksql.types.SqlDecimal)1 BigDecimal (java.math.BigDecimal)1