Skip to content

Commit

Permalink
Support approx_count_distinct
Browse files Browse the repository at this point in the history
  • Loading branch information
dpxcc committed Dec 17, 2024
1 parent eb2c6bb commit d1a2548
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions sql/pg_duckdb--0.2.0--0.3.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE FUNCTION @[email protected]_count_distinct(a anyelement)
RETURNS bigint LANGUAGE 'plpgsql'
SET search_path = pg_catalog, pg_temp
AS
$func$
BEGIN
RAISE EXCEPTION 'Function `approx_count_distinct(ANYELEMENT)` only works with Duckdb execution.';
END;
$func$;
2 changes: 1 addition & 1 deletion src/pgduckdb_metadata_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ BuildDuckdbOnlyFunctions() {
* caching its OID as a DuckDB-only function.
*/
const char *function_names[] = {"read_parquet", "read_csv", "iceberg_scan", "iceberg_metadata",
"iceberg_snapshots", "delta_scan", "read_json"};
"iceberg_snapshots", "delta_scan", "read_json", "approx_count_distinct"};

for (uint32_t i = 0; i < lengthof(function_names); i++) {
CatCList *catlist = SearchSysCacheList1(PROCNAMEARGSNSP, CStringGetDatum(function_names[i]));
Expand Down
10 changes: 10 additions & 0 deletions test/regression/expected/approx_count_distinct.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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)

DROP TABLE t;
1 change: 1 addition & 0 deletions test/regression/expected/transactions.out
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,4 @@ FETCH PRIOR FROM c;

COMMIT;
DROP FUNCTION f, f2;
DROP TABLE t;
1 change: 1 addition & 0 deletions test/regression/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ test: transaction_errors
test: secrets
test: prepare
test: function
test: approx_count_distinct
5 changes: 5 additions & 0 deletions test/regression/sql/approx_count_distinct.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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;
DROP TABLE t;
1 change: 1 addition & 0 deletions test/regression/sql/transactions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,4 @@ FETCH PRIOR FROM c;
COMMIT;

DROP FUNCTION f, f2;
DROP TABLE t;

0 comments on commit d1a2548

Please sign in to comment.