pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <bruce@momjian.us>
To: David G Johnston <david.g.johnston@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: Re: BUG #10256: COUNT(*) behaves sort of like RANK() when used over a window containing an ORDER BY
Date: Wed, 3 Sep 2014 18:14:57 -0400
Message-ID: <20140903221457.GD13008@momjian.us> (raw)
In-Reply-To: <1399504103854-5803103.post@n5.nabble.com>
References: <20140507181702.1397.66739@wrigleys.postgresql.org>
	<5812.1399498939@sss.pgh.pa.us>
	<1399504103854-5803103.post@n5.nabble.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

On Wed, May  7, 2014 at 04:08:23PM -0700, David G Johnston wrote:
> Tom's quote is pretty much an abbreviation of the following taken directly
> from:
> http://www.postgresql.org/docs/9.3/interactive/sql-select.html
> 
> "The default framing option is RANGE UNBOUNDED PRECEDING, which is the same
> as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. With ORDER BY, this
> sets the frame to be all rows from the partition start up through the
> current row's last peer. Without ORDER BY, all rows of the partition are
> included in the window frame, since all rows become peers of the current
> row."
> 
> A more useful description and example is found in the tutorial (about
> halfway down, the sum(*) example):
> 
> http://www.postgresql.org/docs/9.3/interactive/tutorial-window.html
> 
> The term "peer" in the first quotation is confusing to me.  My understanding
> is that "PARTITION BY" defines which rows are "peers" - even if that isn't
> the wording used.  So now using "peers" in relation to the FRAME clause
> confuses the issue.  IMO the "since all rows..." is really superfluous - I
> would understand:
> 
> "In the presence of an ORDER BY only rows up to the current row (including
> all [partition] peers) are considered.  Omitting an ORDER BY causes the
> entire PARTITION to be considered the FRAME.  Other frame definitions can be
> requested by supplying an explicit FRAME clause."
> 
> I knew the answer but in the interest of looking through the user's eyes I
> wanted to go take a look (again, done this before) and really am not
> surprised that these kinds of questions are being asked.  Just having to
> scan through 5 sections of the documentation is difficult.  That said, the
> tutorial section and inline examples make it quite clear how the
> with/without ORDER BY behavior of window functions (sum in the example)
> cause different results.
> 
> At minimum the top of 9.3.4 could provide links to, and
> descriptions/summaries of, what the other 4 sections cover and why things
> are broken out the way they are.  The other cross-references could point
> back to that section-subsection as a kind of launch point: "Please see
> section 3.5.1 for an overview of, and links to, other related sections."

Based on this discussion, I developed the attached patch which qualifies
"peer" in every place it is mentioned --- this should reduce future
confusion.

-- 
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + Everyone has their own god. +

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

