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

From: Seckin Pulatkan <seckinpulatkan(at)hotmail(dot)com>
To: "'Simon Riggs'" <simon(at)2ndQuadrant(dot)com>, "'Maciek Sakrejda'" <msakrejda(at)truviso(dot)com>
Cc: "'Dave Cramer'" <pg(at)fastcrypt(dot)com>, "'pgsql-jdbc'" <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Group by clause problem with postgresql jdbc 9.0-801
Date: 2011-09-08 09:23:24
Message-ID: BAY162-ds607D3F9056B0A431E8D2FCE1E0@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

I converted my code to native query version and then query worked
successfully.
Thank you for pointing out the problem and your suggestions.

Kind Regards,

Seckin

-----Oorspronkelijk bericht-----
Van: Simon Riggs [mailto:simon(at)2ndQuadrant(dot)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(at)truviso(dot)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

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message dv 2011-09-09 11:05:16 Re: Issue getColumns(): Nonstandard use of \\ in a string literal
Previous Message Seckin Pulatkan 2011-09-08 08:11:50 Re: Group by clause problem with postgresql jdbc 9.0-801