DECIMALV3
DECIMALV3β
DECIMALV3
Descriptionβ
DECIMALV3 (M [,D])
High-precision fixed-point number, M represents the total number of significant digits, and D represents the scale.
The range of M is [1, 38], and the range of D is [0, precision].
The default value is DECIMALV3(9, 0).
Precision Deductionβ
DECIMALV3 has a very complex set of type inference rules. For different expressions, different rules will be applied for precision inference.
Arithmetic Expressionsβ
- Plus / Minus: DECIMALV3(a, b) + DECIMALV3(x, y) -> DECIMALV3(max(a - b, x - y) + max(b, y) + 1, max(b, y)).
- Multiply: DECIMALV3(a, b) + DECIMALV3(x, y) -> DECIMALV3(a + x, b + y).
- Divide: DECIMALV3(a, b) + DECIMALV3(x, y) -> DECIMALV3(a + y, b).
Aggregation functionsβ
- SUM / MULTI_DISTINCT_SUM: SUM(DECIMALV3(a, b)) -> DECIMALV3(38, b).
- AVG: AVG(DECIMALV3(a, b)) -> DECIMALV3(38, max(b, 4)).
Default rulesβ
Except for the expressions mentioned above, other expressions use default rules for precision deduction. That is, for the expression expr(DECIMALV3(a, b))
, the result type is also DECIMALV3(a, b).
Adjust the result precisionβ
Different users have different accuracy requirements for DECIMALV3. The above rules are the default behavior of Doris. If users have different accuracy requirements, they can adjust the accuracy in the following ways:
- If the expected result precision is greater than the default precision, you can adjust the result precision by adjusting the parameter's precision. For example, if the user expects to calculate
AVG(col)
and get DECIMALV3(x, y) as the result, where the type ofcol
is DECIMALV3 (a, b), the expression can be rewritten toAVG(CAST(col as DECIMALV3 (x, y))
. - If the expected result precision is less than the default precision, the desired precision can be obtained by approximating the output result. For example, if the user expects to calculate
AVG(col)
and get DECIMALV3(x, y) as the result, where the type ofcol
is DECIMALV3(a, b), the expression can be rewritten asROUND(AVG(col), y)
.
Why DECIMALV3 is requiredβ
DECIMALV3 in Doris is a real high-precision fixed-point number. Compared with the old version of Decimal, DecimalV3 has the following core advantages:
- It can represent a wider range. The value ranges of both precision and scale in DECIMALV3 have been significantly expanded.
- Higher performance. The old version of DECIMAL requires 16 bytes in memory and 12 bytes in storage, while DECIMALV3 has made adaptive adjustments as shown below.
+----------------------+------------------------------+
| precision | Space occupied (memory/disk) |
+----------------------+------------------------------+
| 0 < precision <= 8 | 4 bytes |
+----------------------+------------------------------+
| 8 < precision <= 18 | 8 bytes |
+----------------------+------------------------------+
| 18 < precision <= 38 | 16 bytes |
+----------------------+------------------------------+
- More complete precision deduction. For different expressions, different precision inference rules are applied to deduce the precision of the results.
keywordsβ
DECIMALV3