pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Literal vs parameterized 'timestamp with time zone' value
9+ messages / 5 participants
[nested] [flat]

* Literal vs parameterized 'timestamp with time zone' value
@ 2007-06-18 12:34  Christopher Hunt <huntc@internode.on.net>
  0 siblings, 4 replies; 9+ messages in thread

From: Christopher Hunt @ 2007-06-18 12:34 UTC (permalink / raw)
  To: pgsql-jdbc

Hi there,

This one has been baffling me for several hours so I hope that the  
list can help.

I'm having an awful difficulty specifying a parameter in a select  
where clause of a prepared statement.

Please consider this schema:

           Table "public.moving_object_status"
     Column    |            Type             | Modifiers
--------------+-----------------------------+-----------
validtime    | timestamp(2) with time zone | not null
Indexes:
     "moving_object_status_validtime" btree (validtime)

(other columns removed for brevity).

If I create the following prepared statement:

PreparedStatement sqlStatement =
   sqlConnection.prepareStatement("select validtime from  
moving_object_status where validtime < '2005-06-08T20:05:45.825+0'");

then 1 row is returned as I would expect given my dataset.

However if I use a parameter:

PreparedStatement sqlStatement =
   sqlConnection.prepareStatement("select validtime from  
moving_object_status where validtime < ?");
int columnIndex = 1;
sqlStatement.setString(columnIndex++, "2005-06-08T20:05:45.825+0");

No rows are returned.

Can anyone explain the difference in results given a literal value  
and a parameterised value in this context?

Kind regards,
Christopher



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Literal vs parameterized 'timestamp with time zone' value
@ 2007-06-18 12:41  Christopher Hunt <huntc@internode.on.net>
  parent: Christopher Hunt <huntc@internode.on.net>
  3 siblings, 0 replies; 9+ messages in thread

From: Christopher Hunt @ 2007-06-18 12:41 UTC (permalink / raw)
  To: pgsql-jdbc

I should quickly add that I'm using v.8.0.4 of Postgres and the  
postgresql-8.2-505.jdbc3.jar

Cheers,
-C

On 18/06/2007, at 10:34 PM, Christopher Hunt wrote:

> Hi there,
>
> This one has been baffling me for several hours so I hope that the  
> list can help.
>
> I'm having an awful difficulty specifying a parameter in a select  
> where clause of a prepared statement.
>
> Please consider this schema:
>
>           Table "public.moving_object_status"
>     Column    |            Type             | Modifiers
> --------------+-----------------------------+-----------
> validtime    | timestamp(2) with time zone | not null
> Indexes:
>     "moving_object_status_validtime" btree (validtime)
>
> (other columns removed for brevity).
>
> If I create the following prepared statement:
>
> PreparedStatement sqlStatement =
>   sqlConnection.prepareStatement("select validtime from  
> moving_object_status where validtime < '2005-06-08T20:05:45.825+0'");
>
> then 1 row is returned as I would expect given my dataset.
>
> However if I use a parameter:
>
> PreparedStatement sqlStatement =
>   sqlConnection.prepareStatement("select validtime from  
> moving_object_status where validtime < ?");
> int columnIndex = 1;
> sqlStatement.setString(columnIndex++, "2005-06-08T20:05:45.825+0");
>
> No rows are returned.
>
> Can anyone explain the difference in results given a literal value  
> and a parameterised value in this context?
>
> Kind regards,
> Christopher



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Literal vs parameterized 'timestamp with time zone' value
@ 2007-06-18 12:46  Heikki Linnakangas <heikki@enterprisedb.com>
  parent: Christopher Hunt <huntc@internode.on.net>
  3 siblings, 1 reply; 9+ messages in thread

From: Heikki Linnakangas @ 2007-06-18 12:46 UTC (permalink / raw)
  To: Christopher Hunt <huntc@internode.on.net>; +Cc: pgsql-jdbc

Christopher Hunt wrote:
> However if I use a parameter:
> 
> PreparedStatement sqlStatement =
>   sqlConnection.prepareStatement("select validtime from 
> moving_object_status where validtime < ?");
> int columnIndex = 1;
> sqlStatement.setString(columnIndex++, "2005-06-08T20:05:45.825+0");
> 
> No rows are returned.
> 
> Can anyone explain the difference in results given a literal value and a 
> parameterised value in this context?

I don't really have an explanation, but I would recommend using 
setTimestamp instead of setString for timestamp values.

Maybe there's a time or timezone difference between client and server, 
and in the former case the server setting is used, and in the latter, 
the client setting?

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Literal vs parameterized 'timestamp with time zone' value
@ 2007-06-18 12:51  Dave Cramer <pg@fastcrypt.com>
  parent: Christopher Hunt <huntc@internode.on.net>
  3 siblings, 0 replies; 9+ messages in thread

From: Dave Cramer @ 2007-06-18 12:51 UTC (permalink / raw)
  To: Christopher Hunt <huntc@internode.on.net>; +Cc: pgsql-jdbc


On 18-Jun-07, at 8:34 AM, Christopher Hunt wrote:

> Hi there,
>
> This one has been baffling me for several hours so I hope that the  
> list can help.
>
> I'm having an awful difficulty specifying a parameter in a select  
> where clause of a prepared statement.
>
> Please consider this schema:
>
>           Table "public.moving_object_status"
>     Column    |            Type             | Modifiers
> --------------+-----------------------------+-----------
> validtime    | timestamp(2) with time zone | not null
> Indexes:
>     "moving_object_status_validtime" btree (validtime)
>
> (other columns removed for brevity).
>
> If I create the following prepared statement:
>
> PreparedStatement sqlStatement =
>   sqlConnection.prepareStatement("select validtime from  
> moving_object_status where validtime < '2005-06-08T20:05:45.825+0'");
>
> then 1 row is returned as I would expect given my dataset.
>
> However if I use a parameter:
>
> PreparedStatement sqlStatement =
>   sqlConnection.prepareStatement("select validtime from  
> moving_object_status where validtime < ?");
> int columnIndex = 1;
> sqlStatement.setString(columnIndex++, "2005-06-08T20:05:45.825+0");
>
I think you need to use setTimestamp here...  I'm actually surprised  
it doesn't throw an exception.
> No rows are returned.
>
> Can anyone explain the difference in results given a literal value  
> and a parameterised value in this context?
>
> Kind regards,
> Christopher
>
> ---------------------------(end of  
> broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
>       subscribe-nomail command to majordomo@postgresql.org so that  
> your
>       message can get through to the mailing list cleanly




^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Literal vs parameterized 'timestamp with time zone' value
@ 2007-06-18 12:59  Christopher Hunt <huntc@internode.on.net>
  parent: Heikki Linnakangas <heikki@enterprisedb.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Christopher Hunt @ 2007-06-18 12:59 UTC (permalink / raw)
  To: Heikki Linnakangas <heikki@enterprisedb.com>; +Cc: pgsql-jdbc

Thanks for responding.

Alas, I have tried setTimestamp - in fact I started with that and  
ended up with a similar problem i.e. no rows returned with  
setTimestamp. I think that my problem is something else.

The client and server processes are on the same machine.

Any more ideas?

Cheers,
-C



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Literal vs parameterized 'timestamp with time zone' value
@ 2007-06-18 13:33  Dave Cramer <pg@fastcrypt.com>
  parent: Christopher Hunt <huntc@internode.on.net>
  0 siblings, 1 reply; 9+ messages in thread

From: Dave Cramer @ 2007-06-18 13:33 UTC (permalink / raw)
  To: Christopher Hunt <huntc@internode.on.net>; +Cc: Heikki Linnakangas <heikki@enterprisedb.com>; pgsql-jdbc

Christopher,

Use a  more recent jar. There is a problem with the spec, and  
postgresql. The spec does not have two different timestamps. In  
previous versions we bound the type to timestamp without timezone. In  
more recent versions we bind it to 'unknown' which allows the server  
to decide.

If this fails then appending ?protocolVersion=2 will work, however  
you won't be server side prepared statements.

Dave
On 18-Jun-07, at 8:59 AM, Christopher Hunt wrote:

> Thanks for responding.
>
> Alas, I have tried setTimestamp - in fact I started with that and  
> ended up with a similar problem i.e. no rows returned with  
> setTimestamp. I think that my problem is something else.
>
> The client and server processes are on the same machine.
>
> Any more ideas?
>
> Cheers,
> -C
>
> ---------------------------(end of  
> broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
>               http://archives.postgresql.org




^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Literal vs parameterized 'timestamp with time zone' value
@ 2007-06-18 13:58  Christopher Hunt <huntc@internode.on.net>
  parent: Dave Cramer <pg@fastcrypt.com>
  0 siblings, 0 replies; 9+ messages in thread

From: Christopher Hunt @ 2007-06-18 13:58 UTC (permalink / raw)
  To: Dave Cramer <pg@fastcrypt.com>; +Cc: Heikki Linnakangas <heikki@enterprisedb.com>; pgsql-jdbc

Thanks!

On 18/06/2007, at 11:33 PM, Dave Cramer wrote:

> Christopher,
>
> Use a  more recent jar. There is a problem with the spec, and  
> postgresql. The spec does not have two different timestamps. In  
> previous versions we bound the type to timestamp without timezone.  
> In more recent versions we bind it to 'unknown' which allows the  
> server to decide.
>
> If this fails then appending ?protocolVersion=2 will work, however  
> you won't be server side prepared statements.



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Literal vs parameterized 'timestamp with time zone' value
@ 2007-06-19 16:47  Kris Jurka <books@ejurka.com>
  parent: Christopher Hunt <huntc@internode.on.net>
  3 siblings, 1 reply; 9+ messages in thread

From: Kris Jurka @ 2007-06-19 16:47 UTC (permalink / raw)
  To: Christopher Hunt <huntc@internode.on.net>; +Cc: pgsql-jdbc



On Mon, 18 Jun 2007, Christopher Hunt wrote:

> This one has been baffling me for several hours so I hope that the list can 
> help.
>
> I'm having an awful difficulty specifying a parameter in a select where 
> clause of a prepared statement.
>
> Please consider this schema:
>
>         Table "public.moving_object_status"
>   Column    |            Type             | Modifiers
> --------------+-----------------------------+-----------
> validtime    | timestamp(2) with time zone | not null
> Indexes:
>   "moving_object_status_validtime" btree (validtime)
>
> (other columns removed for brevity).
>
> If I create the following prepared statement:
>
> PreparedStatement sqlStatement =
> sqlConnection.prepareStatement("select validtime from moving_object_status 
> where validtime < '2005-06-08T20:05:45.825+0'");
>
> then 1 row is returned as I would expect given my dataset.
>
> However if I use a parameter:
>
> PreparedStatement sqlStatement =
> sqlConnection.prepareStatement("select validtime from moving_object_status 
> where validtime < ?");
> int columnIndex = 1;
> sqlStatement.setString(columnIndex++, "2005-06-08T20:05:45.825+0");
>
> No rows are returned.
>
> Can anyone explain the difference in results given a literal value and a 
> parameterised value in this context?
>

In the second example a string comparison is being done instead of a 
timestamp comparison.  Consider this psql example:

jurka=# select now() < 'a'::text;
  ?column?
----------
  t
(1 row)

When you say "sqlStatement.setString" you are saying you have a string 
variable and that's the way the server interprets it.  When you leave 
something as a literal it gets typed as "unknown" by the server which then 
infers by the comparison with timestamp that you want a timestamp:

jurka=# select now() < 'a';
ERROR:  invalid input syntax for type timestamp with time zone: "a"

So you really do need to say setTimestamp or use the drivers option to not 
force binding of setString to a string type.  Why setTimestamp is not 
working for you is probably timezone related, but without more details 
I'm not sure how to help you track that down.

Kris Jurka



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Literal vs parameterized 'timestamp with time zone' value
@ 2007-06-19 17:19  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Kris Jurka <books@ejurka.com>
  0 siblings, 0 replies; 9+ messages in thread

From: Tom Lane @ 2007-06-19 17:19 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: Christopher Hunt <huntc@internode.on.net>; pgsql-jdbc

Kris Jurka <books@ejurka.com> writes:
> In the second example a string comparison is being done instead of a 
> timestamp comparison.  Consider this psql example:

> jurka=# select now() < 'a'::text;
>   ?column?
> ----------
>   t
> (1 row)

BTW, as of 8.3 this particular foot-gun will go away:

regression=# select now() < 'a'::text;
ERROR:  operator does not exist: timestamp with time zone < text
LINE 1: select now() < 'a'::text;
                     ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

You'll still be able to force a textual comparison if you really want
one:

regression=# select now()::text < 'a'::text;
 ?column?
----------
 t
(1 row)

but it won't happen silently.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 9+ messages in thread


end of thread, other threads:[~2007-06-19 17:19 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-06-18 12:34 Literal vs parameterized 'timestamp with time zone' value Christopher Hunt <huntc@internode.on.net>
2007-06-18 12:41 ` Christopher Hunt <huntc@internode.on.net>
2007-06-18 12:46 ` Heikki Linnakangas <heikki@enterprisedb.com>
2007-06-18 12:59   ` Christopher Hunt <huntc@internode.on.net>
2007-06-18 13:33     ` Dave Cramer <pg@fastcrypt.com>
2007-06-18 13:58       ` Christopher Hunt <huntc@internode.on.net>
2007-06-18 12:51 ` Dave Cramer <pg@fastcrypt.com>
2007-06-19 16:47 ` Kris Jurka <books@ejurka.com>
2007-06-19 17:19   ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox