Skip to content

Commit dd4ca82

Browse files
committed
Reports missing values as bad.
BAD: INSERT INTO tab (col1, col2) VALUES ('val1'); GOOD: INSERT INTO tab (col1, col2) VALUES ('val1', 'val2'); Regress tests against DEFAULT and normal values as they're managed slightly different. Rod Taylor
1 parent e975123 commit dd4ca82

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

src/backend/parser/analyze.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.231 2002/04/17 20:57:56 tgl Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.232 2002/04/24 02:22:54 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -547,10 +547,12 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt,
547547
}
548548

549549
/*
550-
* XXX It is possible that the targetlist has fewer entries than were
551-
* in the columns list. We do not consider this an error. Perhaps we
552-
* should, if the columns list was explicitly given?
550+
* Ensure that the targetlist has the same number of entries
551+
* that were present in the columns list. Don't do the check
552+
* for select statements.
553553
*/
554+
if (stmt->cols != NIL && (icolumns != NIL || attnos != NIL))
555+
elog(ERROR, "INSERT has more target columns than expressions");
554556

555557
/* done building the range table and jointree */
556558
qry->rtable = pstate->p_rtable;
@@ -3247,7 +3249,7 @@ analyzeCreateSchemaStmt(CreateSchemaStmt *stmt)
32473249
}
32483250
}
32493251

3250-
result = NIL;
3252+
result = NIL;
32513253
result = nconc(result, cxt.tables);
32523254
result = nconc(result, cxt.views);
32533255
result = nconc(result, cxt.grants);

src/test/regress/expected/insert.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,24 @@ select * from inserttest;
1717
| 7 | testing
1818
(4 rows)
1919

20+
--
21+
-- insert with similar expression / target_list values (all fail)
22+
--
23+
insert into inserttest (col1, col2, col3) values (DEFAULT, DEFAULT);
24+
ERROR: INSERT has more target columns than expressions
25+
insert into inserttest (col1, col2, col3) values (1, 2);
26+
ERROR: INSERT has more target columns than expressions
27+
insert into inserttest (col1) values (1, 2);
28+
ERROR: INSERT has more expressions than target columns
29+
insert into inserttest (col1) values (DEFAULT, DEFAULT);
30+
ERROR: INSERT has more expressions than target columns
31+
select * from inserttest;
32+
col1 | col2 | col3
33+
------+------+---------
34+
| 3 | testing
35+
| 5 | testing
36+
| 5 | test
37+
| 7 | testing
38+
(4 rows)
39+
2040
drop table inserttest;

src/test/regress/sql/insert.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,15 @@ insert into inserttest (col1, col2, col3) values (DEFAULT, 5, DEFAULT);
88
insert into inserttest values (DEFAULT, 5, 'test');
99
insert into inserttest values (DEFAULT, 7);
1010

11+
select * from inserttest;
12+
13+
--
14+
-- insert with similar expression / target_list values (all fail)
15+
--
16+
insert into inserttest (col1, col2, col3) values (DEFAULT, DEFAULT);
17+
insert into inserttest (col1, col2, col3) values (1, 2);
18+
insert into inserttest (col1) values (1, 2);
19+
insert into inserttest (col1) values (DEFAULT, DEFAULT);
20+
1121
select * from inserttest;
1222
drop table inserttest;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy