Rewrites a decimal expression for a specific set of SqlOperator's. In
general, most expressions are rewritten in such a way that SqlOperator's
do not have to deal with decimals. Decimals are represented by their
unscaled integer representations, similar to
BigDecimal.unscaledValue() (i.e. 10^scale). Once decimals are
decoded, SqlOperators can then operate on the integer representations. The
value can later be recoded as a decimal.

For example, suppose one casts 2.0 as a decima(10,4). The value is
decoded (20), multiplied by a scale factor (1000), for a result of
(20000) which is encoded as a decimal(10,4), in this case 2.0000

To avoid the lengthy coding of RexNode expressions, this base class
provides succinct methods for building expressions used in rewrites.

Constructor Detail

RexExpander

Method Detail

canExpand

This defaults to the utility method,
RexUtil.requiresDecimalExpansion(RexNode, boolean) which checks
general guidelines on whether a rewrite should be considered at all. In
general, it is helpful to update the utility method since that method is
often used to filter the somewhat expensive rewrite process.

However, this method provides another place for implementations of
RexExpander to make a more detailed analysis before deciding on
whether to perform a rewrite.

ensureScale

Ensures a value is of a required scale. If it is not, then the value
is multiplied by a scale factor. Scaling up an exact value is limited
to max precision - 1, because we cannot represent the result of
larger scales internally. Scaling up a floating point value is more
flexible since the value may be very small despite having a scale of
zero and the scaling may still produce a reasonable result

Parameters:

value - integer representation of decimal, or a floating point
number

scale - current scale, 0 for floating point numbers

required - required scale, must be at least the current scale;
the scale difference may not be greater than max
precision - 1 for exact numerics

Returns:

value * 10^scale, returned as an exact or approximate value
corresponding to the input value

encodeValue

Casts a decimal's integer representation to a decimal node. If the
expression is not the expected integer type, then it is casted first.

An overflow check may be requested to ensure the internal value
does not exceed the maximum value of the decimal type.

Parameters:

value - integer representation of decimal

decimalType - type integer will be reinterpreted as

checkOverflow - indicates whether an overflow check is required
when reinterpreting this particular value as the
decimal type. A check usually not required for
arithmetic, but is often required for rounding and
explicit casts.