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

From: Simon Riggs <simon(at)2ndQuadrant(dot)com>
To: Maciek Sakrejda <msakrejda(at)truviso(dot)com>
Cc: Dave Cramer <pg(at)fastcrypt(dot)com>, Seckin Pulatkan <seckinpulatkan(at)hotmail(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 06:00:53
Message-ID: CA+U5nMJcB5Zy7KOKUa6zNLiPKgz9ZmXyckM_8fTe-VHpmfrhTw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

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

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Seckin Pulatkan 2011-09-08 08:11:50 Re: Group by clause problem with postgresql jdbc 9.0-801
Previous Message Maciek Sakrejda 2011-09-07 21:55:10 Re: Group by clause problem with postgresql jdbc 9.0-801