具有不同列数的记录类型比较不会失败
Record type comparison with different numbers of columns isn't failing
为什么以下查询 不会 在 PostgreSQL 11.6 中触发 "cannot compare record types with different numbers of columns" 错误?
with
s AS (SELECT 1)
, main AS (
SELECT (a) = (b) , (a) = (a), (b) = (b), a, b -- I expect (a) = (b) fails
FROM s
, LATERAL (select 1 as x, 2 as y) AS a
, LATERAL (select 5 as x) AS b
)
select * from main;
虽然这个是:
with
x AS (SELECT 1)
, y AS (select 1, 2)
select (x) = (y) from x, y;
中的注释
Errors related to the number or types of elements might not occur if the comparison is resolved using earlier columns.
在这种情况下,因为 a.x=1 和 b.x=5,它 returns 错误而没有注意到列数不匹配。将它们更改为匹配,您将得到相同的异常(这也是第二个查询确实有该异常的原因)。
testdb=# with
s AS (SELECT 1)
, main AS (
SELECT a = b , (a) = (a), (b) = (b), a, b -- I expect (a) = (b) fails
FROM s
, LATERAL (select 5 as x, 2 as y) AS a
, LATERAL (select 5 as x) AS b
)
select * from main;
ERROR: cannot compare record types with different numbers of columns
为什么以下查询 不会 在 PostgreSQL 11.6 中触发 "cannot compare record types with different numbers of columns" 错误?
with
s AS (SELECT 1)
, main AS (
SELECT (a) = (b) , (a) = (a), (b) = (b), a, b -- I expect (a) = (b) fails
FROM s
, LATERAL (select 1 as x, 2 as y) AS a
, LATERAL (select 5 as x) AS b
)
select * from main;
虽然这个是:
with
x AS (SELECT 1)
, y AS (select 1, 2)
select (x) = (y) from x, y;
Errors related to the number or types of elements might not occur if the comparison is resolved using earlier columns.
在这种情况下,因为 a.x=1 和 b.x=5,它 returns 错误而没有注意到列数不匹配。将它们更改为匹配,您将得到相同的异常(这也是第二个查询确实有该异常的原因)。
testdb=# with
s AS (SELECT 1)
, main AS (
SELECT a = b , (a) = (a), (b) = (b), a, b -- I expect (a) = (b) fails
FROM s
, LATERAL (select 5 as x, 2 as y) AS a
, LATERAL (select 5 as x) AS b
)
select * from main;
ERROR: cannot compare record types with different numbers of columns