UDF Scripts with ORDER BY do not work with GROUPING SETS

Details

Detail name Value
Changelog Number 5062
Type Bug
Status Resolved
Affected Versions EXASOL 6.0.0
Fix Versions EXASOL 6.0.4
Resolution Date 2017-09-11

Problem:

User-defined SET functions do not properly evaluate their ORDER BY declaration when used with GROUPING SETS.

Example:

create schema support1235;

create or replace table t(x varchar(255),y int, z int);
insert into t values
('ijk',2,1), ('jkc:l',2,1),
('klm',2,2), ('lmn',2,2);

create or replace python set script s(x varchar(255) order by x) returns varchar(255) AS
def run(ctx):
    res = ''
    while True:
        res = res+str(ctx.x)
        if not ctx.next(): break
        res = res+","
    return res
/

-- the superaggregate rows (where y or z or both are null in the result set)
-- have the wrong sorting in the udf version
select s(x), group_concat(x), y,z, GROUPING(z), grouping(y) from t
group by GROUPING SETs ( (y,z), (y), ()  );

Workaround
GROUPING SETS always can be rewritten using standard GROUP BY and UNION ALL.
For the example above:

select s(x), group_concat(x), y,z  from t
group by y,z
UNION ALL
select s(x), group_concat(x), y,NULL  from t
group by y
UNION ALL
select s(x), group_concat(x), NULL,NULL  from t;