Class Arithmetic


  • public class Arithmetic
    extends Object
    Provides implementations of common arithmetic functions. These functions can be combined to construct more complex mathematical expressions.

    The provided functions are polymorphic. That is, the type of the output depends on the types of the inputs. Arithmetic types follow the behavior of the analogous operations on Java primitive numeric types; the result is always of the widest of the input types.

    See Also:
    ScalarValuedFunction, DeriveFields
    • Constructor Detail

      • Arithmetic

        public Arithmetic()
    • Method Detail

      • neg

        public static ScalarValuedFunction neg​(String field)
        Builds an expression negating the value of the specified field. The specified fields must be numeric. If the field value is null, the result is null.
        Parameters:
        field - the record field value to negate
        Returns:
        a function evaluating the expression
      • neg

        public static ScalarValuedFunction neg​(ScalarValuedFunction expr)
        Builds an expression negating the value of the specified subexpression. The specified subexpression must be numeric. If the subexpression evaluates to null, the result is null.
        Parameters:
        expr - the numeric expression to negate
        Returns:
        a function evaluating the expression
      • add

        public static ScalarValuedFunction add​(String leftField,
                                               String rightField)
        Builds an expression adding the values of the two specified fields. The specified fields must be numeric; the result type is the wider of the two input types. If either value is null, the result is null.
        Parameters:
        leftField - the record field value on the left side of the addition
        rightField - the record field value on the right side of the addition
        Returns:
        a function evaluating the expression
      • add

        public static ScalarValuedFunction add​(String leftField,
                                               double rightValue)
        Builds an expression adding a constant to the value of the specified field. The specified field must be numeric; the result type is the wider of the two input types. If the field value is null, the result is null.
        Parameters:
        leftField - the record field value on the left side of the addition
        rightValue - the constant value on the right side of the addition
        Returns:
        a function evaluating the expression
      • add

        public static ScalarValuedFunction add​(double leftValue,
                                               String rightField)
        Builds an expression adding a constant to the value of the specified field. The specified field must be numeric; the result type is the wider of the two input types. If the field value is null, the result is null.
        Parameters:
        leftValue - the constant value on the left side of the addition
        rightField - the record field value on the right side of the addition
        Returns:
        a function evaluating the expression
      • add

        public static ScalarValuedFunction add​(String leftField,
                                               ScalarValuedFunction rightExpr)
        Builds an expression adding a the value of the specified field and subexpression. The specified field and subexpression must be numeric; the result type is the wider of the two input types. If either input evaluates to null, the result is null.
        Parameters:
        leftField - the record field value on the left side of the addition
        rightExpr - the numeric expression on the right side of the addition
        Returns:
        a function evaluating the expression
      • add

        public static ScalarValuedFunction add​(ScalarValuedFunction leftExpr,
                                               String rightField)
        Builds an expression adding a the value of the specified field and subexpression. The specified field and subexpression must be numeric; the result type is the wider of the two input types. If either input evaluates to null, the result is null.
        Parameters:
        leftExpr - the numeric expression on the left side of the addition
        rightField - the record field value on the right side of the addition
        Returns:
        a function evaluating the expression
      • add

        public static ScalarValuedFunction add​(double leftValue,
                                               ScalarValuedFunction rightExpr)
        Builds an expression adding a constant to the value of the specified subexpression. The specified subexpression must be numeric; the result type is the wider of the two input types. If the subexpression evaluates to null, the result is null.
        Parameters:
        leftValue - the constant value on the left side of the addition
        rightExpr - the numeric expression on the right side of the addition
        Returns:
        a function evaluating the expression
      • add

        public static ScalarValuedFunction add​(ScalarValuedFunction leftExpr,
                                               double rightValue)
        Builds an expression adding a constant to the value of the specified subexpression. The specified subexpression must be numeric; the result type is the wider of the two input types. If the subexpression evaluates to null, the result is null.
        Parameters:
        leftExpr - the numeric expression on the left side of the addition
        rightValue - the constant value on the right side of the addition
        Returns:
        a function evaluating the expression
      • add

        public static ScalarValuedFunction add​(ScalarValuedFunction leftExpr,
                                               ScalarValuedFunction rightExpr)
        Builds an expression adding the values of the two specified subexpressions. The specified subexpressions must be numeric; the result type is the wider of the two input types. If either subexpression evaluates to null, the result is null.
        Parameters:
        leftExpr - the numeric expression on the left side of the addition
        rightExpr - the numeric expression on the right side of the addition
        Returns:
        a function evaluating the expression
      • sub

        public static ScalarValuedFunction sub​(String leftField,
                                               String rightField)
        Builds an expression subtracting the values of the two specified fields. The specified fields must be numeric; the result type is the wider of the two input types. If either value is null, the result is null.
        Parameters:
        leftField - the record field value on the left side of the subtraction
        rightField - the record field value on the right side of the subtraction
        Returns:
        a function evaluating the expression
      • sub

        public static ScalarValuedFunction sub​(String leftField,
                                               double rightValue)
        Builds an expression subtracting a constant from the value of the specified field. The specified field must be numeric; the result type is the wider of the two input types. If the field value is null, the result is null.
        Parameters:
        leftField - the record field value on the left side of the subtraction
        rightValue - the constant value on the right side of the subtraction
        Returns:
        a function evaluating the expression
      • sub

        public static ScalarValuedFunction sub​(double leftValue,
                                               String rightField)
        Builds an expression subtracting the value of the specified field from a constant. The specified field must be numeric; the result type is the wider of the two input types. If the field value is null, the result is null.
        Parameters:
        leftValue - the constant value on the left side of the subtraction
        rightField - the record field value on the right side of the subtraction
        Returns:
        a function evaluating the expression
      • sub

        public static ScalarValuedFunction sub​(String leftField,
                                               ScalarValuedFunction rightExpr)
        Builds an expression subtracting a subexpression from the value of the specified field. The specified field and subexpression must be numeric; the result type is the wider of the two input types. If either input evaluates to null, the result is null.
        Parameters:
        leftField - the record field value on the left side of the subtraction
        rightExpr - the numeric expression on the right side of the subtraction
        Returns:
        a function evaluating the expression
      • sub

        public static ScalarValuedFunction sub​(ScalarValuedFunction leftExpr,
                                               String rightField)
        Builds an expression subtracting the value of the specified field from a subexpression. The specified field and subexpression must be numeric; the result type is the wider of the two input types. If either input evaluates to null, the result is null.
        Parameters:
        leftExpr - the numeric expression on the left side of the subtraction
        rightField - the record field value on the right side of the subtraction
        Returns:
        a function evaluating the expression
      • sub

        public static ScalarValuedFunction sub​(double leftValue,
                                               ScalarValuedFunction rightExpr)
        Builds an expression subtracting the value of the specified subexpression from a constant. The specified subexpression must be numeric; the result type is the wider of the two input types. If the field value is null, the result is null.
        Parameters:
        leftValue - the constant value on the left side of the subtraction
        rightExpr - the numeric expression on the right side of the subtraction
        Returns:
        a function evaluating the expression
      • sub

        public static ScalarValuedFunction sub​(ScalarValuedFunction leftExpr,
                                               double rightValue)
        Builds an expression subtracting a constant from the value of the specified subexpression. The specified subexpression must be numeric; the result type is the wider of the two input types. If the field value is null, the result is null.
        Parameters:
        leftExpr - the numeric expression on the left side of the subtraction
        rightValue - the constant value on the right side of the subtraction
        Returns:
        a function evaluating the expression
      • sub

        public static ScalarValuedFunction sub​(ScalarValuedFunction leftExpr,
                                               ScalarValuedFunction rightExpr)
        Builds an expression subtracting the values of the two specified subexpressions. The specified subexpressions must be numeric; the result type is the wider of the two input types. If either subexpression evaluates to null, the result is null.
        Parameters:
        leftExpr - the numeric expression on the left side of the subtraction
        rightExpr - the numeric expression on the right side of the subtraction
        Returns:
        a function evaluating the expression
      • mult

        public static ScalarValuedFunction mult​(String leftField,
                                                String rightField)
        Builds an expression multiplying the values of the two specified fields. The specified fields must be numeric; the result type is the wider of the two input types. If either value is null, the result is null.
        Parameters:
        leftField - the record field value on the left side of the multiplication
        rightField - the record field value on the right side of the multiplication
        Returns:
        a function evaluating the expression
      • mult

        public static ScalarValuedFunction mult​(String leftField,
                                                double rightValue)
        Builds an expression multiplying the value of the specified field by a constant. The specified field must be numeric; the result type is the wider of the two input types. If the field value is null, the result is null.
        Parameters:
        leftField - the record field value on the left side of the multiplication
        rightValue - the constant value on the right side of the multiplication
        Returns:
        a function evaluating the expression
      • mult

        public static ScalarValuedFunction mult​(double leftValue,
                                                String rightField)
        Builds an expression multiplying the value of the specified field by a constant. The specified field must be numeric; the result type is the wider of the two input types. If the field value is null, the result is null.
        Parameters:
        rightField - the record field value on the right side of the multiplication
        leftValue - the constant value on the left side of the multiplication
        Returns:
        a function evaluating the expression
      • mult

        public static ScalarValuedFunction mult​(String leftField,
                                                ScalarValuedFunction rightExpr)
        Builds an expression multiplying the values of the specified field and subexpression. The specified field and subexpression must be numeric; the result type is the wider of the two input types. If either input evaluates to null, the result is null.
        Parameters:
        leftField - the record field value on the left side of the multiplication
        rightExpr - the numeric expression on the right side of the multiplication
        Returns:
        a function evaluating the expression
      • mult

        public static ScalarValuedFunction mult​(ScalarValuedFunction leftExpr,
                                                String rightField)
        Builds an expression multiplying the values of the specified field and subexpression. The specified field and subexpression must be numeric; the result type is the wider of the two input types. If either input evaluates to null, the result is null.
        Parameters:
        leftExpr - the numeric expression on the left side of the multiplication
        rightField - the record field value on the right side of the multiplication
        Returns:
        a function evaluating the expression
      • mult

        public static ScalarValuedFunction mult​(double leftValue,
                                                ScalarValuedFunction rightExpr)
        Builds an expression multiplying the value of the specified subexpression by a constant. The specified subexpression must be numeric; the result type is the wider of the two input types. If the subexpression evaluates to null, the result is null.
        Parameters:
        leftValue - the constant value on the left side of the multiplication
        rightExpr - the numeric expression on the right side of the multiplication
        Returns:
        a function evaluating the expression
      • mult

        public static ScalarValuedFunction mult​(ScalarValuedFunction leftExpr,
                                                double rightValue)
        Builds an expression multiplying the value of the specified subexpression by a constant. The specified subexpression must be numeric; the result type is the wider of the two input types. If the subexpression evaluates to null, the result is null.
        Parameters:
        leftExpr - the numeric expression on the left side of the multiplication
        rightValue - the constant value on the right side of the multiplication
        Returns:
        a function evaluating the expression
      • mult

        public static ScalarValuedFunction mult​(ScalarValuedFunction leftExpr,
                                                ScalarValuedFunction rightExpr)
        Builds an expression multiplying the values of the two specified subexpressions. The specified subexpressions must be numeric; the result type is the wider of the two input types. If either subexpression evalautes to null, the result is null.
        Parameters:
        leftExpr - the numeric expression on the left side of the multiplication
        rightExpr - the numeric expression on the right side of the multiplication
        Returns:
        a function evaluating the expression
      • div

        public static ScalarValuedFunction div​(String leftField,
                                               String rightField)
        Builds an expression dividing the values of the two specified fields. The specified fields must be numeric; the result type is the wider of the two input types. If either value is null or if the divisor evaluates to 0, the result is null.
        Parameters:
        leftField - the record field value on the left side of the division
        rightField - the record field value on the right side of the division
        Returns:
        a function evaluating the expression
      • div

        public static ScalarValuedFunction div​(String leftField,
                                               double rightValue)
        Builds an expression dividing the value of the specified field by a constant. The specified field must be numeric; the result type is the wider of the two input types. If the field value is null, the result is null.
        Parameters:
        leftField - the record field value on the left side of the division
        rightValue - the constant value on the right side of the division
        Returns:
        a function evaluating the expression
      • div

        public static ScalarValuedFunction div​(double leftValue,
                                               String rightField)
        Builds an expression dividing a constant by the value of the specified field. The specified field must be numeric; the result type is the wider of the two input types. If the field value is null, the result is null.
        Parameters:
        leftValue - the constant value on the left side of the division
        rightField - the record field value on the right side of the division
        Returns:
        a function evaluating the expression
      • div

        public static ScalarValuedFunction div​(String leftField,
                                               ScalarValuedFunction rightExpr)
        Builds an expression dividing the value of the specified field by a subexpression. The specified field and subexpression must be numeric; the result type is the wider of the two input types. If either input evaluates to null or if the divisor evaluates to 0, the result is null.
        Parameters:
        leftField - the record field value on the left side of the division
        rightExpr - the numeric expression on the right side of the division
        Returns:
        a function evaluating the expression
      • div

        public static ScalarValuedFunction div​(ScalarValuedFunction leftExpr,
                                               String rightField)
        Builds an expression dividing a subexpression by the value of the specified field. The specified field and subexpression must be numeric; the result type is the wider of the two input types. If either input evaluates to null or the divisor evaluates to 0, the result is null.
        Parameters:
        leftExpr - the numeric expression on the left side of the division
        rightField - the record field value on the right side of the division
        Returns:
        a function evaluating the expression
      • div

        public static ScalarValuedFunction div​(double leftValue,
                                               ScalarValuedFunction rightExpr)
        Builds an expression dividing a constant by the value of the specified subexpression. The specified subexpression must be numeric; the result type is the wider of the two input types. If the subexpression evaluates to null or 0, the result is null.
        Parameters:
        leftValue - the constant value on the left side of the division
        rightExpr - the numeric expression on the right side of the division
        Returns:
        a function evaluating the expression
      • div

        public static ScalarValuedFunction div​(ScalarValuedFunction leftExpr,
                                               double rightValue)
        Builds an expression dividing the value of the specified subexpression by a constant. The specified subexpression must be numeric; the result type is the wider of the two input types.
        Parameters:
        leftExpr - the numeric expression on the left side of the division
        rightValue - the constant value on the right side of the division
        Returns:
        a function evaluating the expression
      • div

        public static ScalarValuedFunction div​(ScalarValuedFunction leftExpr,
                                               ScalarValuedFunction rightExpr)
        Builds an expression dividing the values of the two specified subexpressions. The specified subexpressions must be numeric; the result type is the wider of the two input types. If either subexpression evaluates to null or if the divisor evaluates to 0, the result is null.

        Division will be done as appropriate for the result type; if both inputs are integer types, integer division will be used. To force floating point division in this case, convert one expression to floating point using Conversions.asDouble(ScalarValuedFunction) or Conversions.asFloat(ScalarValuedFunction).

        Parameters:
        leftExpr - the numeric expression on the left side of the division
        rightExpr - the numeric expression on the right side of the division
        Returns:
        a function evaluating the expression
      • scaledDiv

        public static ScalarValuedFunction scaledDiv​(String leftField,
                                                     String rightField,
                                                     int scale)
        Builds an expression dividing the values of the two specified fields to the specified scale. The specified fields must be numeric; the result type will be numeric. If either value is null or if the divisor evaluates to 0, the result is null.
        Parameters:
        leftField - the record field value on the left side of the division
        rightField - the record field value on the right side of the division
        scale - the scale, or precision, of the result
        Returns:
        a function evaluating the expression
      • scaledDiv

        public static ScalarValuedFunction scaledDiv​(String leftField,
                                                     BigDecimal rightValue,
                                                     int scale)
        Builds an expression dividing the value of the specified subexpression by a constant to the specified scale. The specified subexpression must be numeric; the result type will be numeric.
        Parameters:
        leftExpr - the numeric expression on the left side of the division
        rightValue - the constant value on the right side of the division
        scale - the scale, or precision, of the result
        Returns:
        a function evaluating the expression
      • scaledDiv

        public static ScalarValuedFunction scaledDiv​(BigDecimal leftValue,
                                                     String rightField,
                                                     int scale)
        Builds an expression dividing a constant by the value of the specified subexpression to the specified scale. The specified subexpression must be numeric; the result type will be numeric. If the subexpression evaluates to null or 0, the result is null.
        Parameters:
        leftValue - the constant value on the left side of the division
        rightExpr - the numeric expression on the right side of the division
        scale - the scale, or precision, of the result
        Returns:
        a function evaluating the expression
      • scaledDiv

        public static ScalarValuedFunction scaledDiv​(String leftField,
                                                     ScalarValuedFunction rightExpr,
                                                     int scale)
        Builds an expression dividing the value of the specified field by a subexpression to the specified scale. The specified field and subexpression must be numeric; the result type will be numeric. If either input evaluates to null or if the divisor evaluates to 0, the result is null.
        Parameters:
        leftField - the record field value on the left side of the division
        rightExpr - the numeric expression on the right side of the division
        scale - the scale, or precision, of the result
        Returns:
        a function evaluating the expression
      • scaledDiv

        public static ScalarValuedFunction scaledDiv​(ScalarValuedFunction leftExpr,
                                                     String rightField,
                                                     int scale)
        Builds an expression dividing a subexpression by the value of the specified field to the specified scale. The specified field and subexpression must be numeric; the result will be numeric. If either input evaluates to null or the divisor evaluates to 0, the result is null.
        Parameters:
        leftExpr - the numeric expression on the left side of the division
        rightField - the record field value on the right side of the division
        scale - the scale, or precision, of the result
        Returns:
        a function evaluating the expression
      • scaledDiv

        public static ScalarValuedFunction scaledDiv​(ScalarValuedFunction leftExpr,
                                                     ScalarValuedFunction rightExpr,
                                                     int scale)
        Builds an expression dividing the values of the two specified subexpressions to the specified scale. The specified subexpressions must be numeric; the result type will be numeric. If either subexpression evaluates to null or if the divisor evaluates to 0, the result is null.
        Parameters:
        leftExpr - the numeric expression on the left side of the division
        rightExpr - the numeric expression on the right side of the division
        scale - the scale, or precision, of the result
        Returns:
        a function evaluating the expression
      • remainder

        public static ScalarValuedFunction remainder​(String leftField,
                                                     String rightField)
        Builds an expression dividing the values of the two specified fields and returning the remainder. The specified fields must be numeric; the result type is the wider of the two input types. If either value is null or if the divisor evaluates to 0, the result is null.
        Parameters:
        leftField - the record field value on the left side of the division
        rightField - the record field value on the right side of the division
        Returns:
        a function evaluating the expression
      • remainder

        public static ScalarValuedFunction remainder​(String leftField,
                                                     double rightValue)
        Builds an expression dividing the value of the specified field by a constant and returning the remainder. The specified field must be numeric; the result type is the wider of the two input types. If the field value is null, the result is null.
        Parameters:
        leftField - the record field value on the left side of the division
        rightValue - the constant value on the right side of the division
        Returns:
        a function evaluating the expression
      • remainder

        public static ScalarValuedFunction remainder​(double leftValue,
                                                     String rightField)
        Builds an expression dividing a constant by the value of the specified field and returning the remainder. The specified field must be numeric; the result type is the wider of the two input types. If the field value is null, the result is null.
        Parameters:
        leftValue - the constant value on the left side of the division
        rightField - the record field value on the right side of the division
        Returns:
        a function evaluating the expression
      • remainder

        public static ScalarValuedFunction remainder​(String leftField,
                                                     ScalarValuedFunction rightExpr)
        Builds an expression dividing the value of the specified field by a subexpression and returning the remainder. The specified field and subexpression must be numeric; the result type is the wider of the two input types. If either input evaluates to null or if the divisor evaluates to 0, the result is null.
        Parameters:
        leftField - the record field value on the left side of the division
        rightExpr - the numeric expression on the right side of the division
        Returns:
        a function evaluating the expression
      • remainder

        public static ScalarValuedFunction remainder​(ScalarValuedFunction leftExpr,
                                                     String rightField)
        Builds an expression dividing a subexpression by the value of the specified field and returning the remainder. The specified field and subexpression must be numeric; the result type is the wider of the two input types. If either input evaluates to null or the divisor evaluates to 0, the result is null.
        Parameters:
        leftExpr - the numeric expression on the left side of the division
        rightField - the record field value on the right side of the division
        Returns:
        a function evaluating the expression
      • remainder

        public static ScalarValuedFunction remainder​(double leftValue,
                                                     ScalarValuedFunction rightExpr)
        Builds an expression dividing a constant by the value of the specified subexpression and returning the remainder. The specified subexpression must be numeric; the result type is the wider of the two input types. If the subexpression evaluates to null or 0, the result is null.
        Parameters:
        leftValue - the constant value on the left side of the division
        rightExpr - the numeric expression on the right side of the division
        Returns:
        a function evaluating the expression
      • remainder

        public static ScalarValuedFunction remainder​(ScalarValuedFunction leftExpr,
                                                     double rightValue)
        Builds an expression dividing the value of the specified subexpression by a constant and returning the remainder. The specified subexpression must be numeric; the result type is the wider of the two input types.
        Parameters:
        leftExpr - the numeric expression on the left side of the division
        rightValue - the constant value on the right side of the division
        Returns:
        a function evaluating the expression
      • remainder

        public static ScalarValuedFunction remainder​(ScalarValuedFunction leftExpr,
                                                     ScalarValuedFunction rightExpr)
        Builds an expression dividing the values of the two specified subexpressions and returning the remainder. The specified subexpressions must be numeric; the result type is the wider of the two input types. If either subexpression evaluates to null or if the divisor evaluates to 0, the result is null.

        Division will be done as appropriate for the result type; if both inputs are integer types, integer division will be used. To force floating point division in this case, convert one expression to floating point using Conversions.asDouble(ScalarValuedFunction) or Conversions.asFloat(ScalarValuedFunction).

        Parameters:
        leftExpr - the numeric expression on the left side of the division
        rightExpr - the numeric expression on the right side of the division
        Returns:
        a function evaluating the expression