Skip to content

Commit b7db3ca

Browse files
author
絵空事スピリット
authored
[Doc] Docs for DECIMAL256 (StarRocks#61579)
1 parent 3f6ef0a commit b7db3ca

6 files changed

Lines changed: 301 additions & 59 deletions

File tree

docs/en/sql-reference/data-types/numeric/DECIMAL.md

Lines changed: 103 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,84 @@ displayed_sidebar: docs
66

77
DECIMAL(P[,S]) is a high-precision fixed-point value. `P` stands for the total number of significant numbers (precision). `S` stands for the maximum number of decimal points (scale).
88

9-
If `P` is omitted, the default is 10. If `S` is omitted, the default is 0.
9+
If `P` is omitted, the default value is 10. If `S` is omitted, the default value is 0.
1010

11-
* Decimal V2
11+
- For versions earlier than v4.0, StarRocks supports DECIMAL128 based on [Fast DECIMAL](#fast-decimal).
12+
- For v4.0 and later versions, StarRocks supports [DECIMAL256](#decimal256).
1213

13-
The range of `P` is [1,27] and the range of `S` is [0,9]. `P` must be greater than or equal to the value of `S`. The default value of `S` is 0.
14+
## Features
1415

15-
* Fast Decimal (Decimal V3)
16+
### Fast DECIMAL
1617

17-
The range of `P` is [1,38] and the range of `S` is [0, P]. The default value of `S` is 0. Fast Decimal provides a higher precision.
18-
19-
Major optimizations:
20-
21-
​1. Fast Decimal uses variable-width integers to express decimals. For example, it uses 64-bit integers to express decimals whose precision is less than or equal to 18. Whereas, Decimal V2 uses 128-bit integers uniformly for all decimals. Arithmetic operations and conversion operations on 64-bit processors use fewer instructions, which greatly improves performance.
22-
23-
​2. Compared with Decimal V2, Fast Decimal made significant optimizations in some algorithms, especially in multiplication, which improves performance by about 4 times.
18+
Controlled by the FE dynamic parameter `enable_decimal_v3`, Fast DECIMAL is enabled by default.
2419

25-
Fast Decimal is controlled by the FE dynamic parameter `enable_decimal_v3`, which is `true` by default.
20+
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.
21+
22+
Fast DECIMAL uses variable-width integers to express decimals.
23+
24+
- Decimal(P ≤ 18, S)
25+
- LogicalType: Decimal64
26+
- Stored as: int64
27+
- Delegate LogicalType: BIGINT
28+
29+
- Decimal(P > 18 & P ≤ 38, S)
30+
- LogicalType: Decimal128
31+
- Stored as: int128
32+
- Delegate LogicalType: LARGEINT
2633

2734
From v3.1 onwards, StarRocks supports Fast Decimal entries in [ARRAY](../semi_structured/Array.md), [MAP](../semi_structured/Map.md), and [STRUCT](../semi_structured/STRUCT.md).
28-
35+
36+
## DECIMAL256
37+
38+
StarRocks introduces DECIMAL256 from v4.0 onwards.
39+
40+
For DECIMAL256, the range of `P` is (38,76] and the range of `S` is [0, P]. The default value of `S` is 0.
41+
42+
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.
43+
44+
DECIMAL256 uses the same strategy to express decimals as that of Fast DECIMAL. In addition to the two decimal types mentioned above, it supports:
45+
46+
- Decimal(P > 38 & P ≤ 76, S)
47+
- LogicalType: Decimal256
48+
- Stored as: int256
49+
- Delegate LogicalType: INT_256
50+
51+
The actual representable range of DECIMAL256 is `-57896044618658097711785492504343953926634992332820282019728792003956564819968` to `57896044618658097711785492504343953926634992332820282019728792003956564819967`.
52+
53+
#### Type casting operations
54+
55+
DECIMAL256 supports bidirectional type conversion between all types listed below:
56+
57+
| Source Type | Target Types |
58+
| --------------- | ----------------------------------------------------------------- |
59+
| TYPE_DECIMAL256 | BOOL |
60+
| TYPE_DECIMAL256 | TYPE_TINYINT, TYPE_SMALLINT, TYPE_INT, TYPE_BIGINT, TYPE_LARGEINT |
61+
| TYPE_DECIMAL256 | TYPE_FLOAT, TYPE_DOUBLE |
62+
| TYPE_DECIMAL256 | TYPE_VARCHAR |
63+
64+
#### Aggregate functions
65+
66+
Currently, DECIMAL256 supports these aggregation functions: `COUNT`, `COUNT DISTINCT`, `SUM`, `SUM DISTINCT`, `AVG`, `AVG DISTINCT`, `MAX`, `MIN`, and `ABS`.
67+
68+
#### Limitations
69+
70+
DECIMAL256 has the following limitations:
71+
72+
- Currently, DECIMAL256 does not support automatic precision scale-up for decimal operations.
73+
74+
For example, `DECIMAL128 * DECIMAL128` will not be automatically scaled up to DECIMAL256. DECIMAL256 features are only applied when `DECIMAL256` is explicitly specified as the operand either in the CREATE TABLE statement or using CAST (`SELECT CAST(p38s10 as DECIMAL(70, 30))`).
75+
76+
- DECIMAL256 does not support window functions.
77+
78+
- Aggregate tables do not support DECIMAL256 type.
79+
2980
## Examples
3081

82+
### Fast DECIMAL example
83+
3184
Define DECIMAL columns when creating a table.
3285

33-
```sql
86+
```SQL
3487
CREATE TABLE decimalDemo (
3588
pk BIGINT(20) NOT NULL COMMENT "",
3689
account DECIMAL(20,10) COMMENT ""
@@ -54,6 +107,40 @@ SELECT * FROM decimalDemo;
54107
+------+-----------------+
55108
```
56109

57-
## keywords
110+
### DECIMAL256 example
111+
112+
Create a table with a DECIMAL256 type column.
113+
114+
```SQL
115+
CREATE TABLE test_decimal256(
116+
p50s48 DECIMAL(50, 48) COMMENT ""
117+
);
118+
```
119+
120+
INSERT different values:
121+
122+
```SQL
123+
INSERT INTO test_decimal256(p50s48) SELECT 1.222222;
124+
```
125+
126+
This statement succeeds with the values written correctly.
127+
128+
```SQL
129+
INSERT INTO test_decimal256(p50s48) SELECT 11111111111111111111111111111111111111111111.222222;
130+
```
131+
132+
This statement succeeds with the values written as NULL. Because the integer part and decimal part (which has 44 digits) with the specified scale (which is `48`) together exceed the maximum integer that can be represented by 256 bits, FE directly casts the value to NULL.
133+
134+
```SQL
135+
INSERT INTO test_decimal256(p50s48) SELECT 333;
136+
```
137+
138+
This statement will be returned with an error `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.
139+
140+
## Usage notes
141+
142+
You can set the system variable `sql_mode` to `ERROR_IF_OVERFLOW` to allow the system to return an error instead of NULL in the case of arithmetic overflow.
143+
144+
## Keywords
58145

59-
decimal, decimalv2, decimalv3, fast decimal
146+
decimal, decimalv3, fast decimal, decimal128, decimal256

docs/en/sql-reference/data-types/semi_structured/Array.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ The following limits apply when you create ARRAY columns in StarRocks tables:
6060
- In versions earlier than v2.1, you can create ARRAY columns only in Duplicate Key tables. From v2.1 onwards, you can also create ARRAY columns in other types of tables (Primary Key, Unique Key, Aggregate). Note that in an Aggregate table, you can create an ARRAY column only when the function used to aggregate data in that column is replace() or replace_if_not_null(). For more information, see [Aggregate table](../../../table_design/table_types/aggregate_table.md).
6161
- ARRAY columns cannot be used as key columns.
6262
- ARRAY columns cannot be used as partition keys (included in PARTITION BY) or bucketing keys (included in DISTRIBUTED BY).
63-
- DECIMAL V3 is not supported in ARRAY.
6463
- An array can have a maximum of 14-level nesting.
6564

6665
## Construct arrays in SQL

docs/ja/sql-reference/data-types/numeric/DECIMAL.md

Lines changed: 102 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,86 @@ displayed_sidebar: docs
44

55
# DECIMAL
66

7-
## 説明
7+
DECIMAL(P[,S]) は高精度の固定小数点値です。`P` は有効桁数(精度)を表します。`S` は小数点以下の最大桁数(スケール)を表します。
88

9-
DECIMAL(P[,S])
9+
`P` が省略された場合、デフォルト値は10です。`S` が省略された場合、デフォルト値は0です。
1010

11-
高精度の固定小数点値です。`P` は有効数字の総数(精度)を表します。`S` は小数点以下の最大桁数(スケール)を表します。
11+
- バージョン v4.0 より前のバージョンでは、StarRocks は [Fast DECIMAL](#fast-decimal) に基づく DECIMAL128 をサポートしています。
12+
- バージョン v4.0 以降では、StarRocks は [DECIMAL256](#decimal256) をサポートしています。
1213

13-
`P` を省略した場合、デフォルトは10です。`S` を省略した場合、デフォルトは0です。
14+
## 機能
1415

15-
* Decimal V2
16+
### Fast DECIMAL
1617

17-
`P` の範囲は [1,27] で、`S` の範囲は [0,9] です。`P``S` の値以上でなければなりません。`S` のデフォルト値は0です
18+
FE の動的パラメータ `enable_decimal_v3` によって制御され、Fast DECIMAL はデフォルトで有効になっています
1819

19-
* Fast Decimal (Decimal V3)
20+
Fast DECIMAL の場合、`P` の範囲は [1,38] で、`S` の範囲は [0, P] です。`S` のデフォルト値は0です。
2021

21-
`P` の範囲は [1,38] で、`S` の範囲は [0, P] です。`S` のデフォルト値は0です。Fast Decimal はより高い精度を提供します。
22-
23-
主な最適化:
24-
25-
​1. Fast Decimal は可変幅の整数を使用して小数を表現します。例えば、精度が18以下の小数を表現するために64ビット整数を使用します。一方、Decimal V2 はすべての小数に対して128ビット整数を一様に使用します。64ビットプロセッサ上での算術演算や変換操作は、より少ない命令を使用し、パフォーマンスを大幅に向上させます。
26-
27-
​2. Decimal V2 と比較して、Fast Decimal はいくつかのアルゴリズム、特に乗算において大幅な最適化を行い、パフォーマンスを約4倍向上させました。
22+
Fast DECIMAL は可変幅の整数を使用して小数を表現します。
2823

29-
Fast Decimal は FE の動的パラメータ `enable_decimal_v3` によって制御され、デフォルトでは `true` です。
24+
- Decimal(P ≤ 18, S)
25+
- LogicalType: Decimal64
26+
- 保存形式: int64
27+
- Delegate LogicalType: BIGINT
3028

31-
バージョン 3.1 以降、StarRocks は [ARRAY](../semi_structured/Array.md)[MAP](../semi_structured/Map.md)、および [STRUCT](../semi_structured/STRUCT.md) における Fast Decimal エントリをサポートしています。
29+
- Decimal(P > 18 & P ≤ 38, S)
30+
- LogicalType: Decimal128
31+
- 保存形式: int128
32+
- Delegate LogicalType: LARGEINT
33+
34+
バージョン v3.1 以降、StarRocks は [ARRAY](../semi_structured/Array.md)[MAP](../semi_structured/Map.md)、および [STRUCT](../semi_structured/STRUCT.md) における Fast Decimal エントリをサポートしています。
35+
36+
## DECIMAL256
37+
38+
StarRocks はバージョン v4.0 以降で DECIMAL256 を導入しています。
39+
40+
DECIMAL256 の場合、`P` の範囲は (38,76] で、`S` の範囲は [0, P] です。`S` のデフォルト値は0です。
41+
42+
DECIMAL256 は精度の上限を76ビットに拡張します。その数値容量は DECIMAL128 の 10³⁸ 倍であり、オーバーフローの可能性を無視できるレベルにまで低減します。
43+
44+
DECIMAL256 は Fast DECIMAL と同じ戦略を使用して小数を表現します。上記の2つの小数タイプに加えて、次のものをサポートします:
45+
46+
- Decimal(P > 38 & P ≤ 76, S)
47+
- LogicalType: Decimal256
48+
- 保存形式: int256
49+
- Delegate LogicalType: INT_256
50+
51+
DECIMAL256 の実際の表現可能な範囲は `-57896044618658097711785492504343953926634992332820282019728792003956564819968` から `57896044618658097711785492504343953926634992332820282019728792003956564819967` です。
52+
53+
#### 型変換操作
54+
55+
DECIMAL256 は以下にリストされたすべての型との双方向の型変換をサポートします:
56+
57+
| ソースタイプ | ターゲットタイプ |
58+
| --------------- | ----------------------------------------------------------------- |
59+
| TYPE_DECIMAL256 | BOOL |
60+
| TYPE_DECIMAL256 | TYPE_TINYINT, TYPE_SMALLINT, TYPE_INT, TYPE_BIGINT, TYPE_LARGEINT |
61+
| TYPE_DECIMAL256 | TYPE_FLOAT, TYPE_DOUBLE |
62+
| TYPE_DECIMAL256 | TYPE_VARCHAR |
63+
64+
#### 集計関数
65+
66+
現在、DECIMAL256 は次の集計関数をサポートしています:`COUNT``COUNT DISTINCT``SUM``SUM DISTINCT``AVG``AVG DISTINCT``MAX``MIN`、および `ABS`
67+
68+
#### 制限事項
69+
70+
DECIMAL256 には以下の制限があります:
71+
72+
- 現在、DECIMAL256 は小数演算における自動精度スケールアップをサポートしていません。
73+
74+
例えば、`DECIMAL128 * DECIMAL128` は自動的に DECIMAL256 にスケールアップされません。DECIMAL256 の機能は、`DECIMAL256` が CREATE TABLE ステートメントで明示的にオペランドとして指定されるか、CAST を使用する場合(`SELECT CAST(p38s10 as DECIMAL(70, 30))`)にのみ適用されます。
75+
76+
- DECIMAL256 はウィンドウ関数をサポートしていません。
77+
78+
- 集計テーブルは DECIMAL256 タイプをサポートしていません。
3279

3380
##
3481

35-
テーブルを作成する際に DECIMAL 列を定義します。
82+
### Fast DECIMAL の例
83+
84+
テーブルを作成する際に DECIMAL カラムを定義します。
3685

37-
```sql
86+
```SQL
3887
CREATE TABLE decimalDemo (
3988
pk BIGINT(20) NOT NULL COMMENT "",
4089
account DECIMAL(20,10) COMMENT ""
@@ -58,6 +107,40 @@ SELECT * FROM decimalDemo;
58107
+------+-----------------+
59108
```
60109

110+
### DECIMAL256 の例
111+
112+
DECIMAL256 タイプのカラムを持つテーブルを作成します。
113+
114+
```SQL
115+
CREATE TABLE test_decimal256(
116+
p50s48 DECIMAL(50, 48) COMMENT ""
117+
);
118+
```
119+
120+
異なる値を挿入します:
121+
122+
```SQL
123+
INSERT INTO test_decimal256(p50s48) SELECT 1.222222;
124+
```
125+
126+
このステートメントは、値が正しく書き込まれて成功します。
127+
128+
```SQL
129+
INSERT INTO test_decimal256(p50s48) SELECT 11111111111111111111111111111111111111111111.222222;
130+
```
131+
132+
このステートメントは、値が NULL として書き込まれて成功します。整数部分と小数部分(44桁)が指定されたスケール(`48`)と共に、256ビットで表現可能な最大整数を超えているため、FE は直接値を NULL にキャストします。
133+
134+
```SQL
135+
INSERT INTO test_decimal256(p50s48) SELECT 333;
136+
```
137+
138+
このステートメントはエラー `ERROR 1064 (HY000): Insert has filtered data` と共に返されます。整数部分と小数部分が指定されたスケール(`48`)と共に、50桁の整数で表現される最大値を超えているため、BE はエラーを返します。
139+
140+
## 使用上の注意
141+
142+
システム変数 `sql_mode``ERROR_IF_OVERFLOW` に設定することで、算術オーバーフローの場合にシステムが NULL の代わりにエラーを返すようにすることができます。
143+
61144
## キーワード
62145

63-
decimal, decimalv2, decimalv3, fast decimal
146+
decimal, decimalv3, fast decimal, decimal128, decimal256

docs/ja/sql-reference/data-types/semi_structured/Array.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ StarRocks テーブルで ARRAY カラムを作成する際には、次の制限
6060
- v2.1 より前のバージョンでは、ARRAY カラムは重複キーテーブルでのみ作成できます。v2.1 以降では、他のタイプのテーブル(Primary Key、Unique Key、Aggregate)でも ARRAY カラムを作成できます。ただし、集計テーブルでは、そのカラムでデータを集計するために使用される関数が replace() または replace_if_not_null() の場合にのみ ARRAY カラムを作成できます。詳細は [集計テーブル](../../../table_design/table_types/aggregate_table.md) を参照してください。
6161
- ARRAY カラムはキー カラムとして使用できません。
6262
- ARRAY カラムはパーティションキー(PARTITION BY に含まれる)またはバケッティングキー(DISTRIBUTED BY に含まれる)として使用できません。
63-
- DECIMAL V3 は ARRAY でサポートされていません。
6463
- 配列は最大 14 レベルのネストが可能です。
6564

6665
## SQL での配列の構築

0 commit comments

Comments
 (0)