dbTalk Databases Forums  

[BUGS] BUG #1629: subquery IN returns incorrect results

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


Discuss [BUGS] BUG #1629: subquery IN returns incorrect results in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mike g
 
Posts: n/a

Default [BUGS] BUG #1629: subquery IN returns incorrect results - 04-27-2005 , 08:50 AM







The following bug has been logged online:

Bug reference: 1629
Logged by: mike g
Email address: mike (AT) thegodshalls (DOT) com
PostgreSQL version: 8.0
Operating system: Windows 2000
Description: subquery IN returns incorrect results
Details:

If I run this query:
SELECT distinct CAST(newprogram as varchar(60)) FROM
(SELECT t.propnbr,
CASE WHEN t.propname = 'A' THEN 'Am'
WHEN t.propname = 'B' THEN 'AMm'
WHEN t.propname = 'C' THEN 'I might vanish'
WHEN t.propname = 'D' THEN 'Bem'
WHEN t.propname = 'E' THEN 'Cm'
WHEN t.propname = 'F' THEN 'Clm'
WHEN t.propname = 'G' THEN 'Com'
WHEN t.propname = 'H' THEN 'Dm'
WHEN t.propname = 'I' THEN 'Er'
WHEN t.propname = 'J' THEN 'Err'
WHEN t.propname = 'K' THEN 'Em'
WHEN t.propname = 'L' THEN 'Fm'
WHEN t.propname = 'M' THEN 'Fm'
WHEN t.propname = 'N' THEN 'Gm'
WHEN t.propname = 'O' THEN 'Hm'
WHEN t.propname = 'P' THEN 'Dm'
WHEN t.propname = 'Q' THEN 'Lm'
WHEN t.propname = 'R' THEN 'Nm'
WHEN t.propname = 'S' THEN 'Om'
WHEN t.propname = 'T' THEN 'Err'
WHEN t.propname = 'U' THEN 'Rm'
WHEN t.propname = 'V' THEN 'Tm'
WHEN t.propname = 'W' THEN 'Tm'
ELSE t.propname
END as newprogram
FROM example_data t
INNER JOIN example_data2 b ON t.propco = b.propco
WHERE
upper(b.serviced) = 'STATE1' and
t.propname in ('A',
'B'
'C', --switch me
'D', -- and switch me
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X')) as my_data

My results are:
newprogram
Am
Bem
Clm
Cm
Com
Dm
Em
Er
Err
Fm
Gm
Hm
Lm
Nm
Om
Rm
Tm


If I just change the order of the data for the IN subquery portion

SELECT distinct CAST(newprogram as varchar(60)) FROM
(SELECT t.propnbr,
CASE WHEN t.propname = 'A' THEN 'Am'
WHEN t.propname = 'B' THEN 'AMm'
WHEN t.propname = 'C' THEN 'I might vanish'
WHEN t.propname = 'D' THEN 'Bem'
WHEN t.propname = 'E' THEN 'Cm'
WHEN t.propname = 'F' THEN 'Clm'
WHEN t.propname = 'G' THEN 'Com'
WHEN t.propname = 'H' THEN 'Dm'
WHEN t.propname = 'I' THEN 'Er'
WHEN t.propname = 'J' THEN 'Err'
WHEN t.propname = 'K' THEN 'Em'
WHEN t.propname = 'L' THEN 'Fm'
WHEN t.propname = 'M' THEN 'Fm'
WHEN t.propname = 'N' THEN 'Gm'
WHEN t.propname = 'O' THEN 'Hm'
WHEN t.propname = 'P' THEN 'Dm'
WHEN t.propname = 'Q' THEN 'Lm'
WHEN t.propname = 'R' THEN 'Nm'
WHEN t.propname = 'S' THEN 'Om'
WHEN t.propname = 'T' THEN 'Err'
WHEN t.propname = 'U' THEN 'Rm'
WHEN t.propname = 'V' THEN 'Tm'
WHEN t.propname = 'W' THEN 'Tm'
ELSE t.propname
END as newprogram
FROM example_data t
INNER JOIN example_data2 b ON t.propco = b.propco
WHERE
upper(b.serviced) = 'STATE1' and
t.propname in ('A',
'B'
'D', -- and switch me
'C', --switch me
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X')) as my_data

Gives this for a result
Am
Clm
Cm
Com
Dm
Em
Er
Err
Fm
Gm
Hm
I might vanish
Lm
Nm
Om
Rm
Tm

I will email a pg_dump of the two tables on request.
The results of either version are incorrect.

If you remove the IN subquery and replace each case with t.propname = 'A'
or... then correct results returned:

AMm
Am
Bem
Clm
Cm
Com
Dm
Em
Er
Err
Fm
Gm
Hm
I might vanish
Lm
Nm
Om
Rm
Tm

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

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

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

Default Re: [BUGS] BUG #1629: subquery IN returns incorrect results - 04-27-2005 , 09:58 AM






"mike g" <mike (AT) thegodshalls (DOT) com> writes:
Quote:
Description: subquery IN returns incorrect results
It's impossible to investigate this with the amount of information you
have provided. Please show a *self contained* example, including any
table declarations and test data needed.

regards, tom lane

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

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


Reply With Quote
  #3  
Old   
Russell Smith
 
Posts: n/a

Default Re: [BUGS] BUG #1629: subquery IN returns incorrect results - 04-27-2005 , 10:02 AM



On Wed, 27 Apr 2005 06:23 am, mike g wrote:
Quote:
=20
The following bug has been logged online:
=20
Bug reference: =A0 =A0 =A01629
Logged by: =A0 =A0 =A0 =A0 =A0mike g
Email address: =A0 =A0 =A0mike (AT) thegodshalls (DOT) com
PostgreSQL version: 8.0
Operating system: =A0 Windows 2000
Description: =A0 =A0 =A0 =A0subquery IN returns incorrect results
Details:=20
=20
If I run this query:
SELECT distinct CAST(newprogram as varchar(60)) FROM
=A0(SELECT t.propnbr,=20
=A0 =A0 =A0CASE WHEN t.propname =3D =A0'A' THEN 'Am'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'B' THEN 'AMm'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'C' THEN 'I might vanish'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'D' THEN 'Bem'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'E' THEN 'Cm'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'F' THEN 'Clm'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'G' THEN 'Com'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'H' THEN 'Dm'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'I' THEN 'Er'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'J' THEN 'Err'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'K' THEN 'Em'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'L' THEN 'Fm'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'M' THEN 'Fm'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'N' THEN 'Gm'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'O' THEN 'Hm'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'P' THEN 'Dm'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'Q' THEN 'Lm'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'R' THEN 'Nm'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'S' THEN 'Om'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'T' THEN 'Err'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'U' THEN 'Rm'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'V' THEN 'Tm'
=A0 =A0 =A0 =A0 =A0 WHEN t.propname =3D =A0'W' THEN 'Tm'
=A0 =A0 =A0 =A0 =A0 ELSE t.propname
=A0 =A0 =A0 END as newprogram
=A0 =A0FROM example_data t
=A0 =A0INNER JOIN example_data2 b ON t.propco =3D b.propco
=A0WHERE
=A0upper(b.serviced) =3D 'STATE1' and
=A0t.propname =A0in ('A',
'B'
Unless this is a copy/paste error, you have missed a , in your query. =A0Wh=
ich effectively turns it into
('A', 'B''C', 'D' ...

Which mean that switching those two will give incorrect results. One will b=
e missing C, and it will be included with B,
and the other D for the same reason.

Quote:
'C', =A0--switch me
'D', =A0-- and switch me
'E',
'F',
[snip]

Regards

Russell Smith.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match


Reply With Quote
  #4  
Old   
Mike G.
 
Posts: n/a

Default Re: [BUGS] BUG #1629: subquery IN returns incorrect results - 04-27-2005 , 10:40 AM



Sorry,

I used the online bug reporting form and it doesn't have an option to attach a file to it. If it had I would have attached a pg_dump file.

Mike

On Wed, Apr 27, 2005 at 10:57:42AM -0400, Tom Lane wrote:
Quote:
"mike g" <mike (AT) thegodshalls (DOT) com> writes:
Description: subquery IN returns incorrect results

It's impossible to investigate this with the amount of information you
have provided. Please show a *self contained* example, including any
table declarations and test data needed.

regards, tom lane
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


Reply With Quote
  #5  
Old   
Mike G.
 
Posts: n/a

Default Re: [BUGS] BUG #1629: subquery IN returns incorrect results - 04-27-2005 , 11:23 AM



[snip]

You are correct about the comma missing between the B and C in the query. It is turning it into B'C. I can't think of a good way for postgres to try and generate a warning in case a typo like this is made.

Thank you.

Mike
Quote:
Unless this is a copy/paste error, you have missed a , in your query. *Which effectively turns it into
('A', 'B''C', 'D' ...

Which mean that switching those two will give incorrect results. One will be missing C, and it will be included with B,
and the other D for the same reason.

'C', *--switch me
'D', *-- and switch me
'E',
'F',
[snip]

Regards

Russell Smith.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly


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.