P stands for the total number of significant numbers (precision). S stands for the maximum number of decimal points (scale).
If P is omitted, the default value is 10. If S is omitted, the default value is 0.
- For versions earlier than v4.0, the database supports DECIMAL128 based on Fast DECIMAL.
- For v4.0 and later versions, the database supports DECIMAL256.
Features
Fast DECIMAL
Controlled by the FE dynamic parameterenable_decimal_v3, Fast DECIMAL is enabled by default.
For Fast DECIMAL, the range of P is [1,38] and the range of S is [0, P]. The default value of S is 0.
Fast DECIMAL uses variable-width integers to express decimals.
-
Decimal(P ≤ 18, S)
- LogicalType: Decimal64
- Stored as: int64
- Delegate LogicalType: BIGINT
-
Decimal(P > 18 & P ≤ 38, S)
- LogicalType: Decimal128
- Stored as: int128
- Delegate LogicalType: LARGEINT
DECIMAL256
The database introduces DECIMAL256 from v4.0 onwards. For DECIMAL256, the range ofP is (38,76] and the range of S is [0, P]. The default value of S is 0.
DECIMAL256 expands the upper limit of precision to 76 bits. Its numerical capacity is 10³⁸ times higher than that of DECIMAL128, reducing the probability of overflow to a negligible level.
DECIMAL256 uses the same strategy to express decimals as that of Fast DECIMAL. In addition to the two decimal types mentioned above, it supports:
- Decimal(P > 38 & P ≤ 76, S)
- LogicalType: Decimal256
- Stored as: int256
- Delegate LogicalType: INT_256
-57896044618658097711785492504343953926634992332820282019728792003956564819968 to 57896044618658097711785492504343953926634992332820282019728792003956564819967.
Type casting operations
DECIMAL256 supports bidirectional type conversion between all types listed below:Aggregate functions
Currently, DECIMAL256 supports these aggregation functions:COUNT, COUNT DISTINCT, SUM, SUM DISTINCT, AVG, AVG DISTINCT, MAX, MIN, and ABS.
Limitations
DECIMAL256 has the following limitations:-
Currently, DECIMAL256 does not support automatic precision scale-up for decimal operations.
For example,
DECIMAL128 * DECIMAL128will not be automatically scaled up to DECIMAL256. DECIMAL256 features are only applied whenDECIMAL256is explicitly specified as the operand either in the CREATE TABLE statement or using CAST (SELECT CAST(p38s10 as DECIMAL(70, 30))). - DECIMAL256 does not support window functions.
- Aggregate tables do not support DECIMAL256 type.
Examples
Fast DECIMAL example
Define DECIMAL columns when creating a table.DECIMAL256 example
Create a table with a DECIMAL256 type column.48) together exceed the maximum integer that can be represented by 256 bits, FE directly casts the value to NULL.
ERROR 1064 (HY000): Insert has filtered data. Because the integer part and decimal part with the specified scale (which is 48) together exceed the maximum value represented by a 50-digit integer, BE returns the error.
Usage notes
You can set the system variablesql_mode to ERROR_IF_OVERFLOW to allow the system to return an error instead of NULL in the case of arithmetic overflow.