Re: FOR integer loop bug?

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: FOR integer loop bug?
Date: 2022-04-08 18:07:06
Message-ID: e06ecb67-8b02-68c6-e5d3-0a99b61e6a24@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 4/8/22 10:58 AM, Pavel Stehule wrote:
>
>
> pá 8. 4. 2022 v 19:56 odesílatel Adrian Klaver
> <a
>
> Why is the three period form allowed through and why does it produce no
> result?
>
>
> Maybe
>
> (2022-04-08 19:57:57) postgres=# select .10;
> ┌──────────┐
> │ ?column? │
> ╞══════════╡
> │     0.10 │
> └──────────┘
> (1 row)

Alright so 1...10 is taken as 1::integer .. 0.10::integer CAST results
in 0 and ends up as 1..0. This then is covered by:

"If the lower bound is greater than the upper bound (or less than, in
the REVERSE case), the loop body is not executed at all. No error is
raised."

Confirmed by:

CREATE OR REPLACE FUNCTION utility.for_loop_test()
RETURNS void
LANGUAGE plpgsql
AS $function$
BEGIN
FOR i IN REVERSE 1...10 LOOP
RAISE NOTICE '%', i;
END LOOP;
END;

$function$
;

select for_loop_test();
NOTICE: 1
NOTICE: 0
for_loop_test
---------------

Hmm, I'm going to have to think on this.

>
>
> --
> Adrian Klaver
> adrian(dot)klaver(at)aklaver(dot)com <mailto:adrian(dot)klaver(at)aklaver(dot)com>
>
>

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ron 2022-04-08 18:23:06 Re: FOR integer loop bug?
Previous Message Ron 2022-04-08 18:01:23 Re: Long running processes and commit writing to disk