Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1da162e

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 asliterals. When this syntax was added all those years ago, it was doneinconsistently, with <x> and <y> being different subsets of thestandard syntax.Rectify that by making <x> and <y> accept the same thing, and allowingeither a (signed) numeric literal or a c_expr there, which allows forparameters, 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.ukDiscussion:http://postgr.es/m/152647780335.27204.16895288237122418685@wrigleys.postgresql.org
1 parent806d08c commit1da162e

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

‎doc/src/sgml/ref/select.sgml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,10 +1399,12 @@ OFFSET <replaceable class="parameter">start</replaceable>
13991399
OFFSET <replaceable class="parameter">start</replaceable> { ROW | ROWS }
14001400
FETCH { FIRST | NEXT } [ <replaceable class="parameter">count</replaceable> ] { ROW | ROWS } ONLY
14011401
</synopsis>
1402-
In this syntax, to write anything except a simple integer constant for
1403-
<replaceable class="parameter">start</replaceable> or <replaceable
1404-
class="parameter">count</replaceable>, you must write parentheses
1405-
around it.
1402+
In this syntax, the <replaceable class="parameter">start</replaceable>
1403+
or <replaceable class="parameter">count</replaceable> value is required by
1404+
the standard to be a literal constant, a parameter, or a variable name;
1405+
as a <productname>PostgreSQL</productname> extension, other expressions
1406+
are allowed, but will generally need to be enclosed in parentheses to avoid
1407+
ambiguity.
14061408
If <replaceable class="parameter">count</replaceable> is
14071409
omitted in a <literal>FETCH</literal> clause, it defaults to 1.
14081410
<literal>ROW</literal>

‎src/backend/parser/gram.y

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

452452
%type<node>fetch_argslimit_clauseselect_limit_value
453453
offset_clauseselect_offset_value
454-
select_offset_value2opt_select_fetch_first_value
454+
select_fetch_first_valueI_or_F_const
455455
%type<ival>row_or_rowsfirst_or_next
456456

457457
%type<list>OptSeqOptListSeqOptListOptParenthesizedSeqOptList
@@ -11570,15 +11570,23 @@ limit_clause:
1157011570
parser_errposition(@1)));
1157111571
}
1157211572
/* SQL:2008 syntax*/
11573-
|FETCHfirst_or_nextopt_select_fetch_first_valuerow_or_rowsONLY
11573+
/* to avoid shift/reduce conflicts, handle the optional value with
11574+
* a separate production rather than an opt_ expression. The fact
11575+
* that ONLY is fully reserved means that this way, we defer any
11576+
* decision about what rule reduces ROW or ROWS to the point where
11577+
* we can see the ONLY token in the lookahead slot.
11578+
*/
11579+
|FETCHfirst_or_nextselect_fetch_first_valuerow_or_rowsONLY
1157411580
{$$ =$3; }
11581+
|FETCHfirst_or_nextrow_or_rowsONLY
11582+
{$$ = makeIntConst(1, -1); }
1157511583
;
1157611584

1157711585
offset_clause:
1157811586
OFFSETselect_offset_value
1157911587
{$$ =$2; }
1158011588
/* SQL:2008 syntax*/
11581-
|OFFSETselect_offset_value2row_or_rows
11589+
|OFFSETselect_fetch_first_valuerow_or_rows
1158211590
{$$ =$2; }
1158311591
;
1158411592

@@ -11597,22 +11605,31 @@ select_offset_value:
1159711605

1159811606
/*
1159911607
* Allowing full expressions without parentheses causes various parsing
11600-
* problems with the trailing ROW/ROWS key words. SQL only calls for
11601-
* constants, so we allow the rest only with parentheses. If omitted,
11602-
* default to 1.
11608+
* problems with the trailing ROW/ROWS key words. SQL spec only calls for
11609+
* <simple value specification>, which is either a literal or a parameter (but
11610+
* an <SQL parameter reference> could be an identifier, bringing up conflicts
11611+
* with ROW/ROWS). We solve this by leveraging the presence of ONLY (see above)
11612+
* to determine whether the expression is missing rather than trying to make it
11613+
* optional in this rule.
11614+
*
11615+
* c_expr covers almost all the spec-required cases (and more), but it doesn't
11616+
* cover signed numeric literals, which are allowed by the spec. So we include
11617+
* those here explicitly. We need FCONST as well as ICONST because values that
11618+
* don't fit in the platform's "long", but do fit in bigint, should still be
11619+
* accepted here. (This is possible in 64-bit Windows as well as all 32-bit
11620+
* builds.)
1160311621
*/
11604-
opt_select_fetch_first_value:
11605-
SignedIconst{$$ = makeIntConst($1,@1); }
11606-
|'('a_expr')'{$$ =$2; }
11607-
|/*EMPTY*/{$$ = makeIntConst(1, -1); }
11622+
select_fetch_first_value:
11623+
c_expr{$$ =$1; }
11624+
|'+'I_or_F_const
11625+
{$$ = (Node *) makeSimpleA_Expr(AEXPR_OP,"+",NULL,$2,@1); }
11626+
|'-'I_or_F_const
11627+
{$$ = doNegate($2,@1); }
1160811628
;
1160911629

11610-
/*
11611-
* Again, the trailing ROW/ROWS in this case prevent the full expression
11612-
* syntax. c_expr is the best we can do.
11613-
*/
11614-
select_offset_value2:
11615-
c_expr{$$ =$1; }
11630+
I_or_F_const:
11631+
Iconst{$$ = makeIntConst($1,@1); }
11632+
|FCONST{$$ = makeFloatConst($1,@1); }
1161611633
;
1161711634

1161811635
/* noise words*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp