Skip to content

Commit

Permalink
Handle exceptions caused by partitioned tables (#412)
Browse files Browse the repository at this point in the history
We don't support reading from partitioned tables yet and 
as reported in #409 this can cause crashes. So for now
disallow DuckDB execution when partitioned tables are
involved in the query. 

Supporting partitioned tables is tracked in #19
  • Loading branch information
Leo-XM-Zeng authored Nov 11, 2024
1 parent f563d6d commit 1b4514d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/pgduckdb_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ IsCatalogTable(List *tables) {
return false;
}

static bool
ContainsPartitionedTable(List *rtes) {
foreach_node(RangeTblEntry, rte, rtes) {
if (rte->rtekind == RTE_SUBQUERY) {
/* Check whether any table in the subquery is a partitioned table */
if (ContainsPartitionedTable(rte->subquery->rtable)) {
return true;
}
}

if (rte->relkind == RELKIND_PARTITIONED_TABLE) {
return true;
}
}
return false;
}

static bool
IsDuckdbTable(Oid relid) {
if (relid == InvalidOid) {
Expand Down Expand Up @@ -147,6 +164,14 @@ IsAllowedStatement(Query *query, bool throw_error = false) {
return false;
}

/*
* When accessing the partitioned table, we temporarily let PG handle it instead of DuckDB.
*/
if (ContainsPartitionedTable(query->rtable)) {
elog(elevel, "DuckDB does not support querying PG partitioned table");
return false;
}

/*
* We don't support multi-statement transactions yet, so don't try to
* execute queries in them even if duckdb.force_execution is enabled.
Expand Down
9 changes: 9 additions & 0 deletions test/regression/expected/basic.out
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,12 @@ SELECT COUNT(b) FROM t WHERE a > 3;
(1 row)

DROP TABLE t;
CREATE TABLE t (a INT) PARTITION BY LIST (a);
CREATE TABLE s (b INT);
SELECT * FROM t JOIN s ON a = b;
a | b
---+---
(0 rows)

DROP TABLE t;
DROP TABLE s;
6 changes: 6 additions & 0 deletions test/regression/sql/basic.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ INSERT INTO t SELECT g % 100, MD5(g::VARCHAR) FROM generate_series(1,1000) g;
INSERT INTO t SELECT g % 100, MD5(g::VARCHAR) FROM generate_series(1,1000) g;
SELECT COUNT(b) FROM t WHERE a > 3;
DROP TABLE t;

CREATE TABLE t (a INT) PARTITION BY LIST (a);
CREATE TABLE s (b INT);
SELECT * FROM t JOIN s ON a = b;
DROP TABLE t;
DROP TABLE s;

0 comments on commit 1b4514d

Please sign in to comment.