Skip to content

Commit

Permalink
Support approx_count_distinct (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpxcc committed Dec 17, 2024
1 parent 43fb1a9 commit 94030bb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
10 changes: 10 additions & 0 deletions sql/pg_mooncake--0.0.1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,16 @@ BEGIN
END;
$func$;

CREATE FUNCTION mooncake.approx_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$;

CREATE TABLE mooncake.secrets (
name TEXT NOT NULL,
type TEXT NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion src/pgduckdb/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/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) USING columnstore;
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 mooncake.approx_count_distinct(a), mooncake.approx_count_distinct(b) FROM t;
approx_count_distinct | approx_count_distinct
-----------------------+-----------------------
5 | 9
(1 row)

DROP TABLE t;
5 changes: 5 additions & 0 deletions test/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) USING columnstore;
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 mooncake.approx_count_distinct(a), mooncake.approx_count_distinct(b) FROM t;
DROP TABLE t;

0 comments on commit 94030bb

Please sign in to comment.