Stable sort?
Stable sort?
От:
"redhog" <redhog@redhog.org>
Дата:
Is sorting in PostgreSQL stable over subqueries, that is, is select * from (select * from A order by x) as B order by y; equivalent with select * from A order by y, x; ?
Re: Stable sort?
От:
Ron Mayer <rm_pg@cheapcomplexdevices.com>
Дата:
redhog wrote: > Is sorting in PostgreSQL stable over subqueries, that is, is > > select * from (select * from A order by x) as B order by y; > > equivalent with > > select * from A order by y, x; Seems as easy to try as to guess. If I did this query right, it seems not. select * from (select random()>0.5 as a, random()>0.5 as b from generate_series(1,10) order by a) as x order by b; a | b ---+--- f | t f | f f | f f | f f | t f | f t | t t | t t | t t | f (10 rows) OTOH, HEY, why isn't that result ordered by 'b' instead of by 'a' (or am I misreading my query or the results)?
Re: Stable sort?
От:
Richard Huxton <dev@archonet.com>
Дата:
redhog wrote: > Is sorting in PostgreSQL stable over subqueries, that is, is > > select * from (select * from A order by x) as B order by y; > > equivalent with > > select * from A order by y, x; I don't see how it could be: SELECT * FROM (SELECT * FROM a ORDER BY x DESC) AS B ORDER BY x ASC; -- Richard Huxton Archonet Ltd
Re: Stable sort?
От:
"redhog" <redhog@redhog.org>
Дата:
> I don't see how it could be: > SELECT * FROM (SELECT * FROM a ORDER BY x DESC) AS B ORDER BY x ASC; That is a rather different query. My question was if the order of two elements whose internal order is not affected by the current ordering clause, still may change places due to technicalities. That is, the normal meaning of the term "stable sort" as it applies to sorting algorithms. Example: Given a subquery that returns the rows a| b+2|1 2|2 1|1 1|2 and an order by a, will the result allways be a| b+1|1 1|2 2|1 2|2 or might it sometimes end up as e.g. a| b+1|2 1|1 2|1 2|2 ?
Re: Stable sort?
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
"redhog" writes: > My question was if the order of two > elements whose internal order is not affected by the current ordering > clause, still may change places due to technicalities. Postgres usually sorts using qsort(), which (on most platforms) is not stable in that sense. regards, tom lane
Re: Stable sort?
От:
"Dann Corbit" <DCorbit@connx.com>
Дата:
> -----Original Message----- > From: pgsql-general-owner@postgresql.org [mailto:pgsql-general- > owner@postgresql.org] On Behalf Of Tom Lane > Sent: Wednesday, November 08, 2006 7:05 AM > To: redhog > Cc: pgsql-general@postgresql.org > Subject: Re: [GENERAL] Stable sort? > > "redhog" writes: > > My question was if the order of two > > elements whose internal order is not affected by the current ordering > > clause, still may change places due to technicalities. > > Postgres usually sorts using qsort(), which (on most platforms) is not > stable in that sense. This is (of course) what we would expect from any SQL database. From ISO/IEC ISO/IEC 9075-2:1999 (E) Section 4.29 Cursors on page 71, we have this: "A cursor in the open state identifies a table, an ordering of the rows of that table, and a position relative to that ordering. If the does not contain an , or contains an that does not specify the order of the rows completely, then the rows of the table have an order that is defined only to the extent that the specifies an order and is otherwise implementation-dependent. When the ordering of a cursor is not defined by an , the relative position of two rows is implementation-dependent. When the ordering of a cursor is partially determined by an , then the relative positions of two rows are determined only by the ; if the two rows have equal values for the purpose of evaluating the , then their relative positions are implementation-dependent." This is talking about cursors specifically, but the same ordering principles would obviously hold throughout. The standard does not prevent the use of a stable sort, but it also does not require it. Obviously, if you order by all the result columns, then the orderings will always be identical (though this would in general be a bad idea).