【问题标题】:RedShift: Unnest subquery's result on leader is not supportedRedShift:不支持领导者上的 Unnest 子查询结果
【发布时间】:2021-09-11 00:51:33
【问题描述】:

我尝试在 RedShift 中解析 JSON。

我在“输入”列中的字符串是:

[{"desc": "How many people does the video contain?", "name": "Number of People", "type": "dropdown", "values": ["", "Only 1", "2-3", "3+"]}, {"desc": "What is the camera position?", "name": "Movement", "type": "dropdown", "values": ["", "Fixed position", "Moving"]}, {"desc": "From which angle did you shoot the video?", "name": "Shoot Angle", "type": "dropdown", "values": ["", "Frontal recording", "Tight angle: 10-40 degree", "Wide angle: 40-70 degree"]}, {"desc": "From which distance did you shoot the video?", "name": "Distance", "type": "dropdown", "values": ["", "Near/Selfie", "Mid (3-6 ft)", "Far (>6 ft)"]}, {"desc": "What is the video lighting direction?", "name": "Lighting Direction", "type": "dropdown", "values": ["", "Front lit", "Side lit", "Back lit"]}, {"desc": "What is the video background?", "name": "Background", "type": "dropdown", "values": ["", "Outdoors", "In office", "At home", "Plain background"]}, {"desc": "What is the topic in your speech?", "name": "Topic", "type": "dropdown", "values": ["", "Arts and Media", "Business", "Education", "Entertainment", "Food/Eating", "Nutrition", "Healthcare ", "High School Life", "Mental Health", "News", "Technology", "Morals and Ethics", "Phones and Apps", "Sports", "Science"]}]

我的任务是:“JSON 中的每个值、名称、desc 都需要在表中存储为一行”。

示例: id: 1, desc: "视频包含多少人?" id: 2, desc: "相机位置是什么?" 等等

我使用查询:

SELECT c.*, d.desc, d.name, d.values FROM source.table AS c, c.inputs AS d; 

得到一个错误:不允许在“输入”列上导航,因为它不是超级类型

并查询:

SELECT c.*, d.desc, d.name, d.values FROM source.table AS c, JSON_PARSE(c.inputs) AS d;

给我另一个错误:“FROM 中的函数表达式可能不引用相同查询级别的其他关系”

但是当我将测试 JSON 创建为:

CREATE TABLE test_parse_json_super
(
  id smallint,
  details super
);
 
INSERT INTO test_parse_json_super VALUES(1, JSON_PARSE('[{"desc": "How many people does the video contain?", "name": "Number of People", "type": "dropdown", "values": ["", "Only 1", "2-3", "3+"]}, {"desc": "What is the camera position?", "name": "Movement", "type": "dropdown", "values": ["", "Fixed position", "Moving"]}, {"desc": "From which angle did you shoot the video?", "name": "Shoot Angle", "type": "dropdown", "values": ["", "Frontal recording", "Tight angle: 10-40 degree", "Wide angle: 40-70 degree"]}, {"desc": "From which distance did you shoot the video?", "name": "Distance", "type": "dropdown", "values": ["", "Near/Selfie", "Mid (3-6 ft)", "Far (>6 ft)"]}, {"desc": "What is the video lighting direction?", "name": "Lighting Direction", "type": "dropdown", "values": ["", "Front lit", "Side lit", "Back lit"]}, {"desc": "What is the video background?", "name": "Background", "type": "dropdown", "values": ["", "Outdoors", "In office", "At home", "Plain background"]}, {"desc": "What is the topic in your speech?", "name": "Topic", "type": "dropdown", "values": ["", "Arts and Media", "Business", "Education", "Entertainment", "Food/Eating", "Nutrition", "Healthcare ", "High School Life", "Mental Health", "News", "Technology", "Morals and Ethics", "Phones and Apps", "Sports", "Science"]}]'));
                                               

并使用来自官方 RedShift 文档的查询 "SELECT c.*, d.desc, d.name, d.values FROM test_parse_json_super AS c, c.details AS d;" - 它工作正常,来自 JSON 的所有数据都解析到每一行,并且 JSON 是正确的。

如何修复查询以使用我的真实数据?

谢谢。

【问题讨论】:

    标签: amazon-redshift


    【解决方案1】:

    您似乎对子选择的 SUPER 数据类型的概念感到困惑。您不能将字符串强制转换为 super(使用 JSON_PARSE),然后在查询的同一级别将其用作 FROM 子句的源。我不完全了解您的情况,但我认为这样的事情应该让您更接近:

    SELECT c.*, d.desc, d.name, d.values 
    FROM (
        SELECT *, JSON_PARSE(inputs) AS inputs_super
        FROM source.table 
        ) AS c, 
    JSON_PARSE(c.inputs_super) AS d
    ;
    

    (这是对显示结构的即兴响应,因此请原谅任何语法问题)

    【讨论】:

    • 谢谢你的建议。但它不起作用:“查询结果表详细信息查询 ELAPSED TIME: 00 m 10 s 错误:函数 json_parse(super) 不存在提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。”
    • 你的问题表明输入是一个字符串,而不是一个超级。此错误表明输入的类型是 super。请说明您使用的数据类型。
    • 知道了——这是第二个 json_parse。是的,我的错误,未经测试的代码
    • 你能再帮我一下吗。在我们解析了上面的 JSON 之后,我有一列“values”,其中包含 ["Agree","Disagree"] 等列表(可以多于或少于 2 个项目)。我需要解析这个值列表并将列表中的每个项目与其他数据一起添加到每一行。示例:desc:“210396”,值:“同意”,desc:“210396”,值:“不同意”。你明白吗?
    • 我认为您正在描述“组内的list_agg()”功能。这将来自多行的列的值连接到带有分隔符的单个字符串中。有没有意图正确?见docs.aws.amazon.com/redshift/latest/dg/r_LISTAGG.html
    【解决方案2】:

    会更正确:

    SELECT c.*, d.desc, d.name, d.values 
    FROM (
        SELECT id, created, JSON_PARSE(inputs) AS inputs_super
        FROM source.table
        WHERE prompttype = 'input'
        ) AS c, 
    c.inputs_super AS d                                                      
    ; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多