Re: FETCH FIRST clause PERCENT option

From: Tomas Vondra <tomas(dot)vondra(at)2ndquadrant(dot)com>
To: Vik Fearing <vik(dot)fearing(at)2ndquadrant(dot)com>, Surafel Temesgen <surafel3000(at)gmail(dot)com>
Cc: Mark Dilger <hornschnorter(at)gmail(dot)com>, Andres Freund <andres(at)anarazel(dot)de>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>, andrew(at)tao11(dot)riddles(dot)org(dot)uk
Subject: Re: FETCH FIRST clause PERCENT option
Date: 2018-11-25 18:23:55
Message-ID: d6567d20-f727-4acc-b036-3b13ddb3d27d@2ndquadrant.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 11/25/18 1:05 PM, Vik Fearing wrote:
> On 25/11/2018 12:49, Surafel Temesgen wrote:
>>
>>
>> On Sun, Nov 25, 2018 at 1:24 PM Vik Fearing <vik(dot)fearing(at)2ndquadrant(dot)com
>> <mailto:vik(dot)fearing(at)2ndquadrant(dot)com>> wrote:
>>
>>
>> Also, this query returns 210 rows instead of the expected 208:
>>
>>     select *
>>     from generate_series(1, 1000)
>>     fetch first 20.8 percent rows only
>>
>> this is because fetch first values work with integer and it change
>> fractional number to nearest integer number like select * from
>> generate_series(1, 1000) fetch first 20.3 rows only; is not an error
>> rather it return 20 rows.
>
> I don't see how this behavior is justified by reading the SQL standard.
> Obviously only an integer number of rows is going to be returned, but
> the percentage should be calculated correctly.
>

Right. My draft of SQL standard says this:

<fetch first clause> ::=
FETCH { FIRST | NEXT } [ <fetch first quantity> ] { ROW | ROWS }
{ ONLY | WITH TIES }

<fetch first quantity> ::= <fetch first row count>
| <fetch first percentage>

<fetch first percentage> ::= <simple value specification> PERCENT

and then

30) The declared type of <fetch first row count> shall be an exact
numeric with scale 0 (zero).

31) The declared type of the <simple value specification> simply
contained in <fetch first percentage> shall
be numeric.

So the standard pretty much requires treating the value as numeric.

regards

--
Tomas Vondra http://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Stephen Frost 2018-11-25 18:24:15 Re: Continue work on changes to recovery.conf API
Previous Message Tom Lane 2018-11-25 18:13:57 Re: Centralize use of PG_INTXX_MIN/MAX for integer limits