473,385 Members | 1,712 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,385 software developers and data experts.

User-defined aggregate function

Folks,

I know that DB2 does not (yet?) support this, but I wonder if anyone can suggest a work-around.

I've seen article http://www-128.ibm.com/developerwork...504greenstein/, which was very helpful for developing the scalar functions I needed, but it does not cover how to emulate aggregate functions.

And I've seen Knut's dW article (http://www-128.ibm.com/developerwork...09stolze.html), but frankly it's a bit advanced for me. For example, AFAIK I don't need a UDT (do I?).

Here's my scenario.

I'm porting a PHP application that currently uses other DBs as a back-end. This application has a query:

SELECT BIT_OR(mycolumn) FROM ...

The BIT_OR() aggregate function is my problem.

The 'mycolumn' column is defined as VARCHAR(32) FOR BIT DATA. The BIT_OR() function is supposed to do a bitwise OR of all of the values in that column.

Can anyone offer any simple, to-the-point guidance on any way to accomplish this? I'm using DB2 V8.2.2 on Windows XP. I can write scalar UDFs all I want, but I'm limited as to the amount of major changes I can make to the SQL itself (most specifically, I can't bust it into multiple statements).

Thanks.

(Jeez, in this porting project I seem to be hitting everything that DB2 doesn't support. And I'm not even getting paid for it! ;-)

--
--------------------
Larry Menard
"Defender of Geese and of All Things Natural"
Nov 18 '05 #1
6 3047
Larry Menard wrote:
Folks,

I know that DB2 does not (yet?) support this, but I wonder if anyone
can suggest a work-around.

I've seen article
http://www-128.ibm.com/developerwork...504greenstein/,
which was very helpful for developing the scalar functions I needed, but
it does not cover how to emulate aggregate functions.

And I've seen Knut's dW article
(http://www-128.ibm.com/developerwork...09stolze.html),
but frankly it's a bit advanced for me. For example, AFAIK I don't need
a UDT (do I?).

Here's my scenario.

I'm porting a PHP application that currently uses other DBs as a
back-end. This application has a query:
SELECT *BIT_OR(mycolumn)* FROM ...
The BIT_OR() aggregate function is my problem.

The 'mycolumn' column is defined as VARCHAR(32) FOR BIT DATA. The
BIT_OR() function is supposed to do a bitwise OR of all of the values in
that column.

Can anyone offer any simple, to-the-point guidance on _any_ way to
accomplish this? I'm using DB2 V8.2.2 on Windows XP. I can write
scalar UDFs all I want, but I'm limited as to the amount of major
changes I can make to the SQL itself (most specifically, I can't bust it
into multiple statements).

Thanks.

(Jeez, in this porting project I seem to be hitting everything that
DB2 doesn't support. And I'm not even getting paid for it! ;-)

--
--------------------
Larry Menard
"Defender of Geese and of All Things Natural"

You can write a recursive query (WITH clause).

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 18 '05 #2
Thanks, Serge. I was under the impression (reinforced by Knut's article)
that recursive queries should be avoided.

--
--------------------
Larry Menard
"Defender of Geese and of All Things Natural"
"Serge Rielau" <sr*****@ca.ibm.com> wrote in message
news:3u************@individual.net...
Larry Menard wrote:
Folks,
I know that DB2 does not (yet?) support this, but I wonder if anyone can
suggest a work-around.
I've seen article
http://www-128.ibm.com/developerwork...504greenstein/,
which was very helpful for developing the scalar functions I needed, but
it does not cover how to emulate aggregate functions.
And I've seen Knut's dW article
(http://www-128.ibm.com/developerwork...09stolze.html),
but frankly it's a bit advanced for me. For example, AFAIK I don't need
a UDT (do I?).
Here's my scenario.
I'm porting a PHP application that currently uses other DBs as a
back-end. This application has a query:
SELECT *BIT_OR(mycolumn)* FROM ...

The BIT_OR() aggregate function is my problem.
The 'mycolumn' column is defined as VARCHAR(32) FOR BIT DATA. The
BIT_OR() function is supposed to do a bitwise OR of all of the values in
that column.
Can anyone offer any simple, to-the-point guidance on _any_ way to
accomplish this? I'm using DB2 V8.2.2 on Windows XP. I can write scalar
UDFs all I want, but I'm limited as to the amount of major changes I can
make to the SQL itself (most specifically, I can't bust it into multiple
statements).
Thanks.
(Jeez, in this porting project I seem to be hitting everything that DB2
doesn't support. And I'm not even getting paid for it! ;-)

--
--------------------
Larry Menard
"Defender of Geese and of All Things Natural"

You can write a recursive query (WITH clause).

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab

Nov 18 '05 #3
Generally speaking you can do aggregate function by putting expressions
into the SUM(), MIN(), or MAX() functions. Here is a version of the
aggregate product function in SQL. You will need to have the logorithm
and exponential functions. They are not standards, but they are very
common. The idea is that there are three special cases - all positive
numbers, one or more zeroes, and some negative numbers in the set.
Within the case where you have negative numbers, there are two
sub-cases: (1) an even number of negatives or (2) an odd number of
negatives. You then need to apply some High School algebra to
determine the sign of the final result.

SELECT CASE MIN(ABS(SIGN(nbr)))
WHEN 0 THEN 0.00 -- some zeroes
ELSE -- no zeroes
EXP(SUM(LOG(ABS(NULLIF(nbr, 0)))))
* CASE WHEN MOD (CAST(SUM(ABS(SIGN(nbr)-1)/2) AS INTEGER), 2)
= 1
THEN -1.00 ELSE 1.00 END
END AS big_pi

However, in your particular situation, you are stuck because some diot
thought that SQL is a language for bit fiddling and not a high level
database language. Nobody uses bits and bytes in SQL. The real
problem is a completely scerewed up schema that needs to be re-done
properly from scratch.
And I'm not even getting paid for it! <<


No good deed goes unpunished.

Nov 18 '05 #4
Thanks Joe & Serge.

I've boned up a bit more on UDFs (Don Chamberlin's "Complete Guide to DB2" book), and I think I should be able to implement this fairly easily using a java scalar SCRATCHPAD UDF.

In Don's book he says (paraphrased):
In statement "SELECT foo(col1) FROM tb1", function "foo" is invoked for each value in column "col1".
So I think that on each invocation, my UDF should:

if (scratchpad is empty) then
{
simply copy all 32 bits of current row value into the scratchpad.
}
else
{
for (each bit of the current scratchpad string contents)
{
if (the appropriate bit in the scratchpad contents is already "non-0") then
{
nop.
}
else // scratchpad bit must be "0"
{
if (the corresponding bit in the current row value is also "0") then
{
set the appropriate bit in the scratchpad string to "0".
}
else // row value bit is "non-0"
{
set the appropriate bit in the scratchpad string to the value of the bit in the current row value.
}
}
}
}
return (current scratchpad string)

And the SQL invocation should remain pretty much unchanged:

SELECT BIT_OR(mycolumn) FROM ...

But I'm not 100% sure of one thing:

Note that my UDF always returns the current scratchpad string. If the UDF is re-invoked for each row value in the column, does the overall SQL statement ever see the returned values for the interim invocations, or does it see only the value returned by the last invocation?

Thanks.

--
--------------------
Larry Menard
"Defender of Geese and of All Things Natural"
"--CELKO--" <jc*******@earthlink.net> wrote in message news:11*********************@o13g2000cwo.googlegro ups.com... Generally speaking you can do aggregate function by putting expressions
into the SUM(), MIN(), or MAX() functions. Here is a version of the
aggregate product function in SQL. You will need to have the logorithm
and exponential functions. They are not standards, but they are very
common. The idea is that there are three special cases - all positive
numbers, one or more zeroes, and some negative numbers in the set.
Within the case where you have negative numbers, there are two
sub-cases: (1) an even number of negatives or (2) an odd number of
negatives. You then need to apply some High School algebra to
determine the sign of the final result.

SELECT CASE MIN(ABS(SIGN(nbr)))
WHEN 0 THEN 0.00 -- some zeroes
ELSE -- no zeroes
EXP(SUM(LOG(ABS(NULLIF(nbr, 0)))))
* CASE WHEN MOD (CAST(SUM(ABS(SIGN(nbr)-1)/2) AS INTEGER), 2)
= 1
THEN -1.00 ELSE 1.00 END
END AS big_pi

However, in your particular situation, you are stuck because some diot
thought that SQL is a language for bit fiddling and not a high level
database language. Nobody uses bits and bytes in SQL. The real
problem is a completely scerewed up schema that needs to be re-done
properly from scratch.
And I'm not even getting paid for it! <<


No good deed goes unpunished.

Nov 18 '05 #5
Never mind, I take back my question. Thanks.

--
--------------------
Larry Menard
"Defender of Geese and of All Things Natural"
"Larry Menard" <ro**@GoSpamYourself.com> wrote in message news:2Z******************************@rogers.com.. .
Thanks Joe & Serge.

I've boned up a bit more on UDFs (Don Chamberlin's "Complete Guide to DB2" book), and I think I should be able to implement this fairly easily using a java scalar SCRATCHPAD UDF.

In Don's book he says (paraphrased):
In statement "SELECT foo(col1) FROM tb1", function "foo" is invoked for each value in column "col1".
So I think that on each invocation, my UDF should:

if (scratchpad is empty) then
{
simply copy all 32 bits of current row value into the scratchpad.
}
else
{
for (each bit of the current scratchpad string contents)
{
if (the appropriate bit in the scratchpad contents is already "non-0") then
{
nop.
}
else // scratchpad bit must be "0"
{
if (the corresponding bit in the current row value is also "0") then
{
set the appropriate bit in the scratchpad string to "0".
}
else // row value bit is "non-0"
{
set the appropriate bit in the scratchpad string to the value of the bit in the current row value.
}
}
}
}
return (current scratchpad string)

And the SQL invocation should remain pretty much unchanged:

SELECT BIT_OR(mycolumn) FROM ...

But I'm not 100% sure of one thing:

Note that my UDF always returns the current scratchpad string. If the UDF is re-invoked for each row value in the column, does the overall SQL statement ever see the returned values for the interim invocations, or does it see only the value returned by the last invocation?

Thanks.

--
--------------------
Larry Menard
"Defender of Geese and of All Things Natural"
"--CELKO--" <jc*******@earthlink.net> wrote in message news:11*********************@o13g2000cwo.googlegro ups.com... Generally speaking you can do aggregate function by putting expressions
into the SUM(), MIN(), or MAX() functions. Here is a version of the
aggregate product function in SQL. You will need to have the logorithm
and exponential functions. They are not standards, but they are very
common. The idea is that there are three special cases - all positive
numbers, one or more zeroes, and some negative numbers in the set.
Within the case where you have negative numbers, there are two
sub-cases: (1) an even number of negatives or (2) an odd number of
negatives. You then need to apply some High School algebra to
determine the sign of the final result.

SELECT CASE MIN(ABS(SIGN(nbr)))
WHEN 0 THEN 0.00 -- some zeroes
ELSE -- no zeroes
EXP(SUM(LOG(ABS(NULLIF(nbr, 0)))))
* CASE WHEN MOD (CAST(SUM(ABS(SIGN(nbr)-1)/2) AS INTEGER), 2)
= 1
THEN -1.00 ELSE 1.00 END
END AS big_pi

However, in your particular situation, you are stuck because some diot
thought that SQL is a language for bit fiddling and not a high level
database language. Nobody uses bits and bytes in SQL. The real
problem is a completely scerewed up schema that needs to be re-done
properly from scratch.
And I'm not even getting paid for it! <<


No good deed goes unpunished.

Nov 18 '05 #6
Larry Menard wrote:
Thanks, Serge. I was under the impression (reinforced by Knut's
article)
that recursive queries should be avoided.


Really, I didn't want to say that.

And to answer your other question: no, you don't need structured types for
the aggregation. That was just meant as an example.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 21 '05 #7

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

Similar topics

3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
2
by: Jesper Stocholm | last post by:
I have implemented role-based security within my ASP.Net application. However, it seems the role is not passed to the authentication ticket I create. I want to use it to display/hide some...
5
by: Michelle Stone | last post by:
Hi everybody I am writing a simple asp.net application using form authentication. I store the list of all users and their passwords in an SQL Server database table. My client recently told me...
6
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
7
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the...
0
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
2
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
1
by: Carlettus | last post by:
Dear All, sorry but I'm not sure if this is the right place to post my problem. I was using the following asp code to create users in Active Directory. Suddenly, and I don't know the reason, users...
0
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have...
3
by: shapper | last post by:
Hello, On my web site I have a property, Visitor, which is available for Anonymous users: public class Visitor { public CultureInfo Culture { get; set; } public List<GuidPolls { get; set;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.