Unix Technical Forum

unary operators, precedence, grouping

This is a discussion on unary operators, precedence, grouping within the Pgsql General forums, part of the PostgreSQL category; --> I defined a function, count_nonnull, to return 1 if not null, 0 otherwise. Then I defined a corresponding unary ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008, 02:31 PM
woger151
 
Posts: n/a
Default unary operators, precedence, grouping

I defined a function, count_nonnull, to return 1 if not null, 0 otherwise.
Then I defined a corresponding unary operator <~~. I wanted it for
expressions like <~~ item_1 + <~~ item_2. But because precedence of
user-defined ops is pretty low, I had to rewrite this as <~~(item_1) +
<~~(item_2), which is already no more efficient in use of parentheses than
count_nonnull.

Even worse, however, it turns out that _more_ parentheses were needed:
(<~~(item_1)) + (<~~(item_2)).

Why wouldn't <~~(item_1) + <~~(item_2) be parsed as (<~~(item_1)) +
(<~~(item_2))?




---------------------------(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
  #2 (permalink)  
Old 04-09-2008, 02:31 PM
Tom Lane
 
Posts: n/a
Default Re: unary operators, precedence, grouping

"woger151" <woger151@jqpx37.cotse.net> writes:
> Why wouldn't <~~(item_1) + <~~(item_2) be parsed as (<~~(item_1)) +
> (<~~(item_2))?


Because it's parsed as
<~~ ( (item_1) + ( <~~ (item_2) ) )
"+" binds more tightly than any non-built-in operator, per the
precedence chart in the manual:
http://www.postgresql.org/docs/8.2/s...SQL-PRECEDENCE
so this interpretation is preferred over the alternative
( <~~ (item_1) ) + ( <~~ (item_2) )
Those are the only two possibilities without getting into right-unary
operators, which the parser is generally designed not to do if it can
avoid it.

regards, tom lane

---------------------------(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 05:41 AM.


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