Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix approx_count_distinct for queries without a FROM #524

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/pgduckdb_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ ContainsDuckdbItems(Node *node, void *context) {
}
}

if (IsA(node, Aggref)) {
Aggref *func = castNode(Aggref, node);
if (pgduckdb::IsDuckdbOnlyFunction(func->aggfnoid)) {
return true;
}
}

#if PG_VERSION_NUM >= 160000
return expression_tree_walker(node, ContainsDuckdbItems, context);
#else
Expand Down Expand Up @@ -160,10 +167,22 @@ IsAllowedStatement(Query *query, bool throw_error = false) {
}

/*
* If there's no rtable, we're only selecting constants. There's no point
* in using DuckDB for that.
* XXX: This is a bit of a weird one, because it is only allowed when
* IsAllowedStatement is called with throw_error.
*
* If there's no rtable, we're only selecting constants. From a performance
* perspective there's not really a point in using DuckDB. If we remove
* this heck many common queries that are used to inspect postgres will
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* this heck many common queries that are used to inspect postgres will
* this hack many common queries that are used to inspect postgres will

* throw a warning or return incorrect results. For example:
*
* SELECT current_setting('work_mem');
*
* So even if a user sets duckdb.force_execution = true, we still won't
* forward such queries to DuckDB. With one exception: If the query
* requires duckdb e.g. due to a duckdb-only function being used, we'll
* still executing this in DuckDB.
*/
if (!query->rtable) {
if (!query->rtable && !throw_error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks odd - if !query->rtable we should stop the function here (otherwise ContainsCatalogTable and subsequent calls would fail pretty badly no?)
Shouldn't it be something like

if (!query->rtable) {
   if (throw_error) {
      elog(...)

elog(elevel, "DuckDB usage requires at least one table");
return false;
}
Expand Down
13 changes: 13 additions & 0 deletions test/regression/expected/approx_count_distinct.out
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,17 @@ SELECT a, approx_count_distinct(b) OVER (PARTITION BY a) FROM t ORDER BY a;
5 | 1
(8 rows)

SELECT approx_count_distinct(1);
approx_count_distinct
-----------------------
1
(1 row)

SET duckdb.force_execution = false;
SELECT approx_count_distinct(1);
approx_count_distinct
-----------------------
1
(1 row)

DROP TABLE t;
3 changes: 3 additions & 0 deletions test/regression/sql/approx_count_distinct.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ 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;
SELECT approx_count_distinct(1);
SET duckdb.force_execution = false;
SELECT approx_count_distinct(1);
DROP TABLE t;