Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doc] posexplode func #1931

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 48 additions & 23 deletions docs/sql-manual/sql-functions/table-functions/posexplode.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,51 @@ under the License.

## Description

The table function is used in conjunction with Lateral View and can support multiple Lateral Views. It only supports the new optimizer.
The `posexplode` table function expands an array column into multiple rows and adds a column indicating the position of each element, returning a struct type. It must be used with LATERAL VIEW and supports multiple LATERAL VIEWs. Only supported with the new optimizer.

It expands an array column into multiple rows and adds a column indicating the position, returning a struct type. When the array is NULL or empty, posexplode_outer returns NULL. Both posexplode and posexplode_outer will return NULL elements within the array.
`posexplode_outer` is similar to `posexplode`, except for the handling of NULL values.

## Syntax

```sql
posexplode(array)
posexplode_outer(array)
POSEXPLODE(<arr>)
POSEXPLODE_OUTER(<arr>)
```

### Example
## Parameters

```sql
CREATE TABLE IF NOT EXISTS `table_test`(
`id` INT NULL,
`name` TEXT NULL,
`score` array<string> NULL
) ENGINE=OLAP
DUPLICATE KEY(`id`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`id`) BUCKETS 1
PROPERTIES ("replication_allocation" = "tag.location.default: 1");
| Parameter | Description |
| -- | -- |
| `<arr>` | An array that is to be expanded |

## Return Value

mysql> insert into table_test values (0, "zhangsan", ["Chinese","Math","English"]),(1, "lisi", ["null"]),(2, "wangwu", ["88a","90b","96c"]),(3, "lisi2", [null]),(4, "amory", NULL);
When the array is NULL or empty, posexplode_outer returns NULL.
Both `posexplode` and `posexplode_outer` include NULL elements inside the array.

## Examples

``` sql
CREATE TABLE IF NOT EXISTS `table_test`(
`id` INT NULL,
`name` TEXT NULL,
`score` array<string> NULL
) ENGINE=OLAP
DUPLICATE KEY(`id`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`id`) BUCKETS 1
PROPERTIES ("replication_allocation" = "tag.location.default: 1");
```

```sql
insert into table_test values (0, "zhangsan", ["Chinese","Math","English"]),(1, "lisi", ["null"]),(2, "wangwu", ["88a","90b","96c"]),(3, "lisi2", [null]),(4, "amory", NULL);
```

```sql
select * from table_test order by id;
```

mysql [test_query_qa]>select * from table_test order by id;
```text
+------+----------+--------------------------------+
| id | name | score |
+------+----------+--------------------------------+
Expand All @@ -62,8 +80,13 @@ mysql [test_query_qa]>select * from table_test order by id;
| 3 | lisi2 | [null] |
| 4 | amory | NULL |
+------+----------+--------------------------------+
```

mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view posexplode(score) tmp as k,v order by id;
```sql
select id,name,score, k,v from table_test lateral view posexplode(score) tmp as k,v order by id;
```

```text
+------+----------+--------------------------------+------+---------+
| id | name | score | k | v |
+------+----------+--------------------------------+------+---------+
Expand All @@ -76,8 +99,13 @@ mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view pos
| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c |
| 3 | lisi2 | [null] | 0 | NULL |
+------+----------+--------------------------------+------+---------+
```

mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view posexplode_outer(score) tmp as k,v order by id;
```sql
select id,name,score, k,v from table_test lateral view posexplode_outer(score) tmp as k,v order by id;
```

```text
+------+----------+--------------------------------+------+---------+
| id | name | score | k | v |
+------+----------+--------------------------------+------+---------+
Expand All @@ -91,7 +119,4 @@ mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view pos
| 3 | lisi2 | [null] | 0 | NULL |
| 4 | amory | NULL | NULL | NULL |
+------+----------+--------------------------------+------+---------+
```

### Keywords
POSEXPLODE,POSEXPLODE_OUTER
```
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,52 @@ specific language governing permissions and limitations
under the License.
-->

