citus/src/test/regress/expected/aggregate_support.out

238 lines
7.2 KiB
Plaintext

--
-- AGGREGATE SUPPORT
--
-- Tests support for user defined aggregates
create schema aggregate_support;
set search_path to aggregate_support;
-- We test with & without STRICT as our code is responsible for managing these NULL checks
create function sum2_sfunc_strict(state int, x int)
returns int immutable strict language plpgsql as $$
begin return state + x;
end;
$$;
create function sum2_finalfunc_strict(state int)
returns int immutable strict language plpgsql as $$
begin return state * 2;
end;
$$;
create function sum2_sfunc(state int, x int)
returns int immutable language plpgsql as $$
begin return state + x;
end;
$$;
create function sum2_finalfunc(state int)
returns int immutable language plpgsql as $$
begin return state * 2;
end;
$$;
create aggregate sum2 (int) (
sfunc = sum2_sfunc,
stype = int,
finalfunc = sum2_finalfunc,
combinefunc = sum2_sfunc,
initcond = '0'
);
create aggregate sum2_strict (int) (
sfunc = sum2_sfunc_strict,
stype = int,
finalfunc = sum2_finalfunc_strict,
combinefunc = sum2_sfunc_strict
);
select create_distributed_function('sum2(int)');
create_distributed_function
-----------------------------
(1 row)
select create_distributed_function('sum2_strict(int)');
create_distributed_function
-----------------------------
(1 row)
create table aggdata (id int, key int, val int, valf float8);
select create_distributed_table('aggdata', 'id');
create_distributed_table
--------------------------
(1 row)
insert into aggdata (id, key, val, valf) values (1, 1, 2, 11.2), (2, 1, NULL, 2.1), (3, 2, 2, 3.22), (4, 2, 3, 4.23), (5, 2, 5, 5.25), (6, 3, 4, 63.4), (7, 5, NULL, 75), (8, 6, NULL, NULL), (9, 6, NULL, 96), (10, 7, 8, 1078), (11, 9, 0, 1.19);
select key, sum2(val), sum2_strict(val), stddev(valf) from aggdata group by key order by key;
key | sum2 | sum2_strict | stddev
-----+------+-------------+------------------
1 | | 4 | 6.43467170879758
2 | 20 | 20 | 1.01500410508201
3 | 8 | 8 |
5 | | |
6 | | |
7 | 16 | 16 |
9 | 0 | 0 |
(7 rows)
-- FILTER supported
select key, sum2(val) filter (where valf < 5), sum2_strict(val) filter (where valf < 5) from aggdata group by key order by key;
key | sum2 | sum2_strict
-----+------+-------------
1 | |
2 | 10 | 10
3 | 0 |
5 | 0 |
6 | 0 |
7 | 0 |
9 | 0 | 0
(7 rows)
-- DISTINCT unsupported, unless grouped by partition key
select key, sum2(distinct val), sum2_strict(distinct val) from aggdata group by key order by key;
ERROR: cannot compute aggregate (distinct)
DETAIL: table partitioning is unsuitable for aggregate (distinct)
select id, sum2(distinct val), sum2_strict(distinct val) from aggdata group by id order by id;
id | sum2 | sum2_strict
----+------+-------------
1 | 4 | 4
2 | |
3 | 4 | 4
4 | 6 | 6
5 | 10 | 10
6 | 8 | 8
7 | |
8 | |
9 | |
10 | 16 | 16
11 | 0 | 0
(11 rows)
-- ORDER BY unsupported
select key, sum2(val order by valf), sum2_strict(val order by valf) from aggdata group by key order by key;
ERROR: unsupported aggregate function sum2
-- Test handling a lack of intermediate results
select sum2(val), sum2_strict(val) from aggdata where valf = 0;
sum2 | sum2_strict
------+-------------
0 |
(1 row)
-- test polymorphic aggregates from https://github.com/citusdata/citus/issues/2397
-- we do not currently support pseudotypes for transition types, so this errors for now
CREATE OR REPLACE FUNCTION first_agg(anyelement, anyelement)
RETURNS anyelement AS $$
SELECT CASE WHEN $1 IS NULL THEN $2 ELSE $1 END;
$$ LANGUAGE SQL STABLE;
CREATE AGGREGATE first (
sfunc = first_agg,
basetype = anyelement,
stype = anyelement,
combinefunc = first_agg
);
CREATE OR REPLACE FUNCTION last_agg(anyelement, anyelement)
RETURNS anyelement AS $$
SELECT $2;
$$ LANGUAGE SQL STABLE;
CREATE AGGREGATE last (
sfunc = last_agg,
basetype = anyelement,
stype = anyelement,
combinefunc = last_agg
);
SELECT create_distributed_function('first(anyelement)');
create_distributed_function
-----------------------------
(1 row)
SELECT create_distributed_function('last(anyelement)');
create_distributed_function
-----------------------------
(1 row)
SELECT key, first(val ORDER BY id), last(val ORDER BY id)
FROM aggdata GROUP BY key ORDER BY key;
ERROR: unsupported aggregate function first
-- test aggregate with stype which is not a by-value datum
-- also test our handling of the aggregate not existing on workers
create function sumstring_sfunc(state text, x text)
returns text immutable language plpgsql as $$
begin return (state::float8 + x::float8)::text;
end;
$$;
create aggregate sumstring(text) (
sfunc = sumstring_sfunc,
stype = text,
combinefunc = sumstring_sfunc,
initcond = '0'
);
select sumstring(valf::text) from aggdata where valf is not null;
ERROR: function "aggregate_support.sumstring(text)" does not exist
CONTEXT: while executing command on localhost:57638
select create_distributed_function('sumstring(text)');
create_distributed_function
-----------------------------
(1 row)
select sumstring(valf::text) from aggdata where valf is not null;
sumstring
-----------
1339.59
(1 row)
-- test aggregate with stype that has an expanded read-write form
CREATE FUNCTION array_sort (int[])
RETURNS int[] LANGUAGE SQL AS $$
SELECT ARRAY(SELECT unnest($1) ORDER BY 1)
$$;
create aggregate array_collect_sort(el int) (
sfunc = array_append,
stype = int[],
combinefunc = array_cat,
finalfunc = array_sort,
initcond = '{}'
);
select create_distributed_function('array_collect_sort(int)');
create_distributed_function
-----------------------------
(1 row)
select array_collect_sort(val) from aggdata;
array_collect_sort
-------------------------------------
{0,2,2,3,4,5,8,NULL,NULL,NULL,NULL}
(1 row)
-- Test multiuser scenario
create user notsuper;
NOTICE: not propagating CREATE ROLE/USER commands to worker nodes
HINT: Connect to worker nodes directly to manually create all necessary users and roles.
select run_command_on_workers($$create user notsuper$$);
run_command_on_workers
-----------------------------------
(localhost,57637,t,"CREATE ROLE")
(localhost,57638,t,"CREATE ROLE")
(2 rows)
grant all on schema aggregate_support to notsuper;
grant all on all tables in schema aggregate_support to notsuper;
select run_command_on_workers($$
grant all on schema aggregate_support to notsuper;
grant all on all tables in schema aggregate_support to notsuper;
$$);
run_command_on_workers
---------------------------
(localhost,57637,t,GRANT)
(localhost,57638,t,GRANT)
(2 rows)
set role notsuper;
select array_collect_sort(val) from aggdata;
array_collect_sort
-------------------------------------
{0,2,2,3,4,5,8,NULL,NULL,NULL,NULL}
(1 row)
reset role;
set client_min_messages to error;
drop schema aggregate_support cascade;