Обсуждение: Parethesis don't work correctly in where clause

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

Parethesis don't work correctly in where clause

От
Casey Allen Shobe
Дата:
Your name         :  Casey Allen Shobe
Your email address:  cshobe@secureworks.com

System Configuration
---------------------
  Architecture (example: Intel Pentium)         :  Intel Pentium III 1GHz
  Operating System (example: Linux 2.0.26 ELF)  :  Linux 2.4.20
  PostgreSQL version (example: PostgreSQL-6.4.2):  PostgreSQL-7.3.2
  Compiler used (example:  gcc 2.8.0)           :  gcc 3.2.2
      $CC environment variable                  :  gcc -s
      $CFLAGS environment variable              :  -O3 -mcpu=i686

Please enter a FULL description of your problem:
------------------------------------------------
I have a query with:
where "n"."revision" = 2

If I add:
and (
    "t"."name" = 'nibc'
and    "n"."identifier" = 12345
)
...it works.

If I add:
and (
    "t"."name" = 'client'
and    "n"."identifier" = 123
)
instead of the previous...it also works as expected.

However, if I add:
and (
        "t"."name" = 'nibc'
    and    "n"."identifier" = 1274
) or (
        "t"."name" = 'client'
    and    "n"."identifier" = 383
)
PostgreSQL returns only the revision 2 tuple for nibc 1274, but also a
revision 1 tuple for client 383.  ONLY things matching revision 2 should be
found, but for some reason the "n"."revision" = 2 isn't being applied
globally, but only to the first set of parenthesis.

This workaround makes it work:
where (
        "t"."name" = 'nibc'
    and    "n"."identifier" = 1274
    and    "n"."revision" = 2
) or (
        "t"."name" = 'client'
    and    "n"."identifier" = 383
    and    "n"."revision" = 2
)
...but that can get horribly repetitive after a while.

Please describe a way to repeat the problem.   Please try to provide a
concise reproducible example, if at all possible:
----------------------------------------------------------------------

See above SQL.  Apply to any table.

If you know how this problem might be fixed, list the solution below:
---------------------------------------------------------------------

By correctly keeping the global "n"."revision" defined outside of the
parenthases in a global state.

--
Casey Allen Shobe, Developer & Linux Administrator
SecureWorks, Inc. / 404.327.6339 x169 / Fax: 404.728.0144
cshobe@secureworks.net / http://www.secureworks.net

00001110 is good for the soul.

Re: Parethesis don't work correctly in where clause

От
Oliver Elphick
Дата:
On Thu, 2003-04-17 at 09:23, Casey Allen Shobe wrote:

> I have a query with:
> where "n"."revision" = 2
...
> However, if I add:
> and (
>         "t"."name" = 'nibc'
>     and    "n"."identifier" = 1274
> ) or (
>         "t"."name" = 'client'
>     and    "n"."identifier" = 383
> )
> PostgreSQL returns only the revision 2 tuple for nibc 1274, but also a
> revision 1 tuple for client 383.  ONLY things matching revision 2 should be
> found, but for some reason the "n"."revision" = 2 isn't being applied
> globally, but only to the first set of parenthesis.

This is simply a matter of boolean logic and the precedence of the AND
and OR operators.

If you instead force the operator precedence and add

and ((
                "t"."name" = 'nibc'
        and     "n"."identifier" = 1274
) or (
                "t"."name" = 'client'
        and     "n"."identifier" = 383
))

it should work, no?

--
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight, UK                             http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "But as many as received him, to them gave he power to
      become the sons of God, even to them that believe on
      his name."    John 1:12

Re: Parethesis don't work correctly in where clause

От
Casey Allen Shobe
Дата:
Ahh, thank you.  I completely missed that typo.

I'm sorry for the bother with the false bug.  I'm quite happy that there
actually wasn't one :-).

- Casey

-----Original Message-----
From: Oliver Elphick [mailto:olly@lfix.co.uk]
Sent: 17. apr=EDl 2003 10:48
To: Casey Allen Shobe
Cc: 'pgsql-bugs@postgresql.org'
Subject: Re: [BUGS] Parethesis don't work correctly in where clause


On Thu, 2003-04-17 at 09:23, Casey Allen Shobe wrote:

> I have a query with:
> where "n"."revision" =3D 2
...
> However, if I add:
> and (
>         "t"."name" =3D 'nibc'
>     and    "n"."identifier" =3D 1274
> ) or (
>         "t"."name" =3D 'client'
>     and    "n"."identifier" =3D 383
> )
> PostgreSQL returns only the revision 2 tuple for nibc 1274, but also a
> revision 1 tuple for client 383.  ONLY things matching revision 2 should
be
> found, but for some reason the "n"."revision" =3D 2 isn't being applied
> globally, but only to the first set of parenthesis.

This is simply a matter of boolean logic and the precedence of the AND
and OR operators.

If you instead force the operator precedence and add=20

and ((
                "t"."name" =3D 'nibc'
        and     "n"."identifier" =3D 1274
) or (
                "t"."name" =3D 'client'
        and     "n"."identifier" =3D 383
))

it should work, no?

--=20
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight, UK                             http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
     "But as many as received him, to them gave he power to=20
      become the sons of God, even to them that believe on=20
      his name."    John 1:12=20