找不到Greenplum CTE-错误[XX000](allpaths.c:904)

时间:2018-08-20 22:20:35

标签: sql common-table-expression greenplum

我正在为PostgreSQL查询建立一个Greenplum数据库,以将表中的所有组合类型“展平”为伪列,以确保使用MySQL的旧系统可以理解正在发生的事情。我不知道这是否是一个合理的解决方案。

为此,我正在使用CTE,但偶然发现了此错误:

  

SQL错误[XX000]:错误:找不到CTE“ aggregated_attributes”(allpaths.c:904)

是什么原因造成的?

这似乎是在特定情况下发生的。我认为这可能与CTE范围有关,但我不知道它们在幕后如何工作。

它适用于以下查询:

WITH RECURSIVE udt_tree (udt_catalog, udt_name, attribute_name) AS (
        SELECT
            udt_catalog,
            udt_name,
            attribute_name
        FROM information_schema."attributes"
        WHERE data_type = 'USER-DEFINED'
    UNION ALL
        SELECT
            _attributes.udt_catalog,
            _attributes.udt_name,
            _attributes.attribute_name
        FROM
            udt_tree,
            information_schema."attributes" AS _attributes
        WHERE ((_attributes.udt_name, _attributes.udt_catalog)
                = (udt_tree.attribute_name, udt_tree.udt_catalog))
    ),
    aggregated_attributes AS (
        SELECT
            udt_catalog,
            udt_name,
            array_agg(
                CAST(attribute_name AS varchar)
                ORDER BY attribute_name) AS attributes_names
        FROM udt_tree
        GROUP BY
            udt_catalog,
            udt_name
    ),
    aggregated_tree (udt_catalog, udt_name, attributes_tree) AS (
        SELECT
            udt_catalog,
            udt_name,
            attributes_names AS attributes_tree
        FROM aggregated_attributes
    UNION ALL
        SELECT
            at.udt_catalog,
            at.udt_name,
        CASE
            WHEN aa.udt_name IS NULL THEN at.attributes_tree
            ELSE ARRAY(
                SELECT CAST(attribute_name AS text)
                    FROM (SELECT unnest(at.attributes_tree) AS attribute_name) AS deepest_level
                    WHERE attribute_name <> aa.udt_name
                UNION
                SELECT aa.udt_name || '_' || attribute_name
                    FROM (SELECT unnest(aa.attributes_names) AS attribute_name) AS aggregated_level)
        END AS attributes_tree
        FROM
            aggregated_tree AS at,
            aggregated_attributes AS aa
        WHERE aa.udt_name = ANY (at.attributes_tree)
    )
SELECT
    DISTINCT ON (udt_catalog, udt_name)
    *
FROM aggregated_tree
ORDER BY
    udt_catalog,
    udt_name,
attributes_tree DESC;

但是我希望它看起来像这样(不起作用)。它们非常相似,除了type_attributes CTE:

WITH RECURSIVE udt_tree (udt_catalog, udt_name, attribute_name) AS (
        SELECT
            udt_catalog,
            udt_name,
            attribute_name
        FROM information_schema."attributes"
        WHERE data_type = 'USER-DEFINED'
    UNION ALL
        SELECT
            _attributes.udt_catalog,
            _attributes.udt_name,
            _attributes.attribute_name
        FROM
            udt_tree,
            information_schema."attributes" AS _attributes
        WHERE ((_attributes.udt_name, _attributes.udt_catalog)
                = (udt_tree.attribute_name, udt_tree.udt_catalog))
    ),
    aggregated_attributes AS (
        SELECT
            udt_catalog,
            udt_name,
            array_agg(
                CAST(attribute_name AS varchar)
                ORDER BY attribute_name) AS attributes_names
        FROM udt_tree
        GROUP BY
            udt_catalog,
            udt_name
    ),
    aggregated_tree (udt_catalog, udt_name, attributes_tree) AS (
        SELECT
            udt_catalog,
            udt_name,
            attributes_names AS attributes_tree
        FROM aggregated_attributes
    UNION ALL
        SELECT
            at.udt_catalog,
            at.udt_name,
        CASE
            WHEN aa.udt_name IS NULL THEN at.attributes_tree
            ELSE ARRAY(
                SELECT CAST(attribute_name AS text)
                    FROM (SELECT unnest(at.attributes_tree) AS attribute_name) AS deepest_level
                    WHERE attribute_name <> aa.udt_name
                UNION
                SELECT aa.udt_name || '_' || attribute_name
                    FROM (SELECT unnest(aa.attributes_names) AS attribute_name) AS aggregated_level)
        END AS attributes_tree
        FROM
            aggregated_tree AS at,
            aggregated_attributes AS aa
        WHERE aa.udt_name = ANY (at.attributes_tree)
    ),
    type_attributes AS (
        SELECT
            DISTINCT ON (udt_catalog, udt_name)
            *
        FROM aggregated_tree
        ORDER BY
            udt_catalog,
            udt_name,
            attributes_tree DESC
    )
