diff --git a/docs/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md b/docs/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md deleted file mode 100644 index 3879e76214407..0000000000000 --- a/docs/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -{ - "title": "EXPLODE_BITMAP_OUTER", - "language": "en" -} ---- - - diff --git a/docs/sql-manual/sql-functions/table-functions/explode-bitmap.md b/docs/sql-manual/sql-functions/table-functions/explode-bitmap.md index be661647d5ce9..3bb7dc69d630f 100644 --- a/docs/sql-manual/sql-functions/table-functions/explode-bitmap.md +++ b/docs/sql-manual/sql-functions/table-functions/explode-bitmap.md @@ -24,24 +24,49 @@ specific language governing permissions and limitations under the License. --> -## explode_bitmap +## Description -### description +The `explode_bitmap` table function accepts a bitmap type data and maps each bit (bit) of the bitmap to a separate row. It is commonly used for processing bitmap data, expanding each element of the bitmap into separate records. It should be used in conjunction with LATERAL VIEW. -Table functions must be used in conjunction with Lateral View. +`explode_bitmap_outer` works similarly to `explode_bitmap`, but its behavior differs when handling NULL or empty values. It allows records with empty or NULL bitmaps to exist, and in the result, it expands an empty or NULL bitmap into NULL rows. -Expand a bitmap type. +## Syntax -#### syntax +```sql +EXPLODE_BITMAP() +EXPLODE_BITMAP_OUTER() +``` + +## Parameters + +| Parameter | Description | +| -- | -- | +| `` | bitmap type | + +## Return Value -`explode_bitmap(bitmap)` +Returns a row for each bit in the bitmap, with each row containing a single bit value. -### example +## Examples -Original table data: +```sql +CREATE TABLE example1 ( + k1 INT +)DUPLICATE KEY(k1) +DISTRIBUTED BY HASH(k1) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example1 values(1),(2),(3),(4),(5),(6); +``` +```sql +select k1 from example1 order by k1; ``` -mysql> select k1 from example1 order by k1; + +```text +------+ | k1 | +------+ @@ -54,22 +79,16 @@ mysql> select k1 from example1 order by k1; +------+ ``` -Lateral View: +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_empty()) tmp1 as e1 order by k1, e1; +Empty set +``` +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1")) tmp1 as e1 order by k1, e1; ``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_empty()) tmp1 as e1 order by k1, e1; -+------+------+ -| k1 | e1 | -+------+------+ -| 1 | NULL | -| 2 | NULL | -| 3 | NULL | -| 4 | NULL | -| 5 | NULL | -| 6 | NULL | -+------+------+ -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -80,8 +99,13 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 5 | 1 | | 6 | 1 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,2")) tmp1 as e1 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,2")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -98,8 +122,13 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 6 | 1 | | 6 | 2 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -116,10 +145,15 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 6 | 1 | | 6 | 1000 | +------+------+ +``` -mysql> select k1, e1, e2 from example1 +```sql +select k1, e1, e2 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 lateral view explode_split("a,b", ",") tmp2 as e2 order by k1, e1, e2; +``` + +```text +------+------+------+ | k1 | e1 | e2 | +------+------+------+ @@ -150,6 +184,47 @@ lateral view explode_split("a,b", ",") tmp2 as e2 order by k1, e1, e2; +------+------+------+ ``` -### keywords +```sql +CREATE TABLE example ( + k1 INT, + v1 bitmap +)DUPLICATE KEY(k1) +DISTRIBUTED BY HASH(k1) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example values(1,to_bitmap('10101')),(2,to_bitmap('0')),(3,to_bitmap(NULL)); +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object(value_json) exploded_table AS k , v; +``` + +```text ++------+-------+ +| k1 | bit | ++------+-------+ +| 2 | 0 | +| 1 | 10101 | ++------+-------+ +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object_outer(value_json) exploded_table AS k, v; +``` -explode,bitmap,explode_bitmap \ No newline at end of file +```text ++------+-------+ +| k1 | bit | ++------+-------+ +| 2 | 0 | +| 1 | 10101 | +| 3 | NULL | ++------+-------+ +``` \ No newline at end of file diff --git a/docs/sql-manual/sql-functions/table-functions/explode-json-array-double.md b/docs/sql-manual/sql-functions/table-functions/explode-json-array-double.md index bd998190f284b..b15ff6b0e768a 100644 --- a/docs/sql-manual/sql-functions/table-functions/explode-json-array-double.md +++ b/docs/sql-manual/sql-functions/table-functions/explode-json-array-double.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_array_double", +"title": "EXPLODE_JSON_ARRAY_DOUBLE", "language": "en" } --- @@ -26,72 +26,89 @@ 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 `explode_json_array_double` table function accepts a JSON array, where each element is of double-precision floating-point type, and expands each floating-point number in the array into multiple rows, with each row containing one floating-point number. It is used in conjunction with 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. +`explode_json_array_double_outer` is similar to `explode_json_array_double`, but the handling of NULL values is different. + +If the JSON string itself is NULL, the `OUTER` version will return one row, with the value as NULL. The normal version will completely ignore such records. + +If the JSON array is empty, the `OUTER` version will return one row, with the value as NULL. The normal version will return no results. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_DOUBLE() +EXPLODE_JSON_ARRAY_DOUBLE_OUTER() +``` + +## Return Value + +| Parameter | Description | +| -- | -- | +| `` | json type | + +## Parameters + +Expands the JSON array, creating a row for each element, returning a double-precision floating-point column. + +## Examples + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE(json_array) tmp1 AS e1 +WHERE id = 2; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 2 | 1.1 | +| 2 | 2.2 | +| 2 | 3.3 | +| 2 | 4.4 | ++------+------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE(json_array) tmp1 AS e1 +WHERE id = 6; +Empty set (0.01 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE_OUTER(json_array) tmp1 AS e1 +WHERE id = 6; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 6 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/docs/sql-manual/sql-functions/table-functions/explode-json-array-int.md b/docs/sql-manual/sql-functions/table-functions/explode-json-array-int.md index 2bb54023db605..dcdc0a260892d 100644 --- a/docs/sql-manual/sql-functions/table-functions/explode-json-array-int.md +++ b/docs/sql-manual/sql-functions/table-functions/explode-json-array-int.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_array_int", +"title": "EXPLODE_JSON_ARRAY_INT", "language": "en" } --- @@ -26,72 +26,90 @@ 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 `explode_json_array_int` table function accepts a JSON array, where each element is of integer type, and expands each integer in the array into multiple rows, with each row containing one integer. It is used in conjunction with 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. +`explode_json_array_int_outer` is similar to `explode_json_array_int`, but the handling of NULL values is different. + +If the JSON string itself is NULL, the `OUTER` version will return one row, with the value as NULL. The normal version will completely ignore such records. + +If the JSON array is empty, the `OUTER` version will return one row, with the value as NULL. The normal version will return no results. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_INT() +EXPLODE_JSON_ARRAY_INT_OUTER() +``` + +## Return Value + +| Parameter | Description | +| -- | -- | +| `` | json type | + +## Parameters + +Expands the JSON array, creating a row for each element, returning an integer column. + +## Examples + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT(json_array) tmp1 AS e1 +WHERE id = 1; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 1 | 1 | +| 1 | 2 | +| 1 | 3 | +| 1 | 4 | +| 1 | 5 | ++------+------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT(json_array) tmp1 AS e1 +WHERE id = 5; +Empty set (0.01 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT_OUTER(json_array) tmp1 AS e1 +WHERE id = 5; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 5 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/docs/sql-manual/sql-functions/table-functions/explode-json-array-json.md b/docs/sql-manual/sql-functions/table-functions/explode-json-array-json.md index 0affab2edadb2..474875c3556bb 100644 --- a/docs/sql-manual/sql-functions/table-functions/explode-json-array-json.md +++ b/docs/sql-manual/sql-functions/table-functions/explode-json-array-json.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-json", +"title": "EXPLODE_JSON_ARRAY_JSON", "language": "en" } --- @@ -26,72 +26,59 @@ 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. - -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. +The `explode_json_array_json` table function accepts a JSON array, where each element is of JSON object type, and expands each JSON object in the array into multiple rows, with each row containing one JSON object. It is used in conjunction with LATERAL VIEW. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_JSON() +EXPLODE_JSON_ARRAY_JSON_OUTER() ``` -### Example +## Return Value -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array NULL - ) ENGINE=OLAP - DUPLICATE KEY(`id`) - COMMENT 'OLAP' - DISTRIBUTED BY HASH(`id`) BUCKETS 1 - PROPERTIES ("replication_allocation" = "tag.location.default: 1"); +| Parameter | Description | +| -- | -- | +| `` | json type | -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); +## Parameters +Expands the JSON array, creating a row for each element, returning a JSON object column. -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 3 | lisi2 | [null] | -| 4 | amory | NULL | -+------+----------+--------------------------------+ +## Examples + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` -mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view posexplode(score) tmp as k,v order by id; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -+------+----------+--------------------------------+------+---------+ +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, '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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_JSON(json_array) tmp1 AS e1 +WHERE id = 4; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+---------+ +| id | e1 | ++------+---------+ +| 4 | {"a":1} | +| 4 | {"b":2} | +| 4 | {"c":3} | ++------+---------+ +``` \ No newline at end of file diff --git a/docs/sql-manual/sql-functions/table-functions/explode-json-array-string.md b/docs/sql-manual/sql-functions/table-functions/explode-json-array-string.md index 99910ce04b55a..8d9aa90718ce8 100644 --- a/docs/sql-manual/sql-functions/table-functions/explode-json-array-string.md +++ b/docs/sql-manual/sql-functions/table-functions/explode-json-array-string.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-string", +"title": "EXPLODE_JSON_ARRAY_STRING", "language": "en" } --- @@ -26,72 +26,88 @@ 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 `explode_json_array_string` table function accepts a JSON array, where each element is of string type, and expands each string in the array into multiple rows, with each row containing one string. It is used in conjunction with 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. +`explode_json_array_string_outer` is similar to `explode_json_array_string`, but the handling of NULL values is different. + +If the JSON string itself is NULL, the `OUTER` version will return one row, with the value as NULL. The normal version will completely ignore such records. + +If the JSON array is empty, the `OUTER` version will return one row, with the value as NULL. The normal version will return no results. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_STRING() +EXPLODE_JSON_ARRAY_STRING_OUTER() +``` + +## Return Value + +| Parameter | Description | +| -- | -- | +| `` | json type | + +## Parameters + +Expands the JSON array, creating a row for each element, returning a string column. + +## Examples + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING(json_array) tmp1 AS e1 +WHERE id = 3; +``` + +```text ++------+--------+ +| id | e1 | ++------+--------+ +| 3 | apple | +| 3 | banana | +| 3 | cherry | ++------+--------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING(json_array) tmp1 AS e1 +WHERE id = 6; +Empty set (0.02 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING_OUTER(json_array) tmp1 AS e1 +WHERE id = 6; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 6 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/docs/sql-manual/sql-functions/table-functions/explode-json-object-outer.md b/docs/sql-manual/sql-functions/table-functions/explode-json-object-outer.md deleted file mode 100644 index 07742f8162bfb..0000000000000 --- a/docs/sql-manual/sql-functions/table-functions/explode-json-object-outer.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -{ -"title": "explode_json_object_outer", -"language": "en" -} ---- - - - -## Description - -The table function is used in conjunction with Lateral View and can support multiple Lateral Views. It only supports 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. - -## Syntax -```sql -posexplode(array) -posexplode_outer(array) -``` - -### Example - -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ -``` - -### Keywords -POSEXPLODE,POSEXPLODE_OUTER diff --git a/docs/sql-manual/sql-functions/table-functions/explode-json-object.md b/docs/sql-manual/sql-functions/table-functions/explode-json-object.md index 3144b67b3ae03..dd10c2394f505 100644 --- a/docs/sql-manual/sql-functions/table-functions/explode-json-object.md +++ b/docs/sql-manual/sql-functions/table-functions/explode-json-object.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_object", +"title": "EXPLODE_JSON_OBJECT", "language": "en" } --- @@ -26,72 +26,92 @@ 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. +`explode_json_object` expands a JSON object into multiple rows, with each row containing a key-value pair. It is typically used to process JSON data and expand the JSON object into a more queryable format. This function only supports non-empty JSON objects. -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. +`explode_json_object_outer` is similar to `explode_json_object`, but with different behavior when handling empty and NULL values. It can retain empty or NULL JSON objects and return corresponding records. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_OBJECT() +EXPLODE_JSON_OBJECT_OUTER() ``` -### Example +## Parameters + +| Parameter | Description | +| -- | -- | +| `` | json type | + +## Return Value + +When the JSON object is neither empty nor NULL, the return values of `explode_json_object` and `explode_json_object_outer` are the same. Each key-value pair generates one row, with the key as one column and the value as another column. + +When the JSON object is empty or NULL: + +`explode_json_object` will not return any rows. +`explode_json_object_outer` will return one row, with the expanded columns being NULL. + +## Examples + +```sql +CREATE TABLE example ( + id INT, + value_json json +) DUPLICATE KEY(id) +DISTRIBUTED BY HASH(`id`) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +INSERT INTO example VALUES +(1, '{"key1": "value1", "key2": "value2"}'), +(2, '{}'), +(3, NULL); +``` + +```sql +select * from example; +``` + +```text ++------+-----------------------------------+ +| id | value_json | ++------+-----------------------------------+ +| 2 | {} | +| 1 | {"key1":"value1","key2":"value2"} | +| 3 | NULL | ++------+-----------------------------------+ +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object(value_json) exploded_table AS k , v; +``` + +```text ++------+------+----------+ +| id | k | v | ++------+------+----------+ +| 1 | key1 | "value1" | +| 1 | key2 | "value2" | ++------+------+----------+ +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object_outer(value_json) exploded_table AS k, v; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+----------+ +| id | k | v | ++------+------+----------+ +| 3 | NULL | NULL | +| 1 | key1 | "value1" | +| 1 | key2 | "value2" | +| 2 | NULL | NULL | ++------+------+----------+ +``` \ No newline at end of file diff --git a/docs/sql-manual/sql-functions/table-functions/explode-map-outer.md b/docs/sql-manual/sql-functions/table-functions/explode-map-outer.md deleted file mode 100644 index fd5e9b84b6c4e..0000000000000 --- a/docs/sql-manual/sql-functions/table-functions/explode-map-outer.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -{ -"title": "explode_map_outer", -"language": "en" -} ---- - - - -## Description - -The table function is used in conjunction with Lateral View and can support multiple Lateral Views. It only supports 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. - -## Syntax -```sql -posexplode(array) -posexplode_outer(array) -``` - -### Example - -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ -``` - -### Keywords -POSEXPLODE,POSEXPLODE_OUTER diff --git a/docs/sql-manual/sql-functions/table-functions/explode-map.md b/docs/sql-manual/sql-functions/table-functions/explode-map.md index 0f79573a285aa..820361e5c95f1 100644 --- a/docs/sql-manual/sql-functions/table-functions/explode-map.md +++ b/docs/sql-manual/sql-functions/table-functions/explode-map.md @@ -24,26 +24,47 @@ specific language governing permissions and limitations under the License. --> -## explode +## Description -### description +The `explode_map` function takes a map (mapping type) and expands it into multiple rows, with each row containing a key-value pair. It is typically used in conjunction with LATERAL VIEW and can support multiple lateral views. It is supported only by the new optimizer. -Table functions must be used in conjunction with Lateral View, support multi conjunction with Lateral View,support new optimizer only. +The main difference between `explode_map` and `explode_map_outer` lies in the handling of null values. -explode map column to rows. `explode_map_outer` will return NULL, while `map` is NULL or empty. -`explode_map` and `explode_map_outer` both keep the nested NULL elements of map. +## Syntax -#### syntax ```sql -explode_map(expr) -explode_map_outer(expr) +EXPLODE_MAP(map) +EXPLODE_MAP_OUTER(map) ``` -### example -```mysql> SET enable_nereids_planner=true -mysql> SET enable_fallback_to_original_planner=false +## Parameters -mysql> CREATE TABLE IF NOT EXISTS `sdu`( +| Parameter | Description | +| -- | -- | +| `map` | map type | + +## Return Value + +When the map is not empty or NULL, the return values of `explode_map` and `explode_map_outer` are the same. + +When the data is empty or NULL: + +`explode_map` Only processes non-empty map types. If the map is empty or NULL, `explode_map` will not return any rows. +`explode_map_outer` If the map is empty or NULL, explode_map_outer will retain the record with the empty or NULL map and return a row with NULL values. + +## Examples + + +```sql +SET enable_nereids_planner=true +``` + +```sql +SET enable_fallback_to_original_planner=false +``` + +```sql +CREATE TABLE IF NOT EXISTS `sdu`( `id` INT NULL, `name` TEXT NULL, `score` MAP NULL @@ -53,12 +74,19 @@ mysql> CREATE TABLE IF NOT EXISTS `sdu`( DISTRIBUTED BY HASH(`id`) BUCKETS 1 PROPERTIES ("replication_allocation" = "tag.location.default: 1"); Query OK, 0 rows affected (0.15 sec) +``` -mysql> insert into sdu values (0, "zhangsan", {"Chinese":"80","Math":"60","English":"90"}), (1, "lisi", {"null":null}), (2, "wangwu", {"Chinese":"88","Math":"90","English":"96"}), (3, "lisi2", {null:null}), (4, "amory", NULL); +```sql +insert into sdu values (0, "zhangsan", {"Chinese":"80","Math":"60","English":"90"}), (1, "lisi", {"null":null}), (2, "wangwu", {"Chinese":"88","Math":"90","English":"96"}), (3, "lisi2", {null:null}), (4, "amory", NULL); Query OK, 5 rows affected (0.23 sec) {'label':'label_9b35d9d9d59147f5_bffb974881ed2133', 'status':'VISIBLE', 'txnId':'4005'} +``` -mysql> select * from sdu order by id; +```sql +select * from sdu order by id; +``` + +```text +------+----------+-----------------------------------------+ | id | name | score | +------+----------+-----------------------------------------+ @@ -68,8 +96,13 @@ mysql> select * from sdu order by id; | 3 | lisi2 | {null:null} | | 4 | amory | NULL | +------+----------+-----------------------------------------+ +``` -mysql> select name, k,v from sdu lateral view explode_map(score) tmp as k,v; +```sql +select name, k,v from sdu lateral view explode_map(score) tmp as k,v; +``` + +```text +----------+---------+------+ | name | k | v | +----------+---------+------+ @@ -82,8 +115,13 @@ mysql> select name, k,v from sdu lateral view explode_map(score) tmp as k,v; | wangwu | English | 96 | | lisi2 | NULL | NULL | +----------+---------+------+ +``` -mysql> select name, k,v from sdu lateral view explode_map_outer(score) tmp as k,v; +```sql +select name, k,v from sdu lateral view explode_map_outer(score) tmp as k,v; +``` + +```text +----------+---------+------+ | name | k | v | +----------+---------+------+ @@ -97,8 +135,13 @@ mysql> select name, k,v from sdu lateral view explode_map_outer(score) tmp as k, | lisi2 | NULL | NULL | | amory | NULL | NULL | +----------+---------+------+ +``` + +```sql +select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp as k,v lateral view explode_map(score) tmp2 as k1,v1; +``` -mysql> select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp as k,v lateral view explode_map(score) tmp2 as k1,v1; +```text +----------+---------+------+---------+------+ | name | k | v | k1 | v1 | +----------+---------+------+---------+------+ @@ -123,7 +166,4 @@ mysql> select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp | wangwu | English | 96 | English | 96 | | lisi2 | NULL | NULL | NULL | NULL | +----------+---------+------+---------+------+ -``` - -### keywords -EXPLODE_MAP,EXPLODE_MAP_OUTER,MAP \ No newline at end of file +``` \ No newline at end of file diff --git a/docs/sql-manual/sql-functions/table-functions/explode-numbers-outer.md b/docs/sql-manual/sql-functions/table-functions/explode-numbers-outer.md deleted file mode 100644 index c0432e300ef01..0000000000000 --- a/docs/sql-manual/sql-functions/table-functions/explode-numbers-outer.md +++ /dev/null @@ -1,51 +0,0 @@ ---- -{ - "title": "EXPLODE_NUMBERS_OUTER", - "language": "en" -} ---- - - - -## outer combinator - -### description - -#### syntax -`explode_numbers(INT x)` - -Adding the `_outer` suffix after the function name of the table function changes the function behavior from `non-outer` to `outer`, and adds a row of `Null` data when the table function generates 0 rows of data. - -### example - -``` -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(0) tmp1 as e1; -Empty set - -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers_outer(0) tmp1 as e1; -+------+ -| e1 | -+------+ -| NULL | -+------+ -``` -### keywords - - outer \ No newline at end of file diff --git a/docs/sql-manual/sql-functions/table-functions/explode-numbers.md b/docs/sql-manual/sql-functions/table-functions/explode-numbers.md index 159043713c527..b2df121711306 100644 --- a/docs/sql-manual/sql-functions/table-functions/explode-numbers.md +++ b/docs/sql-manual/sql-functions/table-functions/explode-numbers.md @@ -24,21 +24,38 @@ specific language governing permissions and limitations under the License. --> -## explode_numbers +## Description -### description +The `explode_numbers` table function takes an integer n and expands all numbers within the range into multiple rows, each containing a single number. It is commonly used to generate a sequence of consecutive numbers and is often paired with LATERAL VIEW. -Table functions must be used in conjunction with Lateral View. +`explode_numbers_outer`, unlike `explode_numbers`, adds a NULL row when the table function generates zero rows. -Get a number sequence [0,n). +## Syntax -#### syntax +```sql +EXPLODE_NUMBERS() +EXPLODE_NUMBERS_OUTER() +``` + +## Parameters + +| Parameter | Description | +| -- | -- | +| `` | Integer type input | + +## Return Value -`explode_numbers(n)` +Returns a sequence of [0, n). -### example +- Does not return any rows when n is 0 or NULL. + +## Examples + +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as e1; ``` -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as e1; + +```text +------+ | e1 | +------+ @@ -49,6 +66,20 @@ mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as | 4 | +------+ ``` -### keywords -explode,numbers,explode_numbers \ No newline at end of file +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers(0) tmp1 as e1; +Empty set +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers_outer(0) tmp1 as e1; +``` + +```text ++------+ +| e1 | ++------+ +| NULL | ++------+ +``` \ No newline at end of file diff --git a/docs/sql-manual/sql-functions/table-functions/explode-split-outer.md b/docs/sql-manual/sql-functions/table-functions/explode-split-outer.md deleted file mode 100644 index b0cab53aa6313..0000000000000 --- a/docs/sql-manual/sql-functions/table-functions/explode-split-outer.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -{ - "title": "EXPLODE_SPLIT_OUTER", - "language": "en" -} ---- - - diff --git a/docs/sql-manual/sql-functions/table-functions/explode-split.md b/docs/sql-manual/sql-functions/table-functions/explode-split.md index 1ccf91c40aa56..71869e8564908 100644 --- a/docs/sql-manual/sql-functions/table-functions/explode-split.md +++ b/docs/sql-manual/sql-functions/table-functions/explode-split.md @@ -24,29 +24,37 @@ specific language governing permissions and limitations under the License. --> -## explode_split +## Description -### description -#### syntax +The `explode_split` table function is used to split a string into multiple substrings based on a specified delimiter and expand each substring into a separate row. Each substring is returned as an individual row, and it is typically used with LATERAL VIEW to break down long strings into individual parts for more granular queries. -`explode_split(str, delimiter)` +`explode_split_outer` is similar to `explode_split`, but it differs in the way it handles empty or NULL strings. -Table functions must be used in conjunction with Lateral View. +## Syntax -Split a string into multiple substrings according to the specified delimiter. +```sql +EXPLODE_SPLIT(, ) +EXPLODE_SPLIT_OUTER(, ) +``` -grammar: +## Parameters -``` -explode_split(str, delimiter) -``` +| Parameter | Description | +| -- | -- | +| `` | String type | +| `` | Delimiter | + +## Return Value -### example +Returns a sequence of the split substrings. If the string is empty or NULL, no rows are returned. -Original table data: +## Examples +```sql +select * from example1 order by k1; ``` -mysql> select * from example1 order by k1; + +```text +------+---------+ | k1 | k2 | +------+---------+ @@ -59,34 +67,52 @@ mysql> select * from example1 order by k1; +------+---------+ ``` -Lateral View: - +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 1 order by k1, e1; ``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 1 order by k1, e1; + +```text +------+------+ | k1 | e1 | +------+------+ | 1 | | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 2 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 2 order by k1, e1; Empty set +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 3 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 3 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ | 3 | | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 4 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 4 order by k1, e1; +``` + +```text +------+------+ | k1 | e1 | +------+------+ | 4 | 1 | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 5 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 5 order by k1, e1; +``` + +```text +------+------+ | k1 | e1 | +------+------+ @@ -94,8 +120,13 @@ mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e | 5 | 3 | | 5 | 1 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 6 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 6 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -105,6 +136,33 @@ mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e +------+------+ ``` -### keywords +```sql +CREATE TABLE example2 ( + id INT, + str string null +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(`id`) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example2 values (1,''),(2,NUll),(3,"1"),(4,"1,2,3"),(5,"a,b,c"); +``` -explode,split,explode_split \ No newline at end of file +```sql +select id, e1 from example2 lateral view explode_split(str, ',') tmp1 as e1 where id = 2 order by id, e1; +Empty set (0.02 sec) +``` + +```sql +select id, e1 from example2 lateral view explode_split_outer(str, ',') tmp1 as e1 where id = 2 order by id, e1; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 2 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/docs/sql-manual/sql-functions/table-functions/explode-variant-array.md b/docs/sql-manual/sql-functions/table-functions/explode-variant-array.md index 0affab2edadb2..73d74dd886744 100644 --- a/docs/sql-manual/sql-functions/table-functions/explode-variant-array.md +++ b/docs/sql-manual/sql-functions/table-functions/explode-variant-array.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-json", +"title": "EXPLODE_VARIANT_ARRAY", "language": "en" } --- @@ -26,72 +26,85 @@ 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. - -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. +The `explode_variant_array` table function accepts a variant type, where each element is a JSON object, and expands each JSON object in the array into multiple rows, with each row containing one JSON object. It is used in conjunction with LATERAL VIEW. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_VARIANT_ARRAY() ``` -### Example +## Return Value -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array NULL - ) ENGINE=OLAP - DUPLICATE KEY(`id`) - COMMENT 'OLAP' - DISTRIBUTED BY HASH(`id`) BUCKETS 1 - PROPERTIES ("replication_allocation" = "tag.location.default: 1"); +| Parameter | Description | +| -- | -- | +| `` | variant type | -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); +## Parameters +Expands the JSON array, creating a row for each element, returning a JSON object column. -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 3 | lisi2 | [null] | -| 4 | amory | NULL | -+------+----------+--------------------------------+ +## Examples + +```sql +CREATE TABLE `simple_nested_test` ( + `k` bigint NULL, + `v` variant NULL +) ENGINE=OLAP +DUPLICATE KEY(`k`) +DISTRIBUTED BY HASH(`k`) BUCKETS 8 +PROPERTIES ( +"file_cache_ttl_seconds" = "0", +"is_being_synced" = "false", +"storage_medium" = "hdd", +"storage_format" = "V2", +"inverted_index_storage_format" = "V2", +"light_schema_change" = "true", +"disable_auto_compaction" = "false", +"variant_enable_flatten_nested" = "true", +"enable_single_replica_compaction" = "false", +"group_commit_interval_ms" = "10000", +"group_commit_data_bytes" = "134217728" +); +``` -mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view posexplode(score) tmp as k,v order by id; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -+------+----------+--------------------------------+------+---------+ +```sql +insert into simple_nested_test values(1, '{ + "eventId": 1, + "firstName": "Name1", + "lastName": "Eric", + "body": { + "phoneNumbers": [ + { + "number": "1111111111", + "type": "GSM", + "callLimit": 5 + }, + { + "number": "222222222", + "type": "HOME", + "callLimit": 3 + }, + { + "number": "33333333", + "callLimit": 2, + "type": "WORK" + } + ] + } +}'); +``` -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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +```sql +select v['eventId'], phone_numbers + from simple_nested_test lateral view explode_variant_array(v['body']['phoneNumbers']) tmp1 as phone_numbers + where phone_numbers['type'] = 'GSM' OR phone_numbers['type'] = 'HOME' and phone_numbers['callLimit'] > 2; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++--------------------------+----------------------------------------------------+ +| element_at(v, 'eventId') | phone_numbers | ++--------------------------+----------------------------------------------------+ +| 1 | {"callLimit":5,"number":"1111111111","type":"GSM"} | +| 1 | {"callLimit":3,"number":"222222222","type":"HOME"} | ++--------------------------+----------------------------------------------------+ +``` \ No newline at end of file diff --git a/docs/sql-manual/sql-functions/table-functions/explode.md b/docs/sql-manual/sql-functions/table-functions/explode.md index 5d8df439474fc..45382e0afce60 100644 --- a/docs/sql-manual/sql-functions/table-functions/explode.md +++ b/docs/sql-manual/sql-functions/table-functions/explode.md @@ -24,24 +24,38 @@ specific language governing permissions and limitations under the License. --> -## explode +## Description -### description +The `explode` function takes an array as input and maps each element of the array to a separate row. It is typically used in conjunction with LATERAL VIEW to flatten nested data structures into a standard tabular format. The main difference between explode and `explode_outer` lies in handling empty values. -Table functions must be used in conjunction with Lateral View. - -explode array column to rows. `explode_outer` will return NULL, while `array` is NULL or empty. -`explode` and `explode_outer` both keep the nested NULL elements of array. - -#### syntax +## Syntax ```sql -explode(expr) -explode_outer(expr) +EXPLODE() +EXPLODE_OUTER() ``` -### example +## Required Parameters + +| Parameter | Description | +| -- | -- | +| `` | Array type | + +## Return Value + +When the array is not empty or NULL, the return values of `explode` and `explode_outer` are the same. + +When the data is empty or NULL: + +`explode` will not produce any rows and will filter out these records. + +`explode_outer` if the array is empty, will generate a single row, but the expanded column value will be NULL. If the array is NULL, it will also retain a row and return NULL. + +## Examples ``` -mysql> select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e1; +select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e1; +``` + +```text +------+ | e1 | +------+ @@ -49,18 +63,30 @@ mysql> select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e | 2 | | 3 | +------+ +``` -mysql> select e1 from (select 1 k1) as t lateral view explode_outer(null) tmp1 as e1; +```sql +select e1 from (select 1 k1) as t lateral view explode_outer(null) tmp1 as e1; +``` + +``` text +------+ | e1 | +------+ | NULL | +------+ +``` -mysql> select e1 from (select 1 k1) as t lateral view explode([]) tmp1 as e1; +```sql +select e1 from (select 1 k1) as t lateral view explode([]) tmp1 as e1; Empty set (0.010 sec) +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp1 as e1; +``` -mysql> select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp1 as e1; +```text +------+ | e1 | +------+ @@ -68,8 +94,13 @@ mysql> select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp | 1 | | NULL | +------+ +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null]) tmp1 as e1; +``` -mysql> select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null]) tmp1 as e1; +```text +------+ | e1 | +------+ @@ -77,7 +108,4 @@ mysql> select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null | 1 | | NULL | +------+ -``` - -### keywords -EXPLODE,EXPLODE_OUTER,ARRAY \ No newline at end of file +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md deleted file mode 100644 index 98043184a78fa..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -{ - "title": "EXPLODE_BITMAP_OUTER", - "language": "zh-CN" -} ---- - - diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-bitmap.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-bitmap.md index e527c7539ad1b..b68277dc7a0e9 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-bitmap.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-bitmap.md @@ -24,23 +24,49 @@ specific language governing permissions and limitations under the License. --> -## explode_bitmap - ## 描述 -表函数,需配合 Lateral View 使用。 +`explode_bitmap` 表函数,接受一个位图(bitmap)类型的数据,将位图中的每个 bit(位)映射为单独的行。通常用于处理位图数据,将位图中的每个元素展开成单独的记录。需配合 Lateral View 使用。 -展开一个bitmap类型。 +`explode_bitmap_outer` 与 `explode_bitmap` 类似,但在处理空值或 NULL 时,行为有所不同。它允许空位图或 NULL 位图的记录存在,并在返回结果中将空位图或者 NULL 位图展开为 NULL 行。 ## 语法 -`explode_bitmap(bitmap)` + +```sql +EXPLODE_BITMAP() +EXPLODE_BITMAP_OUTER() +``` + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | bitmap 类型 | + +## 返回值 + +返回位图中每一位对应的行,其中每一行包含一个位值。 ## 举例 -原表数据: +```sql +CREATE TABLE example1 ( + k1 INT +)DUPLICATE KEY(k1) +DISTRIBUTED BY HASH(k1) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example1 values(1),(2),(3),(4),(5),(6); +``` +```sql +select k1 from example1 order by k1; ``` -mysql> select k1 from example1 order by k1; + +```text +------+ | k1 | +------+ @@ -53,13 +79,16 @@ mysql> select k1 from example1 order by k1; +------+ ``` -Lateral View: +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_empty()) tmp1 as e1 order by k1, e1; +Empty set +``` +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1")) tmp1 as e1 order by k1, e1; ``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_empty()) tmp1 as e1 order by k1, e1; -Empty set -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -70,8 +99,13 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 5 | 1 | | 6 | 1 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,2")) tmp1 as e1 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,2")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -88,8 +122,13 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 6 | 1 | | 6 | 2 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -106,10 +145,15 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 6 | 1 | | 6 | 1000 | +------+------+ +``` -mysql> select k1, e1, e2 from example1 +```sql +select k1, e1, e2 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 lateral view explode_split("a,b", ",") tmp2 as e2 order by k1, e1, e2; +``` + +```text +------+------+------+ | k1 | e1 | e2 | +------+------+------+ @@ -140,6 +184,47 @@ lateral view explode_split("a,b", ",") tmp2 as e2 order by k1, e1, e2; +------+------+------+ ``` -### keywords +```sql +CREATE TABLE example ( + k1 INT, + v1 bitmap +)DUPLICATE KEY(k1) +DISTRIBUTED BY HASH(k1) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example values(1,to_bitmap('10101')),(2,to_bitmap('0')),(3,to_bitmap(NULL)); +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object(value_json) exploded_table AS k , v; +``` + +```text ++------+-------+ +| k1 | bit | ++------+-------+ +| 2 | 0 | +| 1 | 10101 | ++------+-------+ +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object_outer(value_json) exploded_table AS k, v; +``` -explode,bitmap,explode_bitmap +```text ++------+-------+ +| k1 | bit | ++------+-------+ +| 2 | 0 | +| 1 | 10101 | +| 3 | NULL | ++------+-------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-double.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-double.md index c6e944222142b..24fc924f2cecb 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-double.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-double.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_array_double", +"title": "EXPLODE_JSON_ARRAY_DOUBLE", "language": "zh-CN" } --- @@ -24,74 +24,92 @@ 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. +`explode_json_array_double` 表函数,接受一个 JSON 数组,其中每个元素是双精度浮点数类型,将该 JSON 数组中的每个浮点数展开为多行,每行包含一个浮点数。配合 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. +`explode_json_array_double_outer` 和 `explode_json_array_double` 类似,对于空值或 NULL 的处理不同。 + +如果 JSON 字符串本身为 NULL,`OUTER` 版本会返回一行,且该行中的值为 NULL。普通版本会完全忽略这类记录。 + +如果 JSON 数组为空,`OUTER` 版本会返回一行,且该行的值为 NULL。普通版本则不会返回任何结果。 + +## 语法 + +```sql +EXPLODE_JSON_ARRAY_DOUBLE() +EXPLODE_JSON_ARRAY_DOUBLE_OUTER() +``` + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回双精度浮点数列。 + +## 举例 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE(json_array) tmp1 AS e1 +WHERE id = 2; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 2 | 1.1 | +| 2 | 2.2 | +| 2 | 3.3 | +| 2 | 4.4 | ++------+------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE(json_array) tmp1 AS e1 +WHERE id = 6; +Empty set (0.01 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE_OUTER(json_array) tmp1 AS e1 +WHERE id = 6; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 6 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-int.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-int.md index b8bfb4228be1c..bf4a13d19765a 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-int.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-int.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_array_int", +"title": "EXPLODE_JSON_ARRAY_INT", "language": "zh-CN" } --- @@ -24,74 +24,93 @@ 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. +`explode_json_array_int` 表函数,接受一个 JSON 数组,其中每个元素是整数类型,将该 JSON 数组中的每个整数展开为多行,每行包含一个整数。配合 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. +`explode_json_array_int_outer` 和 `explode_json_array_int` 类似,对于空值或 NULL 的处理不同。 + +如果 JSON 字符串本身为 NULL,`OUTER` 版本会返回一行,且该行中的值为 NULL。普通版本会完全忽略这类记录。 + +如果 JSON 数组为空,`OUTER` 版本会返回一行,且该行的值为 NULL。普通版本则不会返回任何结果。 + +## 语法 + +```sql +EXPLODE_JSON_ARRAY_INT() +EXPLODE_JSON_ARRAY_INT_OUTER() +``` + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回整数列。 + +## 举例 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT(json_array) tmp1 AS e1 +WHERE id = 1; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 1 | 1 | +| 1 | 2 | +| 1 | 3 | +| 1 | 4 | +| 1 | 5 | ++------+------+ ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT(json_array) tmp1 AS e1 +WHERE id = 5; +Empty set (0.01 sec) +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT_OUTER(json_array) tmp1 AS e1 +WHERE id = 5; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 5 | NULL | ++------+------+ +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-json.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-json.md index 9215aab847640..9c5fd073d73f4 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-json.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-json.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-json", +"title": "EXPLODE_JSON_ARRAY_JSON", "language": "zh-CN" } --- @@ -24,74 +24,62 @@ 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. +`explode_json_array_json` 表函数,接受一个 JSON 数组,其中每个元素是 JSON 对象类型,将该 JSON 数组中的每个 JSON 对象展开为多行,每行包含一个 JSON 对象。配合 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. +## 语法 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_JSON() +EXPLODE_JSON_ARRAY_JSON_OUTER() ``` -### Example +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回 JSON 对象列。 + +## 举例 + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_JSON(json_array) tmp1 AS e1 +WHERE id = 4; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+---------+ +| id | e1 | ++------+---------+ +| 4 | {"a":1} | +| 4 | {"b":2} | +| 4 | {"c":3} | ++------+---------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-string.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-string.md index 0eb71d1757299..733fcafd9af7e 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-string.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-array-string.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-string", +"title": "EXPLODE_JSON_ARRAY_STRING", "language": "zh-CN" } --- @@ -24,74 +24,91 @@ 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. +`explode_json_array_string` 表函数,接受一个 JSON 数组,其中每个元素是字符串类型,将该 JSON 数组中的每个字符串展开为多行,每行包含一个字符串。配合 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. +`explode_json_array_string_outer` 和 `explode_json_array_string` 类似,对于空值或 NULL 的处理不同。 + +如果 JSON 字符串本身为 NULL,`OUTER` 版本会返回一行,且该行中的值为 NULL。普通版本会完全忽略这类记录。 + +如果 JSON 数组为空,`OUTER` 版本会返回一行,且该行的值为 NULL。普通版本则不会返回任何结果。 + +## 语法 + +```sql +EXPLODE_JSON_ARRAY_STRING() +EXPLODE_JSON_ARRAY_STRING_OUTER() +``` + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回整数列。 + +## 举例 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING(json_array) tmp1 AS e1 +WHERE id = 3; +``` + +```text ++------+--------+ +| id | e1 | ++------+--------+ +| 3 | apple | +| 3 | banana | +| 3 | cherry | ++------+--------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING(json_array) tmp1 AS e1 +WHERE id = 6; +Empty set (0.02 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING_OUTER(json_array) tmp1 AS e1 +WHERE id = 6; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 6 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-object-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-object-outer.md deleted file mode 100644 index 1f4274af4167a..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-object-outer.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -{ -"title": "explode_json_object_outer", -"language": "zh-CN" -} ---- - - - -## Description - -The table function is used in conjunction with Lateral View and can support multiple Lateral Views. It only supports 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. - -## Syntax -```sql -posexplode(array) -posexplode_outer(array) -``` - -### Example - -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ -``` - -### Keywords -POSEXPLODE,POSEXPLODE_OUTER diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-object.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-object.md index ac6d10e04ae80..bb48ec8d33e08 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-object.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-json-object.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_object", +"title": "EXPLODE_JSON_OBJECT", "language": "zh-CN" } --- @@ -24,74 +24,95 @@ 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. +`explode_json_object` 将 JSON 对象展开为多行,每行包含一个键值对。通常用于处理 JSON 数据,将 JSON 对象展开为更易查询的格式。该函数只支持包含元素的 JSON 对象。 -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. +`explode_json_object_outer` 与 `explode_json_object` 类似,但在处理空值和 NULL 值时有不同的行为。它可以保留空的或 NULL 的 JSON 对象,返回相应的记录。 -## Syntax +## 语法 ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_OBJECT() +EXPLODE_JSON_OBJECT_OUTER() ``` -### Example +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +当 JSON 对象不为空或 NULL 时,`explode_json_object` 和 `explode_json_object_outer` 的返回值相同。每个键值对生成一行,键作为列之一,值作为另一个列。 + +当 JSON 对象为空或 NULL 时: + +`explode_json_object` 不会返回任何行。 + +`explode_json_object_outer` 会返回一行,展开列的值为 NULL。 + +## 举例 + +```sql +CREATE TABLE example ( + id INT, + value_json json +) DUPLICATE KEY(id) +DISTRIBUTED BY HASH(`id`) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +INSERT INTO example VALUES +(1, '{"key1": "value1", "key2": "value2"}'), +(2, '{}'), +(3, NULL); +``` + +```sql +select * from example; +``` + +```text ++------+-----------------------------------+ +| id | value_json | ++------+-----------------------------------+ +| 2 | {} | +| 1 | {"key1":"value1","key2":"value2"} | +| 3 | NULL | ++------+-----------------------------------+ +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object(value_json) exploded_table AS k , v; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+----------+ +| id | k | v | ++------+------+----------+ +| 1 | key1 | "value1" | +| 1 | key2 | "value2" | ++------+------+----------+ +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object_outer(value_json) exploded_table AS k, v; +``` + +```text ++------+------+----------+ +| id | k | v | ++------+------+----------+ +| 3 | NULL | NULL | +| 1 | key1 | "value1" | +| 1 | key2 | "value2" | +| 2 | NULL | NULL | ++------+------+----------+ +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-map-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-map-outer.md deleted file mode 100644 index 812ae38e85c03..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-map-outer.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -{ -"title": "explode_map_outer", -"language": "zh-CN" -} ---- - - - -## Description - -The table function is used in conjunction with Lateral View and can support multiple Lateral Views. It only supports 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. - -## Syntax -```sql -posexplode(array) -posexplode_outer(array) -``` - -### Example - -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ -``` - -### Keywords -POSEXPLODE,POSEXPLODE_OUTER diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-map.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-map.md index 38f6fdfbf8507..b5aa8ada67847 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-map.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-map.md @@ -24,27 +24,46 @@ specific language governing permissions and limitations under the License. --> -## explode_map - ## 描述 -表函数,需配合 Lateral View 使用, 可以支持多个 Lateral view, 仅仅支持新优化器。 +`explode_mpa` 函数接受一个 map (映射类型),将 map(映射类型)展开成多个行,每行包含一个键值对。通常与 LATERAL VIEW 配合使用,可以支持多个 Lateral view。仅支持新优化器。 -将 map 列展开成多行。当 map 为NULL或者为空时,`explode_map_outer` 返回NULL。 -`explode_map` 和 `explode_map_outer` 均会返回 map 内部的NULL元素。 +`explode_map` 和 `explode_map_outer` 区别主要在于空值处理。 ## 语法 ```sql -explode_map(expr) -explode_map_outer(expr) +EXPLODE_MAP(map) +EXPLODE_MAP_OUTER(map) ``` +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `map` | map类型 | + +## 返回值 + +当 map 不为空或 NULL 时,`explode_map` 和 `explode_map_outer` 的返回值相同。 + +当数据为空或 NULL 时: + +`explode_map` 只处理包含元素的 map。如果 map 是空的或为 NULL,explode_map 不会返回任何行。 + +`explode_map_outer` 如果 map 是空的或为 NULL,会保留空 map 或 NULL 的记录,返回的行将包含 NULL 值。 + ## 举例 -```mysql> SET enable_nereids_planner=true -mysql> SET enable_fallback_to_original_planner=false +```sql +SET enable_nereids_planner=true +``` + +```sql +SET enable_fallback_to_original_planner=false +``` -mysql> CREATE TABLE IF NOT EXISTS `sdu`( +```sql +CREATE TABLE IF NOT EXISTS `sdu`( `id` INT NULL, `name` TEXT NULL, `score` MAP NULL @@ -54,12 +73,19 @@ mysql> CREATE TABLE IF NOT EXISTS `sdu`( DISTRIBUTED BY HASH(`id`) BUCKETS 1 PROPERTIES ("replication_allocation" = "tag.location.default: 1"); Query OK, 0 rows affected (0.15 sec) +``` -mysql> insert into sdu values (0, "zhangsan", {"Chinese":"80","Math":"60","English":"90"}), (1, "lisi", {"null":null}), (2, "wangwu", {"Chinese":"88","Math":"90","English":"96"}), (3, "lisi2", {null:null}), (4, "amory", NULL); +```sql +insert into sdu values (0, "zhangsan", {"Chinese":"80","Math":"60","English":"90"}), (1, "lisi", {"null":null}), (2, "wangwu", {"Chinese":"88","Math":"90","English":"96"}), (3, "lisi2", {null:null}), (4, "amory", NULL); Query OK, 5 rows affected (0.23 sec) {'label':'label_9b35d9d9d59147f5_bffb974881ed2133', 'status':'VISIBLE', 'txnId':'4005'} +``` -mysql> select * from sdu order by id; +```sql +select * from sdu order by id; +``` + +```text +------+----------+-----------------------------------------+ | id | name | score | +------+----------+-----------------------------------------+ @@ -69,8 +95,13 @@ mysql> select * from sdu order by id; | 3 | lisi2 | {null:null} | | 4 | amory | NULL | +------+----------+-----------------------------------------+ +``` -mysql> select name, k,v from sdu lateral view explode_map(score) tmp as k,v; +```sql +select name, k,v from sdu lateral view explode_map(score) tmp as k,v; +``` + +```text +----------+---------+------+ | name | k | v | +----------+---------+------+ @@ -83,8 +114,13 @@ mysql> select name, k,v from sdu lateral view explode_map(score) tmp as k,v; | wangwu | English | 96 | | lisi2 | NULL | NULL | +----------+---------+------+ +``` -mysql> select name, k,v from sdu lateral view explode_map_outer(score) tmp as k,v; +```sql +select name, k,v from sdu lateral view explode_map_outer(score) tmp as k,v; +``` + +```text +----------+---------+------+ | name | k | v | +----------+---------+------+ @@ -98,8 +134,13 @@ mysql> select name, k,v from sdu lateral view explode_map_outer(score) tmp as k, | lisi2 | NULL | NULL | | amory | NULL | NULL | +----------+---------+------+ +``` + +```sql +select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp as k,v lateral view explode_map(score) tmp2 as k1,v1; +``` -mysql> select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp as k,v lateral view explode_map(score) tmp2 as k1,v1; +```text +----------+---------+------+---------+------+ | name | k | v | k1 | v1 | +----------+---------+------+---------+------+ @@ -124,7 +165,4 @@ mysql> select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp | wangwu | English | 96 | English | 96 | | lisi2 | NULL | NULL | NULL | NULL | +----------+---------+------+---------+------+ -``` - -### keywords -EXPLODE_MAP,EXPLODE_MAP_OUTER,MAP +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-numbers-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-numbers-outer.md deleted file mode 100644 index 6f1a12a2c9990..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-numbers-outer.md +++ /dev/null @@ -1,50 +0,0 @@ ---- -{ - "title": "Outer 组合器", - "language": "zh-CN" -} ---- - - - -## outer组合器 - -## 描述 - -在table function的函数名后面添加`_outer`后缀使得函数行为从`non-outer`变为`outer`,在表函数生成0行数据时添加一行`Null`数据。 -## 语法 -`explode_numbers(INT x)` - -## 举例 - -``` -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(0) tmp1 as e1; -Empty set - -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers_outer(0) tmp1 as e1; -+------+ -| e1 | -+------+ -| NULL | -+------+ -``` -### keywords - - outer diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-numbers.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-numbers.md index 70fe18f1d647a..b7ce2fe06feb3 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-numbers.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-numbers.md @@ -24,21 +24,38 @@ specific language governing permissions and limitations under the License. --> -## explode_numbers - ## 描述 -表函数,需配合 Lateral View 使用。 - -获得一个[0,n)的序列。 + `explode_numbers` 表函数,接受一个整数 n ,将范围的所有数字展开为多行,每行一个数字。常用于生成连续数字的序列,配合 LATERAL VIEW 使用。 + + `explode_numbers_outer` 与 `explode_numbers` 不同的是,会在表函数生成0行数据时添加一行`Null`数据。 ## 语法 -`explode_numbers(n)` +```sql +EXPLODE_NUMBERS() +EXPLODE_NUMBERS_OUTER() +``` + + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | 整数类型 | + +## 返回值 + +返回一个[0,n)的序列 + +- 当为 0 或者 NULL 时不返回 ## 举例 +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as e1; ``` -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as e1; + +```text +------+ | e1 | +------+ @@ -49,6 +66,20 @@ mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as | 4 | +------+ ``` -### keywords -explode,numbers,explode_numbers +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers(0) tmp1 as e1; +Empty set +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers_outer(0) tmp1 as e1; +``` + +```text ++------+ +| e1 | ++------+ +| NULL | ++------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-split-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-split-outer.md deleted file mode 100644 index a2a0b53fc00d2..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-split-outer.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -{ - "title": "EXPLODE_SPLIT_OUTER", - "language": "zh-CN" -} ---- - - diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-split.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-split.md index 18dc42538da30..d1421105cf908 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-split.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-split.md @@ -24,23 +24,36 @@ specific language governing permissions and limitations under the License. --> -## explode_split - ## 描述 -表函数,需配合 Lateral View 使用。 +`explode_split` 表函数用于将字符串按照指定分隔符拆分为多个子字符串,并将每个子字符串展开为一行。每个子字符串作为单独的行返回,通常与 LATERAL VIEW 一起使用,便于将长字符串拆解为单独的部分,进行更细粒度的查询。 -将一个字符串按指定的分隔符分割成多个子串。 +`explode_split_outer` 与 `explode_split` 类似。但与 `explode_split` 不同的是,它在处理空值和 NULL 值时会有不同的行为,能够处理空的或 NULL 的字符串。 ## 语法 -`explode_split(str, delimiter)` +```sql +EXPLODE_SPLIT(, ) +EXPLODE_SPLIT_OUTER(, ) +``` -## 举例 +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | 字符串类型 | +| `` | 分割符 | + +## 返回值 -原表数据: +返回拆分后的子字符串序列。如果字符串为空或 NULL,不返回任何行。 +## 举例 + +```sql +select * from example1 order by k1; ``` -mysql> select * from example1 order by k1; + +```text +------+---------+ | k1 | k2 | +------+---------+ @@ -53,34 +66,52 @@ mysql> select * from example1 order by k1; +------+---------+ ``` -Lateral View: - +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 1 order by k1, e1; ``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 1 order by k1, e1; + +```text +------+------+ | k1 | e1 | +------+------+ | 1 | | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 2 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 2 order by k1, e1; Empty set +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 3 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 3 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ | 3 | | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 4 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 4 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ | 4 | 1 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 5 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 5 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -88,8 +119,13 @@ mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e | 5 | 3 | | 5 | 1 | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 6 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 6 order by k1, e1; +``` + +```text +------+------+ | k1 | e1 | +------+------+ @@ -99,6 +135,33 @@ mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e +------+------+ ``` -### keywords +```sql +CREATE TABLE example2 ( + id INT, + str string null +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(`id`) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example2 values (1,''),(2,NUll),(3,"1"),(4,"1,2,3"),(5,"a,b,c"); +``` + +```sql +select id, e1 from example2 lateral view explode_split(str, ',') tmp1 as e1 where id = 2 order by id, e1; +Empty set (0.02 sec) +``` + +```sql +select id, e1 from example2 lateral view explode_split_outer(str, ',') tmp1 as e1 where id = 2 order by id, e1; +``` -explode,split,explode_split +```text ++------+------+ +| id | e1 | ++------+------+ +| 2 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-variant-array.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-variant-array.md index 9215aab847640..cf113ec3b6c31 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-variant-array.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode-variant-array.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-json", +"title": "EXPLODE_VARIANT_ARRAY", "language": "zh-CN" } --- @@ -24,74 +24,88 @@ 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. +`explode_variant_array` 表函数,接受一个 variant 类型,其中每个元素是 JSON 对象类型,将该 json 数组中的每个 json 对象展开为多行,每行包含一个 JSON 对象。配合 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. +## 语法 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_VARIANT_ARRAY() ``` -### Example +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | variant 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回 JSON 对象列。 + +## 举例 + +```sql +CREATE TABLE `simple_nested_test` ( + `k` bigint NULL, + `v` variant NULL +) ENGINE=OLAP +DUPLICATE KEY(`k`) +DISTRIBUTED BY HASH(`k`) BUCKETS 8 +PROPERTIES ( +"file_cache_ttl_seconds" = "0", +"is_being_synced" = "false", +"storage_medium" = "hdd", +"storage_format" = "V2", +"inverted_index_storage_format" = "V2", +"light_schema_change" = "true", +"disable_auto_compaction" = "false", +"variant_enable_flatten_nested" = "true", +"enable_single_replica_compaction" = "false", +"group_commit_interval_ms" = "10000", +"group_commit_data_bytes" = "134217728" +); +``` + +```sql +insert into simple_nested_test values(1, '{ + "eventId": 1, + "firstName": "Name1", + "lastName": "Eric", + "body": { + "phoneNumbers": [ + { + "number": "1111111111", + "type": "GSM", + "callLimit": 5 + }, + { + "number": "222222222", + "type": "HOME", + "callLimit": 3 + }, + { + "number": "33333333", + "callLimit": 2, + "type": "WORK" + } + ] + } +}'); +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +select v['eventId'], phone_numbers + from simple_nested_test lateral view explode_variant_array(v['body']['phoneNumbers']) tmp1 as phone_numbers + where phone_numbers['type'] = 'GSM' OR phone_numbers['type'] = 'HOME' and phone_numbers['callLimit'] > 2; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++--------------------------+----------------------------------------------------+ +| element_at(v, 'eventId') | phone_numbers | ++--------------------------+----------------------------------------------------+ +| 1 | {"callLimit":5,"number":"1111111111","type":"GSM"} | +| 1 | {"callLimit":3,"number":"222222222","type":"HOME"} | ++--------------------------+----------------------------------------------------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode.md index a719435545331..1764b32e01ee0 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/table-functions/explode.md @@ -24,25 +24,39 @@ specific language governing permissions and limitations under the License. --> -## explode - ## 描述 -表函数,需配合 Lateral View 使用。 - -将 array 列展开成多行。当 array 为NULL或者为空时,`explode_outer` 返回NULL。 -`explode` 和 `explode_outer` 均会返回 array 内部的NULL元素。 +`explode` 函数接受一个数组,会将数组的每个元素映射为单独的行。通常与 LATERAL VIEW 配合使用,以将嵌套数据结构展开为标准的平面表格式。`explode` 和 `explode_outer` 区别主要在于空值处理。 ## 语法 ```sql -explode(expr) -explode_outer(expr) +EXPLODE() +EXPLODE_OUTER() ``` +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | array类型 | + +## 返回值 + +当数组不为空或 NULL 时,`explode` 和 `explode_outer` 的返回值相同。 + +当数据为空或 NULL 时: + +`explode` 不会产生任何行,并且会过滤掉这些记录。 + +`explode_outer` 如果数组是空的,explode_outer 会生成一行记录,但展开的列值会是 NULL。如果数组为 NULL,同样会保留一行,并返回 NULL。 + ## 举例 ``` -mysql> select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e1; +select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e1; +``` + +```text +------+ | e1 | +------+ @@ -50,18 +64,30 @@ mysql> select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e | 2 | | 3 | +------+ +``` -mysql> select e1 from (select 1 k1) as t lateral view explode_outer(null) tmp1 as e1; +```sql +select e1 from (select 1 k1) as t lateral view explode_outer(null) tmp1 as e1; +``` + +``` text +------+ | e1 | +------+ | NULL | +------+ +``` -mysql> select e1 from (select 1 k1) as t lateral view explode([]) tmp1 as e1; +```sql +select e1 from (select 1 k1) as t lateral view explode([]) tmp1 as e1; Empty set (0.010 sec) +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp1 as e1; +``` -mysql> select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp1 as e1; +```text +------+ | e1 | +------+ @@ -69,8 +95,13 @@ mysql> select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp | 1 | | NULL | +------+ +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null]) tmp1 as e1; +``` -mysql> select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null]) tmp1 as e1; +```text +------+ | e1 | +------+ @@ -78,7 +109,4 @@ mysql> select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null | 1 | | NULL | +------+ -``` - -### keywords -EXPLODE,EXPLODE_OUTER,ARRAY +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.0/sql-manual/sql-functions/table-functions/explode-numbers-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.0/sql-manual/sql-functions/table-functions/explode-numbers-outer.md deleted file mode 100644 index 6f1a12a2c9990..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.0/sql-manual/sql-functions/table-functions/explode-numbers-outer.md +++ /dev/null @@ -1,50 +0,0 @@ ---- -{ - "title": "Outer 组合器", - "language": "zh-CN" -} ---- - - - -## outer组合器 - -## 描述 - -在table function的函数名后面添加`_outer`后缀使得函数行为从`non-outer`变为`outer`,在表函数生成0行数据时添加一行`Null`数据。 -## 语法 -`explode_numbers(INT x)` - -## 举例 - -``` -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(0) tmp1 as e1; -Empty set - -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers_outer(0) tmp1 as e1; -+------+ -| e1 | -+------+ -| NULL | -+------+ -``` -### keywords - - outer diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md deleted file mode 100644 index 98043184a78fa..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -{ - "title": "EXPLODE_BITMAP_OUTER", - "language": "zh-CN" -} ---- - - diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-bitmap.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-bitmap.md index e527c7539ad1b..b68277dc7a0e9 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-bitmap.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-bitmap.md @@ -24,23 +24,49 @@ specific language governing permissions and limitations under the License. --> -## explode_bitmap - ## 描述 -表函数,需配合 Lateral View 使用。 +`explode_bitmap` 表函数,接受一个位图(bitmap)类型的数据,将位图中的每个 bit(位)映射为单独的行。通常用于处理位图数据,将位图中的每个元素展开成单独的记录。需配合 Lateral View 使用。 -展开一个bitmap类型。 +`explode_bitmap_outer` 与 `explode_bitmap` 类似,但在处理空值或 NULL 时,行为有所不同。它允许空位图或 NULL 位图的记录存在,并在返回结果中将空位图或者 NULL 位图展开为 NULL 行。 ## 语法 -`explode_bitmap(bitmap)` + +```sql +EXPLODE_BITMAP() +EXPLODE_BITMAP_OUTER() +``` + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | bitmap 类型 | + +## 返回值 + +返回位图中每一位对应的行,其中每一行包含一个位值。 ## 举例 -原表数据: +```sql +CREATE TABLE example1 ( + k1 INT +)DUPLICATE KEY(k1) +DISTRIBUTED BY HASH(k1) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example1 values(1),(2),(3),(4),(5),(6); +``` +```sql +select k1 from example1 order by k1; ``` -mysql> select k1 from example1 order by k1; + +```text +------+ | k1 | +------+ @@ -53,13 +79,16 @@ mysql> select k1 from example1 order by k1; +------+ ``` -Lateral View: +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_empty()) tmp1 as e1 order by k1, e1; +Empty set +``` +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1")) tmp1 as e1 order by k1, e1; ``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_empty()) tmp1 as e1 order by k1, e1; -Empty set -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -70,8 +99,13 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 5 | 1 | | 6 | 1 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,2")) tmp1 as e1 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,2")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -88,8 +122,13 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 6 | 1 | | 6 | 2 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -106,10 +145,15 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 6 | 1 | | 6 | 1000 | +------+------+ +``` -mysql> select k1, e1, e2 from example1 +```sql +select k1, e1, e2 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 lateral view explode_split("a,b", ",") tmp2 as e2 order by k1, e1, e2; +``` + +```text +------+------+------+ | k1 | e1 | e2 | +------+------+------+ @@ -140,6 +184,47 @@ lateral view explode_split("a,b", ",") tmp2 as e2 order by k1, e1, e2; +------+------+------+ ``` -### keywords +```sql +CREATE TABLE example ( + k1 INT, + v1 bitmap +)DUPLICATE KEY(k1) +DISTRIBUTED BY HASH(k1) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example values(1,to_bitmap('10101')),(2,to_bitmap('0')),(3,to_bitmap(NULL)); +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object(value_json) exploded_table AS k , v; +``` + +```text ++------+-------+ +| k1 | bit | ++------+-------+ +| 2 | 0 | +| 1 | 10101 | ++------+-------+ +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object_outer(value_json) exploded_table AS k, v; +``` -explode,bitmap,explode_bitmap +```text ++------+-------+ +| k1 | bit | ++------+-------+ +| 2 | 0 | +| 1 | 10101 | +| 3 | NULL | ++------+-------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-double.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-double.md index c6e944222142b..24fc924f2cecb 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-double.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-double.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_array_double", +"title": "EXPLODE_JSON_ARRAY_DOUBLE", "language": "zh-CN" } --- @@ -24,74 +24,92 @@ 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. +`explode_json_array_double` 表函数,接受一个 JSON 数组,其中每个元素是双精度浮点数类型,将该 JSON 数组中的每个浮点数展开为多行,每行包含一个浮点数。配合 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. +`explode_json_array_double_outer` 和 `explode_json_array_double` 类似,对于空值或 NULL 的处理不同。 + +如果 JSON 字符串本身为 NULL,`OUTER` 版本会返回一行,且该行中的值为 NULL。普通版本会完全忽略这类记录。 + +如果 JSON 数组为空,`OUTER` 版本会返回一行,且该行的值为 NULL。普通版本则不会返回任何结果。 + +## 语法 + +```sql +EXPLODE_JSON_ARRAY_DOUBLE() +EXPLODE_JSON_ARRAY_DOUBLE_OUTER() +``` + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回双精度浮点数列。 + +## 举例 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE(json_array) tmp1 AS e1 +WHERE id = 2; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 2 | 1.1 | +| 2 | 2.2 | +| 2 | 3.3 | +| 2 | 4.4 | ++------+------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE(json_array) tmp1 AS e1 +WHERE id = 6; +Empty set (0.01 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE_OUTER(json_array) tmp1 AS e1 +WHERE id = 6; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 6 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-int.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-int.md index b8bfb4228be1c..bf4a13d19765a 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-int.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-int.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_array_int", +"title": "EXPLODE_JSON_ARRAY_INT", "language": "zh-CN" } --- @@ -24,74 +24,93 @@ 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. +`explode_json_array_int` 表函数,接受一个 JSON 数组,其中每个元素是整数类型,将该 JSON 数组中的每个整数展开为多行,每行包含一个整数。配合 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. +`explode_json_array_int_outer` 和 `explode_json_array_int` 类似,对于空值或 NULL 的处理不同。 + +如果 JSON 字符串本身为 NULL,`OUTER` 版本会返回一行,且该行中的值为 NULL。普通版本会完全忽略这类记录。 + +如果 JSON 数组为空,`OUTER` 版本会返回一行,且该行的值为 NULL。普通版本则不会返回任何结果。 + +## 语法 + +```sql +EXPLODE_JSON_ARRAY_INT() +EXPLODE_JSON_ARRAY_INT_OUTER() +``` + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回整数列。 + +## 举例 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT(json_array) tmp1 AS e1 +WHERE id = 1; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 1 | 1 | +| 1 | 2 | +| 1 | 3 | +| 1 | 4 | +| 1 | 5 | ++------+------+ ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT(json_array) tmp1 AS e1 +WHERE id = 5; +Empty set (0.01 sec) +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT_OUTER(json_array) tmp1 AS e1 +WHERE id = 5; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 5 | NULL | ++------+------+ +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-json.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-json.md index 9215aab847640..9c5fd073d73f4 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-json.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-json.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-json", +"title": "EXPLODE_JSON_ARRAY_JSON", "language": "zh-CN" } --- @@ -24,74 +24,62 @@ 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. +`explode_json_array_json` 表函数,接受一个 JSON 数组,其中每个元素是 JSON 对象类型,将该 JSON 数组中的每个 JSON 对象展开为多行,每行包含一个 JSON 对象。配合 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. +## 语法 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_JSON() +EXPLODE_JSON_ARRAY_JSON_OUTER() ``` -### Example +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回 JSON 对象列。 + +## 举例 + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_JSON(json_array) tmp1 AS e1 +WHERE id = 4; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+---------+ +| id | e1 | ++------+---------+ +| 4 | {"a":1} | +| 4 | {"b":2} | +| 4 | {"c":3} | ++------+---------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-string.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-string.md index 0eb71d1757299..733fcafd9af7e 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-string.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-string.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-string", +"title": "EXPLODE_JSON_ARRAY_STRING", "language": "zh-CN" } --- @@ -24,74 +24,91 @@ 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. +`explode_json_array_string` 表函数,接受一个 JSON 数组,其中每个元素是字符串类型,将该 JSON 数组中的每个字符串展开为多行,每行包含一个字符串。配合 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. +`explode_json_array_string_outer` 和 `explode_json_array_string` 类似,对于空值或 NULL 的处理不同。 + +如果 JSON 字符串本身为 NULL,`OUTER` 版本会返回一行,且该行中的值为 NULL。普通版本会完全忽略这类记录。 + +如果 JSON 数组为空,`OUTER` 版本会返回一行,且该行的值为 NULL。普通版本则不会返回任何结果。 + +## 语法 + +```sql +EXPLODE_JSON_ARRAY_STRING() +EXPLODE_JSON_ARRAY_STRING_OUTER() +``` + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回整数列。 + +## 举例 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING(json_array) tmp1 AS e1 +WHERE id = 3; +``` + +```text ++------+--------+ +| id | e1 | ++------+--------+ +| 3 | apple | +| 3 | banana | +| 3 | cherry | ++------+--------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING(json_array) tmp1 AS e1 +WHERE id = 6; +Empty set (0.02 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING_OUTER(json_array) tmp1 AS e1 +WHERE id = 6; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 6 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-object-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-object-outer.md deleted file mode 100644 index 1f4274af4167a..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-object-outer.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -{ -"title": "explode_json_object_outer", -"language": "zh-CN" -} ---- - - - -## Description - -The table function is used in conjunction with Lateral View and can support multiple Lateral Views. It only supports 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. - -## Syntax -```sql -posexplode(array) -posexplode_outer(array) -``` - -### Example - -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ -``` - -### Keywords -POSEXPLODE,POSEXPLODE_OUTER diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-object.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-object.md index ac6d10e04ae80..bb48ec8d33e08 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-object.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-object.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_object", +"title": "EXPLODE_JSON_OBJECT", "language": "zh-CN" } --- @@ -24,74 +24,95 @@ 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. +`explode_json_object` 将 JSON 对象展开为多行,每行包含一个键值对。通常用于处理 JSON 数据,将 JSON 对象展开为更易查询的格式。该函数只支持包含元素的 JSON 对象。 -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. +`explode_json_object_outer` 与 `explode_json_object` 类似,但在处理空值和 NULL 值时有不同的行为。它可以保留空的或 NULL 的 JSON 对象,返回相应的记录。 -## Syntax +## 语法 ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_OBJECT() +EXPLODE_JSON_OBJECT_OUTER() ``` -### Example +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +当 JSON 对象不为空或 NULL 时,`explode_json_object` 和 `explode_json_object_outer` 的返回值相同。每个键值对生成一行,键作为列之一,值作为另一个列。 + +当 JSON 对象为空或 NULL 时: + +`explode_json_object` 不会返回任何行。 + +`explode_json_object_outer` 会返回一行,展开列的值为 NULL。 + +## 举例 + +```sql +CREATE TABLE example ( + id INT, + value_json json +) DUPLICATE KEY(id) +DISTRIBUTED BY HASH(`id`) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +INSERT INTO example VALUES +(1, '{"key1": "value1", "key2": "value2"}'), +(2, '{}'), +(3, NULL); +``` + +```sql +select * from example; +``` + +```text ++------+-----------------------------------+ +| id | value_json | ++------+-----------------------------------+ +| 2 | {} | +| 1 | {"key1":"value1","key2":"value2"} | +| 3 | NULL | ++------+-----------------------------------+ +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object(value_json) exploded_table AS k , v; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+----------+ +| id | k | v | ++------+------+----------+ +| 1 | key1 | "value1" | +| 1 | key2 | "value2" | ++------+------+----------+ +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object_outer(value_json) exploded_table AS k, v; +``` + +```text ++------+------+----------+ +| id | k | v | ++------+------+----------+ +| 3 | NULL | NULL | +| 1 | key1 | "value1" | +| 1 | key2 | "value2" | +| 2 | NULL | NULL | ++------+------+----------+ +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-map-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-map-outer.md deleted file mode 100644 index 812ae38e85c03..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-map-outer.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -{ -"title": "explode_map_outer", -"language": "zh-CN" -} ---- - - - -## Description - -The table function is used in conjunction with Lateral View and can support multiple Lateral Views. It only supports 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. - -## Syntax -```sql -posexplode(array) -posexplode_outer(array) -``` - -### Example - -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ -``` - -### Keywords -POSEXPLODE,POSEXPLODE_OUTER diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-map.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-map.md index 38f6fdfbf8507..b5aa8ada67847 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-map.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-map.md @@ -24,27 +24,46 @@ specific language governing permissions and limitations under the License. --> -## explode_map - ## 描述 -表函数,需配合 Lateral View 使用, 可以支持多个 Lateral view, 仅仅支持新优化器。 +`explode_mpa` 函数接受一个 map (映射类型),将 map(映射类型)展开成多个行,每行包含一个键值对。通常与 LATERAL VIEW 配合使用,可以支持多个 Lateral view。仅支持新优化器。 -将 map 列展开成多行。当 map 为NULL或者为空时,`explode_map_outer` 返回NULL。 -`explode_map` 和 `explode_map_outer` 均会返回 map 内部的NULL元素。 +`explode_map` 和 `explode_map_outer` 区别主要在于空值处理。 ## 语法 ```sql -explode_map(expr) -explode_map_outer(expr) +EXPLODE_MAP(map) +EXPLODE_MAP_OUTER(map) ``` +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `map` | map类型 | + +## 返回值 + +当 map 不为空或 NULL 时,`explode_map` 和 `explode_map_outer` 的返回值相同。 + +当数据为空或 NULL 时: + +`explode_map` 只处理包含元素的 map。如果 map 是空的或为 NULL,explode_map 不会返回任何行。 + +`explode_map_outer` 如果 map 是空的或为 NULL,会保留空 map 或 NULL 的记录,返回的行将包含 NULL 值。 + ## 举例 -```mysql> SET enable_nereids_planner=true -mysql> SET enable_fallback_to_original_planner=false +```sql +SET enable_nereids_planner=true +``` + +```sql +SET enable_fallback_to_original_planner=false +``` -mysql> CREATE TABLE IF NOT EXISTS `sdu`( +```sql +CREATE TABLE IF NOT EXISTS `sdu`( `id` INT NULL, `name` TEXT NULL, `score` MAP NULL @@ -54,12 +73,19 @@ mysql> CREATE TABLE IF NOT EXISTS `sdu`( DISTRIBUTED BY HASH(`id`) BUCKETS 1 PROPERTIES ("replication_allocation" = "tag.location.default: 1"); Query OK, 0 rows affected (0.15 sec) +``` -mysql> insert into sdu values (0, "zhangsan", {"Chinese":"80","Math":"60","English":"90"}), (1, "lisi", {"null":null}), (2, "wangwu", {"Chinese":"88","Math":"90","English":"96"}), (3, "lisi2", {null:null}), (4, "amory", NULL); +```sql +insert into sdu values (0, "zhangsan", {"Chinese":"80","Math":"60","English":"90"}), (1, "lisi", {"null":null}), (2, "wangwu", {"Chinese":"88","Math":"90","English":"96"}), (3, "lisi2", {null:null}), (4, "amory", NULL); Query OK, 5 rows affected (0.23 sec) {'label':'label_9b35d9d9d59147f5_bffb974881ed2133', 'status':'VISIBLE', 'txnId':'4005'} +``` -mysql> select * from sdu order by id; +```sql +select * from sdu order by id; +``` + +```text +------+----------+-----------------------------------------+ | id | name | score | +------+----------+-----------------------------------------+ @@ -69,8 +95,13 @@ mysql> select * from sdu order by id; | 3 | lisi2 | {null:null} | | 4 | amory | NULL | +------+----------+-----------------------------------------+ +``` -mysql> select name, k,v from sdu lateral view explode_map(score) tmp as k,v; +```sql +select name, k,v from sdu lateral view explode_map(score) tmp as k,v; +``` + +```text +----------+---------+------+ | name | k | v | +----------+---------+------+ @@ -83,8 +114,13 @@ mysql> select name, k,v from sdu lateral view explode_map(score) tmp as k,v; | wangwu | English | 96 | | lisi2 | NULL | NULL | +----------+---------+------+ +``` -mysql> select name, k,v from sdu lateral view explode_map_outer(score) tmp as k,v; +```sql +select name, k,v from sdu lateral view explode_map_outer(score) tmp as k,v; +``` + +```text +----------+---------+------+ | name | k | v | +----------+---------+------+ @@ -98,8 +134,13 @@ mysql> select name, k,v from sdu lateral view explode_map_outer(score) tmp as k, | lisi2 | NULL | NULL | | amory | NULL | NULL | +----------+---------+------+ +``` + +```sql +select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp as k,v lateral view explode_map(score) tmp2 as k1,v1; +``` -mysql> select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp as k,v lateral view explode_map(score) tmp2 as k1,v1; +```text +----------+---------+------+---------+------+ | name | k | v | k1 | v1 | +----------+---------+------+---------+------+ @@ -124,7 +165,4 @@ mysql> select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp | wangwu | English | 96 | English | 96 | | lisi2 | NULL | NULL | NULL | NULL | +----------+---------+------+---------+------+ -``` - -### keywords -EXPLODE_MAP,EXPLODE_MAP_OUTER,MAP +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-numbers-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-numbers-outer.md deleted file mode 100644 index 6f1a12a2c9990..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-numbers-outer.md +++ /dev/null @@ -1,50 +0,0 @@ ---- -{ - "title": "Outer 组合器", - "language": "zh-CN" -} ---- - - - -## outer组合器 - -## 描述 - -在table function的函数名后面添加`_outer`后缀使得函数行为从`non-outer`变为`outer`,在表函数生成0行数据时添加一行`Null`数据。 -## 语法 -`explode_numbers(INT x)` - -## 举例 - -``` -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(0) tmp1 as e1; -Empty set - -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers_outer(0) tmp1 as e1; -+------+ -| e1 | -+------+ -| NULL | -+------+ -``` -### keywords - - outer diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-numbers.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-numbers.md index 70fe18f1d647a..b7ce2fe06feb3 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-numbers.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-numbers.md @@ -24,21 +24,38 @@ specific language governing permissions and limitations under the License. --> -## explode_numbers - ## 描述 -表函数,需配合 Lateral View 使用。 - -获得一个[0,n)的序列。 + `explode_numbers` 表函数,接受一个整数 n ,将范围的所有数字展开为多行,每行一个数字。常用于生成连续数字的序列,配合 LATERAL VIEW 使用。 + + `explode_numbers_outer` 与 `explode_numbers` 不同的是,会在表函数生成0行数据时添加一行`Null`数据。 ## 语法 -`explode_numbers(n)` +```sql +EXPLODE_NUMBERS() +EXPLODE_NUMBERS_OUTER() +``` + + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | 整数类型 | + +## 返回值 + +返回一个[0,n)的序列 + +- 当为 0 或者 NULL 时不返回 ## 举例 +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as e1; ``` -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as e1; + +```text +------+ | e1 | +------+ @@ -49,6 +66,20 @@ mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as | 4 | +------+ ``` -### keywords -explode,numbers,explode_numbers +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers(0) tmp1 as e1; +Empty set +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers_outer(0) tmp1 as e1; +``` + +```text ++------+ +| e1 | ++------+ +| NULL | ++------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-split-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-split-outer.md deleted file mode 100644 index a2a0b53fc00d2..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-split-outer.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -{ - "title": "EXPLODE_SPLIT_OUTER", - "language": "zh-CN" -} ---- - - diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-split.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-split.md index 18dc42538da30..d1421105cf908 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-split.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-split.md @@ -24,23 +24,36 @@ specific language governing permissions and limitations under the License. --> -## explode_split - ## 描述 -表函数,需配合 Lateral View 使用。 +`explode_split` 表函数用于将字符串按照指定分隔符拆分为多个子字符串,并将每个子字符串展开为一行。每个子字符串作为单独的行返回,通常与 LATERAL VIEW 一起使用,便于将长字符串拆解为单独的部分,进行更细粒度的查询。 -将一个字符串按指定的分隔符分割成多个子串。 +`explode_split_outer` 与 `explode_split` 类似。但与 `explode_split` 不同的是,它在处理空值和 NULL 值时会有不同的行为,能够处理空的或 NULL 的字符串。 ## 语法 -`explode_split(str, delimiter)` +```sql +EXPLODE_SPLIT(, ) +EXPLODE_SPLIT_OUTER(, ) +``` -## 举例 +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | 字符串类型 | +| `` | 分割符 | + +## 返回值 -原表数据: +返回拆分后的子字符串序列。如果字符串为空或 NULL,不返回任何行。 +## 举例 + +```sql +select * from example1 order by k1; ``` -mysql> select * from example1 order by k1; + +```text +------+---------+ | k1 | k2 | +------+---------+ @@ -53,34 +66,52 @@ mysql> select * from example1 order by k1; +------+---------+ ``` -Lateral View: - +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 1 order by k1, e1; ``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 1 order by k1, e1; + +```text +------+------+ | k1 | e1 | +------+------+ | 1 | | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 2 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 2 order by k1, e1; Empty set +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 3 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 3 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ | 3 | | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 4 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 4 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ | 4 | 1 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 5 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 5 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -88,8 +119,13 @@ mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e | 5 | 3 | | 5 | 1 | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 6 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 6 order by k1, e1; +``` + +```text +------+------+ | k1 | e1 | +------+------+ @@ -99,6 +135,33 @@ mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e +------+------+ ``` -### keywords +```sql +CREATE TABLE example2 ( + id INT, + str string null +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(`id`) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example2 values (1,''),(2,NUll),(3,"1"),(4,"1,2,3"),(5,"a,b,c"); +``` + +```sql +select id, e1 from example2 lateral view explode_split(str, ',') tmp1 as e1 where id = 2 order by id, e1; +Empty set (0.02 sec) +``` + +```sql +select id, e1 from example2 lateral view explode_split_outer(str, ',') tmp1 as e1 where id = 2 order by id, e1; +``` -explode,split,explode_split +```text ++------+------+ +| id | e1 | ++------+------+ +| 2 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-variant-array.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-variant-array.md index 9215aab847640..cf113ec3b6c31 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-variant-array.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode-variant-array.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-json", +"title": "EXPLODE_VARIANT_ARRAY", "language": "zh-CN" } --- @@ -24,74 +24,88 @@ 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. +`explode_variant_array` 表函数,接受一个 variant 类型,其中每个元素是 JSON 对象类型,将该 json 数组中的每个 json 对象展开为多行,每行包含一个 JSON 对象。配合 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. +## 语法 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_VARIANT_ARRAY() ``` -### Example +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | variant 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回 JSON 对象列。 + +## 举例 + +```sql +CREATE TABLE `simple_nested_test` ( + `k` bigint NULL, + `v` variant NULL +) ENGINE=OLAP +DUPLICATE KEY(`k`) +DISTRIBUTED BY HASH(`k`) BUCKETS 8 +PROPERTIES ( +"file_cache_ttl_seconds" = "0", +"is_being_synced" = "false", +"storage_medium" = "hdd", +"storage_format" = "V2", +"inverted_index_storage_format" = "V2", +"light_schema_change" = "true", +"disable_auto_compaction" = "false", +"variant_enable_flatten_nested" = "true", +"enable_single_replica_compaction" = "false", +"group_commit_interval_ms" = "10000", +"group_commit_data_bytes" = "134217728" +); +``` + +```sql +insert into simple_nested_test values(1, '{ + "eventId": 1, + "firstName": "Name1", + "lastName": "Eric", + "body": { + "phoneNumbers": [ + { + "number": "1111111111", + "type": "GSM", + "callLimit": 5 + }, + { + "number": "222222222", + "type": "HOME", + "callLimit": 3 + }, + { + "number": "33333333", + "callLimit": 2, + "type": "WORK" + } + ] + } +}'); +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +select v['eventId'], phone_numbers + from simple_nested_test lateral view explode_variant_array(v['body']['phoneNumbers']) tmp1 as phone_numbers + where phone_numbers['type'] = 'GSM' OR phone_numbers['type'] = 'HOME' and phone_numbers['callLimit'] > 2; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++--------------------------+----------------------------------------------------+ +| element_at(v, 'eventId') | phone_numbers | ++--------------------------+----------------------------------------------------+ +| 1 | {"callLimit":5,"number":"1111111111","type":"GSM"} | +| 1 | {"callLimit":3,"number":"222222222","type":"HOME"} | ++--------------------------+----------------------------------------------------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode.md index a719435545331..1764b32e01ee0 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/table-functions/explode.md @@ -24,25 +24,39 @@ specific language governing permissions and limitations under the License. --> -## explode - ## 描述 -表函数,需配合 Lateral View 使用。 - -将 array 列展开成多行。当 array 为NULL或者为空时,`explode_outer` 返回NULL。 -`explode` 和 `explode_outer` 均会返回 array 内部的NULL元素。 +`explode` 函数接受一个数组,会将数组的每个元素映射为单独的行。通常与 LATERAL VIEW 配合使用,以将嵌套数据结构展开为标准的平面表格式。`explode` 和 `explode_outer` 区别主要在于空值处理。 ## 语法 ```sql -explode(expr) -explode_outer(expr) +EXPLODE() +EXPLODE_OUTER() ``` +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | array类型 | + +## 返回值 + +当数组不为空或 NULL 时,`explode` 和 `explode_outer` 的返回值相同。 + +当数据为空或 NULL 时: + +`explode` 不会产生任何行,并且会过滤掉这些记录。 + +`explode_outer` 如果数组是空的,explode_outer 会生成一行记录,但展开的列值会是 NULL。如果数组为 NULL,同样会保留一行,并返回 NULL。 + ## 举例 ``` -mysql> select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e1; +select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e1; +``` + +```text +------+ | e1 | +------+ @@ -50,18 +64,30 @@ mysql> select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e | 2 | | 3 | +------+ +``` -mysql> select e1 from (select 1 k1) as t lateral view explode_outer(null) tmp1 as e1; +```sql +select e1 from (select 1 k1) as t lateral view explode_outer(null) tmp1 as e1; +``` + +``` text +------+ | e1 | +------+ | NULL | +------+ +``` -mysql> select e1 from (select 1 k1) as t lateral view explode([]) tmp1 as e1; +```sql +select e1 from (select 1 k1) as t lateral view explode([]) tmp1 as e1; Empty set (0.010 sec) +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp1 as e1; +``` -mysql> select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp1 as e1; +```text +------+ | e1 | +------+ @@ -69,8 +95,13 @@ mysql> select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp | 1 | | NULL | +------+ +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null]) tmp1 as e1; +``` -mysql> select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null]) tmp1 as e1; +```text +------+ | e1 | +------+ @@ -78,7 +109,4 @@ mysql> select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null | 1 | | NULL | +------+ -``` - -### keywords -EXPLODE,EXPLODE_OUTER,ARRAY +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md deleted file mode 100644 index 98043184a78fa..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -{ - "title": "EXPLODE_BITMAP_OUTER", - "language": "zh-CN" -} ---- - - diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-bitmap.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-bitmap.md index e527c7539ad1b..b68277dc7a0e9 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-bitmap.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-bitmap.md @@ -24,23 +24,49 @@ specific language governing permissions and limitations under the License. --> -## explode_bitmap - ## 描述 -表函数,需配合 Lateral View 使用。 +`explode_bitmap` 表函数,接受一个位图(bitmap)类型的数据,将位图中的每个 bit(位)映射为单独的行。通常用于处理位图数据,将位图中的每个元素展开成单独的记录。需配合 Lateral View 使用。 -展开一个bitmap类型。 +`explode_bitmap_outer` 与 `explode_bitmap` 类似,但在处理空值或 NULL 时,行为有所不同。它允许空位图或 NULL 位图的记录存在,并在返回结果中将空位图或者 NULL 位图展开为 NULL 行。 ## 语法 -`explode_bitmap(bitmap)` + +```sql +EXPLODE_BITMAP() +EXPLODE_BITMAP_OUTER() +``` + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | bitmap 类型 | + +## 返回值 + +返回位图中每一位对应的行,其中每一行包含一个位值。 ## 举例 -原表数据: +```sql +CREATE TABLE example1 ( + k1 INT +)DUPLICATE KEY(k1) +DISTRIBUTED BY HASH(k1) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example1 values(1),(2),(3),(4),(5),(6); +``` +```sql +select k1 from example1 order by k1; ``` -mysql> select k1 from example1 order by k1; + +```text +------+ | k1 | +------+ @@ -53,13 +79,16 @@ mysql> select k1 from example1 order by k1; +------+ ``` -Lateral View: +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_empty()) tmp1 as e1 order by k1, e1; +Empty set +``` +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1")) tmp1 as e1 order by k1, e1; ``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_empty()) tmp1 as e1 order by k1, e1; -Empty set -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -70,8 +99,13 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 5 | 1 | | 6 | 1 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,2")) tmp1 as e1 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,2")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -88,8 +122,13 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 6 | 1 | | 6 | 2 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -106,10 +145,15 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 6 | 1 | | 6 | 1000 | +------+------+ +``` -mysql> select k1, e1, e2 from example1 +```sql +select k1, e1, e2 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 lateral view explode_split("a,b", ",") tmp2 as e2 order by k1, e1, e2; +``` + +```text +------+------+------+ | k1 | e1 | e2 | +------+------+------+ @@ -140,6 +184,47 @@ lateral view explode_split("a,b", ",") tmp2 as e2 order by k1, e1, e2; +------+------+------+ ``` -### keywords +```sql +CREATE TABLE example ( + k1 INT, + v1 bitmap +)DUPLICATE KEY(k1) +DISTRIBUTED BY HASH(k1) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example values(1,to_bitmap('10101')),(2,to_bitmap('0')),(3,to_bitmap(NULL)); +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object(value_json) exploded_table AS k , v; +``` + +```text ++------+-------+ +| k1 | bit | ++------+-------+ +| 2 | 0 | +| 1 | 10101 | ++------+-------+ +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object_outer(value_json) exploded_table AS k, v; +``` -explode,bitmap,explode_bitmap +```text ++------+-------+ +| k1 | bit | ++------+-------+ +| 2 | 0 | +| 1 | 10101 | +| 3 | NULL | ++------+-------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-double.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-double.md index c6e944222142b..24fc924f2cecb 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-double.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-double.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_array_double", +"title": "EXPLODE_JSON_ARRAY_DOUBLE", "language": "zh-CN" } --- @@ -24,74 +24,92 @@ 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. +`explode_json_array_double` 表函数,接受一个 JSON 数组,其中每个元素是双精度浮点数类型,将该 JSON 数组中的每个浮点数展开为多行,每行包含一个浮点数。配合 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. +`explode_json_array_double_outer` 和 `explode_json_array_double` 类似,对于空值或 NULL 的处理不同。 + +如果 JSON 字符串本身为 NULL,`OUTER` 版本会返回一行,且该行中的值为 NULL。普通版本会完全忽略这类记录。 + +如果 JSON 数组为空,`OUTER` 版本会返回一行,且该行的值为 NULL。普通版本则不会返回任何结果。 + +## 语法 + +```sql +EXPLODE_JSON_ARRAY_DOUBLE() +EXPLODE_JSON_ARRAY_DOUBLE_OUTER() +``` + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回双精度浮点数列。 + +## 举例 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE(json_array) tmp1 AS e1 +WHERE id = 2; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 2 | 1.1 | +| 2 | 2.2 | +| 2 | 3.3 | +| 2 | 4.4 | ++------+------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE(json_array) tmp1 AS e1 +WHERE id = 6; +Empty set (0.01 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE_OUTER(json_array) tmp1 AS e1 +WHERE id = 6; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 6 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-int.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-int.md index b8bfb4228be1c..bf4a13d19765a 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-int.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-int.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_array_int", +"title": "EXPLODE_JSON_ARRAY_INT", "language": "zh-CN" } --- @@ -24,74 +24,93 @@ 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. +`explode_json_array_int` 表函数,接受一个 JSON 数组,其中每个元素是整数类型,将该 JSON 数组中的每个整数展开为多行,每行包含一个整数。配合 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. +`explode_json_array_int_outer` 和 `explode_json_array_int` 类似,对于空值或 NULL 的处理不同。 + +如果 JSON 字符串本身为 NULL,`OUTER` 版本会返回一行,且该行中的值为 NULL。普通版本会完全忽略这类记录。 + +如果 JSON 数组为空,`OUTER` 版本会返回一行,且该行的值为 NULL。普通版本则不会返回任何结果。 + +## 语法 + +```sql +EXPLODE_JSON_ARRAY_INT() +EXPLODE_JSON_ARRAY_INT_OUTER() +``` + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回整数列。 + +## 举例 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT(json_array) tmp1 AS e1 +WHERE id = 1; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 1 | 1 | +| 1 | 2 | +| 1 | 3 | +| 1 | 4 | +| 1 | 5 | ++------+------+ ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT(json_array) tmp1 AS e1 +WHERE id = 5; +Empty set (0.01 sec) +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT_OUTER(json_array) tmp1 AS e1 +WHERE id = 5; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 5 | NULL | ++------+------+ +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-json.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-json.md index 9215aab847640..9c5fd073d73f4 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-json.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-json.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-json", +"title": "EXPLODE_JSON_ARRAY_JSON", "language": "zh-CN" } --- @@ -24,74 +24,62 @@ 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. +`explode_json_array_json` 表函数,接受一个 JSON 数组,其中每个元素是 JSON 对象类型,将该 JSON 数组中的每个 JSON 对象展开为多行,每行包含一个 JSON 对象。配合 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. +## 语法 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_JSON() +EXPLODE_JSON_ARRAY_JSON_OUTER() ``` -### Example +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回 JSON 对象列。 + +## 举例 + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_JSON(json_array) tmp1 AS e1 +WHERE id = 4; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+---------+ +| id | e1 | ++------+---------+ +| 4 | {"a":1} | +| 4 | {"b":2} | +| 4 | {"c":3} | ++------+---------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-string.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-string.md index 0eb71d1757299..733fcafd9af7e 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-string.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-string.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-string", +"title": "EXPLODE_JSON_ARRAY_STRING", "language": "zh-CN" } --- @@ -24,74 +24,91 @@ 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. +`explode_json_array_string` 表函数,接受一个 JSON 数组,其中每个元素是字符串类型,将该 JSON 数组中的每个字符串展开为多行,每行包含一个字符串。配合 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. +`explode_json_array_string_outer` 和 `explode_json_array_string` 类似,对于空值或 NULL 的处理不同。 + +如果 JSON 字符串本身为 NULL,`OUTER` 版本会返回一行,且该行中的值为 NULL。普通版本会完全忽略这类记录。 + +如果 JSON 数组为空,`OUTER` 版本会返回一行,且该行的值为 NULL。普通版本则不会返回任何结果。 + +## 语法 + +```sql +EXPLODE_JSON_ARRAY_STRING() +EXPLODE_JSON_ARRAY_STRING_OUTER() +``` + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回整数列。 + +## 举例 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING(json_array) tmp1 AS e1 +WHERE id = 3; +``` + +```text ++------+--------+ +| id | e1 | ++------+--------+ +| 3 | apple | +| 3 | banana | +| 3 | cherry | ++------+--------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING(json_array) tmp1 AS e1 +WHERE id = 6; +Empty set (0.02 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING_OUTER(json_array) tmp1 AS e1 +WHERE id = 6; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 6 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-object-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-object-outer.md deleted file mode 100644 index 1f4274af4167a..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-object-outer.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -{ -"title": "explode_json_object_outer", -"language": "zh-CN" -} ---- - - - -## Description - -The table function is used in conjunction with Lateral View and can support multiple Lateral Views. It only supports 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. - -## Syntax -```sql -posexplode(array) -posexplode_outer(array) -``` - -### Example - -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ -``` - -### Keywords -POSEXPLODE,POSEXPLODE_OUTER diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-object.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-object.md index ac6d10e04ae80..bb48ec8d33e08 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-object.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-object.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_object", +"title": "EXPLODE_JSON_OBJECT", "language": "zh-CN" } --- @@ -24,74 +24,95 @@ 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. +`explode_json_object` 将 JSON 对象展开为多行,每行包含一个键值对。通常用于处理 JSON 数据,将 JSON 对象展开为更易查询的格式。该函数只支持包含元素的 JSON 对象。 -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. +`explode_json_object_outer` 与 `explode_json_object` 类似,但在处理空值和 NULL 值时有不同的行为。它可以保留空的或 NULL 的 JSON 对象,返回相应的记录。 -## Syntax +## 语法 ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_OBJECT() +EXPLODE_JSON_OBJECT_OUTER() ``` -### Example +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | json 类型 | + +## 返回值 + +当 JSON 对象不为空或 NULL 时,`explode_json_object` 和 `explode_json_object_outer` 的返回值相同。每个键值对生成一行,键作为列之一,值作为另一个列。 + +当 JSON 对象为空或 NULL 时: + +`explode_json_object` 不会返回任何行。 + +`explode_json_object_outer` 会返回一行,展开列的值为 NULL。 + +## 举例 + +```sql +CREATE TABLE example ( + id INT, + value_json json +) DUPLICATE KEY(id) +DISTRIBUTED BY HASH(`id`) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +INSERT INTO example VALUES +(1, '{"key1": "value1", "key2": "value2"}'), +(2, '{}'), +(3, NULL); +``` + +```sql +select * from example; +``` + +```text ++------+-----------------------------------+ +| id | value_json | ++------+-----------------------------------+ +| 2 | {} | +| 1 | {"key1":"value1","key2":"value2"} | +| 3 | NULL | ++------+-----------------------------------+ +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object(value_json) exploded_table AS k , v; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+----------+ +| id | k | v | ++------+------+----------+ +| 1 | key1 | "value1" | +| 1 | key2 | "value2" | ++------+------+----------+ +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object_outer(value_json) exploded_table AS k, v; +``` + +```text ++------+------+----------+ +| id | k | v | ++------+------+----------+ +| 3 | NULL | NULL | +| 1 | key1 | "value1" | +| 1 | key2 | "value2" | +| 2 | NULL | NULL | ++------+------+----------+ +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-map-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-map-outer.md deleted file mode 100644 index 812ae38e85c03..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-map-outer.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -{ -"title": "explode_map_outer", -"language": "zh-CN" -} ---- - - - -## Description - -The table function is used in conjunction with Lateral View and can support multiple Lateral Views. It only supports 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. - -## Syntax -```sql -posexplode(array) -posexplode_outer(array) -``` - -### Example - -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ -``` - -### Keywords -POSEXPLODE,POSEXPLODE_OUTER diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-map.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-map.md index 38f6fdfbf8507..b5aa8ada67847 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-map.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-map.md @@ -24,27 +24,46 @@ specific language governing permissions and limitations under the License. --> -## explode_map - ## 描述 -表函数,需配合 Lateral View 使用, 可以支持多个 Lateral view, 仅仅支持新优化器。 +`explode_mpa` 函数接受一个 map (映射类型),将 map(映射类型)展开成多个行,每行包含一个键值对。通常与 LATERAL VIEW 配合使用,可以支持多个 Lateral view。仅支持新优化器。 -将 map 列展开成多行。当 map 为NULL或者为空时,`explode_map_outer` 返回NULL。 -`explode_map` 和 `explode_map_outer` 均会返回 map 内部的NULL元素。 +`explode_map` 和 `explode_map_outer` 区别主要在于空值处理。 ## 语法 ```sql -explode_map(expr) -explode_map_outer(expr) +EXPLODE_MAP(map) +EXPLODE_MAP_OUTER(map) ``` +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `map` | map类型 | + +## 返回值 + +当 map 不为空或 NULL 时,`explode_map` 和 `explode_map_outer` 的返回值相同。 + +当数据为空或 NULL 时: + +`explode_map` 只处理包含元素的 map。如果 map 是空的或为 NULL,explode_map 不会返回任何行。 + +`explode_map_outer` 如果 map 是空的或为 NULL,会保留空 map 或 NULL 的记录,返回的行将包含 NULL 值。 + ## 举例 -```mysql> SET enable_nereids_planner=true -mysql> SET enable_fallback_to_original_planner=false +```sql +SET enable_nereids_planner=true +``` + +```sql +SET enable_fallback_to_original_planner=false +``` -mysql> CREATE TABLE IF NOT EXISTS `sdu`( +```sql +CREATE TABLE IF NOT EXISTS `sdu`( `id` INT NULL, `name` TEXT NULL, `score` MAP NULL @@ -54,12 +73,19 @@ mysql> CREATE TABLE IF NOT EXISTS `sdu`( DISTRIBUTED BY HASH(`id`) BUCKETS 1 PROPERTIES ("replication_allocation" = "tag.location.default: 1"); Query OK, 0 rows affected (0.15 sec) +``` -mysql> insert into sdu values (0, "zhangsan", {"Chinese":"80","Math":"60","English":"90"}), (1, "lisi", {"null":null}), (2, "wangwu", {"Chinese":"88","Math":"90","English":"96"}), (3, "lisi2", {null:null}), (4, "amory", NULL); +```sql +insert into sdu values (0, "zhangsan", {"Chinese":"80","Math":"60","English":"90"}), (1, "lisi", {"null":null}), (2, "wangwu", {"Chinese":"88","Math":"90","English":"96"}), (3, "lisi2", {null:null}), (4, "amory", NULL); Query OK, 5 rows affected (0.23 sec) {'label':'label_9b35d9d9d59147f5_bffb974881ed2133', 'status':'VISIBLE', 'txnId':'4005'} +``` -mysql> select * from sdu order by id; +```sql +select * from sdu order by id; +``` + +```text +------+----------+-----------------------------------------+ | id | name | score | +------+----------+-----------------------------------------+ @@ -69,8 +95,13 @@ mysql> select * from sdu order by id; | 3 | lisi2 | {null:null} | | 4 | amory | NULL | +------+----------+-----------------------------------------+ +``` -mysql> select name, k,v from sdu lateral view explode_map(score) tmp as k,v; +```sql +select name, k,v from sdu lateral view explode_map(score) tmp as k,v; +``` + +```text +----------+---------+------+ | name | k | v | +----------+---------+------+ @@ -83,8 +114,13 @@ mysql> select name, k,v from sdu lateral view explode_map(score) tmp as k,v; | wangwu | English | 96 | | lisi2 | NULL | NULL | +----------+---------+------+ +``` -mysql> select name, k,v from sdu lateral view explode_map_outer(score) tmp as k,v; +```sql +select name, k,v from sdu lateral view explode_map_outer(score) tmp as k,v; +``` + +```text +----------+---------+------+ | name | k | v | +----------+---------+------+ @@ -98,8 +134,13 @@ mysql> select name, k,v from sdu lateral view explode_map_outer(score) tmp as k, | lisi2 | NULL | NULL | | amory | NULL | NULL | +----------+---------+------+ +``` + +```sql +select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp as k,v lateral view explode_map(score) tmp2 as k1,v1; +``` -mysql> select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp as k,v lateral view explode_map(score) tmp2 as k1,v1; +```text +----------+---------+------+---------+------+ | name | k | v | k1 | v1 | +----------+---------+------+---------+------+ @@ -124,7 +165,4 @@ mysql> select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp | wangwu | English | 96 | English | 96 | | lisi2 | NULL | NULL | NULL | NULL | +----------+---------+------+---------+------+ -``` - -### keywords -EXPLODE_MAP,EXPLODE_MAP_OUTER,MAP +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-numbers-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-numbers-outer.md deleted file mode 100644 index 6f1a12a2c9990..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-numbers-outer.md +++ /dev/null @@ -1,50 +0,0 @@ ---- -{ - "title": "Outer 组合器", - "language": "zh-CN" -} ---- - - - -## outer组合器 - -## 描述 - -在table function的函数名后面添加`_outer`后缀使得函数行为从`non-outer`变为`outer`,在表函数生成0行数据时添加一行`Null`数据。 -## 语法 -`explode_numbers(INT x)` - -## 举例 - -``` -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(0) tmp1 as e1; -Empty set - -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers_outer(0) tmp1 as e1; -+------+ -| e1 | -+------+ -| NULL | -+------+ -``` -### keywords - - outer diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-numbers.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-numbers.md index 70fe18f1d647a..b7ce2fe06feb3 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-numbers.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-numbers.md @@ -24,21 +24,38 @@ specific language governing permissions and limitations under the License. --> -## explode_numbers - ## 描述 -表函数,需配合 Lateral View 使用。 - -获得一个[0,n)的序列。 + `explode_numbers` 表函数,接受一个整数 n ,将范围的所有数字展开为多行,每行一个数字。常用于生成连续数字的序列,配合 LATERAL VIEW 使用。 + + `explode_numbers_outer` 与 `explode_numbers` 不同的是,会在表函数生成0行数据时添加一行`Null`数据。 ## 语法 -`explode_numbers(n)` +```sql +EXPLODE_NUMBERS() +EXPLODE_NUMBERS_OUTER() +``` + + +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | 整数类型 | + +## 返回值 + +返回一个[0,n)的序列 + +- 当为 0 或者 NULL 时不返回 ## 举例 +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as e1; ``` -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as e1; + +```text +------+ | e1 | +------+ @@ -49,6 +66,20 @@ mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as | 4 | +------+ ``` -### keywords -explode,numbers,explode_numbers +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers(0) tmp1 as e1; +Empty set +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers_outer(0) tmp1 as e1; +``` + +```text ++------+ +| e1 | ++------+ +| NULL | ++------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-split-outer.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-split-outer.md deleted file mode 100644 index a2a0b53fc00d2..0000000000000 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-split-outer.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -{ - "title": "EXPLODE_SPLIT_OUTER", - "language": "zh-CN" -} ---- - - diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-split.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-split.md index 18dc42538da30..d1421105cf908 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-split.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-split.md @@ -24,23 +24,36 @@ specific language governing permissions and limitations under the License. --> -## explode_split - ## 描述 -表函数,需配合 Lateral View 使用。 +`explode_split` 表函数用于将字符串按照指定分隔符拆分为多个子字符串,并将每个子字符串展开为一行。每个子字符串作为单独的行返回,通常与 LATERAL VIEW 一起使用,便于将长字符串拆解为单独的部分,进行更细粒度的查询。 -将一个字符串按指定的分隔符分割成多个子串。 +`explode_split_outer` 与 `explode_split` 类似。但与 `explode_split` 不同的是,它在处理空值和 NULL 值时会有不同的行为,能够处理空的或 NULL 的字符串。 ## 语法 -`explode_split(str, delimiter)` +```sql +EXPLODE_SPLIT(, ) +EXPLODE_SPLIT_OUTER(, ) +``` -## 举例 +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | 字符串类型 | +| `` | 分割符 | + +## 返回值 -原表数据: +返回拆分后的子字符串序列。如果字符串为空或 NULL,不返回任何行。 +## 举例 + +```sql +select * from example1 order by k1; ``` -mysql> select * from example1 order by k1; + +```text +------+---------+ | k1 | k2 | +------+---------+ @@ -53,34 +66,52 @@ mysql> select * from example1 order by k1; +------+---------+ ``` -Lateral View: - +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 1 order by k1, e1; ``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 1 order by k1, e1; + +```text +------+------+ | k1 | e1 | +------+------+ | 1 | | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 2 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 2 order by k1, e1; Empty set +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 3 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 3 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ | 3 | | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 4 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 4 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ | 4 | 1 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 5 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 5 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -88,8 +119,13 @@ mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e | 5 | 3 | | 5 | 1 | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 6 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 6 order by k1, e1; +``` + +```text +------+------+ | k1 | e1 | +------+------+ @@ -99,6 +135,33 @@ mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e +------+------+ ``` -### keywords +```sql +CREATE TABLE example2 ( + id INT, + str string null +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(`id`) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example2 values (1,''),(2,NUll),(3,"1"),(4,"1,2,3"),(5,"a,b,c"); +``` + +```sql +select id, e1 from example2 lateral view explode_split(str, ',') tmp1 as e1 where id = 2 order by id, e1; +Empty set (0.02 sec) +``` + +```sql +select id, e1 from example2 lateral view explode_split_outer(str, ',') tmp1 as e1 where id = 2 order by id, e1; +``` -explode,split,explode_split +```text ++------+------+ +| id | e1 | ++------+------+ +| 2 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-variant-array.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-variant-array.md index 9215aab847640..cf113ec3b6c31 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-variant-array.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode-variant-array.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-json", +"title": "EXPLODE_VARIANT_ARRAY", "language": "zh-CN" } --- @@ -24,74 +24,88 @@ 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. +`explode_variant_array` 表函数,接受一个 variant 类型,其中每个元素是 JSON 对象类型,将该 json 数组中的每个 json 对象展开为多行,每行包含一个 JSON 对象。配合 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. +## 语法 -## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_VARIANT_ARRAY() ``` -### Example +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | variant 类型 | + +## 返回值 + +展开 JSON 数组,每个元素生成一行,返回 JSON 对象列。 + +## 举例 + +```sql +CREATE TABLE `simple_nested_test` ( + `k` bigint NULL, + `v` variant NULL +) ENGINE=OLAP +DUPLICATE KEY(`k`) +DISTRIBUTED BY HASH(`k`) BUCKETS 8 +PROPERTIES ( +"file_cache_ttl_seconds" = "0", +"is_being_synced" = "false", +"storage_medium" = "hdd", +"storage_format" = "V2", +"inverted_index_storage_format" = "V2", +"light_schema_change" = "true", +"disable_auto_compaction" = "false", +"variant_enable_flatten_nested" = "true", +"enable_single_replica_compaction" = "false", +"group_commit_interval_ms" = "10000", +"group_commit_data_bytes" = "134217728" +); +``` + +```sql +insert into simple_nested_test values(1, '{ + "eventId": 1, + "firstName": "Name1", + "lastName": "Eric", + "body": { + "phoneNumbers": [ + { + "number": "1111111111", + "type": "GSM", + "callLimit": 5 + }, + { + "number": "222222222", + "type": "HOME", + "callLimit": 3 + }, + { + "number": "33333333", + "callLimit": 2, + "type": "WORK" + } + ] + } +}'); +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +select v['eventId'], phone_numbers + from simple_nested_test lateral view explode_variant_array(v['body']['phoneNumbers']) tmp1 as phone_numbers + where phone_numbers['type'] = 'GSM' OR phone_numbers['type'] = 'HOME' and phone_numbers['callLimit'] > 2; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++--------------------------+----------------------------------------------------+ +| element_at(v, 'eventId') | phone_numbers | ++--------------------------+----------------------------------------------------+ +| 1 | {"callLimit":5,"number":"1111111111","type":"GSM"} | +| 1 | {"callLimit":3,"number":"222222222","type":"HOME"} | ++--------------------------+----------------------------------------------------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode.md index a719435545331..1764b32e01ee0 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/table-functions/explode.md @@ -24,25 +24,39 @@ specific language governing permissions and limitations under the License. --> -## explode - ## 描述 -表函数,需配合 Lateral View 使用。 - -将 array 列展开成多行。当 array 为NULL或者为空时,`explode_outer` 返回NULL。 -`explode` 和 `explode_outer` 均会返回 array 内部的NULL元素。 +`explode` 函数接受一个数组,会将数组的每个元素映射为单独的行。通常与 LATERAL VIEW 配合使用,以将嵌套数据结构展开为标准的平面表格式。`explode` 和 `explode_outer` 区别主要在于空值处理。 ## 语法 ```sql -explode(expr) -explode_outer(expr) +EXPLODE() +EXPLODE_OUTER() ``` +## 参数 + +| 参数 | 说明 | +| -- | -- | +| `` | array类型 | + +## 返回值 + +当数组不为空或 NULL 时,`explode` 和 `explode_outer` 的返回值相同。 + +当数据为空或 NULL 时: + +`explode` 不会产生任何行,并且会过滤掉这些记录。 + +`explode_outer` 如果数组是空的,explode_outer 会生成一行记录,但展开的列值会是 NULL。如果数组为 NULL,同样会保留一行,并返回 NULL。 + ## 举例 ``` -mysql> select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e1; +select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e1; +``` + +```text +------+ | e1 | +------+ @@ -50,18 +64,30 @@ mysql> select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e | 2 | | 3 | +------+ +``` -mysql> select e1 from (select 1 k1) as t lateral view explode_outer(null) tmp1 as e1; +```sql +select e1 from (select 1 k1) as t lateral view explode_outer(null) tmp1 as e1; +``` + +``` text +------+ | e1 | +------+ | NULL | +------+ +``` -mysql> select e1 from (select 1 k1) as t lateral view explode([]) tmp1 as e1; +```sql +select e1 from (select 1 k1) as t lateral view explode([]) tmp1 as e1; Empty set (0.010 sec) +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp1 as e1; +``` -mysql> select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp1 as e1; +```text +------+ | e1 | +------+ @@ -69,8 +95,13 @@ mysql> select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp | 1 | | NULL | +------+ +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null]) tmp1 as e1; +``` -mysql> select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null]) tmp1 as e1; +```text +------+ | e1 | +------+ @@ -78,7 +109,4 @@ mysql> select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null | 1 | | NULL | +------+ -``` - -### keywords -EXPLODE,EXPLODE_OUTER,ARRAY +``` \ No newline at end of file diff --git a/versioned_docs/version-2.0/sql-manual/sql-functions/table-functions/explode-numbers-outer.md b/versioned_docs/version-2.0/sql-manual/sql-functions/table-functions/explode-numbers-outer.md deleted file mode 100644 index c0432e300ef01..0000000000000 --- a/versioned_docs/version-2.0/sql-manual/sql-functions/table-functions/explode-numbers-outer.md +++ /dev/null @@ -1,51 +0,0 @@ ---- -{ - "title": "EXPLODE_NUMBERS_OUTER", - "language": "en" -} ---- - - - -## outer combinator - -### description - -#### syntax -`explode_numbers(INT x)` - -Adding the `_outer` suffix after the function name of the table function changes the function behavior from `non-outer` to `outer`, and adds a row of `Null` data when the table function generates 0 rows of data. - -### example - -``` -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(0) tmp1 as e1; -Empty set - -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers_outer(0) tmp1 as e1; -+------+ -| e1 | -+------+ -| NULL | -+------+ -``` -### keywords - - outer \ No newline at end of file diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md deleted file mode 100644 index 3879e76214407..0000000000000 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -{ - "title": "EXPLODE_BITMAP_OUTER", - "language": "en" -} ---- - - diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-bitmap.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-bitmap.md index be661647d5ce9..3bb7dc69d630f 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-bitmap.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-bitmap.md @@ -24,24 +24,49 @@ specific language governing permissions and limitations under the License. --> -## explode_bitmap +## Description -### description +The `explode_bitmap` table function accepts a bitmap type data and maps each bit (bit) of the bitmap to a separate row. It is commonly used for processing bitmap data, expanding each element of the bitmap into separate records. It should be used in conjunction with LATERAL VIEW. -Table functions must be used in conjunction with Lateral View. +`explode_bitmap_outer` works similarly to `explode_bitmap`, but its behavior differs when handling NULL or empty values. It allows records with empty or NULL bitmaps to exist, and in the result, it expands an empty or NULL bitmap into NULL rows. -Expand a bitmap type. +## Syntax -#### syntax +```sql +EXPLODE_BITMAP() +EXPLODE_BITMAP_OUTER() +``` + +## Parameters + +| Parameter | Description | +| -- | -- | +| `` | bitmap type | + +## Return Value -`explode_bitmap(bitmap)` +Returns a row for each bit in the bitmap, with each row containing a single bit value. -### example +## Examples -Original table data: +```sql +CREATE TABLE example1 ( + k1 INT +)DUPLICATE KEY(k1) +DISTRIBUTED BY HASH(k1) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example1 values(1),(2),(3),(4),(5),(6); +``` +```sql +select k1 from example1 order by k1; ``` -mysql> select k1 from example1 order by k1; + +```text +------+ | k1 | +------+ @@ -54,22 +79,16 @@ mysql> select k1 from example1 order by k1; +------+ ``` -Lateral View: +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_empty()) tmp1 as e1 order by k1, e1; +Empty set +``` +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1")) tmp1 as e1 order by k1, e1; ``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_empty()) tmp1 as e1 order by k1, e1; -+------+------+ -| k1 | e1 | -+------+------+ -| 1 | NULL | -| 2 | NULL | -| 3 | NULL | -| 4 | NULL | -| 5 | NULL | -| 6 | NULL | -+------+------+ -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -80,8 +99,13 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 5 | 1 | | 6 | 1 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,2")) tmp1 as e1 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,2")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -98,8 +122,13 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 6 | 1 | | 6 | 2 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -116,10 +145,15 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 6 | 1 | | 6 | 1000 | +------+------+ +``` -mysql> select k1, e1, e2 from example1 +```sql +select k1, e1, e2 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 lateral view explode_split("a,b", ",") tmp2 as e2 order by k1, e1, e2; +``` + +```text +------+------+------+ | k1 | e1 | e2 | +------+------+------+ @@ -150,6 +184,47 @@ lateral view explode_split("a,b", ",") tmp2 as e2 order by k1, e1, e2; +------+------+------+ ``` -### keywords +```sql +CREATE TABLE example ( + k1 INT, + v1 bitmap +)DUPLICATE KEY(k1) +DISTRIBUTED BY HASH(k1) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example values(1,to_bitmap('10101')),(2,to_bitmap('0')),(3,to_bitmap(NULL)); +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object(value_json) exploded_table AS k , v; +``` + +```text ++------+-------+ +| k1 | bit | ++------+-------+ +| 2 | 0 | +| 1 | 10101 | ++------+-------+ +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object_outer(value_json) exploded_table AS k, v; +``` -explode,bitmap,explode_bitmap \ No newline at end of file +```text ++------+-------+ +| k1 | bit | ++------+-------+ +| 2 | 0 | +| 1 | 10101 | +| 3 | NULL | ++------+-------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-double.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-double.md index bd998190f284b..b15ff6b0e768a 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-double.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-double.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_array_double", +"title": "EXPLODE_JSON_ARRAY_DOUBLE", "language": "en" } --- @@ -26,72 +26,89 @@ 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 `explode_json_array_double` table function accepts a JSON array, where each element is of double-precision floating-point type, and expands each floating-point number in the array into multiple rows, with each row containing one floating-point number. It is used in conjunction with 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. +`explode_json_array_double_outer` is similar to `explode_json_array_double`, but the handling of NULL values is different. + +If the JSON string itself is NULL, the `OUTER` version will return one row, with the value as NULL. The normal version will completely ignore such records. + +If the JSON array is empty, the `OUTER` version will return one row, with the value as NULL. The normal version will return no results. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_DOUBLE() +EXPLODE_JSON_ARRAY_DOUBLE_OUTER() +``` + +## Return Value + +| Parameter | Description | +| -- | -- | +| `` | json type | + +## Parameters + +Expands the JSON array, creating a row for each element, returning a double-precision floating-point column. + +## Examples + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE(json_array) tmp1 AS e1 +WHERE id = 2; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 2 | 1.1 | +| 2 | 2.2 | +| 2 | 3.3 | +| 2 | 4.4 | ++------+------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE(json_array) tmp1 AS e1 +WHERE id = 6; +Empty set (0.01 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE_OUTER(json_array) tmp1 AS e1 +WHERE id = 6; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 6 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-int.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-int.md index 2bb54023db605..dcdc0a260892d 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-int.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-int.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_array_int", +"title": "EXPLODE_JSON_ARRAY_INT", "language": "en" } --- @@ -26,72 +26,90 @@ 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 `explode_json_array_int` table function accepts a JSON array, where each element is of integer type, and expands each integer in the array into multiple rows, with each row containing one integer. It is used in conjunction with 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. +`explode_json_array_int_outer` is similar to `explode_json_array_int`, but the handling of NULL values is different. + +If the JSON string itself is NULL, the `OUTER` version will return one row, with the value as NULL. The normal version will completely ignore such records. + +If the JSON array is empty, the `OUTER` version will return one row, with the value as NULL. The normal version will return no results. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_INT() +EXPLODE_JSON_ARRAY_INT_OUTER() +``` + +## Return Value + +| Parameter | Description | +| -- | -- | +| `` | json type | + +## Parameters + +Expands the JSON array, creating a row for each element, returning an integer column. + +## Examples + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT(json_array) tmp1 AS e1 +WHERE id = 1; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 1 | 1 | +| 1 | 2 | +| 1 | 3 | +| 1 | 4 | +| 1 | 5 | ++------+------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT(json_array) tmp1 AS e1 +WHERE id = 5; +Empty set (0.01 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT_OUTER(json_array) tmp1 AS e1 +WHERE id = 5; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 5 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-json.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-json.md index 0affab2edadb2..474875c3556bb 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-json.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-json.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-json", +"title": "EXPLODE_JSON_ARRAY_JSON", "language": "en" } --- @@ -26,72 +26,59 @@ 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. - -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. +The `explode_json_array_json` table function accepts a JSON array, where each element is of JSON object type, and expands each JSON object in the array into multiple rows, with each row containing one JSON object. It is used in conjunction with LATERAL VIEW. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_JSON() +EXPLODE_JSON_ARRAY_JSON_OUTER() ``` -### Example +## Return Value -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array NULL - ) ENGINE=OLAP - DUPLICATE KEY(`id`) - COMMENT 'OLAP' - DISTRIBUTED BY HASH(`id`) BUCKETS 1 - PROPERTIES ("replication_allocation" = "tag.location.default: 1"); +| Parameter | Description | +| -- | -- | +| `` | json type | -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); +## Parameters +Expands the JSON array, creating a row for each element, returning a JSON object column. -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 3 | lisi2 | [null] | -| 4 | amory | NULL | -+------+----------+--------------------------------+ +## Examples + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` -mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view posexplode(score) tmp as k,v order by id; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -+------+----------+--------------------------------+------+---------+ +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, '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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_JSON(json_array) tmp1 AS e1 +WHERE id = 4; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+---------+ +| id | e1 | ++------+---------+ +| 4 | {"a":1} | +| 4 | {"b":2} | +| 4 | {"c":3} | ++------+---------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-string.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-string.md index 99910ce04b55a..8d9aa90718ce8 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-string.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-array-string.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-string", +"title": "EXPLODE_JSON_ARRAY_STRING", "language": "en" } --- @@ -26,72 +26,88 @@ 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 `explode_json_array_string` table function accepts a JSON array, where each element is of string type, and expands each string in the array into multiple rows, with each row containing one string. It is used in conjunction with 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. +`explode_json_array_string_outer` is similar to `explode_json_array_string`, but the handling of NULL values is different. + +If the JSON string itself is NULL, the `OUTER` version will return one row, with the value as NULL. The normal version will completely ignore such records. + +If the JSON array is empty, the `OUTER` version will return one row, with the value as NULL. The normal version will return no results. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_STRING() +EXPLODE_JSON_ARRAY_STRING_OUTER() +``` + +## Return Value + +| Parameter | Description | +| -- | -- | +| `` | json type | + +## Parameters + +Expands the JSON array, creating a row for each element, returning a string column. + +## Examples + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING(json_array) tmp1 AS e1 +WHERE id = 3; +``` + +```text ++------+--------+ +| id | e1 | ++------+--------+ +| 3 | apple | +| 3 | banana | +| 3 | cherry | ++------+--------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING(json_array) tmp1 AS e1 +WHERE id = 6; +Empty set (0.02 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING_OUTER(json_array) tmp1 AS e1 +WHERE id = 6; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 6 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-object-outer.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-object-outer.md deleted file mode 100644 index 07742f8162bfb..0000000000000 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-object-outer.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -{ -"title": "explode_json_object_outer", -"language": "en" -} ---- - - - -## Description - -The table function is used in conjunction with Lateral View and can support multiple Lateral Views. It only supports 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. - -## Syntax -```sql -posexplode(array) -posexplode_outer(array) -``` - -### Example - -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ -``` - -### Keywords -POSEXPLODE,POSEXPLODE_OUTER diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-object.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-object.md index 3144b67b3ae03..dd10c2394f505 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-object.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-json-object.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_object", +"title": "EXPLODE_JSON_OBJECT", "language": "en" } --- @@ -26,72 +26,92 @@ 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. +`explode_json_object` expands a JSON object into multiple rows, with each row containing a key-value pair. It is typically used to process JSON data and expand the JSON object into a more queryable format. This function only supports non-empty JSON objects. -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. +`explode_json_object_outer` is similar to `explode_json_object`, but with different behavior when handling empty and NULL values. It can retain empty or NULL JSON objects and return corresponding records. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_OBJECT() +EXPLODE_JSON_OBJECT_OUTER() ``` -### Example +## Parameters + +| Parameter | Description | +| -- | -- | +| `` | json type | + +## Return Value + +When the JSON object is neither empty nor NULL, the return values of `explode_json_object` and `explode_json_object_outer` are the same. Each key-value pair generates one row, with the key as one column and the value as another column. + +When the JSON object is empty or NULL: + +`explode_json_object` will not return any rows. +`explode_json_object_outer` will return one row, with the expanded columns being NULL. + +## Examples + +```sql +CREATE TABLE example ( + id INT, + value_json json +) DUPLICATE KEY(id) +DISTRIBUTED BY HASH(`id`) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +INSERT INTO example VALUES +(1, '{"key1": "value1", "key2": "value2"}'), +(2, '{}'), +(3, NULL); +``` + +```sql +select * from example; +``` + +```text ++------+-----------------------------------+ +| id | value_json | ++------+-----------------------------------+ +| 2 | {} | +| 1 | {"key1":"value1","key2":"value2"} | +| 3 | NULL | ++------+-----------------------------------+ +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object(value_json) exploded_table AS k , v; +``` + +```text ++------+------+----------+ +| id | k | v | ++------+------+----------+ +| 1 | key1 | "value1" | +| 1 | key2 | "value2" | ++------+------+----------+ +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object_outer(value_json) exploded_table AS k, v; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+----------+ +| id | k | v | ++------+------+----------+ +| 3 | NULL | NULL | +| 1 | key1 | "value1" | +| 1 | key2 | "value2" | +| 2 | NULL | NULL | ++------+------+----------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-map-outer.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-map-outer.md deleted file mode 100644 index fd5e9b84b6c4e..0000000000000 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-map-outer.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -{ -"title": "explode_map_outer", -"language": "en" -} ---- - - - -## Description - -The table function is used in conjunction with Lateral View and can support multiple Lateral Views. It only supports 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. - -## Syntax -```sql -posexplode(array) -posexplode_outer(array) -``` - -### Example - -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ -``` - -### Keywords -POSEXPLODE,POSEXPLODE_OUTER diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-map.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-map.md index 0f79573a285aa..820361e5c95f1 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-map.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-map.md @@ -24,26 +24,47 @@ specific language governing permissions and limitations under the License. --> -## explode +## Description -### description +The `explode_map` function takes a map (mapping type) and expands it into multiple rows, with each row containing a key-value pair. It is typically used in conjunction with LATERAL VIEW and can support multiple lateral views. It is supported only by the new optimizer. -Table functions must be used in conjunction with Lateral View, support multi conjunction with Lateral View,support new optimizer only. +The main difference between `explode_map` and `explode_map_outer` lies in the handling of null values. -explode map column to rows. `explode_map_outer` will return NULL, while `map` is NULL or empty. -`explode_map` and `explode_map_outer` both keep the nested NULL elements of map. +## Syntax -#### syntax ```sql -explode_map(expr) -explode_map_outer(expr) +EXPLODE_MAP(map) +EXPLODE_MAP_OUTER(map) ``` -### example -```mysql> SET enable_nereids_planner=true -mysql> SET enable_fallback_to_original_planner=false +## Parameters -mysql> CREATE TABLE IF NOT EXISTS `sdu`( +| Parameter | Description | +| -- | -- | +| `map` | map type | + +## Return Value + +When the map is not empty or NULL, the return values of `explode_map` and `explode_map_outer` are the same. + +When the data is empty or NULL: + +`explode_map` Only processes non-empty map types. If the map is empty or NULL, `explode_map` will not return any rows. +`explode_map_outer` If the map is empty or NULL, explode_map_outer will retain the record with the empty or NULL map and return a row with NULL values. + +## Examples + + +```sql +SET enable_nereids_planner=true +``` + +```sql +SET enable_fallback_to_original_planner=false +``` + +```sql +CREATE TABLE IF NOT EXISTS `sdu`( `id` INT NULL, `name` TEXT NULL, `score` MAP NULL @@ -53,12 +74,19 @@ mysql> CREATE TABLE IF NOT EXISTS `sdu`( DISTRIBUTED BY HASH(`id`) BUCKETS 1 PROPERTIES ("replication_allocation" = "tag.location.default: 1"); Query OK, 0 rows affected (0.15 sec) +``` -mysql> insert into sdu values (0, "zhangsan", {"Chinese":"80","Math":"60","English":"90"}), (1, "lisi", {"null":null}), (2, "wangwu", {"Chinese":"88","Math":"90","English":"96"}), (3, "lisi2", {null:null}), (4, "amory", NULL); +```sql +insert into sdu values (0, "zhangsan", {"Chinese":"80","Math":"60","English":"90"}), (1, "lisi", {"null":null}), (2, "wangwu", {"Chinese":"88","Math":"90","English":"96"}), (3, "lisi2", {null:null}), (4, "amory", NULL); Query OK, 5 rows affected (0.23 sec) {'label':'label_9b35d9d9d59147f5_bffb974881ed2133', 'status':'VISIBLE', 'txnId':'4005'} +``` -mysql> select * from sdu order by id; +```sql +select * from sdu order by id; +``` + +```text +------+----------+-----------------------------------------+ | id | name | score | +------+----------+-----------------------------------------+ @@ -68,8 +96,13 @@ mysql> select * from sdu order by id; | 3 | lisi2 | {null:null} | | 4 | amory | NULL | +------+----------+-----------------------------------------+ +``` -mysql> select name, k,v from sdu lateral view explode_map(score) tmp as k,v; +```sql +select name, k,v from sdu lateral view explode_map(score) tmp as k,v; +``` + +```text +----------+---------+------+ | name | k | v | +----------+---------+------+ @@ -82,8 +115,13 @@ mysql> select name, k,v from sdu lateral view explode_map(score) tmp as k,v; | wangwu | English | 96 | | lisi2 | NULL | NULL | +----------+---------+------+ +``` -mysql> select name, k,v from sdu lateral view explode_map_outer(score) tmp as k,v; +```sql +select name, k,v from sdu lateral view explode_map_outer(score) tmp as k,v; +``` + +```text +----------+---------+------+ | name | k | v | +----------+---------+------+ @@ -97,8 +135,13 @@ mysql> select name, k,v from sdu lateral view explode_map_outer(score) tmp as k, | lisi2 | NULL | NULL | | amory | NULL | NULL | +----------+---------+------+ +``` + +```sql +select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp as k,v lateral view explode_map(score) tmp2 as k1,v1; +``` -mysql> select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp as k,v lateral view explode_map(score) tmp2 as k1,v1; +```text +----------+---------+------+---------+------+ | name | k | v | k1 | v1 | +----------+---------+------+---------+------+ @@ -123,7 +166,4 @@ mysql> select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp | wangwu | English | 96 | English | 96 | | lisi2 | NULL | NULL | NULL | NULL | +----------+---------+------+---------+------+ -``` - -### keywords -EXPLODE_MAP,EXPLODE_MAP_OUTER,MAP \ No newline at end of file +``` \ No newline at end of file diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-numbers-outer.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-numbers-outer.md deleted file mode 100644 index c0432e300ef01..0000000000000 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-numbers-outer.md +++ /dev/null @@ -1,51 +0,0 @@ ---- -{ - "title": "EXPLODE_NUMBERS_OUTER", - "language": "en" -} ---- - - - -## outer combinator - -### description - -#### syntax -`explode_numbers(INT x)` - -Adding the `_outer` suffix after the function name of the table function changes the function behavior from `non-outer` to `outer`, and adds a row of `Null` data when the table function generates 0 rows of data. - -### example - -``` -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(0) tmp1 as e1; -Empty set - -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers_outer(0) tmp1 as e1; -+------+ -| e1 | -+------+ -| NULL | -+------+ -``` -### keywords - - outer \ No newline at end of file diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-numbers.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-numbers.md index 159043713c527..b2df121711306 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-numbers.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-numbers.md @@ -24,21 +24,38 @@ specific language governing permissions and limitations under the License. --> -## explode_numbers +## Description -### description +The `explode_numbers` table function takes an integer n and expands all numbers within the range into multiple rows, each containing a single number. It is commonly used to generate a sequence of consecutive numbers and is often paired with LATERAL VIEW. -Table functions must be used in conjunction with Lateral View. +`explode_numbers_outer`, unlike `explode_numbers`, adds a NULL row when the table function generates zero rows. -Get a number sequence [0,n). +## Syntax -#### syntax +```sql +EXPLODE_NUMBERS() +EXPLODE_NUMBERS_OUTER() +``` + +## Parameters + +| Parameter | Description | +| -- | -- | +| `` | Integer type input | + +## Return Value -`explode_numbers(n)` +Returns a sequence of [0, n). -### example +- Does not return any rows when n is 0 or NULL. + +## Examples + +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as e1; ``` -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as e1; + +```text +------+ | e1 | +------+ @@ -49,6 +66,20 @@ mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as | 4 | +------+ ``` -### keywords -explode,numbers,explode_numbers \ No newline at end of file +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers(0) tmp1 as e1; +Empty set +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers_outer(0) tmp1 as e1; +``` + +```text ++------+ +| e1 | ++------+ +| NULL | ++------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-split-outer.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-split-outer.md deleted file mode 100644 index b0cab53aa6313..0000000000000 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-split-outer.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -{ - "title": "EXPLODE_SPLIT_OUTER", - "language": "en" -} ---- - - diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-split.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-split.md index 1ccf91c40aa56..71869e8564908 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-split.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-split.md @@ -24,29 +24,37 @@ specific language governing permissions and limitations under the License. --> -## explode_split +## Description -### description -#### syntax +The `explode_split` table function is used to split a string into multiple substrings based on a specified delimiter and expand each substring into a separate row. Each substring is returned as an individual row, and it is typically used with LATERAL VIEW to break down long strings into individual parts for more granular queries. -`explode_split(str, delimiter)` +`explode_split_outer` is similar to `explode_split`, but it differs in the way it handles empty or NULL strings. -Table functions must be used in conjunction with Lateral View. +## Syntax -Split a string into multiple substrings according to the specified delimiter. +```sql +EXPLODE_SPLIT(, ) +EXPLODE_SPLIT_OUTER(, ) +``` -grammar: +## Parameters -``` -explode_split(str, delimiter) -``` +| Parameter | Description | +| -- | -- | +| `` | String type | +| `` | Delimiter | + +## Return Value -### example +Returns a sequence of the split substrings. If the string is empty or NULL, no rows are returned. -Original table data: +## Examples +```sql +select * from example1 order by k1; ``` -mysql> select * from example1 order by k1; + +```text +------+---------+ | k1 | k2 | +------+---------+ @@ -59,34 +67,52 @@ mysql> select * from example1 order by k1; +------+---------+ ``` -Lateral View: - +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 1 order by k1, e1; ``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 1 order by k1, e1; + +```text +------+------+ | k1 | e1 | +------+------+ | 1 | | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 2 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 2 order by k1, e1; Empty set +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 3 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 3 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ | 3 | | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 4 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 4 order by k1, e1; +``` + +```text +------+------+ | k1 | e1 | +------+------+ | 4 | 1 | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 5 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 5 order by k1, e1; +``` + +```text +------+------+ | k1 | e1 | +------+------+ @@ -94,8 +120,13 @@ mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e | 5 | 3 | | 5 | 1 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 6 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 6 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -105,6 +136,33 @@ mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e +------+------+ ``` -### keywords +```sql +CREATE TABLE example2 ( + id INT, + str string null +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(`id`) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example2 values (1,''),(2,NUll),(3,"1"),(4,"1,2,3"),(5,"a,b,c"); +``` -explode,split,explode_split \ No newline at end of file +```sql +select id, e1 from example2 lateral view explode_split(str, ',') tmp1 as e1 where id = 2 order by id, e1; +Empty set (0.02 sec) +``` + +```sql +select id, e1 from example2 lateral view explode_split_outer(str, ',') tmp1 as e1 where id = 2 order by id, e1; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 2 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-variant-array.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-variant-array.md index 0affab2edadb2..73d74dd886744 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-variant-array.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode-variant-array.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-json", +"title": "EXPLODE_VARIANT_ARRAY", "language": "en" } --- @@ -26,72 +26,85 @@ 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. - -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. +The `explode_variant_array` table function accepts a variant type, where each element is a JSON object, and expands each JSON object in the array into multiple rows, with each row containing one JSON object. It is used in conjunction with LATERAL VIEW. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_VARIANT_ARRAY() ``` -### Example +## Return Value -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array NULL - ) ENGINE=OLAP - DUPLICATE KEY(`id`) - COMMENT 'OLAP' - DISTRIBUTED BY HASH(`id`) BUCKETS 1 - PROPERTIES ("replication_allocation" = "tag.location.default: 1"); +| Parameter | Description | +| -- | -- | +| `` | variant type | -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); +## Parameters +Expands the JSON array, creating a row for each element, returning a JSON object column. -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 3 | lisi2 | [null] | -| 4 | amory | NULL | -+------+----------+--------------------------------+ +## Examples + +```sql +CREATE TABLE `simple_nested_test` ( + `k` bigint NULL, + `v` variant NULL +) ENGINE=OLAP +DUPLICATE KEY(`k`) +DISTRIBUTED BY HASH(`k`) BUCKETS 8 +PROPERTIES ( +"file_cache_ttl_seconds" = "0", +"is_being_synced" = "false", +"storage_medium" = "hdd", +"storage_format" = "V2", +"inverted_index_storage_format" = "V2", +"light_schema_change" = "true", +"disable_auto_compaction" = "false", +"variant_enable_flatten_nested" = "true", +"enable_single_replica_compaction" = "false", +"group_commit_interval_ms" = "10000", +"group_commit_data_bytes" = "134217728" +); +``` -mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view posexplode(score) tmp as k,v order by id; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -+------+----------+--------------------------------+------+---------+ +```sql +insert into simple_nested_test values(1, '{ + "eventId": 1, + "firstName": "Name1", + "lastName": "Eric", + "body": { + "phoneNumbers": [ + { + "number": "1111111111", + "type": "GSM", + "callLimit": 5 + }, + { + "number": "222222222", + "type": "HOME", + "callLimit": 3 + }, + { + "number": "33333333", + "callLimit": 2, + "type": "WORK" + } + ] + } +}'); +``` -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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +```sql +select v['eventId'], phone_numbers + from simple_nested_test lateral view explode_variant_array(v['body']['phoneNumbers']) tmp1 as phone_numbers + where phone_numbers['type'] = 'GSM' OR phone_numbers['type'] = 'HOME' and phone_numbers['callLimit'] > 2; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++--------------------------+----------------------------------------------------+ +| element_at(v, 'eventId') | phone_numbers | ++--------------------------+----------------------------------------------------+ +| 1 | {"callLimit":5,"number":"1111111111","type":"GSM"} | +| 1 | {"callLimit":3,"number":"222222222","type":"HOME"} | ++--------------------------+----------------------------------------------------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode.md b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode.md index 5d8df439474fc..45382e0afce60 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/table-functions/explode.md @@ -24,24 +24,38 @@ specific language governing permissions and limitations under the License. --> -## explode +## Description -### description +The `explode` function takes an array as input and maps each element of the array to a separate row. It is typically used in conjunction with LATERAL VIEW to flatten nested data structures into a standard tabular format. The main difference between explode and `explode_outer` lies in handling empty values. -Table functions must be used in conjunction with Lateral View. - -explode array column to rows. `explode_outer` will return NULL, while `array` is NULL or empty. -`explode` and `explode_outer` both keep the nested NULL elements of array. - -#### syntax +## Syntax ```sql -explode(expr) -explode_outer(expr) +EXPLODE() +EXPLODE_OUTER() ``` -### example +## Required Parameters + +| Parameter | Description | +| -- | -- | +| `` | Array type | + +## Return Value + +When the array is not empty or NULL, the return values of `explode` and `explode_outer` are the same. + +When the data is empty or NULL: + +`explode` will not produce any rows and will filter out these records. + +`explode_outer` if the array is empty, will generate a single row, but the expanded column value will be NULL. If the array is NULL, it will also retain a row and return NULL. + +## Examples ``` -mysql> select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e1; +select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e1; +``` + +```text +------+ | e1 | +------+ @@ -49,18 +63,30 @@ mysql> select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e | 2 | | 3 | +------+ +``` -mysql> select e1 from (select 1 k1) as t lateral view explode_outer(null) tmp1 as e1; +```sql +select e1 from (select 1 k1) as t lateral view explode_outer(null) tmp1 as e1; +``` + +``` text +------+ | e1 | +------+ | NULL | +------+ +``` -mysql> select e1 from (select 1 k1) as t lateral view explode([]) tmp1 as e1; +```sql +select e1 from (select 1 k1) as t lateral view explode([]) tmp1 as e1; Empty set (0.010 sec) +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp1 as e1; +``` -mysql> select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp1 as e1; +```text +------+ | e1 | +------+ @@ -68,8 +94,13 @@ mysql> select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp | 1 | | NULL | +------+ +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null]) tmp1 as e1; +``` -mysql> select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null]) tmp1 as e1; +```text +------+ | e1 | +------+ @@ -77,7 +108,4 @@ mysql> select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null | 1 | | NULL | +------+ -``` - -### keywords -EXPLODE,EXPLODE_OUTER,ARRAY \ No newline at end of file +``` \ No newline at end of file diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md deleted file mode 100644 index 3879e76214407..0000000000000 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-bitmap-outer.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -{ - "title": "EXPLODE_BITMAP_OUTER", - "language": "en" -} ---- - - diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-bitmap.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-bitmap.md index be661647d5ce9..3bb7dc69d630f 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-bitmap.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-bitmap.md @@ -24,24 +24,49 @@ specific language governing permissions and limitations under the License. --> -## explode_bitmap +## Description -### description +The `explode_bitmap` table function accepts a bitmap type data and maps each bit (bit) of the bitmap to a separate row. It is commonly used for processing bitmap data, expanding each element of the bitmap into separate records. It should be used in conjunction with LATERAL VIEW. -Table functions must be used in conjunction with Lateral View. +`explode_bitmap_outer` works similarly to `explode_bitmap`, but its behavior differs when handling NULL or empty values. It allows records with empty or NULL bitmaps to exist, and in the result, it expands an empty or NULL bitmap into NULL rows. -Expand a bitmap type. +## Syntax -#### syntax +```sql +EXPLODE_BITMAP() +EXPLODE_BITMAP_OUTER() +``` + +## Parameters + +| Parameter | Description | +| -- | -- | +| `` | bitmap type | + +## Return Value -`explode_bitmap(bitmap)` +Returns a row for each bit in the bitmap, with each row containing a single bit value. -### example +## Examples -Original table data: +```sql +CREATE TABLE example1 ( + k1 INT +)DUPLICATE KEY(k1) +DISTRIBUTED BY HASH(k1) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example1 values(1),(2),(3),(4),(5),(6); +``` +```sql +select k1 from example1 order by k1; ``` -mysql> select k1 from example1 order by k1; + +```text +------+ | k1 | +------+ @@ -54,22 +79,16 @@ mysql> select k1 from example1 order by k1; +------+ ``` -Lateral View: +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_empty()) tmp1 as e1 order by k1, e1; +Empty set +``` +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1")) tmp1 as e1 order by k1, e1; ``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_empty()) tmp1 as e1 order by k1, e1; -+------+------+ -| k1 | e1 | -+------+------+ -| 1 | NULL | -| 2 | NULL | -| 3 | NULL | -| 4 | NULL | -| 5 | NULL | -| 6 | NULL | -+------+------+ -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -80,8 +99,13 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 5 | 1 | | 6 | 1 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,2")) tmp1 as e1 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,2")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -98,8 +122,13 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 6 | 1 | | 6 | 2 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -116,10 +145,15 @@ mysql> select k1, e1 from example1 lateral view explode_bitmap(bitmap_from_strin | 6 | 1 | | 6 | 1000 | +------+------+ +``` -mysql> select k1, e1, e2 from example1 +```sql +select k1, e1, e2 from example1 lateral view explode_bitmap(bitmap_from_string("1,1000")) tmp1 as e1 lateral view explode_split("a,b", ",") tmp2 as e2 order by k1, e1, e2; +``` + +```text +------+------+------+ | k1 | e1 | e2 | +------+------+------+ @@ -150,6 +184,47 @@ lateral view explode_split("a,b", ",") tmp2 as e2 order by k1, e1, e2; +------+------+------+ ``` -### keywords +```sql +CREATE TABLE example ( + k1 INT, + v1 bitmap +)DUPLICATE KEY(k1) +DISTRIBUTED BY HASH(k1) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example values(1,to_bitmap('10101')),(2,to_bitmap('0')),(3,to_bitmap(NULL)); +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object(value_json) exploded_table AS k , v; +``` + +```text ++------+-------+ +| k1 | bit | ++------+-------+ +| 2 | 0 | +| 1 | 10101 | ++------+-------+ +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object_outer(value_json) exploded_table AS k, v; +``` -explode,bitmap,explode_bitmap \ No newline at end of file +```text ++------+-------+ +| k1 | bit | ++------+-------+ +| 2 | 0 | +| 1 | 10101 | +| 3 | NULL | ++------+-------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-double.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-double.md index bd998190f284b..b15ff6b0e768a 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-double.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-double.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_array_double", +"title": "EXPLODE_JSON_ARRAY_DOUBLE", "language": "en" } --- @@ -26,72 +26,89 @@ 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 `explode_json_array_double` table function accepts a JSON array, where each element is of double-precision floating-point type, and expands each floating-point number in the array into multiple rows, with each row containing one floating-point number. It is used in conjunction with 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. +`explode_json_array_double_outer` is similar to `explode_json_array_double`, but the handling of NULL values is different. + +If the JSON string itself is NULL, the `OUTER` version will return one row, with the value as NULL. The normal version will completely ignore such records. + +If the JSON array is empty, the `OUTER` version will return one row, with the value as NULL. The normal version will return no results. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_DOUBLE() +EXPLODE_JSON_ARRAY_DOUBLE_OUTER() +``` + +## Return Value + +| Parameter | Description | +| -- | -- | +| `` | json type | + +## Parameters + +Expands the JSON array, creating a row for each element, returning a double-precision floating-point column. + +## Examples + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE(json_array) tmp1 AS e1 +WHERE id = 2; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 2 | 1.1 | +| 2 | 2.2 | +| 2 | 3.3 | +| 2 | 4.4 | ++------+------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE(json_array) tmp1 AS e1 +WHERE id = 6; +Empty set (0.01 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_DOUBLE_OUTER(json_array) tmp1 AS e1 +WHERE id = 6; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 6 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-int.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-int.md index 2bb54023db605..dcdc0a260892d 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-int.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-int.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_array_int", +"title": "EXPLODE_JSON_ARRAY_INT", "language": "en" } --- @@ -26,72 +26,90 @@ 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 `explode_json_array_int` table function accepts a JSON array, where each element is of integer type, and expands each integer in the array into multiple rows, with each row containing one integer. It is used in conjunction with 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. +`explode_json_array_int_outer` is similar to `explode_json_array_int`, but the handling of NULL values is different. + +If the JSON string itself is NULL, the `OUTER` version will return one row, with the value as NULL. The normal version will completely ignore such records. + +If the JSON array is empty, the `OUTER` version will return one row, with the value as NULL. The normal version will return no results. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_INT() +EXPLODE_JSON_ARRAY_INT_OUTER() +``` + +## Return Value + +| Parameter | Description | +| -- | -- | +| `` | json type | + +## Parameters + +Expands the JSON array, creating a row for each element, returning an integer column. + +## Examples + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT(json_array) tmp1 AS e1 +WHERE id = 1; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 1 | 1 | +| 1 | 2 | +| 1 | 3 | +| 1 | 4 | +| 1 | 5 | ++------+------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT(json_array) tmp1 AS e1 +WHERE id = 5; +Empty set (0.01 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_INT_OUTER(json_array) tmp1 AS e1 +WHERE id = 5; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 5 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-json.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-json.md index 0affab2edadb2..474875c3556bb 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-json.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-json.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-json", +"title": "EXPLODE_JSON_ARRAY_JSON", "language": "en" } --- @@ -26,72 +26,59 @@ 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. - -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. +The `explode_json_array_json` table function accepts a JSON array, where each element is of JSON object type, and expands each JSON object in the array into multiple rows, with each row containing one JSON object. It is used in conjunction with LATERAL VIEW. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_JSON() +EXPLODE_JSON_ARRAY_JSON_OUTER() ``` -### Example +## Return Value -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array NULL - ) ENGINE=OLAP - DUPLICATE KEY(`id`) - COMMENT 'OLAP' - DISTRIBUTED BY HASH(`id`) BUCKETS 1 - PROPERTIES ("replication_allocation" = "tag.location.default: 1"); +| Parameter | Description | +| -- | -- | +| `` | json type | -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); +## Parameters +Expands the JSON array, creating a row for each element, returning a JSON object column. -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 3 | lisi2 | [null] | -| 4 | amory | NULL | -+------+----------+--------------------------------+ +## Examples + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` -mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view posexplode(score) tmp as k,v order by id; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -+------+----------+--------------------------------+------+---------+ +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, '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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_JSON(json_array) tmp1 AS e1 +WHERE id = 4; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+---------+ +| id | e1 | ++------+---------+ +| 4 | {"a":1} | +| 4 | {"b":2} | +| 4 | {"c":3} | ++------+---------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-string.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-string.md index 99910ce04b55a..8d9aa90718ce8 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-string.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-array-string.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-string", +"title": "EXPLODE_JSON_ARRAY_STRING", "language": "en" } --- @@ -26,72 +26,88 @@ 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 `explode_json_array_string` table function accepts a JSON array, where each element is of string type, and expands each string in the array into multiple rows, with each row containing one string. It is used in conjunction with 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. +`explode_json_array_string_outer` is similar to `explode_json_array_string`, but the handling of NULL values is different. + +If the JSON string itself is NULL, the `OUTER` version will return one row, with the value as NULL. The normal version will completely ignore such records. + +If the JSON array is empty, the `OUTER` version will return one row, with the value as NULL. The normal version will return no results. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_ARRAY_STRING() +EXPLODE_JSON_ARRAY_STRING_OUTER() +``` + +## Return Value + +| Parameter | Description | +| -- | -- | +| `` | json type | + +## Parameters + +Expands the JSON array, creating a row for each element, returning a string column. + +## Examples + +```sql +CREATE TABLE json_array_example ( + id INT, + json_array STRING +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(id) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); ``` -### Example +```sql +INSERT INTO json_array_example (id, json_array) VALUES +(1, '[1, 2, 3, 4, 5]'), +(2, '[1.1, 2.2, 3.3, 4.4]'), +(3, '["apple", "banana", "cherry"]'), +(4, '[{"a": 1}, {"b": 2}, {"c": 3}]'), +(5, '[]'), +(6, 'NULL'); +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING(json_array) tmp1 AS e1 +WHERE id = 3; +``` + +```text ++------+--------+ +| id | e1 | ++------+--------+ +| 3 | apple | +| 3 | banana | +| 3 | cherry | ++------+--------+ +``` + +```sql +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING(json_array) tmp1 AS e1 +WHERE id = 6; +Empty set (0.02 sec) +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, e1 +FROM json_array_example +LATERAL VIEW EXPLODE_JSON_ARRAY_STRING_OUTER(json_array) tmp1 AS e1 +WHERE id = 6; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+ +| id | e1 | ++------+------+ +| 6 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-object-outer.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-object-outer.md deleted file mode 100644 index 07742f8162bfb..0000000000000 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-object-outer.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -{ -"title": "explode_json_object_outer", -"language": "en" -} ---- - - - -## Description - -The table function is used in conjunction with Lateral View and can support multiple Lateral Views. It only supports 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. - -## Syntax -```sql -posexplode(array) -posexplode_outer(array) -``` - -### Example - -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ -``` - -### Keywords -POSEXPLODE,POSEXPLODE_OUTER diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-object.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-object.md index 3144b67b3ae03..dd10c2394f505 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-object.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-json-object.md @@ -1,6 +1,6 @@ --- { -"title": "explode_json_object", +"title": "EXPLODE_JSON_OBJECT", "language": "en" } --- @@ -26,72 +26,92 @@ 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. +`explode_json_object` expands a JSON object into multiple rows, with each row containing a key-value pair. It is typically used to process JSON data and expand the JSON object into a more queryable format. This function only supports non-empty JSON objects. -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. +`explode_json_object_outer` is similar to `explode_json_object`, but with different behavior when handling empty and NULL values. It can retain empty or NULL JSON objects and return corresponding records. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_JSON_OBJECT() +EXPLODE_JSON_OBJECT_OUTER() ``` -### Example +## Parameters + +| Parameter | Description | +| -- | -- | +| `` | json type | + +## Return Value + +When the JSON object is neither empty nor NULL, the return values of `explode_json_object` and `explode_json_object_outer` are the same. Each key-value pair generates one row, with the key as one column and the value as another column. + +When the JSON object is empty or NULL: + +`explode_json_object` will not return any rows. +`explode_json_object_outer` will return one row, with the expanded columns being NULL. + +## Examples + +```sql +CREATE TABLE example ( + id INT, + value_json json +) DUPLICATE KEY(id) +DISTRIBUTED BY HASH(`id`) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +INSERT INTO example VALUES +(1, '{"key1": "value1", "key2": "value2"}'), +(2, '{}'), +(3, NULL); +``` + +```sql +select * from example; +``` + +```text ++------+-----------------------------------+ +| id | value_json | ++------+-----------------------------------+ +| 2 | {} | +| 1 | {"key1":"value1","key2":"value2"} | +| 3 | NULL | ++------+-----------------------------------+ +``` + +```sql +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object(value_json) exploded_table AS k , v; +``` + +```text ++------+------+----------+ +| id | k | v | ++------+------+----------+ +| 1 | key1 | "value1" | +| 1 | key2 | "value2" | ++------+------+----------+ +``` ```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +SELECT id, k, v +FROM example +LATERAL VIEW explode_json_object_outer(value_json) exploded_table AS k, v; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++------+------+----------+ +| id | k | v | ++------+------+----------+ +| 3 | NULL | NULL | +| 1 | key1 | "value1" | +| 1 | key2 | "value2" | +| 2 | NULL | NULL | ++------+------+----------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-map-outer.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-map-outer.md deleted file mode 100644 index fd5e9b84b6c4e..0000000000000 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-map-outer.md +++ /dev/null @@ -1,97 +0,0 @@ ---- -{ -"title": "explode_map_outer", -"language": "en" -} ---- - - - -## Description - -The table function is used in conjunction with Lateral View and can support multiple Lateral Views. It only supports 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. - -## Syntax -```sql -posexplode(array) -posexplode_outer(array) -``` - -### Example - -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array 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); - - -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ -``` - -### Keywords -POSEXPLODE,POSEXPLODE_OUTER diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-map.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-map.md index 0f79573a285aa..820361e5c95f1 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-map.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-map.md @@ -24,26 +24,47 @@ specific language governing permissions and limitations under the License. --> -## explode +## Description -### description +The `explode_map` function takes a map (mapping type) and expands it into multiple rows, with each row containing a key-value pair. It is typically used in conjunction with LATERAL VIEW and can support multiple lateral views. It is supported only by the new optimizer. -Table functions must be used in conjunction with Lateral View, support multi conjunction with Lateral View,support new optimizer only. +The main difference between `explode_map` and `explode_map_outer` lies in the handling of null values. -explode map column to rows. `explode_map_outer` will return NULL, while `map` is NULL or empty. -`explode_map` and `explode_map_outer` both keep the nested NULL elements of map. +## Syntax -#### syntax ```sql -explode_map(expr) -explode_map_outer(expr) +EXPLODE_MAP(map) +EXPLODE_MAP_OUTER(map) ``` -### example -```mysql> SET enable_nereids_planner=true -mysql> SET enable_fallback_to_original_planner=false +## Parameters -mysql> CREATE TABLE IF NOT EXISTS `sdu`( +| Parameter | Description | +| -- | -- | +| `map` | map type | + +## Return Value + +When the map is not empty or NULL, the return values of `explode_map` and `explode_map_outer` are the same. + +When the data is empty or NULL: + +`explode_map` Only processes non-empty map types. If the map is empty or NULL, `explode_map` will not return any rows. +`explode_map_outer` If the map is empty or NULL, explode_map_outer will retain the record with the empty or NULL map and return a row with NULL values. + +## Examples + + +```sql +SET enable_nereids_planner=true +``` + +```sql +SET enable_fallback_to_original_planner=false +``` + +```sql +CREATE TABLE IF NOT EXISTS `sdu`( `id` INT NULL, `name` TEXT NULL, `score` MAP NULL @@ -53,12 +74,19 @@ mysql> CREATE TABLE IF NOT EXISTS `sdu`( DISTRIBUTED BY HASH(`id`) BUCKETS 1 PROPERTIES ("replication_allocation" = "tag.location.default: 1"); Query OK, 0 rows affected (0.15 sec) +``` -mysql> insert into sdu values (0, "zhangsan", {"Chinese":"80","Math":"60","English":"90"}), (1, "lisi", {"null":null}), (2, "wangwu", {"Chinese":"88","Math":"90","English":"96"}), (3, "lisi2", {null:null}), (4, "amory", NULL); +```sql +insert into sdu values (0, "zhangsan", {"Chinese":"80","Math":"60","English":"90"}), (1, "lisi", {"null":null}), (2, "wangwu", {"Chinese":"88","Math":"90","English":"96"}), (3, "lisi2", {null:null}), (4, "amory", NULL); Query OK, 5 rows affected (0.23 sec) {'label':'label_9b35d9d9d59147f5_bffb974881ed2133', 'status':'VISIBLE', 'txnId':'4005'} +``` -mysql> select * from sdu order by id; +```sql +select * from sdu order by id; +``` + +```text +------+----------+-----------------------------------------+ | id | name | score | +------+----------+-----------------------------------------+ @@ -68,8 +96,13 @@ mysql> select * from sdu order by id; | 3 | lisi2 | {null:null} | | 4 | amory | NULL | +------+----------+-----------------------------------------+ +``` -mysql> select name, k,v from sdu lateral view explode_map(score) tmp as k,v; +```sql +select name, k,v from sdu lateral view explode_map(score) tmp as k,v; +``` + +```text +----------+---------+------+ | name | k | v | +----------+---------+------+ @@ -82,8 +115,13 @@ mysql> select name, k,v from sdu lateral view explode_map(score) tmp as k,v; | wangwu | English | 96 | | lisi2 | NULL | NULL | +----------+---------+------+ +``` -mysql> select name, k,v from sdu lateral view explode_map_outer(score) tmp as k,v; +```sql +select name, k,v from sdu lateral view explode_map_outer(score) tmp as k,v; +``` + +```text +----------+---------+------+ | name | k | v | +----------+---------+------+ @@ -97,8 +135,13 @@ mysql> select name, k,v from sdu lateral view explode_map_outer(score) tmp as k, | lisi2 | NULL | NULL | | amory | NULL | NULL | +----------+---------+------+ +``` + +```sql +select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp as k,v lateral view explode_map(score) tmp2 as k1,v1; +``` -mysql> select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp as k,v lateral view explode_map(score) tmp2 as k1,v1; +```text +----------+---------+------+---------+------+ | name | k | v | k1 | v1 | +----------+---------+------+---------+------+ @@ -123,7 +166,4 @@ mysql> select name, k,v,k1,v1 from sdu lateral view explode_map_outer(score) tmp | wangwu | English | 96 | English | 96 | | lisi2 | NULL | NULL | NULL | NULL | +----------+---------+------+---------+------+ -``` - -### keywords -EXPLODE_MAP,EXPLODE_MAP_OUTER,MAP \ No newline at end of file +``` \ No newline at end of file diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-numbers-outer.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-numbers-outer.md deleted file mode 100644 index c0432e300ef01..0000000000000 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-numbers-outer.md +++ /dev/null @@ -1,51 +0,0 @@ ---- -{ - "title": "EXPLODE_NUMBERS_OUTER", - "language": "en" -} ---- - - - -## outer combinator - -### description - -#### syntax -`explode_numbers(INT x)` - -Adding the `_outer` suffix after the function name of the table function changes the function behavior from `non-outer` to `outer`, and adds a row of `Null` data when the table function generates 0 rows of data. - -### example - -``` -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(0) tmp1 as e1; -Empty set - -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers_outer(0) tmp1 as e1; -+------+ -| e1 | -+------+ -| NULL | -+------+ -``` -### keywords - - outer \ No newline at end of file diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-numbers.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-numbers.md index 159043713c527..b2df121711306 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-numbers.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-numbers.md @@ -24,21 +24,38 @@ specific language governing permissions and limitations under the License. --> -## explode_numbers +## Description -### description +The `explode_numbers` table function takes an integer n and expands all numbers within the range into multiple rows, each containing a single number. It is commonly used to generate a sequence of consecutive numbers and is often paired with LATERAL VIEW. -Table functions must be used in conjunction with Lateral View. +`explode_numbers_outer`, unlike `explode_numbers`, adds a NULL row when the table function generates zero rows. -Get a number sequence [0,n). +## Syntax -#### syntax +```sql +EXPLODE_NUMBERS() +EXPLODE_NUMBERS_OUTER() +``` + +## Parameters + +| Parameter | Description | +| -- | -- | +| `` | Integer type input | + +## Return Value -`explode_numbers(n)` +Returns a sequence of [0, n). -### example +- Does not return any rows when n is 0 or NULL. + +## Examples + +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as e1; ``` -mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as e1; + +```text +------+ | e1 | +------+ @@ -49,6 +66,20 @@ mysql> select e1 from (select 1 k1) as t lateral view explode_numbers(5) tmp1 as | 4 | +------+ ``` -### keywords -explode,numbers,explode_numbers \ No newline at end of file +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers(0) tmp1 as e1; +Empty set +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode_numbers_outer(0) tmp1 as e1; +``` + +```text ++------+ +| e1 | ++------+ +| NULL | ++------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-split-outer.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-split-outer.md deleted file mode 100644 index b0cab53aa6313..0000000000000 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-split-outer.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -{ - "title": "EXPLODE_SPLIT_OUTER", - "language": "en" -} ---- - - diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-split.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-split.md index 1ccf91c40aa56..71869e8564908 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-split.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-split.md @@ -24,29 +24,37 @@ specific language governing permissions and limitations under the License. --> -## explode_split +## Description -### description -#### syntax +The `explode_split` table function is used to split a string into multiple substrings based on a specified delimiter and expand each substring into a separate row. Each substring is returned as an individual row, and it is typically used with LATERAL VIEW to break down long strings into individual parts for more granular queries. -`explode_split(str, delimiter)` +`explode_split_outer` is similar to `explode_split`, but it differs in the way it handles empty or NULL strings. -Table functions must be used in conjunction with Lateral View. +## Syntax -Split a string into multiple substrings according to the specified delimiter. +```sql +EXPLODE_SPLIT(, ) +EXPLODE_SPLIT_OUTER(, ) +``` -grammar: +## Parameters -``` -explode_split(str, delimiter) -``` +| Parameter | Description | +| -- | -- | +| `` | String type | +| `` | Delimiter | + +## Return Value -### example +Returns a sequence of the split substrings. If the string is empty or NULL, no rows are returned. -Original table data: +## Examples +```sql +select * from example1 order by k1; ``` -mysql> select * from example1 order by k1; + +```text +------+---------+ | k1 | k2 | +------+---------+ @@ -59,34 +67,52 @@ mysql> select * from example1 order by k1; +------+---------+ ``` -Lateral View: - +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 1 order by k1, e1; ``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 1 order by k1, e1; + +```text +------+------+ | k1 | e1 | +------+------+ | 1 | | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 2 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 2 order by k1, e1; Empty set +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 3 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 3 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ | 3 | | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 4 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 4 order by k1, e1; +``` + +```text +------+------+ | k1 | e1 | +------+------+ | 4 | 1 | +------+------+ +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 5 order by k1, e1; +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 5 order by k1, e1; +``` + +```text +------+------+ | k1 | e1 | +------+------+ @@ -94,8 +120,13 @@ mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e | 5 | 3 | | 5 | 1 | +------+------+ +``` + +```sql +select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 6 order by k1, e1; +``` -mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e1 where k1 = 6 order by k1, e1; +```text +------+------+ | k1 | e1 | +------+------+ @@ -105,6 +136,33 @@ mysql> select k1, e1 from example1 lateral view explode_split(k2, ',') tmp1 as e +------+------+ ``` -### keywords +```sql +CREATE TABLE example2 ( + id INT, + str string null +)DUPLICATE KEY(id) +DISTRIBUTED BY HASH(`id`) BUCKETS AUTO +PROPERTIES ( +"replication_allocation" = "tag.location.default: 1"); +``` + +```sql +insert into example2 values (1,''),(2,NUll),(3,"1"),(4,"1,2,3"),(5,"a,b,c"); +``` -explode,split,explode_split \ No newline at end of file +```sql +select id, e1 from example2 lateral view explode_split(str, ',') tmp1 as e1 where id = 2 order by id, e1; +Empty set (0.02 sec) +``` + +```sql +select id, e1 from example2 lateral view explode_split_outer(str, ',') tmp1 as e1 where id = 2 order by id, e1; +``` + +```text ++------+------+ +| id | e1 | ++------+------+ +| 2 | NULL | ++------+------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-variant-array.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-variant-array.md index 0affab2edadb2..73d74dd886744 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-variant-array.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode-variant-array.md @@ -1,6 +1,6 @@ --- { -"title": "explode-json-array-json", +"title": "EXPLODE_VARIANT_ARRAY", "language": "en" } --- @@ -26,72 +26,85 @@ 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. - -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. +The `explode_variant_array` table function accepts a variant type, where each element is a JSON object, and expands each JSON object in the array into multiple rows, with each row containing one JSON object. It is used in conjunction with LATERAL VIEW. ## Syntax ```sql -posexplode(array) -posexplode_outer(array) +EXPLODE_VARIANT_ARRAY() ``` -### Example +## Return Value -```sql - CREATE TABLE IF NOT EXISTS `table_test`( - `id` INT NULL, - `name` TEXT NULL, - `score` array NULL - ) ENGINE=OLAP - DUPLICATE KEY(`id`) - COMMENT 'OLAP' - DISTRIBUTED BY HASH(`id`) BUCKETS 1 - PROPERTIES ("replication_allocation" = "tag.location.default: 1"); +| Parameter | Description | +| -- | -- | +| `` | variant type | -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); +## Parameters +Expands the JSON array, creating a row for each element, returning a JSON object column. -mysql [test_query_qa]>select * from table_test order by id; -+------+----------+--------------------------------+ -| id | name | score | -+------+----------+--------------------------------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | -| 1 | lisi | ["null"] | -| 2 | wangwu | ["88a", "90b", "96c"] | -| 3 | lisi2 | [null] | -| 4 | amory | NULL | -+------+----------+--------------------------------+ +## Examples + +```sql +CREATE TABLE `simple_nested_test` ( + `k` bigint NULL, + `v` variant NULL +) ENGINE=OLAP +DUPLICATE KEY(`k`) +DISTRIBUTED BY HASH(`k`) BUCKETS 8 +PROPERTIES ( +"file_cache_ttl_seconds" = "0", +"is_being_synced" = "false", +"storage_medium" = "hdd", +"storage_format" = "V2", +"inverted_index_storage_format" = "V2", +"light_schema_change" = "true", +"disable_auto_compaction" = "false", +"variant_enable_flatten_nested" = "true", +"enable_single_replica_compaction" = "false", +"group_commit_interval_ms" = "10000", +"group_commit_data_bytes" = "134217728" +); +``` -mysql [test_query_qa]>select id,name,score, k,v from table_test lateral view posexplode(score) tmp as k,v order by id; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -+------+----------+--------------------------------+------+---------+ +```sql +insert into simple_nested_test values(1, '{ + "eventId": 1, + "firstName": "Name1", + "lastName": "Eric", + "body": { + "phoneNumbers": [ + { + "number": "1111111111", + "type": "GSM", + "callLimit": 5 + }, + { + "number": "222222222", + "type": "HOME", + "callLimit": 3 + }, + { + "number": "33333333", + "callLimit": 2, + "type": "WORK" + } + ] + } +}'); +``` -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; -+------+----------+--------------------------------+------+---------+ -| id | name | score | k | v | -+------+----------+--------------------------------+------+---------+ -| 0 | zhangsan | ["Chinese", "Math", "English"] | 0 | Chinese | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 1 | Math | -| 0 | zhangsan | ["Chinese", "Math", "English"] | 2 | English | -| 1 | lisi | ["null"] | 0 | null | -| 2 | wangwu | ["88a", "90b", "96c"] | 0 | 88a | -| 2 | wangwu | ["88a", "90b", "96c"] | 1 | 90b | -| 2 | wangwu | ["88a", "90b", "96c"] | 2 | 96c | -| 3 | lisi2 | [null] | 0 | NULL | -| 4 | amory | NULL | NULL | NULL | -+------+----------+--------------------------------+------+---------+ +```sql +select v['eventId'], phone_numbers + from simple_nested_test lateral view explode_variant_array(v['body']['phoneNumbers']) tmp1 as phone_numbers + where phone_numbers['type'] = 'GSM' OR phone_numbers['type'] = 'HOME' and phone_numbers['callLimit'] > 2; ``` -### Keywords -POSEXPLODE,POSEXPLODE_OUTER +```text ++--------------------------+----------------------------------------------------+ +| element_at(v, 'eventId') | phone_numbers | ++--------------------------+----------------------------------------------------+ +| 1 | {"callLimit":5,"number":"1111111111","type":"GSM"} | +| 1 | {"callLimit":3,"number":"222222222","type":"HOME"} | ++--------------------------+----------------------------------------------------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode.md b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode.md index 5d8df439474fc..45382e0afce60 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/table-functions/explode.md @@ -24,24 +24,38 @@ specific language governing permissions and limitations under the License. --> -## explode +## Description -### description +The `explode` function takes an array as input and maps each element of the array to a separate row. It is typically used in conjunction with LATERAL VIEW to flatten nested data structures into a standard tabular format. The main difference between explode and `explode_outer` lies in handling empty values. -Table functions must be used in conjunction with Lateral View. - -explode array column to rows. `explode_outer` will return NULL, while `array` is NULL or empty. -`explode` and `explode_outer` both keep the nested NULL elements of array. - -#### syntax +## Syntax ```sql -explode(expr) -explode_outer(expr) +EXPLODE() +EXPLODE_OUTER() ``` -### example +## Required Parameters + +| Parameter | Description | +| -- | -- | +| `` | Array type | + +## Return Value + +When the array is not empty or NULL, the return values of `explode` and `explode_outer` are the same. + +When the data is empty or NULL: + +`explode` will not produce any rows and will filter out these records. + +`explode_outer` if the array is empty, will generate a single row, but the expanded column value will be NULL. If the array is NULL, it will also retain a row and return NULL. + +## Examples ``` -mysql> select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e1; +select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e1; +``` + +```text +------+ | e1 | +------+ @@ -49,18 +63,30 @@ mysql> select e1 from (select 1 k1) as t lateral view explode([1,2,3]) tmp1 as e | 2 | | 3 | +------+ +``` -mysql> select e1 from (select 1 k1) as t lateral view explode_outer(null) tmp1 as e1; +```sql +select e1 from (select 1 k1) as t lateral view explode_outer(null) tmp1 as e1; +``` + +``` text +------+ | e1 | +------+ | NULL | +------+ +``` -mysql> select e1 from (select 1 k1) as t lateral view explode([]) tmp1 as e1; +```sql +select e1 from (select 1 k1) as t lateral view explode([]) tmp1 as e1; Empty set (0.010 sec) +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp1 as e1; +``` -mysql> select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp1 as e1; +```text +------+ | e1 | +------+ @@ -68,8 +94,13 @@ mysql> select e1 from (select 1 k1) as t lateral view explode([null,1,null]) tmp | 1 | | NULL | +------+ +``` + +```sql +select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null]) tmp1 as e1; +``` -mysql> select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null]) tmp1 as e1; +```text +------+ | e1 | +------+ @@ -77,7 +108,4 @@ mysql> select e1 from (select 1 k1) as t lateral view explode_outer([null,1,null | 1 | | NULL | +------+ -``` - -### keywords -EXPLODE,EXPLODE_OUTER,ARRAY \ No newline at end of file +``` \ No newline at end of file