dbTalk Databases Forums  

Re: [BUGS] [GENERAL] CASE in where statement. BUG ??

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


Discuss Re: [BUGS] [GENERAL] CASE in where statement. BUG ?? in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Marek Lewczuk
 
Posts: n/a

Default Re: [BUGS] [GENERAL] CASE in where statement. BUG ?? - 12-19-2003 , 03:05 AM






Alexander Litvinov wrote:
Quote:
This is normaol behavior.


I belive that I have found a bug - or maybe it was done on purpose. Have
a look at this query:


SELECT integer_field FROM bugtable WHERE (CASE WHEN '' <> '' THEN
integer_field = '' ELSE integer_field = 0 END);
Result: ERROR: invalid input syntax for integer: ""


Here CASE have int type and you are trying to cast '' to int. Error.
Well, I see that you have not run this query: In this case the query is
trying to make integer_field = '' --> but it shouldn't, becouse "CASE
WHEN '' <> ''" is false, so "ELSE integer_field = 0" should be used.

Quote:


SELECT text_field FROM bugtable WHERE (CASE WHEN '' <> '' THEN
text_field = '' ELSE text_field = '1' END);
Result: 1 rows fetched (0,02 sec)


Here CASE have text type. Everything is fine.
Yes, becouse "CASE WHEN '' <> ''" is send false and is using "text_field
= 1".

Please execute this insert:
INSERT INTO bugtable (integer_field, text_field) VALUES (0, '1');

Then, executing both selects should return "1 rows fetched (0,02 sec)"...

Check it out.



---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster


Reply With Quote
  #2  
Old   
Marek Lewczuk
 
Posts: n/a

Default Re: [BUGS] [GENERAL] CASE in where statement. BUG ?? - 12-19-2003 , 04:03 AM






Richard Huxton wrote:
Quote:
On Friday 19 December 2003 09:02, Marek Lewczuk wrote:

But until it's evaluated ''<>'' it doesn't know it is false. When building the
CASE expression, it's trying to map types to each elements and notes that ''
is not an int. Only after this stage will it actually try to evaluate the
expression.

Well, I must say that it is wird action... Why to check both elements of
CASE expression, if we know for sure that only one will be executed ?



---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings


Reply With Quote
  #3  
Old   
Marek Lewczuk
 
Posts: n/a

Default Re: [BUGS] [GENERAL] CASE in where statement. BUG ?? - 12-19-2003 , 04:41 AM



Csaba Nagy wrote:
Quote:
Because that's a syntax check, which does NOT evaluate any of the
expressions, only the types of them. Even if it will not be executed
ever, it is still wrong, and it should generate an error, because it
means a mistake on your side which potentially could leave to results
you didn't expect if executed as is.
If it would not generate error in these cases, you would scream when you
by mistake write a wrong query and it doesn't give you the right errors.
Say thanks to the parser that it catches your errors and doesn't let you
do detective work to find out why your queries are not working as you
expect them to work.
Maybe you are right. Thanks.

Quote:
Cheers,
Csaba.







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

http://archives.postgresql.org


Reply With Quote
  #4  
Old   
Richard Huxton
 
Posts: n/a

Default Re: [BUGS] [GENERAL] CASE in where statement. BUG ?? - 12-19-2003 , 04:44 AM



On Friday 19 December 2003 09:02, Marek Lewczuk wrote:
Quote:
Alexander Litvinov wrote:
This is normaol behavior.

I belive that I have found a bug - or maybe it was done on purpose. Have
a look at this query:


SELECT integer_field FROM bugtable WHERE (CASE WHEN '' <> '' THEN
integer_field = '' ELSE integer_field = 0 END);
Result: ERROR: invalid input syntax for integer: ""

Here CASE have int type and you are trying to cast '' to int. Error.

Well, I see that you have not run this query: In this case the query is
trying to make integer_field = '' --> but it shouldn't, becouse "CASE
WHEN '' <> ''" is false, so "ELSE integer_field = 0" should be used.
But until it's evaluated ''<>'' it doesn't know it is false. When building the
CASE expression, it's trying to map types to each elements and notes that ''
is not an int. Only after this stage will it actually try to evaluate the
expression.

--
Richard Huxton
Archonet Ltd

---------------------------(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
  #5  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] [GENERAL] CASE in where statement. BUG ?? - 12-19-2003 , 09:44 AM



Marek Lewczuk <newsy (AT) lewczuk (DOT) com> writes:
Quote:
Well, I must say that it is wird action... Why to check both elements of
CASE expression, if we know for sure that only one will be executed ?
It cannot discover that the WHEN clause is constant-false until after it
has performed syntactic analysis --- which includes assigning datatypes
to all elements of the expression.

In theory perhaps we could do syntactic analysis of the WHEN part, then
stop and do constant-folding to see if we could prove that the WHEN is
always false before we move on to syntactic analysis of the THEN part.
In practice, no one does it that way --- it would arguably be illegal to
do so according to the SQL spec, which draws a very clear distinction
between parse-time checking and execution-time activity. Early constant
folding would have unpleasant properties for user-defined functions,
too. You might find that a change you make in one of your functions
doesn't show up in the behavior of some existing rule, because the call
to that function in the rule got constant-folded before the rule was
stored.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings


Reply With Quote
  #6  
Old   
Alexander Litvinov
 
Posts: n/a

Default Re: [BUGS] [GENERAL] CASE in where statement. BUG ?? - 12-19-2003 , 11:48 AM



This is normaol behavior.

Quote:
I belive that I have found a bug - or maybe it was done on purpose. Have
a look at this query:


SELECT integer_field FROM bugtable WHERE (CASE WHEN '' <> '' THEN
integer_field =3D '' ELSE integer_field =3D 0 END);
Result: ERROR: invalid input syntax for integer: ""
Here CASE have int type and you are trying to cast '' to int. Error.

Quote:

SELECT text_field FROM bugtable WHERE (CASE WHEN '' <> '' THEN
text_field =3D '' ELSE text_field =3D '1' END);
Result: 1 rows fetched (0,02 sec)
Here CASE have text type. Everything is fine.


---------------------------(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
  #7  
Old   
Csaba Nagy
 
Posts: n/a

Default Re: [BUGS] [GENERAL] CASE in where statement. BUG ?? - 12-19-2003 , 11:48 AM



Quote:
Well, I must say that it is wird action... Why to check both elements of
CASE expression, if we know for sure that only one will be executed ?
Because that's a syntax check, which does NOT evaluate any of the
expressions, only the types of them. Even if it will not be executed
ever, it is still wrong, and it should generate an error, because it
means a mistake on your side which potentially could leave to results
you didn't expect if executed as is.
If it would not generate error in these cases, you would scream when you
by mistake write a wrong query and it doesn't give you the right errors.
Say thanks to the parser that it catches your errors and doesn't let you
do detective work to find out why your queries are not working as you
expect them to work.

Cheers,
Csaba.



---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org


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.