peer.diff (text/x-diff, 4.8K) download diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml new file mode 100644 index daa56e9..e50408c *** a/doc/src/sgml/func.sgml --- b/doc/src/sgml/func.sgml *************** SELECT xmlagg(x) FROM (SELECT x FROM tes *** 13050,13056 **** <firstterm>Window functions</firstterm> provide the ability to perform calculations across sets of rows that are related to the current query row. See <xref linkend="tutorial-window"> for an introduction to this ! feature. </para> <para> --- 13050,13057 ---- <firstterm>Window functions</firstterm> provide the ability to perform calculations across sets of rows that are related to the current query row. See <xref linkend="tutorial-window"> for an introduction to this ! feature, and <xref linkend="syntax-window-functions"> for syntax ! details. </para> <para> diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml new file mode 100644 index b69b634..940d1aa *** a/doc/src/sgml/ref/select.sgml --- b/doc/src/sgml/ref/select.sgml *************** UNBOUNDED FOLLOWING *** 792,799 **** The default framing option is <literal>RANGE UNBOUNDED PRECEDING</>, which is the same as <literal>RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW</>; it sets the frame to be all rows from the partition start ! up through the current row's last peer in the <literal>ORDER BY</> ! ordering (which means all rows if there is no <literal>ORDER BY</>). In general, <literal>UNBOUNDED PRECEDING</> means that the frame starts with the first row of the partition, and similarly <literal>UNBOUNDED FOLLOWING</> means that the frame ends with the last --- 792,800 ---- The default framing option is <literal>RANGE UNBOUNDED PRECEDING</>, which is the same as <literal>RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW</>; it sets the frame to be all rows from the partition start ! up through the current row's last peer (a row that <literal>ORDER ! BY</> considers equivalent to the current row, or all rows if there ! is no <literal>ORDER BY</>). In general, <literal>UNBOUNDED PRECEDING</> means that the frame starts with the first row of the partition, and similarly <literal>UNBOUNDED FOLLOWING</> means that the frame ends with the last *************** UNBOUNDED FOLLOWING *** 817,823 **** results if the <literal>ORDER BY</> ordering does not order the rows uniquely. The <literal>RANGE</> options are designed to ensure that rows that are peers in the <literal>ORDER BY</> ordering are treated ! alike; any two peer rows will be both in or both not in the frame. </para> <para> --- 818,824 ---- results if the <literal>ORDER BY</> ordering does not order the rows uniquely. The <literal>RANGE</> options are designed to ensure that rows that are peers in the <literal>ORDER BY</> ordering are treated ! alike; all peer rows will be in the same frame. </para> <para> diff --git a/doc/src/sgml/syntax.sgml b/doc/src/sgml/syntax.sgml new file mode 100644 index 434a894..2f0680f *** a/doc/src/sgml/syntax.sgml --- b/doc/src/sgml/syntax.sgml *************** UNBOUNDED FOLLOWING *** 1877,1883 **** first <firstterm>peer</> row (a row that <literal>ORDER BY</> considers equivalent to the current row), while a <replaceable>frame_end</> of <literal>CURRENT ROW</> means the frame ends with the last equivalent ! peer. In <literal>ROWS</> mode, <literal>CURRENT ROW</> simply means the current row. </para> --- 1877,1883 ---- first <firstterm>peer</> row (a row that <literal>ORDER BY</> considers equivalent to the current row), while a <replaceable>frame_end</> of <literal>CURRENT ROW</> means the frame ends with the last equivalent ! <literal>ORDER BY</> peer. In <literal>ROWS</> mode, <literal>CURRENT ROW</> simply means the current row. </para> *************** UNBOUNDED FOLLOWING *** 1897,1903 **** which is the same as <literal>RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW</>. With <literal>ORDER BY</>, this sets the frame to be all rows from the partition start up through the current row's last ! peer. Without <literal>ORDER BY</>, all rows of the partition are included in the window frame, since all rows become peers of the current row. </para> --- 1897,1903 ---- which is the same as <literal>RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW</>. With <literal>ORDER BY</>, this sets the frame to be all rows from the partition start up through the current row's last ! <literal>ORDER BY</> peer. Without <literal>ORDER BY</>, all rows of the partition are included in the window frame, since all rows become peers of the current row. </para>

view thread (8+ messages)  latest in thread

Message-ID: <20140903221457.GD13008@momjian.us>
Permalink:  ../20140903221457.GD13008@momjian.us/
Also on:    postgresql.org/message-id/20140903221457.GD13008@momjian.us

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: bruce@momjian.us, david.g.johnston@gmail.com
  Subject: Re: BUG #10256: COUNT(*) behaves sort of like RANK() when used over a window containing an ORDER BY
  In-Reply-To: <20140903221457.GD13008@momjian.us>
  References: <20140507181702.1397.66739@wrigleys.postgresql.org> <5812.1399498939@sss.pgh.pa.us> <1399504103854-5803103.post@n5.nabble.com> <20140903221457.GD13008@momjian.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox