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

Add support for CREATE TABLE AS #130

Merged
merged 5 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion include/pgduckdb/pgduckdb_planner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ extern "C" {
#include "optimizer/planner.h"
}

PlannedStmt *DuckdbPlanNode(Query *parse, const char *query_string, int cursor_options, ParamListInfo bound_params);
PlannedStmt *DuckdbPlanNode(Query *parse, int cursor_options, ParamListInfo bound_params);
37 changes: 27 additions & 10 deletions src/pgduckdb_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,37 @@ IsAllowedStatement(Query *query) {
if (query->hasModifyingCTE) {
return false;
}
/* For `SELECT ..` ActivePortal doesn't exist */
if (!ActivePortal)
return true;
/* `EXPLAIN ...` should be allowed */
if (ActivePortal->commandTag == CMDTAG_EXPLAIN)
return true;
return false;

/* We don't support modifying statements yet */
if (query->commandType != CMD_SELECT) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We already check this condition in DuckdbPlannerHook if statement but this is better place to contain this check commandType == SELECT.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I moved a few more checks from the DuckDBPlannerHook to IsAllowedStatement now.

return false;
}

/*
* If there's no rtable, we're only selecting constants. There's no point
* in using DuckDB for that.
*/
if (!query->rtable) {
return false;
}

/*
* If any table is from pg_catalog, we don't want to use DuckDB. This is
* because DuckDB has its own pg_catalog tables that contain different data
* then Postgres its pg_catalog tables.
*/
if (IsCatalogTable(query->rtable)) {
return false;
}

/* Anything else is hopefully fine... */
return true;
}

static PlannedStmt *
DuckdbPlannerHook(Query *parse, const char *query_string, int cursor_options, ParamListInfo bound_params) {
if (duckdb_execution && IsAllowedStatement(parse) && pgduckdb::IsExtensionRegistered() && parse->rtable &&
!IsCatalogTable(parse->rtable) && parse->commandType == CMD_SELECT) {
PlannedStmt *duckdb_plan = DuckdbPlanNode(parse, query_string, cursor_options, bound_params);
if (duckdb_execution && IsAllowedStatement(parse) && pgduckdb::IsExtensionRegistered()) {
PlannedStmt *duckdb_plan = DuckdbPlanNode(parse, cursor_options, bound_params);
if (duckdb_plan) {
return duckdb_plan;
}
Expand Down
9 changes: 8 additions & 1 deletion src/pgduckdb_planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ extern "C" {
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "optimizer/optimizer.h"
#include "tcop/pquery.h"
#include "utils/ruleutils.h"
#include "utils/syscache.h"
}

Expand Down Expand Up @@ -100,7 +102,12 @@ CreatePlan(Query *query, const char *query_string, ParamListInfo bound_params) {
}

PlannedStmt *
DuckdbPlanNode(Query *parse, const char *query_string, int cursor_options, ParamListInfo bound_params) {
DuckdbPlanNode(Query *parse, int cursor_options, ParamListInfo bound_params) {
const char *query_string = pg_get_querydef(parse, false);

if (ActivePortal && ActivePortal->commandTag == CMDTAG_EXPLAIN) {
query_string = psprintf("EXPLAIN %s", query_string);
}
/* We need to check can we DuckDB create plan */
Plan *duckdb_plan = (Plan *)castNode(CustomScan, CreatePlan(parse, query_string, bound_params));

Expand Down
60 changes: 60 additions & 0 deletions test/regression/data/web_page.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
1|AAAAAAAABAAAAAAA|1997-09-03||2450810|2452620|Y|98539|http://www.foo.com|welcome|2531|8|3|4
2|AAAAAAAACAAAAAAA|1997-09-03|2000-09-02|2450814|2452580|N||http://www.foo.com|protected|1564|4|3|1
3|AAAAAAAACAAAAAAA|2000-09-03||2450814|2452611|N||http://www.foo.com|feedback|1564|4|3|4
4|AAAAAAAAEAAAAAAA|1997-09-03|1999-09-03|2450812|2452579|N||http://www.foo.com|general|3732|18|7|1
5|AAAAAAAAEAAAAAAA|1999-09-04|2001-09-02|2450812|2452597|N||http://www.foo.com|welcome|3732|18|3|1
6|AAAAAAAAEAAAAAAA|2001-09-03||2450814|2452597|N||http://www.foo.com|ad|3732|18|7|4
7|AAAAAAAAHAAAAAAA|1997-09-03||2450815|2452574|N||http://www.foo.com|feedback|3034|18|7|4
8|AAAAAAAAIAAAAAAA|1997-09-03|2000-09-02|2450815|2452646|Y|1898|http://www.foo.com|protected|3128|12|2|4
9|AAAAAAAAIAAAAAAA|2000-09-03||2450807|2452579|Y|84146|http://www.foo.com|welcome|3128|13|5|3
10|AAAAAAAAKAAAAAAA|1997-09-03|1999-09-03||2452623|N||http://www.foo.com|||||
11|AAAAAAAAKAAAAAAA|1999-09-04|2001-09-02|2450814|2452611|N||http://www.foo.com|welcome|7046|23|4|4
12|AAAAAAAAKAAAAAAA|2001-09-03||2450815|2452611|N||http://www.foo.com|protected|7046|17|4|4
13|AAAAAAAANAAAAAAA|1997-09-03||2450807|2452629|N||http://www.foo.com|protected|2281|6|4|1
14|AAAAAAAAOAAAAAAA|1997-09-03|2000-09-02|2450810|2452639|N||http://www.foo.com|dynamic|5676|19|6|0
15|AAAAAAAAOAAAAAAA|2000-09-03||2450810|2452639|N||http://www.foo.com|dynamic|2469|10|5|2
16|AAAAAAAAABAAAAAA|1997-09-03|1999-09-03|2450814|2452601|Y|33463|http://www.foo.com|feedback|701|2|1|4
17|AAAAAAAAABAAAAAA|1999-09-04|2001-09-02|2450812|2452645|N||http://www.foo.com|general|701|11|1|3
18|AAAAAAAAABAAAAAA|2001-09-03||2450812|2452608|N||http://www.foo.com|ad|4080|11|6|3
19|AAAAAAAADBAAAAAA|1997-09-03||2450808|2452648|Y|57610|http://www.foo.com|general|2347|9|7|4
20|AAAAAAAAEBAAAAAA|1997-09-03|2000-09-02|2450809|2452555|Y|46487|http://www.foo.com|ad|1147|3|6|0
21|AAAAAAAAEBAAAAAA|2000-09-03||2450809|2452555|Y|10897|http://www.foo.com|general|1147|3|6|4
22|AAAAAAAAGBAAAAAA|1997-09-03|1999-09-03|2450812|2452565|Y|20213|http://www.foo.com|general|5663|25|3|4
23|AAAAAAAAGBAAAAAA|1999-09-04|2001-09-02|2450812|2452623|Y|20213|http://www.foo.com|order|4729|23|6|4
24|AAAAAAAAGBAAAAAA|2001-09-03||2450812|2452646|Y|20213|http://www.foo.com|dynamic|5918|23|6|1
25|AAAAAAAAJBAAAAAA|1997-09-03||2450811|2452620|N||http://www.foo.com|feedback|1526|9|4|2
26|AAAAAAAAKBAAAAAA|1997-09-03|2000-09-02|2450812|2452636|Y|98376|http://www.foo.com|ad|1826|9|3|1
27|AAAAAAAAKBAAAAAA|2000-09-03||2450812|2452607|Y|98376|http://www.foo.com|protected|1553|9|1|1
28|AAAAAAAAMBAAAAAA|1997-09-03|1999-09-03|2450807|2452572|N||http://www.foo.com|protected|1308|4|1|2
29|AAAAAAAAMBAAAAAA|1999-09-04|2001-09-02|2450808|2452611|N||http://www.foo.com|order|1308|4|1|2
30|AAAAAAAAMBAAAAAA|2001-09-03||2450808|2452611|N||http://www.foo.com|general|3872|18|1|4
31|AAAAAAAAPBAAAAAA|1997-09-03||2450810|2452596|N||http://www.foo.com|general|1732|3|6|0
32|AAAAAAAAACAAAAAA|1997-09-03|2000-09-02|2450808|2452585|N||http://www.foo.com|welcome|5104|20|7|4
33|AAAAAAAAACAAAAAA|2000-09-03||2450808|2452585|N||http://www.foo.com|protected|2129|7|1|0
34|AAAAAAAACCAAAAAA|1997-09-03|1999-09-03|2450808|2452616|N||http://www.foo.com|welcome|2726|12|5|2
35|AAAAAAAACCAAAAAA|1999-09-04|2001-09-02|2450808|2452591|N||http://www.foo.com|protected|2726|12|1|2
36|AAAAAAAACCAAAAAA|2001-09-03||2450812|2452613|N||http://www.foo.com|dynamic|2726|3|1|2
37|AAAAAAAAFCAAAAAA|1997-09-03||2450809|2452556|N||http://www.foo.com|ad|3076|15|3|0
38|AAAAAAAAGCAAAAAA|1997-09-03|2000-09-02|2450811|2452583|Y|37285|http://www.foo.com|general|3096|18|3|0
39|AAAAAAAAGCAAAAAA|2000-09-03||2450815|2452583|N||http://www.foo.com|general|3096|18|3|0
40|AAAAAAAAICAAAAAA|1997-09-03|1999-09-03|2450813|2452576|N||http://www.foo.com|general|4402|18|4|2
41|AAAAAAAAICAAAAAA|1999-09-04|2001-09-02|2450813|2452579|Y|16769|http://www.foo.com|welcome|784|3|4|4
42|AAAAAAAAICAAAAAA|2001-09-03||2450813|2452579|Y|60150|http://www.foo.com|dynamic|1451|3|4|4
43|AAAAAAAALCAAAAAA|1997-09-03||2450814|2452580|Y|64793|http://www.foo.com|ad|3760|12|3|2
44|AAAAAAAAMCAAAAAA|1997-09-03|2000-09-02|2450811|2452602|Y|92078|http://www.foo.com|ad|4179|19|7|1
45|AAAAAAAAMCAAAAAA|2000-09-03||2450811|2452575|Y|98633|http://www.foo.com|feedback|4584|19|7|4
46|AAAAAAAAOCAAAAAA|1997-09-03|1999-09-03|2450809|2452574|N||http://www.foo.com|protected|1711|4|5|1
47|AAAAAAAAOCAAAAAA|1999-09-04|2001-09-02|2450815|2452574|N||http://www.foo.com|welcome|1711|4|5|1
48|AAAAAAAAOCAAAAAA|2001-09-03||2450815|2452622|N||http://www.foo.com|ad|1732|9|5|1
49|AAAAAAAABDAAAAAA|1997-09-03||2450809|2452618|N||http://www.foo.com|order|4894|20|3|2
50|AAAAAAAACDAAAAAA|1997-09-03|2000-09-02|2450808|2452615|N||http://www.foo.com|welcome|5262|16|5|2
51|AAAAAAAACDAAAAAA|2000-09-03||2450811|2452564|N||http://www.foo.com|general|3423|19|7|1
52|AAAAAAAAEDAAAAAA|1997-09-03|1999-09-03|2450815|2452606|N||http://www.foo.com|welcome|3306|21|7|1
53|AAAAAAAAEDAAAAAA|1999-09-04|2001-09-02|2450808|2452636|N||http://www.foo.com|dynamic|3306|21|7|1
54|AAAAAAAAEDAAAAAA|2001-09-03||2450808|2452629|N||http://www.foo.com|protected|1931|7|2|2
55|AAAAAAAAHDAAAAAA|1997-09-03||2450811|2452549|N||http://www.foo.com|order|3788|19|1|0
56|AAAAAAAAIDAAAAAA|1997-09-03|2000-09-02|2450815|2452554|N||http://www.foo.com|protected|5733|24|2|2
57|AAAAAAAAIDAAAAAA|2000-09-03||2450811|2452568|N||http://www.foo.com|ad|5733|16|2|2
58|AAAAAAAAKDAAAAAA|1997-09-03|1999-09-03|2450813|2452619|Y|7625|http://www.foo.com|ad|6577|24|4|3
59|AAAAAAAAKDAAAAAA|1999-09-04|2001-09-02|2450813|2452624|Y|80555|http://www.foo.com|general|6577|24|2|3
60|AAAAAAAAKDAAAAAA|2001-09-03||2450813|2452566|Y|80555|http://www.foo.com|welcome|6577|24|2|3
42 changes: 21 additions & 21 deletions test/regression/expected/basic.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@ CREATE TABLE t(a INT);
INSERT INTO t SELECT g % 10 from generate_series(1,1000000) g;
SET client_min_messages to 'DEBUG1';
SELECT COUNT(*) FROM t;
count_star()
--------------
1000000
count
---------
1000000
(1 row)

SELECT a, COUNT(*) FROM t WHERE a > 5 GROUP BY a ORDER BY a;
a | count_star()
---+--------------
6 | 100000
7 | 100000
8 | 100000
9 | 100000
a | count
---+--------
6 | 100000
7 | 100000
8 | 100000
9 | 100000
(4 rows)

SET duckdb.max_threads_per_query to 4;
SELECT COUNT(*) FROM t;
count_star()
--------------
1000000
count
---------
1000000
(1 row)

SELECT a, COUNT(*) FROM t WHERE a > 5 GROUP BY a ORDER BY a;
a | count_star()
---+--------------
6 | 100000
7 | 100000
8 | 100000
9 | 100000
a | count
---+--------
6 | 100000
7 | 100000
8 | 100000
9 | 100000
(4 rows)

CREATE TABLE empty(a INT);
SELECT COUNT(*) FROM empty;
count_star()
--------------
0
count
-------
0
(1 row)

SET duckdb.max_threads_per_query TO default;
Expand Down
16 changes: 16 additions & 0 deletions test/regression/expected/create_table_as.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
\getenv pwd PWD
CREATE TABLE webpages AS SELECT * FROM read_csv(:'pwd' || '/data/web_page.csv') as (column00 int, column01 text, column02 date);
select * from webpages order by column00 limit 2;
column00 | column01 | column02
----------+------------------+------------
1 | AAAAAAAABAAAAAAA | 09-03-1997
2 | AAAAAAAACAAAAAAA | 09-03-1997
(2 rows)

select count(*) from webpages;
count
-------
60
(1 row)

DROP TABLE webpages;
4 changes: 2 additions & 2 deletions test/regression/expected/execution_error.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ insert into int_as_varchar SELECT * from (
) t(a);
select a::INTEGER from int_as_varchar;
ERROR: Duckdb execute returned an error: Conversion Error: Could not convert string 'abc' to INT32
LINE 1: select a::INTEGER from int_as_varchar;
^
LINE 1: SELECT (a)::integer AS a
^
4 changes: 2 additions & 2 deletions test/regression/expected/hugeint_conversion.out
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
create table hugeint_sum(a int);
insert into hugeint_sum select g from generate_series(1,100) g;
select pg_typeof(sum(a)) from hugeint_sum;
pg_typeof(sum(a))
-------------------
pg_typeof
-----------
hugeint
(1 row)

Expand Down
40 changes: 20 additions & 20 deletions test/regression/expected/materialized_view.out
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
CREATE TABLE t(a INT, b TEXT);
INSERT INTO t SELECT g % 1000, MD5(g::TEXT) FROM generate_series(1,20000) g;
CREATE TABLE t(a INT, b VARCHAR);
INSERT INTO t SELECT g % 1000, MD5(g::VARCHAR) FROM generate_series(1,20000) g;
SELECT COUNT(*) FROM t WHERE a % 100 = 0;
count_star()
--------------
200
count
-------
200
(1 row)

CREATE MATERIALIZED VIEW tv AS SELECT * FROM t WHERE a % 100 = 0;
SELECT COUNT(*) FROM tv;
count_star()
--------------
200
count
-------
200
(1 row)

INSERT INTO t SELECT g % 1000, MD5(g::TEXT) FROM generate_series(1,20000) g;
SELECT COUNT(*) FROM t WHERE a % 100 = 0;
count_star()
--------------
400
count
-------
400
(1 row)

REFRESH MATERIALIZED VIEW tv;
SELECT COUNT(*) FROM tv;
count_star()
--------------
400
count
-------
400
(1 row)

SELECT COUNT(*) FROM t WHERE (a % 100 = 0) AND (a < 30);
count_star()
--------------
40
count
-------
40
(1 row)

SELECT COUNT(*) FROM tv WHERE a < 30;
count_star()
--------------
40
count
-------
40
(1 row)

DROP MATERIALIZED VIEW tv;
Expand Down
48 changes: 24 additions & 24 deletions test/regression/expected/search_path.out
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
CREATE TABLE t(a INT);
INSERT INTO t SELECT g from generate_series(1,10) g;
SELECT count(*) FROM t;
count_star()
--------------
10
count
-------
10
(1 row)

SELECT count(*) FROM public.t;
count_star()
--------------
10
count
-------
10
(1 row)

-- Create schema `other`
CREATE SCHEMA other;
CREATE TABLE other.t(a INT);
INSERT INTO other.t SELECT g from generate_series(1,100) g;
SELECT count(*) FROM other.t;
count_star()
--------------
100
count
-------
100
(1 row)

-- Test fully qualified table name combinations
SELECT count(*) FROM public.t, other.t;
count_star()
--------------
1000
count
-------
1000
(1 row)

SELECT count(*) FROM t, other.t;
count_star()
--------------
1000
count
-------
1000
(1 row)

SELECT count(*) FROM t,t;
ERROR: table name "t" specified more than once
-- search_path ORDER matters.
SET search_path TO other, public;
SELECT count(*) FROM t;
count_star()
--------------
100
count
-------
100
(1 row)

SELECT count(*) FROM t, public.t;
count_star()
--------------
1000
count
-------
1000
(1 row)

-- No search_path
Expand All @@ -58,9 +58,9 @@ ERROR: relation "t" does not exist
LINE 1: SELECT count(*) FROM t, other.t;
^
SELECT count(*) FROM public.t, other.t;
count_star()
--------------
1000
count
-------
1000
(1 row)

-- Cleanup
Expand Down
Loading