Re: Group by clause problem with postgresql jdbc 9.0-801 - Mailing list pgsql-jdbc

From Simon Riggs
Subject Re: Group by clause problem with postgresql jdbc 9.0-801
Date
Msg-id CA+U5nMJcB5Zy7KOKUa6zNLiPKgz9ZmXyckM_8fTe-VHpmfrhTw@mail.gmail.com
Whole thread Raw
In response to Re: Group by clause problem with postgresql jdbc 9.0-801  (Maciek Sakrejda <msakrejda@truviso.com>)
Responses Re: Group by clause problem with postgresql jdbc 9.0-801
Re: Group by clause problem with postgresql jdbc 9.0-801
List pgsql-jdbc
On Wed, Sep 7, 2011 at 10:55 PM, Maciek Sakrejda <msakrejda@truviso.com> wrote:
> Could this be the difference between
>
> cqdb=# prepare foo(int, int) as select x / $1 from generate_series(1,10) x
> group by x / $2; execute foo(3,3);
> ERROR:  column "x.x" must appear in the GROUP BY clause or be used in an
> aggregate function
> ERROR:  prepared statement "foo" does not exist
>
> and
>
> cqdb=# select x / 3 from generate_series(1,10) x group by x / 3;
>  ?column?
> ----------
>         2
>         0
>         3
>         1
> (4 rows)
>
> ?
>
> This could be the case if Navicat is inlining the parameters. Unfortunately,
> I can't think of a good suggestion for a fix if this is indeed the case
> (except that the generated queries should group by the columns of interest,
> rather than repeating the expressions, but that's probably not an easy
> fix)...

Yes, agreed.

The server error message is clear from the messages shown, nothing more needed.

The problem is that
  date_part(?, age(t1.create_timestamp))

in the SELECT caluse does not match
  date_part(?, age(t1.create_timestamp))

in the GROUP BY clause, because the presence of parameter markers
means that they are potentially different expressions.

Try re-writing the query like this:

* In SELECT clause write
  date_part(?, age(t1.create_timestamp)) AS expression1

* In GROUP BY clause write
  expression1 instead of date_part(?, age(t1.create_timestamp))

The SQL parser will then recognise the GROUP BY correctly.

I think we could regard this as a JDBC annoyance, but we're not
parsing the SQL at that point so it's got no way to know the two
parameter markers ("?") would be the same. I'm sure the same problem
would exist in JDBC with other RDBMS also, since its a problem caused
by unnamed parameter markers.

--
 Simon Riggs                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services

pgsql-jdbc by date:

Previous
From: Maciek Sakrejda
Date:
Subject: Re: Group by clause problem with postgresql jdbc 9.0-801
Next
From: Seckin Pulatkan
Date:
Subject: Re: Group by clause problem with postgresql jdbc 9.0-801