Search in sources :

Example 1 with Operator

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);
    }
}
Also used : CastInterpreter.castToLongFunction(io.confluent.ksql.execution.interpreter.CastInterpreter.castToLongFunction) CastTerm(io.confluent.ksql.execution.interpreter.terms.CastTerm) DecimalUtil(io.confluent.ksql.util.DecimalUtil) ArithmeticBinaryTerm(io.confluent.ksql.execution.interpreter.terms.ArithmeticBinaryTerm) CastInterpreter.castToDoubleFunction(io.confluent.ksql.execution.interpreter.CastInterpreter.castToDoubleFunction) MathContext(java.math.MathContext) CastInterpreter.castToIntegerFunction(io.confluent.ksql.execution.interpreter.CastInterpreter.castToIntegerFunction) KsqlConfig(io.confluent.ksql.util.KsqlConfig) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) ArithmeticUnaryFunction(io.confluent.ksql.execution.interpreter.terms.ArithmeticUnaryTerm.ArithmeticUnaryFunction) BigDecimal(java.math.BigDecimal) Sign(io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression.Sign) ArithmeticBinaryFunction(io.confluent.ksql.execution.interpreter.terms.ArithmeticBinaryTerm.ArithmeticBinaryFunction) ArithmeticUnaryTerm(io.confluent.ksql.execution.interpreter.terms.ArithmeticUnaryTerm) ComparableCastFunction(io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction) Term(io.confluent.ksql.execution.interpreter.terms.Term) SqlDecimal(io.confluent.ksql.schema.ksql.types.SqlDecimal) Operator(io.confluent.ksql.schema.Operator) KsqlException(io.confluent.ksql.util.KsqlException) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) SqlTypes(io.confluent.ksql.schema.ksql.types.SqlTypes) RoundingMode(java.math.RoundingMode) ComparableCastFunction(io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction) SqlBaseType(io.confluent.ksql.schema.ksql.types.SqlBaseType) KsqlException(io.confluent.ksql.util.KsqlException)

Aggregations

Sign (io.confluent.ksql.execution.expression.tree.ArithmeticUnaryExpression.Sign)1 CastInterpreter.castToDoubleFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToDoubleFunction)1 CastInterpreter.castToIntegerFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToIntegerFunction)1 CastInterpreter.castToLongFunction (io.confluent.ksql.execution.interpreter.CastInterpreter.castToLongFunction)1 ArithmeticBinaryTerm (io.confluent.ksql.execution.interpreter.terms.ArithmeticBinaryTerm)1 ArithmeticBinaryFunction (io.confluent.ksql.execution.interpreter.terms.ArithmeticBinaryTerm.ArithmeticBinaryFunction)1 ArithmeticUnaryTerm (io.confluent.ksql.execution.interpreter.terms.ArithmeticUnaryTerm)1 ArithmeticUnaryFunction (io.confluent.ksql.execution.interpreter.terms.ArithmeticUnaryTerm.ArithmeticUnaryFunction)1 CastTerm (io.confluent.ksql.execution.interpreter.terms.CastTerm)1 ComparableCastFunction (io.confluent.ksql.execution.interpreter.terms.CastTerm.ComparableCastFunction)1 Term (io.confluent.ksql.execution.interpreter.terms.Term)1 Operator (io.confluent.ksql.schema.Operator)1 SqlBaseType (io.confluent.ksql.schema.ksql.types.SqlBaseType)1 SqlDecimal (io.confluent.ksql.schema.ksql.types.SqlDecimal)1 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)1 SqlTypes (io.confluent.ksql.schema.ksql.types.SqlTypes)1 DecimalUtil (io.confluent.ksql.util.DecimalUtil)1 KsqlConfig (io.confluent.ksql.util.KsqlConfig)1 KsqlException (io.confluent.ksql.util.KsqlException)1 BigDecimal (java.math.BigDecimal)1