diff --git a/src/custom_types.c b/src/custom_types.c index 19171d71..62a8548f 100644 --- a/src/custom_types.c +++ b/src/custom_types.c @@ -344,10 +344,13 @@ chfdw_check_for_ordered_aggregate(Aggref* agg) { } /* Accept all ordered aggregates defined by pg_clickhouse. */ - Oid extoid = getExtensionOfObject(ProcedureRelationId, agg->aggfnoid); - char* extname = get_extension_name(extoid); + Oid extoid = getExtensionOfObject(ProcedureRelationId, agg->aggfnoid); - return STR_EQUAL(extname, "pg_clickhouse"); + if (!OidIsValid(extoid)) { + return false; + } + + return get_extension_oid("pg_clickhouse", true) == extoid; } /* diff --git a/test/expected/functions.out b/test/expected/functions.out index 61e1d39c..6e126b29 100644 --- a/test/expected/functions.out +++ b/test/expected/functions.out @@ -801,6 +801,35 @@ SELECT percentile_disc(ARRAY[0.25, 0.5, 0.75]) WITHIN GROUP (ORDER BY a NULLS LA {2,2,2} (1 row) +CREATE FUNCTION local_ordered_sfunc(integer, integer) +RETURNS integer +LANGUAGE SQL +IMMUTABLE +AS 'SELECT $1 + 1'; +CREATE AGGREGATE local_ordered_count(ORDER BY integer) ( + SFUNC = local_ordered_sfunc, + STYPE = integer, + INITCOND = '0' +); +EXPLAIN (VERBOSE, COSTS OFF) +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + QUERY PLAN +----------------------------------------------------------- + Aggregate + Output: local_ordered_count() WITHIN GROUP (ORDER BY a) + -> Foreign Scan on public.t1 + Output: a, b, c + Remote SQL: SELECT a FROM functions_test.t1 +(5 rows) + +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + local_ordered_count +--------------------- + 4 +(1 row) + +DROP AGGREGATE local_ordered_count(integer); +DROP FUNCTION local_ordered_sfunc(integer, integer); EXPLAIN (VERBOSE, COSTS OFF) SELECT date_trunc('dAy', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/test/expected/functions_0.out b/test/expected/functions_0.out index 05f94594..c3926614 100644 --- a/test/expected/functions_0.out +++ b/test/expected/functions_0.out @@ -801,6 +801,35 @@ SELECT percentile_disc(ARRAY[0.25, 0.5, 0.75]) WITHIN GROUP (ORDER BY a NULLS LA {2,2,2} (1 row) +CREATE FUNCTION local_ordered_sfunc(integer, integer) +RETURNS integer +LANGUAGE SQL +IMMUTABLE +AS 'SELECT $1 + 1'; +CREATE AGGREGATE local_ordered_count(ORDER BY integer) ( + SFUNC = local_ordered_sfunc, + STYPE = integer, + INITCOND = '0' +); +EXPLAIN (VERBOSE, COSTS OFF) +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + QUERY PLAN +----------------------------------------------------------- + Aggregate + Output: local_ordered_count() WITHIN GROUP (ORDER BY a) + -> Foreign Scan on public.t1 + Output: a, b, c + Remote SQL: SELECT a FROM functions_test.t1 +(5 rows) + +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + local_ordered_count +--------------------- + 4 +(1 row) + +DROP AGGREGATE local_ordered_count(integer); +DROP FUNCTION local_ordered_sfunc(integer, integer); EXPLAIN (VERBOSE, COSTS OFF) SELECT date_trunc('dAy', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/test/expected/functions_1.out b/test/expected/functions_1.out index 733a6474..65510563 100644 --- a/test/expected/functions_1.out +++ b/test/expected/functions_1.out @@ -801,6 +801,35 @@ SELECT percentile_disc(ARRAY[0.25, 0.5, 0.75]) WITHIN GROUP (ORDER BY a NULLS LA {2,2,2} (1 row) +CREATE FUNCTION local_ordered_sfunc(integer, integer) +RETURNS integer +LANGUAGE SQL +IMMUTABLE +AS 'SELECT $1 + 1'; +CREATE AGGREGATE local_ordered_count(ORDER BY integer) ( + SFUNC = local_ordered_sfunc, + STYPE = integer, + INITCOND = '0' +); +EXPLAIN (VERBOSE, COSTS OFF) +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + QUERY PLAN +----------------------------------------------------------- + Aggregate + Output: local_ordered_count() WITHIN GROUP (ORDER BY a) + -> Foreign Scan on public.t1 + Output: a, b, c + Remote SQL: SELECT a FROM functions_test.t1 +(5 rows) + +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + local_ordered_count +--------------------- + 4 +(1 row) + +DROP AGGREGATE local_ordered_count(integer); +DROP FUNCTION local_ordered_sfunc(integer, integer); EXPLAIN (VERBOSE, COSTS OFF) SELECT date_trunc('dAy', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/test/expected/functions_2.out b/test/expected/functions_2.out index 89433525..6637a97e 100644 --- a/test/expected/functions_2.out +++ b/test/expected/functions_2.out @@ -801,6 +801,35 @@ SELECT percentile_disc(ARRAY[0.25, 0.5, 0.75]) WITHIN GROUP (ORDER BY a NULLS LA {2,2,2} (1 row) +CREATE FUNCTION local_ordered_sfunc(integer, integer) +RETURNS integer +LANGUAGE SQL +IMMUTABLE +AS 'SELECT $1 + 1'; +CREATE AGGREGATE local_ordered_count(ORDER BY integer) ( + SFUNC = local_ordered_sfunc, + STYPE = integer, + INITCOND = '0' +); +EXPLAIN (VERBOSE, COSTS OFF) +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + QUERY PLAN +----------------------------------------------------------- + Aggregate + Output: local_ordered_count() WITHIN GROUP (ORDER BY a) + -> Foreign Scan on public.t1 + Output: a, b, c + Remote SQL: SELECT a FROM functions_test.t1 +(5 rows) + +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + local_ordered_count +--------------------- + 4 +(1 row) + +DROP AGGREGATE local_ordered_count(integer); +DROP FUNCTION local_ordered_sfunc(integer, integer); EXPLAIN (VERBOSE, COSTS OFF) SELECT date_trunc('dAy', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/test/expected/functions_3.out b/test/expected/functions_3.out index 8a0a802c..2783940e 100644 --- a/test/expected/functions_3.out +++ b/test/expected/functions_3.out @@ -801,6 +801,35 @@ SELECT percentile_disc(ARRAY[0.25, 0.5, 0.75]) WITHIN GROUP (ORDER BY a NULLS LA {2,2,2} (1 row) +CREATE FUNCTION local_ordered_sfunc(integer, integer) +RETURNS integer +LANGUAGE SQL +IMMUTABLE +AS 'SELECT $1 + 1'; +CREATE AGGREGATE local_ordered_count(ORDER BY integer) ( + SFUNC = local_ordered_sfunc, + STYPE = integer, + INITCOND = '0' +); +EXPLAIN (VERBOSE, COSTS OFF) +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + QUERY PLAN +----------------------------------------------------------- + Aggregate + Output: local_ordered_count() WITHIN GROUP (ORDER BY a) + -> Foreign Scan on public.t1 + Output: a, b, c + Remote SQL: SELECT a FROM functions_test.t1 +(5 rows) + +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + local_ordered_count +--------------------- + 4 +(1 row) + +DROP AGGREGATE local_ordered_count(integer); +DROP FUNCTION local_ordered_sfunc(integer, integer); EXPLAIN (VERBOSE, COSTS OFF) SELECT date_trunc('dAy', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/test/expected/functions_4.out b/test/expected/functions_4.out index 15b9f691..a14fe927 100644 --- a/test/expected/functions_4.out +++ b/test/expected/functions_4.out @@ -801,6 +801,35 @@ SELECT percentile_disc(ARRAY[0.25, 0.5, 0.75]) WITHIN GROUP (ORDER BY a NULLS LA {2,2,2} (1 row) +CREATE FUNCTION local_ordered_sfunc(integer, integer) +RETURNS integer +LANGUAGE SQL +IMMUTABLE +AS 'SELECT $1 + 1'; +CREATE AGGREGATE local_ordered_count(ORDER BY integer) ( + SFUNC = local_ordered_sfunc, + STYPE = integer, + INITCOND = '0' +); +EXPLAIN (VERBOSE, COSTS OFF) +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + QUERY PLAN +----------------------------------------------------------- + Aggregate + Output: local_ordered_count() WITHIN GROUP (ORDER BY a) + -> Foreign Scan on public.t1 + Output: a, b, c + Remote SQL: SELECT a FROM functions_test.t1 +(5 rows) + +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + local_ordered_count +--------------------- + 4 +(1 row) + +DROP AGGREGATE local_ordered_count(integer); +DROP FUNCTION local_ordered_sfunc(integer, integer); EXPLAIN (VERBOSE, COSTS OFF) SELECT date_trunc('dAy', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/test/expected/functions_5.out b/test/expected/functions_5.out index 283a2d82..6c576012 100644 --- a/test/expected/functions_5.out +++ b/test/expected/functions_5.out @@ -801,6 +801,35 @@ SELECT percentile_disc(ARRAY[0.25, 0.5, 0.75]) WITHIN GROUP (ORDER BY a NULLS LA {2,2,2} (1 row) +CREATE FUNCTION local_ordered_sfunc(integer, integer) +RETURNS integer +LANGUAGE SQL +IMMUTABLE +AS 'SELECT $1 + 1'; +CREATE AGGREGATE local_ordered_count(ORDER BY integer) ( + SFUNC = local_ordered_sfunc, + STYPE = integer, + INITCOND = '0' +); +EXPLAIN (VERBOSE, COSTS OFF) +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + QUERY PLAN +----------------------------------------------------------- + Aggregate + Output: local_ordered_count() WITHIN GROUP (ORDER BY a) + -> Foreign Scan on public.t1 + Output: a, b, c + Remote SQL: SELECT a FROM functions_test.t1 +(5 rows) + +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + local_ordered_count +--------------------- + 4 +(1 row) + +DROP AGGREGATE local_ordered_count(integer); +DROP FUNCTION local_ordered_sfunc(integer, integer); EXPLAIN (VERBOSE, COSTS OFF) SELECT date_trunc('dAy', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/test/expected/functions_6.out b/test/expected/functions_6.out index a3095973..2a0110b1 100644 --- a/test/expected/functions_6.out +++ b/test/expected/functions_6.out @@ -801,6 +801,35 @@ SELECT percentile_disc(ARRAY[0.25, 0.5, 0.75]) WITHIN GROUP (ORDER BY a NULLS LA {2,2,2} (1 row) +CREATE FUNCTION local_ordered_sfunc(integer, integer) +RETURNS integer +LANGUAGE SQL +IMMUTABLE +AS 'SELECT $1 + 1'; +CREATE AGGREGATE local_ordered_count(ORDER BY integer) ( + SFUNC = local_ordered_sfunc, + STYPE = integer, + INITCOND = '0' +); +EXPLAIN (VERBOSE, COSTS OFF) +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + QUERY PLAN +----------------------------------------------------------- + Aggregate + Output: local_ordered_count() WITHIN GROUP (ORDER BY a) + -> Foreign Scan on public.t1 + Output: a, b, c + Remote SQL: SELECT a FROM functions_test.t1 +(5 rows) + +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + local_ordered_count +--------------------- + 4 +(1 row) + +DROP AGGREGATE local_ordered_count(integer); +DROP FUNCTION local_ordered_sfunc(integer, integer); EXPLAIN (VERBOSE, COSTS OFF) SELECT date_trunc('dAy', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/test/expected/functions_7.out b/test/expected/functions_7.out index d4ab979b..5749f386 100644 --- a/test/expected/functions_7.out +++ b/test/expected/functions_7.out @@ -801,6 +801,35 @@ SELECT percentile_disc(ARRAY[0.25, 0.5, 0.75]) WITHIN GROUP (ORDER BY a NULLS LA {2,2,2} (1 row) +CREATE FUNCTION local_ordered_sfunc(integer, integer) +RETURNS integer +LANGUAGE SQL +IMMUTABLE +AS 'SELECT $1 + 1'; +CREATE AGGREGATE local_ordered_count(ORDER BY integer) ( + SFUNC = local_ordered_sfunc, + STYPE = integer, + INITCOND = '0' +); +EXPLAIN (VERBOSE, COSTS OFF) +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + QUERY PLAN +----------------------------------------------------------- + Aggregate + Output: local_ordered_count() WITHIN GROUP (ORDER BY a) + -> Foreign Scan on public.t1 + Output: a, b, c + Remote SQL: SELECT a FROM functions_test.t1 +(5 rows) + +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + local_ordered_count +--------------------- + 4 +(1 row) + +DROP AGGREGATE local_ordered_count(integer); +DROP FUNCTION local_ordered_sfunc(integer, integer); EXPLAIN (VERBOSE, COSTS OFF) SELECT date_trunc('dAy', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/test/expected/functions_8.out b/test/expected/functions_8.out index 82b41789..47f3315b 100644 --- a/test/expected/functions_8.out +++ b/test/expected/functions_8.out @@ -801,6 +801,35 @@ SELECT percentile_disc(ARRAY[0.25, 0.5, 0.75]) WITHIN GROUP (ORDER BY a NULLS LA {2,2,2} (1 row) +CREATE FUNCTION local_ordered_sfunc(integer, integer) +RETURNS integer +LANGUAGE SQL +IMMUTABLE +AS 'SELECT $1 + 1'; +CREATE AGGREGATE local_ordered_count(ORDER BY integer) ( + SFUNC = local_ordered_sfunc, + STYPE = integer, + INITCOND = '0' +); +EXPLAIN (VERBOSE, COSTS OFF) +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + QUERY PLAN +----------------------------------------------------------- + Aggregate + Output: local_ordered_count() WITHIN GROUP (ORDER BY a) + -> Foreign Scan on public.t1 + Output: a, b, c + Remote SQL: SELECT a FROM functions_test.t1 +(5 rows) + +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + local_ordered_count +--------------------- + 4 +(1 row) + +DROP AGGREGATE local_ordered_count(integer); +DROP FUNCTION local_ordered_sfunc(integer, integer); EXPLAIN (VERBOSE, COSTS OFF) SELECT date_trunc('dAy', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/test/expected/functions_9.out b/test/expected/functions_9.out index b2de027e..069e9686 100644 --- a/test/expected/functions_9.out +++ b/test/expected/functions_9.out @@ -801,6 +801,35 @@ SELECT percentile_disc(ARRAY[0.25, 0.5, 0.75]) WITHIN GROUP (ORDER BY a NULLS LA {2,2,2} (1 row) +CREATE FUNCTION local_ordered_sfunc(integer, integer) +RETURNS integer +LANGUAGE SQL +IMMUTABLE +AS 'SELECT $1 + 1'; +CREATE AGGREGATE local_ordered_count(ORDER BY integer) ( + SFUNC = local_ordered_sfunc, + STYPE = integer, + INITCOND = '0' +); +EXPLAIN (VERBOSE, COSTS OFF) +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + QUERY PLAN +----------------------------------------------------------- + Aggregate + Output: local_ordered_count() WITHIN GROUP (ORDER BY a) + -> Foreign Scan on public.t1 + Output: a, b, c + Remote SQL: SELECT a FROM functions_test.t1 +(5 rows) + +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + local_ordered_count +--------------------- + 4 +(1 row) + +DROP AGGREGATE local_ordered_count(integer); +DROP FUNCTION local_ordered_sfunc(integer, integer); EXPLAIN (VERBOSE, COSTS OFF) SELECT date_trunc('dAy', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/test/sql/functions.sql b/test/sql/functions.sql index 74b467b7..4cdc1bae 100644 --- a/test/sql/functions.sql +++ b/test/sql/functions.sql @@ -220,6 +220,25 @@ EXPLAIN (VERBOSE, COSTS OFF) SELECT percentile_disc(ARRAY[0.25, 0.5, 0.75]) WITH SELECT percentile_disc(ARRAY[0.25, 0.5, 0.75]) WITHIN GROUP (ORDER BY a) FILTER (WHERE b = 1) FROM t1; SELECT percentile_disc(ARRAY[0.25, 0.5, 0.75]) WITHIN GROUP (ORDER BY a NULLS LAST) FROM t1; +CREATE FUNCTION local_ordered_sfunc(integer, integer) +RETURNS integer +LANGUAGE SQL +IMMUTABLE +AS 'SELECT $1 + 1'; + +CREATE AGGREGATE local_ordered_count(ORDER BY integer) ( + SFUNC = local_ordered_sfunc, + STYPE = integer, + INITCOND = '0' +); + +EXPLAIN (VERBOSE, COSTS OFF) +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; +SELECT local_ordered_count() WITHIN GROUP (ORDER BY a) FROM t1; + +DROP AGGREGATE local_ordered_count(integer); +DROP FUNCTION local_ordered_sfunc(integer, integer); + EXPLAIN (VERBOSE, COSTS OFF) SELECT date_trunc('dAy', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1; SELECT date_trunc('day', c at time zone 'UTC') as d1 FROM t1 GROUP BY d1 ORDER BY d1;