-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support approx_count_distinct (#499)
Add support for `approx_count_distinct()`
- Loading branch information
Showing
7 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
CREATE FUNCTION @extschema@.approx_count_distinct_sfunc(bigint, anyelement) | ||
RETURNS bigint LANGUAGE 'plpgsql' | ||
SET search_path = pg_catalog, pg_temp | ||
AS | ||
$func$ | ||
BEGIN | ||
RAISE EXCEPTION 'Aggregate `approx_count_distinct(ANYELEMENT)` only works with Duckdb execution.'; | ||
END; | ||
$func$; | ||
|
||
CREATE AGGREGATE @extschema@.approx_count_distinct(anyelement) | ||
( | ||
sfunc = @extschema@.approx_count_distinct_sfunc, | ||
stype = bigint, | ||
initcond = 0 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
CREATE TABLE t (a int, b text); | ||
INSERT INTO t VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'); | ||
INSERT INTO t VALUES (2, 'f'), (3, 'g'), (4, 'h'); | ||
SELECT approx_count_distinct(a), approx_count_distinct(b) FROM t; | ||
approx_count_distinct | approx_count_distinct | ||
-----------------------+----------------------- | ||
5 | 9 | ||
(1 row) | ||
|
||
SELECT a, approx_count_distinct(b) FROM t GROUP BY a ORDER BY a; | ||
a | approx_count_distinct | ||
---+----------------------- | ||
1 | 1 | ||
2 | 2 | ||
3 | 2 | ||
4 | 2 | ||
5 | 1 | ||
(5 rows) | ||
|
||
SELECT a, approx_count_distinct(b) OVER (PARTITION BY a) FROM t ORDER BY a; | ||
a | approx_count_distinct | ||
---+----------------------- | ||
1 | 1 | ||
2 | 2 | ||
2 | 2 | ||
3 | 2 | ||
3 | 2 | ||
4 | 2 | ||
4 | 2 | ||
5 | 1 | ||
(8 rows) | ||
|
||
DROP TABLE t; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,3 +159,4 @@ FETCH PRIOR FROM c; | |
|
||
COMMIT; | ||
DROP FUNCTION f, f2; | ||
DROP TABLE t; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,4 @@ test: transaction_errors | |
test: secrets | ||
test: prepare | ||
test: function | ||
test: approx_count_distinct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CREATE TABLE t (a int, b text); | ||
INSERT INTO t VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'); | ||
INSERT INTO t VALUES (2, 'f'), (3, 'g'), (4, 'h'); | ||
SELECT approx_count_distinct(a), approx_count_distinct(b) FROM t; | ||
SELECT a, approx_count_distinct(b) FROM t GROUP BY a ORDER BY a; | ||
SELECT a, approx_count_distinct(b) OVER (PARTITION BY a) FROM t ORDER BY a; | ||
DROP TABLE t; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,3 +115,4 @@ FETCH PRIOR FROM c; | |
COMMIT; | ||
|
||
DROP FUNCTION f, f2; | ||
DROP TABLE t; |