dbTalk Databases Forums  

[BUGS] BUG #2242: Inconsistent casting in query with literal vs query with parameter

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss [BUGS] BUG #2242: Inconsistent casting in query with literal vs query with parameter in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Matthew Bellew
 
Posts: n/a

Default [BUGS] BUG #2242: Inconsistent casting in query with literal vs query with parameter - 02-06-2006 , 07:09 PM







The following bug has been logged online:

Bug reference: 2242
Logged by: Matthew Bellew
Email address: postgres (AT) bellew (DOT) net
PostgreSQL version: 8.1.0
Operating system: XP SP2
Description: Inconsistent casting in query with literal vs query with
parameter
Details:

In the script below, I'd expect all four queries to return 10 rows
(1,2,3,4,5,10,20,30,40,50). However, function bystr() returns two rows
(1,10). Clearly, in this one case the query processor is casting the column
to the parameter type, rather than the other way around. The optimizer
should always preferentially cast the parameter to the type of the column.


--drop table Floats;
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');

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] BUG #2242: Inconsistent casting in query with literal vs query with parameter - 02-07-2006 , 08:49 AM






"Matthew Bellew" <postgres (AT) bellew (DOT) net> writes:
Quote:
In the script below, I'd expect all four queries to return 10 rows
(1,2,3,4,5,10,20,30,40,50). However, function bystr() returns two rows
(1,10). Clearly, in this one case the query processor is casting the column
to the parameter type, rather than the other way around. The optimizer
should always preferentially cast the parameter to the type of the column.
I see no bug here. You are confused about the difference between an
unknown literal ('100') and a value that is actually declared to be of
type text. See
http://www.postgresql.org/docs/8.1/static/typeconv.html

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.