如何将字符串列拆分为包含 NULL 的行?

How to split a string column into rows including NULLs?

我想将字符串列拆分为包含 NULL 值的行。

我使用以下内容,但是,它没有 return NULL 行。 有没有另一种方法可以在不消除 NULL 行的情况下实现这一目标?

SELECT TRIM(REGEXP_REPLACE(s.value, '[{}_]', ' ')) AS extracted
FROM t1, LATERAL SPLIT_TO_TABLE(val, ',') s

示例数据如下:

WITH t1 AS (
SELECT 'A' AS id, '{Other}' AS val UNION ALL
SELECT 'A' AS id, NULL AS val UNION ALL
SELECT 'A' AS id, '{Other Questions,Missing Document}' AS val UNION ALL
SELECT 'A' AS id, NULL AS val )

期望的输出:

+----+------------------+
| id |    extracted     |
+----+------------------+
| A  | Other            |
| A  |                  |
| A  | Other Questions  |
| A  | Missing Document |
| A  |                  |
+----+------------------+

你可以在table:

的横向分割上放置一个敌我识别器
WITH t1 AS (
SELECT 'A' AS id, '{Other}' AS val UNION ALL
SELECT 'A' AS id, NULL AS val UNION ALL
SELECT 'A' AS id, '{Other Questions,Missing Document}' AS val UNION ALL
SELECT 'A' AS id, NULL AS val )


SELECT TRIM(REGEXP_REPLACE(s.value, '[{}_]', ' ')) AS extracted
FROM t1, LATERAL SPLIT_TO_TABLE(iff(val is null, '', val), ',') s

;

使用 LEFT JOIN:

SELECT TRIM(REGEXP_REPLACE(s.value, '[{}_]', ' ')) AS extracted
FROM t1 LEFT JOIN LATERAL
     SPLIT_TO_TABLE(t1.val, ',') s
     ON 1=1;