pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Matthew Bellew <matthew@bellew.net>
To: pgsql-jdbc@postgresql.org
Subject: Inconsistent casting with literal vs parameter
Date: Tue, 07 Feb 2006 14:28:10 -0800
Message-ID: <43E91EFA.4080106@bellew.net> (raw)

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..

Thanks,
Matt  

    -- SQL
    create table Floats (x float);
    insert into Floats values (1);
    insert into Floats values (2);
    insert into Floats values (3);
    insert into Floats values (4);
    insert into Floats values (5);
    insert into Floats values (10);
    insert into Floats values (20);
    insert into Floats values (30);
    insert into Floats values (40);
    insert into Floats values (50);
    insert into Floats values (100);
    insert into Floats values (200);
    insert into Floats values (300);
    insert into Floats values (400);
    insert into Floats values (500);

    select 'QUERY 1', *
    from Floats
    where x < 100;

    create or replace function byint(int) returns setof float as $$
    select *
    from Floats
    where x < $1
    $$ LANGUAGE SQL;

    select byint(100);

    select 'QUERY 2', *
    from Floats
    where x < '100';

    create or replace function bystr(text) returns setof float as $$
    select *
    from Floats
    where x < $1
    $$ LANGUAGE SQL;

    select bystr('100');


    -- JAVA
    PreparedStatement stmtA = conn.prepareStatement("SELECT * FROM
    Floats WHERE x < '100'");
    ResultSet rsltA = stmtA.executeQuery();
    System.out.println("A\n----------");
    while (rsltA.next())
    System.out.println(rsltA.getFloat(1));

    PreparedStatement stmtB = conn.prepareStatement("SELECT * FROM
    Floats WHERE x < ?");
    stmtB.setObject(1, "100");
    ResultSet rsltB = stmtB.executeQuery();
    System.out.println("B\n----------");
    while (rsltB.next())
    System.out.println(rsltB.getFloat(1));

    PreparedStatement stmtC = conn.prepareStatement("SELECT * FROM
    Floats WHERE x < ?");
    stmtC.setString(1, "100");
    ResultSet rsltC = stmtB.executeQuery();
    System.out.println("C\n----------");
    while (rsltC.next())
    System.out.println(rsltC.getFloat(1));

    -- OUTPUT
    A
    ----------
    1.0
    2.0
    3.0
    4.0
    5.0
    10.0
    20.0
    30.0
    40.0
    50.0
    B
    ----------
    1.0
    10.0
    C
    ----------
    1.0
    10.0

view thread (7+ messages)  latest in thread

Message-ID: <43E91EFA.4080106@bellew.net>
Permalink:  ../43E91EFA.4080106@bellew.net/
Also on:    postgresql.org/message-id/43E91EFA.4080106@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
  Subject: Re: Inconsistent casting with literal vs parameter
  In-Reply-To: <43E91EFA.4080106@bellew.net>
  References: <43E91EFA.4080106@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