Selecting multiple parameters from UDF or procedure

主宰稳场 提交于 2020-01-06 23:56:12

问题


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.

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.


回答1:


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. I.e. 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, e.g.

  • 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/



来源:https://stackoverflow.com/questions/44314720/selecting-multiple-parameters-from-udf-or-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!