## Description
## 描述

表函数,需配合 Lateral View 使用, 可以支持多个 Lateral view, 仅仅支持新优化器
`posexplode` 表函数,将 array 列展开成多行, 并且增加一列标明位置的列,组成 struct类型返回。需配合 Lateral View 使用, 可以支持多个 Lateral view, 仅支持新优化器

将 array 列展开成多行, 并且增加一列标明位置的列,组成struct类型返回。
当 array 为NULL或者为空时,`posexplode_outer` 返回NULL。
`posexplode` 和 `posexplode_outer` 均会返回 array 内部的NULL元素。
`posexplode_outer` 和 `posexplode` 类似,只是对于 NULL 值的处理不同。

## Syntax
## 语法
```sql
posexplode(expr)
posexplode_outer(expr)
POSEXPLODE(<arr>)
POSEXPLODE_OUTER(<arr>)
```

## 参数

| 参数 | 说明 |
| -- | -- |
| `<arr>` | 待展开的 array 数组 |


## 返回值

当 array 为 NULL 或者为空时,`posexplode_outer` 返回NULL。 `posexplode` 和 `posexplode_outer` 均会返回 array 内部的NULL元素。

## 举例

``` sql
CREATE TABLE IF NOT EXISTS `table_test`(
`id` INT NULL,
`name` TEXT NULL,
`score` array<string> NULL
) ENGINE=OLAP
DUPLICATE KEY(`id`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`id`) BUCKETS 1
PROPERTIES ("replication_allocation" = "tag.location.default: 1");
CREATE TABLE IF NOT EXISTS `table_test`(
`id` INT NULL,
`name` TEXT NULL,
`score` array<string> NULL
) ENGINE=OLAP
DUPLICATE KEY(`id`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`id`) BUCKETS 1
PROPERTIES ("replication_allocation" = "tag.location.default: 1");
```

mysql> insert into table_test values (0, "zhangsan", ["Chinese","Math","English"]),(1, "lisi", ["null"]),(2, "wangwu", ["88a","90b","96c"]),(3, "lisi2", [null]),(4, "amory", NULL);
```sql
insert into table_test values (0, "zhangsan", ["Chinese","Math","English"]),(1, "lisi", ["null"]),(2, "wangwu", ["88a","90b","96c"]),(3, "lisi2", [null]),(4, "amory", NULL);
```

```sql
select * from table_test order by id;
```

mysql [test_query_qa]>select * from table_test order by id;
```text
+------+----------+--------------------------------+
| id | name | score |
+------+----------+--------------------------------+
Expand All @@ -64,8 +79,13 @@ mysql [test_query_qa]>select * from table_test order by id;
| 3 | lisi2 | [null] |
| 4 | amory | NULL |
+------+----------+--------------------------------+
```

mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view posexplode(score) tmp as k,v order by id;
```sql
select id,name,score, k,v from table_test lateral view posexplode(score) tmp as k,v order by id;
```

```text
+------+----------+--------------------------------+------+---------+
| id | name | score | k | v |
+------+----------+--------------------------------+------+---------+
Expand All @@ -78,8 +98,13 @@ mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view pos
| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c |
| 3 | lisi2 | [null] | 0 | NULL |
+------+----------+--------------------------------+------+---------+
```

mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view posexplode_outer(score) tmp as k,v order by id;
```sql
select id,name,score, k,v from table_test lateral view posexplode_outer(score) tmp as k,v order by id;
```

```text
+------+----------+--------------------------------+------+---------+
| id | name | score | k | v |
+------+----------+--------------------------------+------+---------+
Expand All @@ -94,6 +119,3 @@ mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view pos
| 4 | amory | NULL | NULL | NULL |
+------+----------+--------------------------------+------+---------+
```

