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

INSERT ... SELECT or multi-row VALUES errors when using default values #448

Merged
merged 12 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
20 changes: 20 additions & 0 deletions src/vendor/pg_ruleutils_14.c
Original file line number Diff line number Diff line change
Expand Up @@ -6637,6 +6637,26 @@ get_insert_query_def(Query *query, deparse_context *context,
if (tle->resjunk)
continue; /* ignore junk entries */

/*
* If it's an INSERT ... SELECT or multi-row VALUES, the entry
* with the default value is ignored unless it is specified
*/
if (values_rte)
{
if (!(IsA(tle->expr, Var)))
continue;
}
else if (select_rte)
{
/*
* If it's INSERT... SELECT needs to make more careful decisions about
* Const structures. If location is -1, it comes from the DEFAULT clause
*/
if (IsA(tle->expr, Const) &&
((Const *) tle->expr)->location == -1)
continue;
}

appendStringInfoString(buf, sep);
sep = ", ";

Expand Down
20 changes: 20 additions & 0 deletions src/vendor/pg_ruleutils_15.c
Original file line number Diff line number Diff line change
Expand Up @@ -6718,6 +6718,26 @@ get_insert_query_def(Query *query, deparse_context *context,
if (tle->resjunk)
continue; /* ignore junk entries */

/*
* If it's an INSERT ... SELECT or multi-row VALUES, the entry
* with the default value is ignored unless it is specified
*/
if (values_rte)
{
if (!(IsA(tle->expr, Var)))
continue;
}
else if (select_rte)
{
/*
* If it's INSERT... SELECT needs to make more careful decisions about
* Const structures. If location is -1, it comes from the DEFAULT clause
*/
if (IsA(tle->expr, Const) &&
((Const *) tle->expr)->location == -1)
continue;
}

appendStringInfoString(buf, sep);
sep = ", ";

Expand Down
20 changes: 20 additions & 0 deletions src/vendor/pg_ruleutils_16.c
Original file line number Diff line number Diff line change
Expand Up @@ -6678,6 +6678,26 @@ get_insert_query_def(Query *query, deparse_context *context,
if (tle->resjunk)
continue; /* ignore junk entries */

/*
* If it's an INSERT ... SELECT or multi-row VALUES, the entry
* with the default value is ignored unless it is specified
*/
if (values_rte)
{
if (!(IsA(tle->expr, Var)))
continue;
}
else if (select_rte)
{
/*
* If it's INSERT... SELECT needs to make more careful decisions about
* Const structures. If location is -1, it comes from the DEFAULT clause
*/
if (IsA(tle->expr, Const) &&
((Const *) tle->expr)->location == -1)
continue;
}

appendStringInfoString(buf, sep);
sep = ", ";

Expand Down
20 changes: 20 additions & 0 deletions src/vendor/pg_ruleutils_17.c
Original file line number Diff line number Diff line change
Expand Up @@ -6692,6 +6692,26 @@ get_insert_query_def(Query *query, deparse_context *context,
if (tle->resjunk)
continue; /* ignore junk entries */

/*
* If it's an INSERT ... SELECT or multi-row VALUES, the entry
* with the default value is ignored unless it is specified
*/
if (values_rte)
{
if (!(IsA(tle->expr, Var)))
continue;
}
else if (select_rte)
{
/*
* If it's INSERT... SELECT needs to make more careful decisions about
* Const structures. If location is -1, it comes from the DEFAULT clause
*/
if (IsA(tle->expr, Const) &&
((Const *) tle->expr)->location == -1)
continue;
}

appendStringInfoString(buf, sep);
sep = ", ";

Expand Down
85 changes: 84 additions & 1 deletion test/regression/expected/temporary_tables.out
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,87 @@ pg_temp main CREATE TABLE webpages(column00 INTEGER, column01 VARCHAR, column02

(1 row)

DROP TABLE webpages, t, t_heap, t_heap2;
-- multi-VALUES
CREATE TEMP TABLE ta (a int DEFAULT 3, b int) USING duckdb;
INSERT INTO ta (b) VALUES (123), (456);
INSERT INTO ta (a, b) VALUES (123, 456), (456, 123);
SELECT * FROM ta;
a | b
-----+-----
3 | 123
3 | 456
123 | 456
456 | 123
(4 rows)

CREATE TEMP TABLE tb (a int DEFAULT 3, b int, c varchar DEFAULT 'pg_duckdb') USING duckdb;
INSERT INTO tb (a) VALUES (123), (456);
INSERT INTO tb (b) VALUES (123), (456);
INSERT INTO tb (c) VALUES ('ta'), ('tb');
SELECT * FROM tb;
a | b | c
-----+-----+-----------
123 | | pg_duckdb
456 | | pg_duckdb
3 | 123 | pg_duckdb
3 | 456 | pg_duckdb
3 | | ta
3 | | tb
(6 rows)

-- INSERT ... SELECT
TRUNCATE TABLE ta;
INSERT INTO ta (a) SELECT 789;
INSERT INTO ta (b) SELECT 789;
INSERT INTO ta (a) SELECT * FROM t_heap;
INSERT INTO ta (b) SELECT * FROM t_heap;
SELECT * FROM ta;
a | b
-----+-----
789 |
3 | 789
1 |
3 | 1
(4 rows)

INSERT INTO ta (a) SELECT generate_series(1, 3); -- no support
ERROR: (PGDuckDB/Duckdb_ExecCustomScan) Conversion Error: Unimplemented type for cast (BIGINT[] -> INTEGER)
LINE 1: INSERT INTO pg_temp.main.ta (a) SELECT generate_series(1, 3) AS generate_serie...
^
TRUNCATE TABLE tb;
INSERT INTO tb (a) SELECT 789;
INSERT INTO tb (b) SELECT 789;
INSERT INTO tb (a) SELECT * FROM t_heap;
INSERT INTO tb (b) SELECT * FROM t_heap;
SELECT * FROM tb;
a | b | c
-----+-----+-----------
789 | | pg_duckdb
3 | 789 | pg_duckdb
1 | | pg_duckdb
3 | 1 | pg_duckdb
(4 rows)

TRUNCATE TABLE tb;
INSERT INTO tb (c) SELECT 'ta';
INSERT INTO tb (c) SELECT 'ta' || 'tb';
INSERT INTO tb (a) SELECT (2)::numeric;
INSERT INTO tb (b) SELECT (3)::numeric;
INSERT INTO tb (c) SELECT t.a FROM (SELECT 'ta' || 'tb' AS a) t;
INSERT INTO tb (b, c) SELECT t.b, t.c FROM (SELECT (3)::numeric AS b, 'ta' || 'tb' AS c) t;
INSERT INTO tb (a, b, c) SELECT 1, 2, 'tb';
INSERT INTO tb SELECT * FROM (SELECT (3)::numeric AS a, (3)::numeric AS b, 'ta' || 'tb' AS c) t;
SELECT * FROM tb;
a | b | c
---+---+-----------
3 | | ta
3 | | tatb
2 | | pg_duckdb
3 | 3 | pg_duckdb
3 | | tatb
3 | 3 | tatb
1 | 2 | tb
3 | 3 | tatb
(8 rows)

DROP TABLE webpages, t, t_heap, t_heap2, ta, tb;
41 changes: 40 additions & 1 deletion test/regression/sql/temporary_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,43 @@ SELECT * FROM t_heap2;

SELECT duckdb.raw_query($$ SELECT database_name, schema_name, sql FROM duckdb_tables() $$);

DROP TABLE webpages, t, t_heap, t_heap2;
-- multi-VALUES
CREATE TEMP TABLE ta (a int DEFAULT 3, b int) USING duckdb;
INSERT INTO ta (b) VALUES (123), (456);
INSERT INTO ta (a, b) VALUES (123, 456), (456, 123);
SELECT * FROM ta;

CREATE TEMP TABLE tb (a int DEFAULT 3, b int, c varchar DEFAULT 'pg_duckdb') USING duckdb;
Leo-XM-Zeng marked this conversation as resolved.
Show resolved Hide resolved
INSERT INTO tb (a) VALUES (123), (456);
INSERT INTO tb (b) VALUES (123), (456);
INSERT INTO tb (c) VALUES ('ta'), ('tb');
SELECT * FROM tb;

-- INSERT ... SELECT
TRUNCATE TABLE ta;
INSERT INTO ta (a) SELECT 789;
INSERT INTO ta (b) SELECT 789;
INSERT INTO ta (a) SELECT * FROM t_heap;
INSERT INTO ta (b) SELECT * FROM t_heap;
SELECT * FROM ta;
INSERT INTO ta (a) SELECT generate_series(1, 3); -- no support

TRUNCATE TABLE tb;
INSERT INTO tb (a) SELECT 789;
INSERT INTO tb (b) SELECT 789;
INSERT INTO tb (a) SELECT * FROM t_heap;
INSERT INTO tb (b) SELECT * FROM t_heap;
SELECT * FROM tb;

TRUNCATE TABLE tb;
INSERT INTO tb (c) SELECT 'ta';
INSERT INTO tb (c) SELECT 'ta' || 'tb';
INSERT INTO tb (a) SELECT (2)::numeric;
INSERT INTO tb (b) SELECT (3)::numeric;
INSERT INTO tb (c) SELECT t.a FROM (SELECT 'ta' || 'tb' AS a) t;
INSERT INTO tb (b, c) SELECT t.b, t.c FROM (SELECT (3)::numeric AS b, 'ta' || 'tb' AS c) t;
INSERT INTO tb (a, b, c) SELECT 1, 2, 'tb';
INSERT INTO tb SELECT * FROM (SELECT (3)::numeric AS a, (3)::numeric AS b, 'ta' || 'tb' AS c) t;
SELECT * FROM tb;

DROP TABLE webpages, t, t_heap, t_heap2, ta, tb;