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);
}
}
Aggregations