pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Matthew Bellew <matthew@bellew.net>
To: Kris Jurka <books@ejurka.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Inconsistent casting with literal vs parameter
Date: Tue, 07 Feb 2006 14:59:42 -0800
Message-ID: <43E9265E.3070209@bellew.net> (raw)
In-Reply-To: <Pine.BSO.4.61.0602071733300.5391@leary.csoft.net>
References: <43E91EFA.4080106@bellew.net>
	<Pine.BSO.4.61.0602071733300.5391@leary.csoft.net>

Fantastic, thanks for the pointer this parameter.  I would argue that 
the expected behavior would be that these two cases are semantically 
equivalent.  The fact that this is configurable is great, but my gripe 
would be that the default setting results in 'wrong' answers for a very 
unobvious reason.

Again thanks,
Matt

Kris Jurka wrote:

>
>
> On Tue, 7 Feb 2006, Matthew Bellew wrote:
>
>> I have an example here where replacing a string literal with a string 
>> parameter in the same query yields different results.  See Java code 
>> below, and note that this example works with int as well as float.  
>> In one case the comparisions are done as numbers in the other they 
>> are done as strings. The explanation I received from Tom Lane is that 
>> in stmtA with (x < '100'), '100' is an untyped literal, in stmtB and 
>> stmtC (x < ?), the parameter is treated as typed, and thus the 
>> coercion occurs differently.   I'm afraid someone is going to answer 
>> "it works this way because..."  I'm more interested to know if anyone 
>> else thinks it is a problem that these two statements return 
>> different results. I seems to me that these queries reasonably have 
>> to be considered the same, and should return the same answer..
>
>
> I don't think these queries are the same, consider
> SELECT '12' < '2', 12 < 2;
>
> In the first case the server considers it text and the second it 
> considers it numeric.  Now when considering the mixed case the server 
> has to decide what to do.  When you say 12 < '2' you aren't 
> conclusively stating what type '2' is and it gets coerced to numeric, 
> but when you say setString you are explicitly telling it that it is a 
> text data type and a text comparison should be done.  When you say 
> setObject with a String object you also say that it is text data.  If 
> you want it interpreted as numeric data use setFloat or similar.
>
> The 8.2 driver has an option to allow setString data to passed to the 
> server without a type and you will get the result you desire.  See the 
> stringtype parameter here:
>
> http://jdbc.postgresql.org/documentation/head/connect.html#connection-parameters 
>
>
> Kris Jurka
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: explain analyze is your friend




view thread (7+ messages)  latest in thread

Message-ID: <43E9265E.3070209@bellew.net>
Permalink:  ../43E9265E.3070209@bellew.net/
Also on:    postgresql.org/message-id/43E9265E.3070209@bellew.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: matthew@bellew.net, books@ejurka.com
  Subject: Re: Inconsistent casting with literal vs parameter
  In-Reply-To: <43E9265E.3070209@bellew.net>
  References: <43E91EFA.4080106@bellew.net> <Pine.BSO.4.61.0602071733300.5391@leary.csoft.net> <43E9265E.3070209@bellew.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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