]> granicus.if.org Git - postgresql/commit
Support all SQL:2011 options for window frame clauses.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 7 Feb 2018 05:06:50 +0000 (00:06 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 7 Feb 2018 05:06:56 +0000 (00:06 -0500)
commit0a459cec96d3856f476c2db298c6b52f592894e8
tree3d10f137b48de039c46914fa8e854bd69daaaec1
parent23209457314f6fd89fcd251a8173b0129aaa95a2
Support all SQL:2011 options for window frame clauses.

This patch adds the ability to use "RANGE offset PRECEDING/FOLLOWING"
frame boundaries in window functions.  We'd punted on that back in the
original patch to add window functions, because it was not clear how to
do it in a reasonably data-type-extensible fashion.  That problem is
resolved here by adding the ability for btree operator classes to provide
an "in_range" support function that defines how to add or subtract the
RANGE offset value.  Factoring it this way also allows the operator class
to avoid overflow problems near the ends of the datatype's range, if it
wishes to expend effort on that.  (In the committed patch, the integer
opclasses handle that issue, but it did not seem worth the trouble to
avoid overflow failures for datetime types.)

The patch includes in_range support for the integer_ops opfamily
(int2/int4/int8) as well as the standard datetime types.  Support for
other numeric types has been requested, but that seems like suitable
material for a follow-on patch.

In addition, the patch adds GROUPS mode which counts the offset in
ORDER-BY peer groups rather than rows, and it adds the frame_exclusion
options specified by SQL:2011.  As far as I can see, we are now fully
up to spec on window framing options.

Existing behaviors remain unchanged, except that I changed the errcode
for a couple of existing error reports to meet the SQL spec's expectation
that negative "offset" values should be reported as SQLSTATE 22013.

Internally and in relevant parts of the documentation, we now consistently
use the terminology "offset PRECEDING/FOLLOWING" rather than "value
PRECEDING/FOLLOWING", since the term "value" is confusingly vague.

Oliver Ford, reviewed and whacked around some by me

Discussion: https://postgr.es/m/CAGMVOdu9sivPAxbNN0X+q19Sfv9edEPv=HibOJhB14TJv_RCQg@mail.gmail.com
38 files changed:
doc/src/sgml/btree.sgml
doc/src/sgml/func.sgml
doc/src/sgml/ref/select.sgml
doc/src/sgml/syntax.sgml
doc/src/sgml/xindex.sgml
src/backend/access/nbtree/nbtvalidate.c
src/backend/catalog/dependency.c
src/backend/catalog/sql_features.txt
src/backend/commands/opclasscmds.c
src/backend/executor/nodeWindowAgg.c
src/backend/nodes/copyfuncs.c
src/backend/nodes/equalfuncs.c
src/backend/nodes/outfuncs.c
src/backend/nodes/readfuncs.c
src/backend/optimizer/plan/createplan.c
src/backend/parser/gram.y
src/backend/parser/parse_agg.c
src/backend/parser/parse_clause.c
src/backend/parser/parse_expr.c
src/backend/parser/parse_func.c
src/backend/utils/adt/date.c
src/backend/utils/adt/int.c
src/backend/utils/adt/int8.c
src/backend/utils/adt/ruleutils.c
src/backend/utils/adt/timestamp.c
src/backend/utils/errcodes.txt
src/include/access/nbtree.h
src/include/catalog/catversion.h
src/include/catalog/pg_amproc.h
src/include/catalog/pg_proc.h
src/include/nodes/execnodes.h
src/include/nodes/parsenodes.h
src/include/nodes/plannodes.h
src/include/parser/kwlist.h
src/include/parser/parse_node.h
src/test/regress/expected/alter_generic.out
src/test/regress/expected/window.out
src/test/regress/sql/window.sql