473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Strangeness with CASE stmts and ATOMIC keyword

I'm experiencing some puzzling behaviour with some of my UDFs when
declaring them as ATOMIC.. Basically I'm invoking another UDF (which
uses some Java code) in one branch of a CASE statment, and if that UDF
is declared as ATOMIC then both branches get executed!

I've put together some simple SQL (see below) which shows this. When I
invoke "select bar(cast (null as integer)) from sysibm.sysdummy1" I get
this error:

SQL0470N The user defined routine "BAZ" (specific name
"SQL050825134727100") has a null value for argument "1" that could not
be
passed. SQLSTATE=39004
So it seems that even though I passed in a null value, the other CASE
branch got executed. But.. if I change bar to use foo2() instead, it
works as expected.

Any ideas? btw, I'm running DB2 8.1.0 on AIX

Thanks!
Seenu


CREATE FUNCTION BAZ (INTEGER) RETURNS VARCHAR(5)
LANGUAGE Java
EXTERNAL NAME 'Dummy!printInt'
PARAMETER STYLE JAVA
/
CREATE FUNCTION FOO1 (
inputData INTEGER
)
RETURNS VARCHAR(5)
F1: BEGIN ATOMIC
RETURN
BAZ(inputData);
END
/

CREATE FUNCTION FOO2 (
inputData INTEGER
)
RETURNS VARCHAR(5)
RETURN
BAZ(inputData)
/

CREATE FUNCTION BAR (
inputData INTEGER
)
RETURNS VARCHAR(5)
RETURN
CASE
WHEN inputData IS NULL THEN
NULL
ELSE
foo1(inputData)
-- foo2(inputData)
END
/

Nov 12 '05 #1
4 1817
Seenu wrote:
I'm experiencing some puzzling behaviour with some of my UDFs when
declaring them as ATOMIC.. Basically I'm invoking another UDF (which
uses some Java code) in one branch of a CASE statment, and if that UDF
is declared as ATOMIC then both branches get executed!

<snip>
This is a known quirk.
To explain why one works and the other doesn't would blow the scope of
this group.
DB2 generally treats CASE more like a function and may precompute all
arguments IFF the arguments are sufficiently complex (like a subquery or
a complex SQL function).
Strictly speaking the SQL standard requires the THEN expression
toexecute when the WHEN expression has been evalutaed to true.
DB2 for LUW behaves more like: "return the THEN expression for which the
WHEN expression is true".

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
Thanks for the quick reply Serge!

I was able to fix my original problem by simplifying my UDFs a bit (and
making some of them not atomic). Is there some documentation available
which explains when DB2 would choose to precompute all CASE branches?

Thanks,
Seenu

Nov 12 '05 #3
Seenu wrote:
Thanks for the quick reply Serge!

I was able to fix my original problem by simplifying my UDFs a bit (and
making some of them not atomic). Is there some documentation available
which explains when DB2 would choose to precompute all CASE branches?

There will likely be a sentence like this in a doc refresh:
"When a CASE expression contains a subquery or a SQL UDF DB2 does not
guarantee that WHEN clauses are executed in the order specified and that
THEN cluases are executed only if this respective WHEN clause is TRUE."

Again being more specific than that would require intimate understanding
of the query compiler and not be suitable for the SQL Ref.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #4
Serge Rielau wrote:
Seenu wrote:
Thanks for the quick reply Serge!

I was able to fix my original problem by simplifying my UDFs a bit (and
making some of them not atomic). Is there some documentation available
which explains when DB2 would choose to precompute all CASE branches?

There will likely be a sentence like this in a doc refresh:
"When a CASE expression contains a subquery or a SQL UDF DB2 does not
guarantee that WHEN clauses are executed in the order specified and that
THEN cluases are executed only if this respective WHEN clause is TRUE."

Again being more specific than that would require intimate understanding
of the query compiler and not be suitable for the SQL Ref.


The usual work-around for a CASE expression like

CASE
WHEN cond1
THEN expr1
WHEN cond2
THEN expr2
ELSE expr3
END

is to use a construct like this:

CASE
WHEN cond1
THEN ( SELECT expr1
FROM sysibm.sysdummy1
WHERE cond1 )
WHEN cond2
THEN ( SELECT expr2
FROM sysibm.sysdummy1
WHERE NOT ( cond1 ) AND cond2 )
ELSE ( SELECT expr3
FROM sysibm.sysdummy1
WHERE NOT ( cond1 ) AND NOT ( cond2 ) )
END
And depending on the complexity of the various conditions, you can do this:

SELECT CASE
WHEN t.c = 1
THEN ( SELECT expr1
FROM sysibm.sysdummy1
WHERE t.c = 1 )
WHEN t.c = 2
THEN ( SELECT expr2
FROM sysibm.sysdummy1
WHERE t.c = 2 )
ELSE ( SELECT expr3
FROM sysibm.sysdummy1
WHERE t.c = 3 )
FROM ..., TABLE ( VALUES (
CASE
WHEN cond1 THEN 1
WHEN cond2 THEN 2
ELSE 3
END ) ) AS t(c)

Here you will have the various conditions to be evaluated only once in the
WHEN branches of the CASE in the FROM clause.

--
Knut Stolze
Information Integration Development
IBM Germany / University of Jena
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Hareesh | last post by:
Hi, -- SQL Stmt 1 SELECT * FROM Table1 WHERE Field1 = 123 AND Field2 = 234 AND Field3 = 345 AND Field4 = 456 AND Field5 = 567 AND Field6 = 678
2
by: Robert M. Gary | last post by:
I'm using JRE 1.5 on Solaris Japanese (Sparc). The JVM claims its default character set is EUC-JP I'm seeing two strange things when using Japanese character sets... 1) If I write a program that...
42
by: Shayan | last post by:
Is there a boolean flag that can be set atomically without needing to wrap it in a mutex? This flag will be checked constantly by multiple threads so I don't really want to deal with the overhead...
10
by: Wonderinguy | last post by:
I have a table with data stored on upper case. I would like to change it proper mixed case. For example : change data from , THIS IS A TEST to This is a test or This Is A Test. Is there any easy...
6
by: blackstreetcat | last post by:
consider this code : int i; //gobal var Thread1: i=some value; Thread2: if (i==2) dosomething(); else dosomethingelse();
7
by: Joe HM | last post by:
Hello - I was wondering if there is a simple way of ensuring that some statements are executed as an "atomic operation". Here is what I am dealing with in a GUI ... Dim mAppDomain As...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
3
by: issam | last post by:
Hello, When i execute "db2 select * from TABLE(SYSPROC.SNAPSHOT_LOCK('Test',0)) as test_locks" , i get the details of all the locks on db test i want to know how to get the correspondence...
2
by: Freedom fighter | last post by:
Hello, Is a singleton class the same as an atomic class? I know that a singleton class can only be instantiated once, but does that concept apply to an atomic class? Thank you.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.