简体   繁体   中英

Selecting multiple parameters from UDF or procedure

I'm trying to compute multiple values and fetch them in a select clause. Whether its computed via UDF or procedure does not matter to me but I can't figure out how to do it in either way. I want to use it like this:

SELECT ID, BITMAP(ID) FROM X;

which then should return a table with columns ID, Bitset1, Bitset2 . If possible it should be as performant as possible.

I have three versions currently (simplified, there's actual more computation):

Table UDF

CREATE FUNCTION TPCH.BITMAP(IN col BIGINT)
RETURNS table("BITSET1" bigint, "BITSET2" bigint)
AS BEGIN
    declare bitset1, bitset2 bigint;
    bitset1 = 1;
    bitset2 = 2;
    return select bitset1 as "BITSET1", bitset2 as "BITSET2" from sys.dummy;
END; 

Scalar UDF

CREATE FUNCTION BITMAP(IN col BIGINT)
RETURNS bitset1 bigint, bitset2 bigint
AS BEGIN
    declare bitset1, bitset2 bigint;
    bitset1 = 1;
    bitset2 = 2;
END; 

Procedure

create procedure BITMAP(in col BIGINT,
                        out bitsets table("BITSET1" bigint, "BITSET2" bigint))
as begin
    bitsets = select 1 as "BITSET1", 2 as "BITSET2" from sys.dummy;
end;

If I execute the select statement from above I get different exceptions:

For TUDF / Procedure : cannot use procedure or table function in select projection column or cannot use window function w/o OVER: P_BITMAP

For SUDF : This user defined function has multiple outputs, but this usage requires a single output. Specify a single output. This user defined function has multiple outputs, but this usage requires a single output. Specify a single output.

I figured out that for SUDF I can write it like this:

select ID, BITMAP(ID).bitset1, BITMAP(ID).bitset2 from X;

But this executes BITMAP(ID) twice which makes it slow. I want to fetch both values in one go.

Is this even possible to do in Hana or are there other options?

EDIT 1: for clarification the udfs and procedure usually depend on the input. I just return 1 and 2 in the example for simplicity.

As discussed, in HANA 1 there exist the following limitation concerning user defined functions:

  • table functions don't accept values of joined tables as input parameters. Ie they cannot be used to implement a "lateral join"
  • scalar functions will be called and executed for every instance occurring in the statement. Fetching multiple return parameters from equivalent calls will also call the function multiple times.
  • table functions and procedures cannot be used in the projection list of a SELECT statement (not HANA specific)

To apply a user defined function to many values in parallel, HANA 2 provides multiple new features, eg

  • MAP_MERGE function, to implement a map-reduce calling pattern
  • deterministic user-defined functions, that cache results, so that sub-sequent calls can use the precomputed result

add : a good example for the MAP_MERGE feature in HANA 2 can be found here: https://blogs.sap.com/2016/12/01/sap-hana-2.0-sps-0-new-developer-features-database-development/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM