Unix Technical Forum

SQL UDF works very strange. Bug or feature?

This is a discussion on SQL UDF works very strange. Bug or feature? within the DB2 forums, part of the Database Server Software category; --> 8.2.2 (fixpack 9) Simple function - corrects call for substr. But doesn't work. Look this sample - any call ...


Go Back   Unix Technical Forum > Database Server Software > DB2

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 05:12 AM
bughunter@ru
 
Posts: n/a
Default SQL UDF works very strange. Bug or feature?

8.2.2 (fixpack 9)

Simple function - corrects call for substr. But doesn't work.
Look this sample - any call with wrong Idx

values( Test_Substr('01234567890123456789', -1 ))

SQL0901N The SQL statement failed because of a non-severe system
error.
Subsequent SQL statements can be processed. (Reason "Invalid position
or length parameters".) SQLSTATE=58004

CREATE FUNCTION Test_Substr
(vStr VARCHAR(20), Idx INTEGER
)
RETURNS CHARACTER(1)
LANGUAGE SQL
DETERMINISTIC
CONTAINS SQL
return
case
when Idx < 1 THEN '0'
when LENGTH(vStr) < Idx THEN '1'
when LENGTH(vStr) >= Idx THEN SUBSTR(vStr, Idx, 1)
else 'x'
end
@

Rewritten UDF for test case work correctly...

drop FUNCTION Test_Substr

DB20000I The SQL command completed successfully.



CREATE FUNCTION Test_Substr
(vStr VARCHAR(20), Idx INTEGER
)
RETURNS CHARACTER(1)
LANGUAGE SQL
DETERMINISTIC
CONTAINS SQL
return
case
when Idx < 1 THEN '0'
when LENGTH(vStr) < Idx THEN '1'
when LENGTH(vStr) >= Idx THEN '!'
else 'x'
end


DB20000I The SQL command completed successfully.



values( Test_Substr('01234567890123456789', -1 ))



1

-

0



1 record(s) selected.





values( Test_Substr('01234567890123456789', 2 ))



1

-

!



1 record(s) selected.





values( Test_Substr('01234567890123456789', 32 ))



1

-

1



1 record(s) selected.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 05:13 AM
Serge Rielau
 
Posts: n/a
Default Re: SQL UDF works very strange. Bug or feature?

bughunter@ru wrote:
> 8.2.2 (fixpack 9)
>
> Simple function - corrects call for substr. But doesn't work.
> Look this sample - any call with wrong Idx
>
> values( Test_Substr('01234567890123456789', -1 ))
>
> SQL0901N The SQL statement failed because of a non-severe system
> error.
> Subsequent SQL statements can be processed. (Reason "Invalid position
> or length parameters".) SQLSTATE=58004
>
> CREATE FUNCTION Test_Substr
> (vStr VARCHAR(20), Idx INTEGER
> )
> RETURNS CHARACTER(1)
> LANGUAGE SQL
> DETERMINISTIC
> CONTAINS SQL
> return
> case
> when Idx < 1 THEN '0'
> when LENGTH(vStr) < Idx THEN '1'
> when LENGTH(vStr) >= Idx THEN SUBSTR(vStr, Idx, 1)
> else 'x'
> end
> @
>

This is an incarnation of IY70983 (will be fixed in FP10).
You should get a -138 though, instead of a -901, I'll look into that.

The problem lies in overzealous "constant folding" and (in case of
variable) pre-execution.
You can try this for a workaround:
THEN SUBSTR(vStr, Idx, COALESCE(1, INT(RAND())))

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 05:13 AM
Phil Sherman
 
Posts: n/a
Default Re: SQL UDF works very strange. Bug or feature?

Strangely enough, this works fine on my RHEL4 Linux (2.6 kernel) DB2 8.1
FP9a! Maybe it was fixed a little early.

Phil Sherman



Serge Rielau wrote:

> This is an incarnation of IY70983 (will be fixed in FP10).
> You should get a -138 though, instead of a -901, I'll look into that.
>
> The problem lies in overzealous "constant folding" and (in case of
> variable) pre-execution.
> You can try this for a workaround:
> THEN SUBSTR(vStr, Idx, COALESCE(1, INT(RAND())))
>
> Cheers
> Serge

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:05 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com