SELECT * FROM type_attributes;

要重现,请创建一个名为ericsson_voz的数据库,然后使用此ddl创建表:

CREATE TYPE CAMELTDPData AS (
    serviceKey varchar,
    gsmSCFAddress varchar
);
CREATE TYPE UserRate AS ENUM ('urindneg', 'ur600bps', 'ur1200bps', 'ur2400bps', 'ur3600bps', 'ur4800bps', 'ur7200bps', 'ur8000bps', 'ur9600bps', 'ur14400bps', 'ur16000bps', 'ur19200bps', 'ur32000bps', 'ur38400bps', 'ur48000bps', 'ur56000bps', 'ur64000bps', 'ur38400bps1', 'ur57600bps', 'ur28800bps', 'ur134-5bps', 'ur100bps', 'ur75bps-1200bps', 'ur1200bps-75bps', 'ur50bps', 'ur75bps', 'ur110bps', 'ur150bps', 'ur200bps', 'ur300bps', 'ur12000bps');
CREATE TYPE AsyncSyncIndicator AS ENUM ('syncdata', 'asyncdata');
CREATE TYPE UILayer1Protocol AS ENUM ('v110-x30', 'g711mulaw', 'g711alaw', 'g721-32000bps-i460', 'h221-h242', 'h223-h245', 'nonitu-t', 'v120', 'x31', 'vselp-speech');
CREATE TYPE MultimediaInformation AS (
    userRate UserRate,
    asyncSyncIndicator AsyncSyncIndicator,
    uILayer1Protocol UILayer1Protocol
);
CREATE TYPE TriggerDetectionPoint AS ENUM ('originatingcallattemptauthorized', 'collectedinformation', 'analyzedinformation', 'originatingcallattemptrouteselectfailure', 'originatingcallattemptcalledpartybusy', 'originatingcallattemptcalledpartynotanswer', 'originatingcallattemptcalledpartyanswer', 'originatingcallattemptmid-calleventdetected', 'originatingcallattemptcalldisconnecting', 'originatingcallattemptcallabandon', 'terminatingcallattemptauthorized', 'terminatingcallattemptcalledpartybusy', 'terminatingcallattemptnoanswer', 'terminatingcallattemptanswer', 'terminatingcallattemptmid-calleventdetected', 'terminatingcallattemptcalldisconnect', 'terminatingcallattemptcallabandon', 'terminatingcallattemptcallreanswer', 'terminatingcallattemptcallsuspended', 'terminatingcallattemptcalledpartynotreachable', 'terminatingcallattemptalerting', 'terminatingcallattemptrouteselectfailure', 'originatingcallattemptcalledpartyreanswer', 'originatingcallattemptcallsuspended', 'originatingcallattemptcalledpartynotreachable', 'originatingcallattemptalerting');
CREATE TYPE SCPAddress AS (
    pointCodeAndSubSystemNumber varchar,
    globalTitle varchar,
    globalTitleAndSubSystemNumber varchar
);
CREATE TYPE TriggerData AS (
    triggerDetectionPoint TriggerDetectionPoint,
    serviceKey varchar,
    sCPAddress SCPAddress
);
CREATE TABLE MSOriginating (
    bCSMTDPData1 CAMELTDPData
) WITH (APPENDONLY=true, ORIENTATION=column) DISTRIBUTED RANDOMLY;
CREATE TABLE Transit (
    multimediaInformation MultimediaInformation
) WITH (APPENDONLY=true, ORIENTATION=column) DISTRIBUTED RANDOMLY;
CREATE TABLE INIncomingCall (
    triggerData0 TriggerData
) WITH (APPENDONLY=true, ORIENTATION=column) DISTRIBUTED RANDOMLY;

预期结果是:

udt_catalog udt_name    attributes_tree
ericsson_voz    multimediainformation   {asyncsyncindicator,uilayer1protocol,userrate}
ericsson_voz    scpaddress  {globaltitle,globaltitleandsubsystemnumber,pointcodeandsubsystemnumber}
ericsson_voz    triggerdata {scpaddress_globaltitle,scpaddress_globaltitleandsubsystemnumber,scpaddress_pointcodeandsubsystemnumber,triggerdetectionpoint}

0 个答案:

没有答案