Обсуждение: Operator Precedence problem?

Поиск
Список
Период
Сортировка

Operator Precedence problem?

От
Philip Warner
Дата:
I have a peculiar problem that I can't reproduce on a trivial database:

select product, priority, count(*) as completed from issue 
where   product = 'DIS'    and create_date < '1-Aug-2000' and finish_date >= '1-Aug-2000' or
finish_date is null
group by product, priority;

This produces a list of all products - not just 'DIS'. If I put the last
two clauses in parnthesis, then it works as expected:

select product, priority, count(*) as completed from issue 
where   product = 'DIS'    and create_date < '1-Aug-2000' and (finish_date >= '1-Aug-2000' or
finish_date is null)
group by product, priority;

Which makes me think that the precedence of 'or' is not what I expected. Is
this a feature? If so, the fact that I get precisely the opposite behaviour
in simple test databases must be a bug, I think.

Any help or explanation would be appreciated...



----------------------------------------------------------------
Philip Warner                    |     __---_____
Albatross Consulting Pty. Ltd.   |----/       -  \
(A.B.N. 75 008 659 498)          |          /(@)   ______---_
Tel: (+61) 0500 83 82 81         |                 _________  \
Fax: (+61) 0500 83 82 82         |                 ___________ |
Http://www.rhyme.com.au          |                /           \|                                |    --________--
PGP key available upon request,  |  /
and from pgp5.ai.mit.edu:11371   |/


Re: Operator Precedence problem?

От
John McKown
Дата:
Every language that I've ever used (other than APL) has the precedence of
"or" being less than "and". So I would always expect the "and" clauses to
be evaluated first, then the "or". Just like in math, where in an
equation, I expect that the multiplication (and) is done before the
addition (or). Unless modified by parentheses just as in your second
example.

On Sat, 12 Aug 2000, Philip Warner wrote:

> 
> I have a peculiar problem that I can't reproduce on a trivial database:
> 
> select product, priority, count(*) as completed from issue 
> where
>     product = 'DIS' 
>     and create_date < '1-Aug-2000' and finish_date >= '1-Aug-2000' or
> finish_date is null
> group by product, priority;
> 
> This produces a list of all products - not just 'DIS'. If I put the last
> two clauses in parnthesis, then it works as expected:
> 
> select product, priority, count(*) as completed from issue 
> where
>     product = 'DIS' 
>     and create_date < '1-Aug-2000' and (finish_date >= '1-Aug-2000' or
> finish_date is null)
> group by product, priority;
> 
> Which makes me think that the precedence of 'or' is not what I expected. Is
> this a feature? If so, the fact that I get precisely the opposite behaviour
> in simple test databases must be a bug, I think.
> 
> Any help or explanation would be appreciated...
> 
> 
> 
> ----------------------------------------------------------------
> Philip Warner                    |     __---_____
> Albatross Consulting Pty. Ltd.   |----/       -  \
> (A.B.N. 75 008 659 498)          |          /(@)   ______---_
> Tel: (+61) 0500 83 82 81         |                 _________  \
> Fax: (+61) 0500 83 82 82         |                 ___________ |
> Http://www.rhyme.com.au          |                /           \|
>                                  |    --________--
> PGP key available upon request,  |  /
> and from pgp5.ai.mit.edu:11371   |/
> 



Re: Operator Precedence problem?

От
Philip Warner
Дата:
At 08:53 12/08/00 -0500, John McKown wrote:
>Every language that I've ever used (other than APL) has the precedence of
>"or" being less than "and". So I would always expect the "and" clauses to
>be evaluated first, then the "or". Just like in math, where in an
>equation, I expect that the multiplication (and) is done before the
>addition (or). Unless modified by parentheses just as in your second
>example.

You're quite right; I think I must have been punch drunk from beating my
head against another problem. Thanks.


----------------------------------------------------------------
Philip Warner                    |     __---_____
Albatross Consulting Pty. Ltd.   |----/       -  \
(A.B.N. 75 008 659 498)          |          /(@)   ______---_
Tel: (+61) 0500 83 82 81         |                 _________  \
Fax: (+61) 0500 83 82 82         |                 ___________ |
Http://www.rhyme.com.au          |                /           \|                                |    --________--
PGP key available upon request,  |  /
and from pgp5.ai.mit.edu:11371   |/


Re: Operator Precedence problem?

От
Tom Lane
Дата:
Philip Warner <pjw@rhyme.com.au> writes:
> Which makes me think that the precedence of 'or' is not what I
> expected.

OR is certainly lower-precedence than AND --- this is hard-wired in the
grammar and not subject to change across databases.  It's also required
by SQL92:
        <search condition> ::=               <boolean term>             | <search condition> OR <boolean term>
        <boolean term> ::=               <boolean factor>             | <boolean term> AND <boolean factor>
        <boolean factor> ::=             [ NOT ] <boolean test>
        <boolean test> ::=             <boolean primary> [ IS [ NOT ] <truth value> ]
        <truth value> ::=               TRUE             | FALSE             | UNKNOWN
        <boolean primary> ::=               <predicate>             | <left paren> <search condition> <right paren>

BTW, I notice that we do not correctly implement the IS tests.
The parser turns them into "<primary> = 't'::bool" and so on,
which is wrong because it will yield NULL for NULL input, which
is contrary to the spec for these tests.  We need specialized
functions comparable to the ones for IS NULL (in fact, IS UNKNOWN
should be equivalent to IS NULL except for requiring a boolean
input, AFAICT).
        regards, tom lane


Re: Operator Precedence problem?

От
Jesus Aneiros
Дата:
The ANSI/ISO standard specifies that NOT has the highest priority, then
AND and finally OR.

--
Jesus Aneiros Sosa
mailto:aneiros@jagua.cfg.sld.cu
http://jagua.cfg.sld.cu/~aneiros

On Sat, 12 Aug 2000, Philip Warner wrote:

> 
> I have a peculiar problem that I can't reproduce on a trivial database:
> 
> select product, priority, count(*) as completed from issue 
> where
>     product = 'DIS' 
>     and create_date < '1-Aug-2000' and finish_date >= '1-Aug-2000' or
> finish_date is null
> group by product, priority;
> 
> This produces a list of all products - not just 'DIS'. If I put the last
> two clauses in parnthesis, then it works as expected:
> 
> select product, priority, count(*) as completed from issue 
> where
>     product = 'DIS' 
>     and create_date < '1-Aug-2000' and (finish_date >= '1-Aug-2000' or
> finish_date is null)
> group by product, priority;
> 
> Which makes me think that the precedence of 'or' is not what I expected. Is
> this a feature? If so, the fact that I get precisely the opposite behaviour
> in simple test databases must be a bug, I think.
> 
> Any help or explanation would be appreciated...
> 
> 
> 
> ----------------------------------------------------------------
> Philip Warner                    |     __---_____
> Albatross Consulting Pty. Ltd.   |----/       -  \
> (A.B.N. 75 008 659 498)          |          /(@)   ______---_
> Tel: (+61) 0500 83 82 81         |                 _________  \
> Fax: (+61) 0500 83 82 82         |                 ___________ |
> Http://www.rhyme.com.au          |                /           \|
>                                  |    --________--
> PGP key available upon request,  |  /
> and from pgp5.ai.mit.edu:11371   |/
>