嵌套左连接和合并的问题

发布于 2024-11-28 12:52:02 字数 1038 浏览 2 评论 0原文

我在 postgresql 中遇到了嵌套左连接的奇怪问题...这很难解释,但很容易显示 =) 我们在这里:

SELECT * FROM
(
    SELECT 1 as key1
) sub1
LEFT JOIN 
(
    SELECT sub3.key3, sub4.value2 FROM
    (
        SELECT 1 as key3
    ) sub3
    LEFT JOIN 
    (
        SELECT sub5.key5, COALESCE(sub6.value1, 1) as value2
        FROM
        (
            SELECT 1 as key5
        ) sub5
        LEFT JOIN
        (
            SELECT 1 as key6, value1
            FROM
            (
                SELECT NULL::integer as value1
            ) sub7
            WHERE false
        ) sub6 ON false

    )
    sub4 ON sub4.key5=sub3.key3
)
sub2 ON sub1.key1 = sub2.key3

这个查询的结果:

key1;key3;value2
1;1;NULL

这就是问题 - value2 不能为 NULL 因为 COALESCE在sub4中(至少我认为不可能=)) 无论如何,如果我们改变

SELECT sub3.key3, sub4.value2 FROM

的话,

SELECT sub3.key3, value2 FROM

我们会得到正确的结果:

key1;key3;value2
1;1;1

我的思想和手有问题吗?或者这是一个错误?

提前致谢!

I have strange issue with nested left-joins in postgresql... It's hard to explain, but easy to show =) here we are:

SELECT * FROM
(
    SELECT 1 as key1
) sub1
LEFT JOIN 
(
    SELECT sub3.key3, sub4.value2 FROM
    (
        SELECT 1 as key3
    ) sub3
    LEFT JOIN 
    (
        SELECT sub5.key5, COALESCE(sub6.value1, 1) as value2
        FROM
        (
            SELECT 1 as key5
        ) sub5
        LEFT JOIN
        (
            SELECT 1 as key6, value1
            FROM
            (
                SELECT NULL::integer as value1
            ) sub7
            WHERE false
        ) sub6 ON false

    )
    sub4 ON sub4.key5=sub3.key3
)
sub2 ON sub1.key1 = sub2.key3

The result of this query:

key1;key3;value2
1;1;NULL

And this is the problem - value2 can't be NULL because of COALESCE in sub4 (at least I think that it can't be =))
Anyway if we'll change

SELECT sub3.key3, sub4.value2 FROM

with

SELECT sub3.key3, value2 FROM

we will got correct result:

key1;key3;value2
1;1;1

Is there something wrong with my mind&hands? or is it a bug?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

够运 2024-12-05 12:52:02

我已将您的查询转换为 Oracle 语法,并且得到了预期的结果

+------+------+--------+
| KEY1 | KEY3 | VALUE2 |
+------+------+--------+
|    1 |    1 |      1 |
+------+------+--------+

另外

SELECT * FROM (
    SELECT 1 as key1 from dual
) sub1
LEFT JOIN (
    SELECT sub3.key3, sub4.value2 FROM (
        SELECT 1 as key3 from dual
    ) sub3
    LEFT JOIN (
        SELECT sub5.key5, COALESCE(sub6.value1, 1) as value2
        FROM (
            SELECT 1 as key5 from dual
        ) sub5
        LEFT JOIN (
            SELECT 1 as key6, value1
            FROM (
                SELECT cast(NULL as NUMBER(7)) as value1 from dual
            ) sub7
            WHERE 1=0
        ) sub6 ON 1=0
    )
    sub4 ON sub4.key5 = sub3.key3
)
sub2 ON sub1.key1 = sub2.key3

,通过阅读这个相当奇怪的查询,我仍然认为您从 Postgres 获得的结果是错误的。我认为您可以安全地将其作为错误提交给 Postgres

I have translated your query to Oracle syntax and I get the expected result

+------+------+--------+
| KEY1 | KEY3 | VALUE2 |
+------+------+--------+
|    1 |    1 |      1 |
+------+------+--------+

resulting from

SELECT * FROM (
    SELECT 1 as key1 from dual
) sub1
LEFT JOIN (
    SELECT sub3.key3, sub4.value2 FROM (
        SELECT 1 as key3 from dual
    ) sub3
    LEFT JOIN (
        SELECT sub5.key5, COALESCE(sub6.value1, 1) as value2
        FROM (
            SELECT 1 as key5 from dual
        ) sub5
        LEFT JOIN (
            SELECT 1 as key6, value1
            FROM (
                SELECT cast(NULL as NUMBER(7)) as value1 from dual
            ) sub7
            WHERE 1=0
        ) sub6 ON 1=0
    )
    sub4 ON sub4.key5 = sub3.key3
)
sub2 ON sub1.key1 = sub2.key3

Also, from reading this rather odd query, I still think that the result you get from Postgres is wrong. I think you can safely file this as a bug to Postgres

网名女生简单气质 2024-12-05 12:52:02

我从 postgresql 团队得到了答案。
结论:

This is on HEAD from today.  Clearly there's a problem.

所以,这是(而且确实是)一个错误。感谢所有参与本期的人! =)

I got answer from postgresql team.
Verdict:

This is on HEAD from today.  Clearly there's a problem.

So, it was (and it is) a bug. Thanks for all, who participated in this issue! =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文