Thread: BUG #2889: Syntax error: WHERE ANY(arrayfield) = N

BUG #2889: Syntax error: WHERE ANY(arrayfield) = N

From
"David"
Date:
The following bug has been logged online:

Bug reference:      2889
Logged by:          David
Email address:      higgsd@gmail.com
PostgreSQL version: 8.1.5
Operating system:   OpenBSD 3.9
Description:        Syntax error: WHERE ANY(arrayfield) = N
Details:

In reference to the array documentation at:
http://www.postgresql.org/docs/8.1/static/arrays.html#AEN5698

This statement works:
=> SELECT * FROM sal_emp WHERE 10000 = ANY (pay_by_quarter);

But this does not:
=> SELECT * FROM sal_emp WHERE ANY (pay_by_quarter) = 10000;
ERROR:  syntax error at or near "ANY" at character ...

The ANY operator has the same problem, others may as well.

While trivial to work around, it is certainly annoying and I can't think of
a good reason why it shouldn't work either way.

Re: BUG #2889: Syntax error: WHERE ANY(arrayfield) = N

From
Tom Lane
Date:
"David" <higgsd@gmail.com> writes:
> This statement works:
> => SELECT * FROM sal_emp WHERE 10000 = ANY (pay_by_quarter);

> But this does not:
> => SELECT * FROM sal_emp WHERE ANY (pay_by_quarter) = 10000;
> ERROR:  syntax error at or near "ANY" at character ...

This is not a bug, it's the way the syntax works per SQL spec.
ANY must immediately follow the operator it relates to.  See
<quantified comparison predicate> syntax in the spec.

            regards, tom lane

Re: BUG #2889: Syntax error: WHERE ANY(arrayfield) = N

From
"David Higgs"
Date:
On 1/13/07, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> "David" <higgsd@gmail.com> writes:
> > This statement works:
> > => SELECT * FROM sal_emp WHERE 10000 = ANY (pay_by_quarter);
>
> > But this does not:
> > => SELECT * FROM sal_emp WHERE ANY (pay_by_quarter) = 10000;
> > ERROR:  syntax error at or near "ANY" at character ...
>
> This is not a bug, it's the way the syntax works per SQL spec.
> ANY must immediately follow the operator it relates to.  See
> <quantified comparison predicate> syntax in the spec.
>
>                         regards, tom lane
>

Aha, I see it in the docs now, although it's still rather unintuitive.
 Could the appropriate section on arrays be crosslinked to the ANY/ALL
page, to preempt this question in the future?

Thanks.

--david

Re: BUG #2889: Syntax error: WHERE ANY(arrayfield) = N

From
Bruce Momjian
Date:
David Higgs wrote:
> On 1/13/07, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> > "David" <higgsd@gmail.com> writes:
> > > This statement works:
> > > => SELECT * FROM sal_emp WHERE 10000 = ANY (pay_by_quarter);
> >
> > > But this does not:
> > > => SELECT * FROM sal_emp WHERE ANY (pay_by_quarter) = 10000;
> > > ERROR:  syntax error at or near "ANY" at character ...
> >
> > This is not a bug, it's the way the syntax works per SQL spec.
> > ANY must immediately follow the operator it relates to.  See
> > <quantified comparison predicate> syntax in the spec.
> >
> >                         regards, tom lane
> >
>
> Aha, I see it in the docs now, although it's still rather unintuitive.
>  Could the appropriate section on arrays be crosslinked to the ANY/ALL
> page, to preempt this question in the future?

I researched this and found this line right above the example you quoted
above:

    An alternative method is described in Section 9.17. The above query
    could be replaced by:

    SELECT * FROM sal_emp WHERE 10000 = ANY (pay_by_quarter);

and section 9.17 is 9.17. Row and Array Comparisons.  Not sure we can do
any better than that.

--
  Bruce Momjian   bruce@momjian.us
  EnterpriseDB    http://www.enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +