473,412 Members | 2,087 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,412 software developers and data experts.

Concept Question re UDTs in UDFs

I am trying to figure out if a UDF can accept a UDT (User-defined Distinct
Type, *not* structured type) as one of its parameters. I'm running DB2 for
Linux, Unix, and Windows V8.2 (FP 8) on Windows XP.

For instance, let's say I have two UDTs, ShoesizeEuropean and
ShoesizeNorthAmerican, both based on integers: can I write a UDF that
accepts a ShoesizeEuropean as its input parameter and returns a
ShoesizeNorthAmerican?

I'm finding the manuals very confusing on this point. The CREATE FUNCTION
(external scalar) article says this about the datatypes of the input
parameters:

"SQL data type specifications and abbreviations which may be specified
in the data-type1 definition of a CREATE TABLE statement and have a
correspondence in the language that is being used to write the function may
be specified."

This seems to imply that any datatype which you can put in a CREATE TABLE
statement is legal as an input parameter to a UDF provided it has a
corresponding datatype in the programming language being used for the UDF.
Therefore, if I want to have a ShoesizeEuropean distinct type in a Java UDF
it's okay as long as there is a corresponding Java class, presumably named
ShoesizeEuropean.

But I don't see how DB2 could be aware of a Java class named
ShoesizeEuropean when it executes a CREATE FUNCTION statement that looks
like this:

CREATE FUNCTION convert(ShoesizeEuropean inputSize)
returns ShoesizeNorthAmerican ...

Don't I need something that maps UDTs to their Java classes too? If so, how
do I do that mapping? CREATE TYPE MAPPING seemed like a possibility but it
is apparently for federated databases and I am using only DB2. CREATE
TRANSFORM seemed like a possibility, especially when I saw the TRANSFORM
GROUP clause in the CREATE FUNCTION (external scalar) statement, but the
article for CREATE TRANSFORM says it is only used for structured types, not
distinct types.

Can someone please explain the basic concepts to me so I know what I need to
do, assuming it is possible in the first place?

--
Rhino
Nov 12 '05 #1
3 1604
Rhino wrote:
I am trying to figure out if a UDF can accept a UDT (User-defined Distinct
Type, *not* structured type) as one of its parameters. I'm running DB2 for
Linux, Unix, and Windows V8.2 (FP 8) on Windows XP.

For instance, let's say I have two UDTs, ShoesizeEuropean and
ShoesizeNorthAmerican, both based on integers: can I write a UDF that
accepts a ShoesizeEuropean as its input parameter and returns a
ShoesizeNorthAmerican?

I'm finding the manuals very confusing on this point. The CREATE FUNCTION
(external scalar) article says this about the datatypes of the input
parameters:

"SQL data type specifications and abbreviations which may be specified
in the data-type1 definition of a CREATE TABLE statement and have a
correspondence in the language that is being used to write the function may
be specified."

This seems to imply that any datatype which you can put in a CREATE TABLE
statement is legal as an input parameter to a UDF provided it has a
corresponding datatype in the programming language being used for the UDF.
Therefore, if I want to have a ShoesizeEuropean distinct type in a Java UDF
it's okay as long as there is a corresponding Java class, presumably named
ShoesizeEuropean.

But I don't see how DB2 could be aware of a Java class named
ShoesizeEuropean when it executes a CREATE FUNCTION statement that looks
like this:

CREATE FUNCTION convert(ShoesizeEuropean inputSize)
returns ShoesizeNorthAmerican ...

Don't I need something that maps UDTs to their Java classes too? If so, how
do I do that mapping? CREATE TYPE MAPPING seemed like a possibility but it
is apparently for federated databases and I am using only DB2. CREATE
TRANSFORM seemed like a possibility, especially when I saw the TRANSFORM
GROUP clause in the CREATE FUNCTION (external scalar) statement, but the
article for CREATE TRANSFORM says it is only used for structured types, not
distinct types.

Can someone please explain the basic concepts to me so I know what I need to
do, assuming it is possible in the first place?

For a UDT the implementation of the external UDT will simply use the
basetype (e.g. integer). As long as teh basetype exists you're cool.

When dealing with UDT you can also look at sourced functions and SQL
Functions.
If your american2europeansizes fucntion just is a fancy expression an
SQL Function should beat Java in performance hands down.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2

"Serge Rielau" <sr*****@ca.ibm.com> wrote in message
news:3b*************@individual.net...
Rhino wrote:
I am trying to figure out if a UDF can accept a UDT (User-defined Distinct Type, *not* structured type) as one of its parameters. I'm running DB2 for Linux, Unix, and Windows V8.2 (FP 8) on Windows XP.

For instance, let's say I have two UDTs, ShoesizeEuropean and
ShoesizeNorthAmerican, both based on integers: can I write a UDF that
accepts a ShoesizeEuropean as its input parameter and returns a
ShoesizeNorthAmerican?

I'm finding the manuals very confusing on this point. The CREATE FUNCTION (external scalar) article says this about the datatypes of the input
parameters:

"SQL data type specifications and abbreviations which may be specified in the data-type1 definition of a CREATE TABLE statement and have a
correspondence in the language that is being used to write the function may be specified."

This seems to imply that any datatype which you can put in a CREATE TABLE statement is legal as an input parameter to a UDF provided it has a
corresponding datatype in the programming language being used for the UDF. Therefore, if I want to have a ShoesizeEuropean distinct type in a Java UDF it's okay as long as there is a corresponding Java class, presumably named ShoesizeEuropean.

But I don't see how DB2 could be aware of a Java class named
ShoesizeEuropean when it executes a CREATE FUNCTION statement that looks
like this:

CREATE FUNCTION convert(ShoesizeEuropean inputSize)
returns ShoesizeNorthAmerican ...

Don't I need something that maps UDTs to their Java classes too? If so, how do I do that mapping? CREATE TYPE MAPPING seemed like a possibility but it is apparently for federated databases and I am using only DB2. CREATE
TRANSFORM seemed like a possibility, especially when I saw the TRANSFORM
GROUP clause in the CREATE FUNCTION (external scalar) statement, but the
article for CREATE TRANSFORM says it is only used for structured types, not distinct types.

Can someone please explain the basic concepts to me so I know what I need to do, assuming it is possible in the first place?
For a UDT the implementation of the external UDT will simply use the
basetype (e.g. integer). As long as teh basetype exists you're cool.

So, since my ShoesizeEuropean is based on an Integer, there is no problem?
That's very useful to know. So, if I understand you correctly, my CREATE
FUNCTION needs to refer to integer rather than ShoesizeEuropean in the input
and output parameters. Is that correct?

Also, does the SQL statement that invokes the UDF need to convert the
distinct type to the base type manually or does this happen "automagically"?

For example, do I need this in my SQL (where convert() is the function that
changes European shoesizes to North American and euro_size is the name of
the column containing the European show size:

select id, name, convert(cast (euro_size as int)) ...

or will the cast happen "automagically" if I do this:

select id, name, convert(euro_size) ...

When dealing with UDT you can also look at sourced functions and SQL
Functions.
If your american2europeansizes fucntion just is a fancy expression an
SQL Function should beat Java in performance hands down.

That's also very useful to know. However, the Java UDFs will tend to be more
complex than a shoe size conversion; I just used shoe sizes as a simple
example ;-)

Rhino
Nov 12 '05 #3
Rhino wrote:
For a UDT the implementation of the external UDT will simply use the
basetype (e.g. integer). As long as teh basetype exists you're cool.

So, since my ShoesizeEuropean is based on an Integer, there is no problem?
That's very useful to know. So, if I understand you correctly, my CREATE
FUNCTION needs to refer to integer rather than ShoesizeEuropean in the input
and output parameters. Is that correct?

The CREATE FUNCTION statement uses the UDT.
Your java implementation uses the base type.
UDT's are purely a game of the SQL Compiler frontend to enforce type
safety. The optimizer and runtime don't know they exist.
Also, does the SQL statement that invokes the UDF need to convert the
distinct type to the base type manually or does this happen "automagically"? It's what we call a no-op cast. runtime doesn't waste a cycle. Just like
the C usage of a typedef does not change the executable.
For example, do I need this in my SQL (where convert() is the function that
changes European shoesizes to North American and euro_size is the name of
the column containing the European show size:

select id, name, convert(cast (euro_size as int)) ...

or will the cast happen "automagically" if I do this:

select id, name, convert(euro_size) ...

Given that convert() was CREATEd to accept the UDT only teh later will
work. if you try the first DB2 will give you a function not found error.
Not sure how much epxerience you have with UDT, but make sure the schema
of the UDT is in your PATH. Otherwise DB2 will not find either casts or
comparisons.
When dealing with UDT you can also look at sourced functions and SQL
Functions.
If your american2europeansizes fucntion just is a fancy expression an
SQL Function should beat Java in performance hands down.


That's also very useful to know. However, the Java UDFs will tend to be more
complex than a shoe size conversion; I just used shoe sizes as a simple
example ;-)

And I was about to ask you for the conversion table I still confuse the
local retailers by telling them my German shoesize ;-)

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #4

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

Similar topics

3
by: Andrew Mayo | last post by:
There is something very strange going on here. Tested with ADO 2.7 and MSDE/2000. At first, things look quite sensible. You have a simple SQL query, let's say select * from mytab where col1 =...
1
by: Eugene | last post by:
In a DB2 V8.1 FP4 database I am trying to create a table SQL UDF that is to return a contents of a temporary table with in this UDF: create function getitemdata(pint int) returns table (...
3
by: David Carver | last post by:
We are running into a problem with a Communication Link failure when calling an External Stored procedure written in ILE Cobol from an SQL UDF. When calling the stored procedure by itself and not...
0
by: Fan Ruo Xin | last post by:
The developer rewrote one C program. So I need to replace the current library, drop and recreate the UDFs. I don't want to restart the db server, I just terminate all the sessions which might call...
4
by: Pete H | last post by:
Hi All; I'm trying to get some of the samples that are amply illustrated in multiple docs to work. When I try to create a Warehouse Center view "...for MQ Series messages" or use the UDF wizard in...
7
by: Rhino | last post by:
I am updating some Java UDFs from DB2GENERAL to DB2JAVA as suggested in the manuals for DB2 Version 8 but I'm having problems with setSQLstate() and setSQLmessage(). If I'm reading the manuals...
1
by: jimfollett1 via DotNetMonster.com | last post by:
Hey all, I was wondering if you could put me out of my misery (hopefully, not literaly) .. I am currently trying to port some VB6 code to VB.NET because of a possible significant performance...
0
by: Helmut Tessarek | last post by:
Hi everybody, I've written some UDFs to generate passwords within DB2. They are compatible to the functions that are used in Apache's htpasswd utility. Maybe someone can use them. ...
6
by: Carsten | last post by:
Hello Folks, I encountered a problem with SQL server 2000 and UDFs. I have a scalar UDF and a table UDF where I would like the scalar UDF to provide the argument for the table UDF like in: ...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.