Unix Technical Forum

Re: PL/pgSQL 'i = i + 1' Syntax

This is a discussion on Re: PL/pgSQL 'i = i + 1' Syntax within the pgsql Hackers forums, part of the PostgreSQL category; --> Tom Lane wrote: >> It ought to be illegal to modify the loop control variable anyway, >> IMNSHO - ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Hackers

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-12-2008, 02:24 AM
Albe Laurenz
 
Posts: n/a
Default Re: PL/pgSQL 'i = i + 1' Syntax

Tom Lane wrote:
>> It ought to be illegal to modify the loop control variable anyway,
>> IMNSHO - it certainly is in Ada, the distant antecedent of pl/pgsql.

>
> If modifying the loop variable is disallowed in PL/SQL, I'm all for
> disallowing it in plpgsql, otherwise not. Anyone have a
> recent copy of Oracle to try it on?


I tried this on Oracle 10.2.0.2.0 (which is the most recent version):

SET SERVEROUTPUT ON
BEGIN
FOR i IN 1..10 LOOP
i := i + 1;
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;
/
i := i + 1;
*
ERROR at line 3:
ORA-06550: line 3, column 7:
PLS-00363: expression 'I' cannot be used as an assignment target
ORA-06550: line 3, column 7:
PL/SQL: Statement ignored

And the documentation also explicitly states that it is not allowed.

By the way, PL/SQL screams if you want to do an assignment with '='.
But I guess that the current behaviour of PL/pgSQL should not reflect
that to maintain backward compatibility, right?

Yours,
Laurenz Albe

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-12-2008, 02:24 AM
Andrew Dunstan
 
Posts: n/a
Default Re: PL/pgSQL 'i = i + 1' Syntax

Albe Laurenz said:
> Tom Lane wrote:
>>> It ought to be illegal to modify the loop control variable anyway,
>>> IMNSHO - it certainly is in Ada, the distant antecedent of pl/pgsql.

>>
>> If modifying the loop variable is disallowed in PL/SQL, I'm all for
>> disallowing it in plpgsql, otherwise not. Anyone have a
>> recent copy of Oracle to try it on?

>
> I tried this on Oracle 10.2.0.2.0 (which is the most recent version):
>
> SET SERVEROUTPUT ON
> BEGIN
> FOR i IN 1..10 LOOP
> i := i + 1;
> DBMS_OUTPUT.PUT_LINE(i);
> END LOOP;
> END;
> /
> i := i + 1;
> *
> ERROR at line 3:
> ORA-06550: line 3, column 7:
> PLS-00363: expression 'I' cannot be used as an assignment target
> ORA-06550: line 3, column 7:
> PL/SQL: Statement ignored
>
> And the documentation also explicitly states that it is not allowed.
>


So should we if it can be done conveniently. That might be a big IF - IIRC
many Pascal compilers ignore the similar language rule because implementing
it is a pain in the neck.


> By the way, PL/SQL screams if you want to do an assignment with '='.
> But I guess that the current behaviour of PL/pgSQL should not reflect
> that to maintain backward compatibility, right?
>


I think it should. The current behaviour is undocumented and more than icky.

cheers

andrew



---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-12-2008, 02:25 AM
Tom Lane
 
Posts: n/a
Default Re: PL/pgSQL 'i = i + 1' Syntax

"Andrew Dunstan" <andrew@dunslane.net> writes:
> Albe Laurenz said:
>> ERROR at line 3:
>> ORA-06550: line 3, column 7:
>> PLS-00363: expression 'I' cannot be used as an assignment target
>> ORA-06550: line 3, column 7:
>> PL/SQL: Statement ignored
>>
>> And the documentation also explicitly states that it is not allowed.


> So should we if it can be done conveniently. That might be a big IF - IIRC
> many Pascal compilers ignore the similar language rule because implementing
> it is a pain in the neck.


Since we already have the notion of a "const" variable in plpgsql,
I think it might work to just mark the loop variable as const.

>> By the way, PL/SQL screams if you want to do an assignment with '='.
>> But I guess that the current behaviour of PL/pgSQL should not reflect
>> that to maintain backward compatibility, right?


> I think it should. The current behaviour is undocumented and more than icky.


The lack of documentation is easily fixed ;-). I don't think this is
icky enough to justify breaking all the existing functions we'd
undoubtedly break if we changed it.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-12-2008, 02:25 AM
Andrew Dunstan
 
Posts: n/a
Default Re: PL/pgSQL 'i = i + 1' Syntax

Tom Lane wrote:
>>> By the way, PL/SQL screams if you want to do an assignment with '='.
>>> But I guess that the current behaviour of PL/pgSQL should not reflect
>>> that to maintain backward compatibility, right?
>>>

>
>
>> I think it should. The current behaviour is undocumented and more than icky.
>>

>
> The lack of documentation is easily fixed ;-). I don't think this is
> icky enough to justify breaking all the existing functions we'd
> undoubtedly break if we changed it.
>
>



We have tightened behaviour in ways much harder to fix in the past, e.g.
actually following UTF8 rules. Fixing breakage in this case would be
pretty trivial, and nobody has any real right to expect the current
behaviour to work.

But I won't be surprised to be in a minority on this ....

