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

From Seckin Pulatkan
Subject Re: Group by clause problem with postgresql jdbc 9.0-801
Date
Msg-id BAY162-ds828EC6D8F9275E84D7975CE1E0@phx.gbl
Whole thread Raw
In response to Re: Group by clause problem with postgresql jdbc 9.0-801  (Simon Riggs <simon@2ndQuadrant.com>)
List pgsql-jdbc
Thanks for replies.
I've tried giving alias with criteria api before posting the message but
couldn't see on the query in the logs.
I will work more on that and let you know about the result.

Thanks,

Seckin

-----Oorspronkelijk bericht-----
Van: Simon Riggs [mailto:simon@2ndQuadrant.com]
Verzonden: 08 September 2011 08:01
Aan: Maciek Sakrejda
CC: Dave Cramer; Seckin Pulatkan; pgsql-jdbc
Onderwerp: Re: [JDBC] Group by clause problem with postgresql jdbc 9.0-801

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: Simon Riggs
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