Skip to content

Commit 769e6fc

Browse files
committed
Fix SQL:2008 FETCH FIRST syntax to allow parameters.
OFFSET <x> ROWS FETCH FIRST <y> ROWS ONLY syntax is supposed to accept <simple value specification>, which includes parameters as well as literals. When this syntax was added all those years ago, it was done inconsistently, with <x> and <y> being different subsets of the standard syntax. Rectify that by making <x> and <y> accept the same thing, and allowing either a (signed) numeric literal or a c_expr there, which allows for parameters, variables, and parenthesized arbitrary expressions. Per bug #15200 from Lukas Eder. Backpatch all the way, since this has been broken from the start. Discussion: https://postgr.es/m/877enz476l.fsf@news-spur.riddles.org.uk Discussion: http://postgr.es/m/152647780335.27204.16895288237122418685@wrigleys.postgresql.org
1 parent 5517367 commit 769e6fc

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

doc/src/sgml/ref/select.sgml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,12 +1242,14 @@ OFFSET <replaceable class="parameter">start</replaceable>
12421242
OFFSET <replaceable class="parameter">start</replaceable> { ROW | ROWS }
12431243
FETCH { FIRST | NEXT } [ <replaceable class="parameter">count</replaceable> ] { ROW | ROWS } ONLY
12441244
</synopsis>
1245-
In this syntax, to write anything except a simple integer constant for
1246-
<replaceable class="parameter">start</> or <replaceable
1247-
class="parameter">count</replaceable>, you must write parentheses
1248-
around it.
1249-
If <replaceable class="parameter">count</> is
1250-
omitted in a <literal>FETCH</> clause, it defaults to 1.
1245+
In this syntax, the <replaceable class="parameter">start</replaceable>
1246+
or <replaceable class="parameter">count</replaceable> value is required by
1247+
the standard to be a literal constant, a parameter, or a variable name;
1248+
as a <productname>PostgreSQL</productname> extension, other expressions
1249+
are allowed, but will generally need to be enclosed in parentheses to avoid
1250+
ambiguity.
1251+
If <replaceable class="parameter">count</replaceable> is
1252+
omitted in a <literal>FETCH</literal> clause, it defaults to 1.
12511253
<literal>ROW</literal>
12521254
and <literal>ROWS</literal> as well as <literal>FIRST</literal>
12531255
and <literal>NEXT</literal> are noise words that don't influence

src/backend/parser/gram.y

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
390390

391391
%type <node> fetch_args limit_clause select_limit_value
392392
offset_clause select_offset_value
393-
select_offset_value2 opt_select_fetch_first_value
393+
select_fetch_first_value I_or_F_const
394394
%type <ival> row_or_rows first_or_next
395395

396396
%type <list> OptSeqOptList SeqOptList
@@ -9704,15 +9704,23 @@ limit_clause:
97049704
parser_errposition(@1)));
97059705
}
97069706
/* SQL:2008 syntax */
9707-
| FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
9707+
/* to avoid shift/reduce conflicts, handle the optional value with
9708+
* a separate production rather than an opt_ expression. The fact
9709+
* that ONLY is fully reserved means that this way, we defer any
9710+
* decision about what rule reduces ROW or ROWS to the point where
9711+
* we can see the ONLY token in the lookahead slot.
9712+
*/
9713+
| FETCH first_or_next select_fetch_first_value row_or_rows ONLY
97089714
{ $$ = $3; }
9715+
| FETCH first_or_next row_or_rows ONLY
9716+
{ $$ = makeIntConst(1, -1); }
97099717
;
97109718

97119719
offset_clause:
97129720
OFFSET select_offset_value
97139721
{ $$ = $2; }
97149722
/* SQL:2008 syntax */
9715-
| OFFSET select_offset_value2 row_or_rows
9723+
| OFFSET select_fetch_first_value row_or_rows
97169724
{ $$ = $2; }
97179725
;
97189726

@@ -9731,22 +9739,31 @@ select_offset_value:
97319739

97329740
/*
97339741
* Allowing full expressions without parentheses causes various parsing
9734-
* problems with the trailing ROW/ROWS key words. SQL only calls for
9735-
* constants, so we allow the rest only with parentheses. If omitted,
9736-
* default to 1.
9742+
* problems with the trailing ROW/ROWS key words. SQL spec only calls for
9743+
* <simple value specification>, which is either a literal or a parameter (but
9744+
* an <SQL parameter reference> could be an identifier, bringing up conflicts
9745+
* with ROW/ROWS). We solve this by leveraging the presence of ONLY (see above)
9746+
* to determine whether the expression is missing rather than trying to make it
9747+
* optional in this rule.
9748+
*
9749+
* c_expr covers almost all the spec-required cases (and more), but it doesn't
9750+
* cover signed numeric literals, which are allowed by the spec. So we include
9751+
* those here explicitly. We need FCONST as well as ICONST because values that
9752+
* don't fit in the platform's "long", but do fit in bigint, should still be
9753+
* accepted here. (This is possible in 64-bit Windows as well as all 32-bit
9754+
* builds.)
97379755
*/
9738-
opt_select_fetch_first_value:
9739-
SignedIconst { $$ = makeIntConst($1, @1); }
9740-
| '(' a_expr ')' { $$ = $2; }
9741-
| /*EMPTY*/ { $$ = makeIntConst(1, -1); }
9756+
select_fetch_first_value:
9757+
c_expr { $$ = $1; }
9758+
| '+' I_or_F_const
9759+
{ $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
9760+
| '-' I_or_F_const
9761+
{ $$ = doNegate($2, @1); }
97429762
;
97439763

9744-
/*
9745-
* Again, the trailing ROW/ROWS in this case prevent the full expression
9746-
* syntax. c_expr is the best we can do.
9747-
*/
9748-
select_offset_value2:
9749-
c_expr { $$ = $1; }
9764+
I_or_F_const:
9765+
Iconst { $$ = makeIntConst($1,@1); }
9766+
| FCONST { $$ = makeFloatConst($1,@1); }
97509767
;
97519768

97529769
/* noise words */

0 commit comments

Comments
 (0)