cheers

andrew

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-12-2008, 02:25 AM
William ZHANG
 
Posts: n/a
Default Re: PL/pgSQL 'i = i + 1' Syntax


""Albe Laurenz"" <all@adv.magwien.gv.at>
> Tom Lane wrote:

....
> > If modifying the loop variable is disallowed in PL/SQL, I'm all for
> > disallowing it in plpgsql, otherwise not. Anyone have a
> > recent copy of Oracle to try it on?

>
> I tried this on Oracle 10.2.0.2.0 (which is the most recent version):
>
> SET SERVEROUTPUT ON
> BEGIN
> FOR i IN 1..10 LOOP
> i := i + 1;
> DBMS_OUTPUT.PUT_LINE(i);
> END LOOP;
> END;
> /
> i := i + 1;
> *
> ERROR at line 3:
> ORA-06550: line 3, column 7:
> PLS-00363: expression 'I' cannot be used as an assignment target
> ORA-06550: line 3, column 7:
> PL/SQL: Statement ignored
>
> And the documentation also explicitly states that it is not allowed.
>
> By the way, PL/SQL screams if you want to do an assignment with '='.
> But I guess that the current behaviour of PL/pgSQL should not reflect
> that to maintain backward compatibility, right?
>


I think Oracle's syntax and behaviour are better.
As for this feature, breaking the backward compatibility is acceptable.

Regards,
William ZHANG


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-12-2008, 02:25 AM
Tom Lane
 
Posts: n/a
Default Re: PL/pgSQL 'i = i + 1' Syntax

Andrew Dunstan <andrew@dunslane.net> writes:
> Tom Lane wrote:
>> The lack of documentation is easily fixed ;-). I don't think this is
>> icky enough to justify breaking all the existing functions we'd
>> undoubtedly break if we changed it.


> We have tightened behaviour in ways much harder to fix in the past, e.g.
> actually following UTF8 rules.


True, but there were clear benefits from doing so. Disallowing "="
assignment in plpgsql wouldn't buy anything, just break programs.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-12-2008, 02:25 AM
Jim C. Nasby
 
Posts: n/a
Default Re: PL/pgSQL 'i = i + 1' Syntax

On Wed, May 17, 2006 at 10:11:39AM -0400, Tom Lane wrote:
> The lack of documentation is easily fixed ;-). I don't think this is
> icky enough to justify breaking all the existing functions we'd
> undoubtedly break if we changed it.


I thought the suggestion was to complain loudly (presumably during
CREATE FUNCTION), but not throw an error.
--
Jim C. Nasby, Sr. Engineering Consultant jnasby@pervasive.com
Pervasive Software http://pervasive.com work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-12-2008, 02:26 AM
Josh Berkus
 
Posts: n/a
Default Re: PL/pgSQL 'i = i + 1' Syntax

Tom,

> True, but there were clear benefits from doing so. Disallowing "="
> assignment in plpgsql wouldn't buy anything, just break programs.


But it's already disallowed in most places. The i = i + 1 seems to be an
exception.

So what happens to "i" if I do:

IF i = i + 1 THEN ....

does "i" increment? If so, isn't that a bug?

I don't think too many people are using that functionality intentionally; I
probably write more PL/pgSQL than anyone and would regard any assignment
without ":=" as a bug.

--
--Josh

Josh Berkus
PostgreSQL @ Sun
San Francisco

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-12-2008, 02:26 AM
Tom Lane
 
Posts: n/a
Default Re: PL/pgSQL 'i = i + 1' Syntax

Josh Berkus <josh@agliodbs.com> writes:
>> True, but there were clear benefits from doing so. Disallowing "="
>> assignment in plpgsql wouldn't buy anything, just break programs.


> But it's already disallowed in most places.


No it isn't. The plpgsql scanner treats := and = as *the same token*.
They can be interchanged freely. This has nothing to do with the case
of modifying a loop variable in particular.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-12-2008, 02:26 AM
Hannu Krosing
 
Posts: n/a
Default Re: PL/pgSQL 'i = i + 1' Syntax

Ühel kenal päeval, K, 2006-05-17 kell 10:22, kirjutas Josh Berkus:
> Tom,
>
> > True, but there were clear benefits from doing so. Disallowing "="
> > assignment in plpgsql wouldn't buy anything, just break programs.

>
> But it's already disallowed in most places. The i = i + 1 seems to be an
> exception.
>
> So what happens to "i" if I do:
>
> IF i = i + 1 THEN ....
>
> does "i" increment? If so, isn't that a bug?
>
> I don't think too many people are using that functionality intentionally; I
> probably write more PL/pgSQL than anyone and would regard any assignment
> without ":=" as a bug.


I do occasionally write some pl/pgSQL, and have at some points written a
lot of it. And most of it uses = instead of := , including all code
written during last 1.5 years.

Once I found out that = works for assignment, i completely stopped
using := .I have treated := as "deprecated" for some time already.

So allowing only := for assignment would make me very sad .

--
----------------
Hannu Krosing
Database Architect
Skype Technologies OÜ
Akadeemia tee 21 F, Tallinn, 12618, Estonia

Skype me: callto:hkrosing
Get Skype for free: http://www.skype.com



---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:37 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com