use of io.confluent.ksql.schema.Operator in project ksql by confluentinc.
the class ArithmeticInterpreter method getNonDecimalArithmeticFunction.
private static ArithmeticBinaryFunction getNonDecimalArithmeticFunction(final Operator operator, final SqlType leftType, final SqlType rightType) {
final SqlBaseType leftBaseType = leftType.baseType();
final SqlBaseType rightBaseType = rightType.baseType();
if (leftBaseType == SqlBaseType.STRING && rightBaseType == SqlBaseType.STRING) {
return (o1, o2) -> (String) o1 + (String) o2;
} else if (leftBaseType == SqlBaseType.DOUBLE || rightBaseType == SqlBaseType.DOUBLE) {
final TypedArithmeticBinaryFunction<Double> fn = getDoubleFunction(operator);
final ComparableCastFunction<Double> castLeft = castToDoubleFunction(leftType);
final ComparableCastFunction<Double> castRight = castToDoubleFunction(rightType);
return (o1, o2) -> fn.doFunction(castLeft.cast(o1), castRight.cast(o2));
} else if (leftBaseType == SqlBaseType.BIGINT || rightBaseType == SqlBaseType.BIGINT) {
final TypedArithmeticBinaryFunction<Long> fn = getLongFunction(operator);
final ComparableCastFunction<Long> castLeft = castToLongFunction(leftType);
final ComparableCastFunction<Long> castRight = castToLongFunction(rightType);
return (o1, o2) -> fn.doFunction(castLeft.cast(o1), castRight.cast(o2));
} else if (leftBaseType == SqlBaseType.INTEGER || rightBaseType == SqlBaseType.INTEGER) {
final TypedArithmeticBinaryFunction<Integer> fn = getIntegerFunction(operator);
final ComparableCastFunction<Integer> castLeft = castToIntegerFunction(leftType);
final ComparableCastFunction<Integer> castRight = castToIntegerFunction(rightType);
return (o1, o2) -> fn.doFunction(castLeft.cast(o1), castRight.cast(o2));
} else {
throw new KsqlException("Can't do arithmetic for types " + leftType + " and " + rightType);
}
}
Aggregations