### Keywords
POSEXPLODE,POSEXPLODE_OUTER
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
{
"title": "posexplode_outer",
"title": "POSEXPLODE",
"language": "zh-CN"
}
---
Expand All @@ -24,35 +24,52 @@ specific language governing permissions and limitations
under the License.
-->

## Description
## 描述

The table function is used in conjunction with Lateral View and can support multiple Lateral Views. It only supports the new optimizer.
`posexplode` 表函数,将 array 列展开成多行, 并且增加一列标明位置的列,组成 struct类型返回。需配合 Lateral View 使用, 可以支持多个 Lateral view, 仅支持新优化器。

It expands an array column into multiple rows and adds a column indicating the position, returning a struct type. When the array is NULL or empty, posexplode_outer returns NULL. Both posexplode and posexplode_outer will return NULL elements within the array.
`posexplode_outer` 和 `posexplode` 类似,只是对于 NULL 值的处理不同。

## Syntax
## 语法
```sql
posexplode(array)
posexplode_outer(array)
POSEXPLODE(<arr>)
POSEXPLODE_OUTER(<arr>)
```

### Example
## 参数

```sql
CREATE TABLE IF NOT EXISTS `table_test`(
`id` INT NULL,
`name` TEXT NULL,
`score` array<string> NULL
) ENGINE=OLAP
DUPLICATE KEY(`id`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`id`) BUCKETS 1
PROPERTIES ("replication_allocation" = "tag.location.default: 1");
| 参数 | 说明 |
| -- | -- |
| `<arr>` | 待展开的 array 数组 |


## 返回值

当 array 为 NULL 或者为空时,`posexplode_outer` 返回NULL。 `posexplode` 和 `posexplode_outer` 均会返回 array 内部的NULL元素。

## 举例

mysql> insert into table_test values (0, "zhangsan", ["Chinese","Math","English"]),(1, "lisi", ["null"]),(2, "wangwu", ["88a","90b","96c"]),(3, "lisi2", [null]),(4, "amory", NULL);
``` sql
CREATE TABLE IF NOT EXISTS `table_test`(
`id` INT NULL,
`name` TEXT NULL,
`score` array<string> NULL
) ENGINE=OLAP
DUPLICATE KEY(`id`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`id`) BUCKETS 1
PROPERTIES ("replication_allocation" = "tag.location.default: 1");
```

```sql
insert into table_test values (0, "zhangsan", ["Chinese","Math","English"]),(1, "lisi", ["null"]),(2, "wangwu", ["88a","90b","96c"]),(3, "lisi2", [null]),(4, "amory", NULL);
```

```sql
select * from table_test order by id;
```

mysql [test_query_qa]>select * from table_test order by id;
```text
+------+----------+--------------------------------+
| id | name | score |
+------+----------+--------------------------------+
Expand All @@ -62,8 +79,13 @@ mysql [test_query_qa]>select * from table_test order by id;
| 3 | lisi2 | [null] |
| 4 | amory | NULL |
+------+----------+--------------------------------+
```

```sql
select id,name,score, k,v from table_test lateral view posexplode(score) tmp as k,v order by id;
```

mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view posexplode(score) tmp as k,v order by id;
```text
+------+----------+--------------------------------+------+---------+
| id | name | score | k | v |
+------+----------+--------------------------------+------+---------+
Expand All @@ -76,8 +98,13 @@ mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view pos
| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c |
| 3 | lisi2 | [null] | 0 | NULL |
+------+----------+--------------------------------+------+---------+
```

mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view posexplode_outer(score) tmp as k,v order by id;
```sql
select id,name,score, k,v from table_test lateral view posexplode_outer(score) tmp as k,v order by id;
```

```text
+------+----------+--------------------------------+------+---------+
| id | name | score | k | v |
+------+----------+--------------------------------+------+---------+
Expand All @@ -92,6 +119,3 @@ mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view pos
| 4 | amory | NULL | NULL | NULL |
+------+----------+--------------------------------+------+---------+
```

### Keywords
POSEXPLODE,POSEXPLODE_OUTER
Loading