X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id E4FE49DC82A for ; Tue, 7 Feb 2006 18:28:30 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 65896-05 for ; Tue, 7 Feb 2006 18:28:32 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 564D69DC80F for ; Tue, 7 Feb 2006 18:28:28 -0400 (AST) Received: from jarlite.fhcrc.org (JARLITE.FHCRC.ORG [140.107.42.11]) by svr4.postgresql.org (Postfix) with ESMTP id 1B95B5AF0A4 for ; Tue, 7 Feb 2006 22:28:30 +0000 (GMT) Received: from jade.fhcrc.org (JADE.FHCRC.ORG [140.107.42.223]) by jarlite.fhcrc.org (8.12.7/8.12.7/SuSE Linux 0.6) with ESMTP id k17MSRpR001440 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Tue, 7 Feb 2006 14:28:27 -0800 Received: from [140.107.157.114] (DHCP157114.FHCRC.ORG [140.107.157.114]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by jade.fhcrc.org (Postfix) with ESMTP id A0257E510 for ; Tue, 7 Feb 2006 15:28:27 -0800 (PST) Message-ID: <43E91EFA.4080106@bellew.net> Date: Tue, 07 Feb 2006 14:28:10 -0800 From: Matthew Bellew User-Agent: Mozilla Thunderbird 1.0.7 (Windows/20050923) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Inconsistent casting with literal vs parameter Content-Type: multipart/alternative; boundary="------------010308060405080209060703" X-PMX-Version: 4.7.1.128075, Antispam-Engine: 2.2.0.0, Antispam-Data: 2006.02.07.134605 X-FHCRC-SCANNED: Tue Feb 7 14:28:28 2006 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.001 required=5 tests=[HTML_MESSAGE=0.001] X-Spam-Score: 0.001 X-Spam-Level: X-Archive-Number: 200602/44 X-Sequence-Number: 14641 This is a multi-part message in MIME format. --------------010308060405080209060703 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 --------------010308060405080209060703 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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

--------------010308060405080209060703--