Обсуждение: Query broken under 7.1RC2

Поиск
Список
Период
Сортировка

Query broken under 7.1RC2

От
Kyle
Дата:
This query worked under 7.0.3 but yields an error under 7.1RC1 or RC2.

The error message is:
psql:outbug.sql:43: ERROR:  Sub-SELECT uses un-GROUPed attribute h.ordnum from outer query

Tom, you patched util/clauses.c (near line 540) a while back to prevent the same error message on a different query.  This may be related.

The SQL is a little strange because the subquery tries to reference individual records from the outer query and then sum them.
The more I look at it, I wonder if it is not just bad SQL. But the last one I found like this turned out to reveal a bug, so here it is:

drop table ord_hdr;
drop table cinv_hdr;
drop table cinv_items;
drop table inc_link;

create table ord_hdr (
    ordnum      int4,
    proj        int4
);
 

create table cinv_hdr (
    ordnum      int4,
    hinum       int4,
    status      varchar
);

create table cinv_items (
    ordnum      int4,
    hinum       int4,
    quant       int4,
    unit_price  numeric(12,2)
);

create table inc_link (
    ordnum      int4,
    hinum       int4,
    amount      numeric(12,2)
);

select sum(i.quant*i.unit_price::float8),
   (select coalesce(sum(amount),0) from inc_link where ordnum = h.ordnum and hinum = h.hinum)

    from cinv_hdr h, cinv_items i, ord_hdr o where

    o.ordnum = h.ordnum and
    h.ordnum = i.ordnum and
    h.hinum = i.hinum and

    o.proj = 1051 and
    h.status = 'open'
;

Вложения

Re: Query broken under 7.1RC2

От
Tom Lane
Дата:
Kyle <kyle@actarg.com> writes:
> The SQL is a little strange because the subquery tries to reference
> individual records from the outer query and then sum them.
> The more I look at it, I wonder if it is not just bad SQL.

It is bad SQL, but pre-7.1 Postgres failed to catch it.  Here's the
CVS log entry:

2000-09-25 14:09  tgl
* src/backend/optimizer/plan/planner.c: System neglected tocomplain about ungrouped variables passed to sublinks when
outerquerycontained aggregates but no GROUP clause.
 

Since you have a SUM(), the outer query is a grouped query, and so
references to the ungrouped h.* columns in the SELECT targetlist are
not well defined.  Lord knows what result you were getting from 7.0 ...
        regards, tom lane