Skip to content

Commit b565292

Browse files
author
zhangshixin.1024
committed
[feature](agg) support aggregation function agg_array_sum
1 parent 09fc139 commit b565292

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
{
3+
"title": "AGG_ARRAY_SUM",
4+
"language": "en"
5+
}
6+
---
7+
8+
## Description
9+
10+
Calculate the sum of elements at each position in the input array and return a new array
11+
12+
## Syntax
13+
14+
```sql
15+
AGG_ARRAY_SUM(<expr>)
16+
```
17+
18+
## Parameters
19+
20+
| Parameter | Description |
21+
| -- |----------------------------------------------------------------|
22+
| `<expr>` | Expression to be summed, an Array with element type Numerical. |
23+
24+
## Return Value
25+
26+
Returns a new array containing the sum of elements at each position of the array.
27+
If there is no valid data within the group, return an empty array.
28+
29+
## Example
30+
31+
```sql
32+
-- setup
33+
CREATE TABLE agg_array_sum_test (
34+
`group_id` bigint(20) NOT NULL,
35+
`array_column` array<bigint(20)> NULL
36+
) ENGINE=OLAP
37+
duplicate KEY(`group_id`)
38+
DISTRIBUTED BY HASH(`group_id`) BUCKETS 2
39+
PROPERTIES (
40+
"replication_allocation" = "tag.location.default: 1",
41+
"in_memory" = "false"
42+
);
43+
insert into agg_array_sum_test values (1,[1,2,3]),(1,[4,5,6]),(2,[10,20]), (2,[40,50,60]), (2, NULL), (3,[30, 50]), (3,[20, null]), (4, null);
44+
```
45+
46+
```sql
47+
select group_id, agg_array_sum(array_column) as sum_array from agg_array_sum_test group by group_id;
48+
```
49+
50+
```text
51+
+----------+--------------+
52+
| group_id | sum_array |
53+
+----------+--------------+
54+
| 2 | [50, 70, 60] |
55+
| 3 | [50, 50] |
56+
| 1 | [5, 7, 9] |
57+
| 4 | [] |
58+
+----------+--------------+
59+
```
60+
61+
```sql
62+
select agg_array_sum(array_column) as sum_array from agg_array_sum_test where array_column is null;
63+
```
64+
65+
```text
66+
+-----------+
67+
| sum_array |
68+
+-----------+
69+
| [] |
70+
+-----------+
71+
```
72+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
{
3+
"title": "AGG_ARRAY_SUM",
4+
"language": "zh-CN"
5+
}
6+
---
7+
8+
## 描述
9+
10+
计算输入数组每一位置元素和,返回一个新的数组
11+
12+
## 语法
13+
14+
```sql
15+
AGG_ARRAY_SUM(<expr>)
16+
```
17+
18+
## 参数
19+
20+
| 参数 | 说明 |
21+
| -- |---------------------------------------------|
22+
| `<expr>` | 需要求和的表达式,元素类型为Numerical的Array。 |
23+
24+
## 返回值
25+
26+
返回一个包含数组每个位置元素和的新数组。
27+
如果组内没有合法数据,则返回空数组。
28+
29+
## 举例
30+
31+
```sql
32+
-- setup
33+
CREATE TABLE agg_array_sum_test (
34+
`group_id` bigint(20) NOT NULL,
35+
`array_column` array<bigint(20)> NULL
36+
) ENGINE=OLAP
37+
duplicate KEY(`group_id`)
38+
DISTRIBUTED BY HASH(`group_id`) BUCKETS 2
39+
PROPERTIES (
40+
"replication_allocation" = "tag.location.default: 1",
41+
"in_memory" = "false"
42+
);
43+
insert into agg_array_sum_test values (1,[1,2,3]),(1,[4,5,6]),(2,[10,20]), (2,[40,50,60]), (2, NULL), (3,[30, 50]), (3,[20, null]), (4, null);
44+
```
45+
46+
```sql
47+
select group_id, agg_array_sum(array_column) as sum_array from agg_array_sum_test group by group_id;
48+
```
49+
50+
```text
51+
+----------+--------------+
52+
| group_id | sum_array |
53+
+----------+--------------+
54+
| 2 | [50, 70, 60] |
55+
| 3 | [50, 50] |
56+
| 1 | [5, 7, 9] |
57+
| 4 | [] |
58+
+----------+--------------+
59+
```
60+
61+
```sql
62+
select agg_array_sum(array_column) as sum_array from agg_array_sum_test where array_column is null;
63+
```
64+
65+
```text
66+
+-----------+
67+
| sum_array |
68+
+-----------+
69+
| [] |
70+
+-----------+
71+
```

sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,7 @@
18221822
"sql-manual/sql-functions/aggregate-functions/count-by-enum",
18231823
"sql-manual/sql-functions/aggregate-functions/covar",
18241824
"sql-manual/sql-functions/aggregate-functions/covar-samp",
1825+
"sql-manual/sql-functions/aggregate-functions/agg-array-sum",
18251826
"sql-manual/sql-functions/aggregate-functions/group-array-intersect",
18261827
"sql-manual/sql-functions/aggregate-functions/group-array-union",
18271828
"sql-manual/sql-functions/aggregate-functions/group-bit-and",

0 commit comments

Comments
 (0)