Skip to content

Commit 97b4e5a

Browse files
committed
Add INSERT(..., DEFAULT, ).
Rod Taylor
1 parent aab0b8f commit 97b4e5a

File tree

11 files changed

+123
-16
lines changed

11 files changed

+123
-16
lines changed

src/backend/nodes/copyfuncs.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.174 2002/03/29 19:06:08 tgl Exp $
18+
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.175 2002/04/05 11:56:48 momjian Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -1947,6 +1947,15 @@ _copyFuncWithArgs(FuncWithArgs *from)
19471947
return newnode;
19481948
}
19491949

1950+
static InsertDefault *
1951+
_copyInsertDefault(InsertDefault *from)
1952+
{
1953+
InsertDefault *newnode = makeNode(InsertDefault);
1954+
1955+
return newnode;
1956+
}
1957+
1958+
19501959
static ClosePortalStmt *
19511960
_copyClosePortalStmt(ClosePortalStmt *from)
19521961
{
@@ -3055,6 +3064,9 @@ copyObject(void *from)
30553064
case T_FuncWithArgs:
30563065
retval = _copyFuncWithArgs(from);
30573066
break;
3067+
case T_InsertDefault:
3068+
retval = _copyInsertDefault(from);
3069+
break;
30583070

30593071
default:
30603072
elog(ERROR, "copyObject: don't know how to copy node type %d",

src/backend/nodes/equalfuncs.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* Portions Copyright (c) 1994, Regents of the University of California
2121
*
2222
* IDENTIFICATION
23-
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.122 2002/03/29 19:06:08 tgl Exp $
23+
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.123 2002/04/05 11:56:50 momjian Exp $
2424
*
2525
*-------------------------------------------------------------------------
2626
*/
@@ -773,6 +773,12 @@ _equalFuncWithArgs(FuncWithArgs *a, FuncWithArgs *b)
773773
&& equal(a->funcargs, b->funcargs);
774774
}
775775

776+
static bool
777+
_equalInsertDefault(InsertDefault *a, InsertDefault *b)
778+
{
779+
return true;
780+
}
781+
776782
static bool
777783
_equalClosePortalStmt(ClosePortalStmt *a, ClosePortalStmt *b)
778784
{
@@ -2215,6 +2221,9 @@ equal(void *a, void *b)
22152221
case T_FuncWithArgs:
22162222
retval = _equalFuncWithArgs(a, b);
22172223
break;
2224+
case T_InsertDefault:
2225+
retval = _equalInsertDefault(a, b);
2226+
break;
22182227

22192228
default:
22202229
elog(WARNING, "equal: don't know whether nodes of type %d are equal",

src/backend/parser/analyze.c

Lines changed: 20 additions & 4 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.226 2002/04/02 06:30:34 tgl Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.227 2002/04/05 11:56:51 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -518,13 +518,29 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt,
518518
TargetEntry *tle = (TargetEntry *) lfirst(tl);
519519
ResTarget *col;
520520

521-
Assert(!tle->resdom->resjunk);
522521
if (icolumns == NIL || attnos == NIL)
523522
elog(ERROR, "INSERT has more expressions than target columns");
524523
col = (ResTarget *) lfirst(icolumns);
525-
Assert(IsA(col, ResTarget));
526-
updateTargetListEntry(pstate, tle, col->name, lfirsti(attnos),
524+
525+
/*
526+
* When the value is to be set to the column default we can simply
527+
* drop it now and handle it later on using methods for missing
528+
* columns.
529+
*/
530+
if (!IsA(tle, InsertDefault))
531+
{
532+
Assert(IsA(col, ResTarget));
533+
Assert(!tle->resdom->resjunk);
534+
updateTargetListEntry(pstate, tle, col->name, lfirsti(attnos),
527535
col->indirection);
536+
}
537+
else
538+
{
539+
icolumns = lremove(icolumns, icolumns);
540+
attnos = lremove(attnos, attnos);
541+
qry->targetList = lremove(tle, qry->targetList);
542+
}
543+
528544
icolumns = lnext(icolumns);
529545
attnos = lnext(attnos);
530546
}

src/backend/parser/gram.y

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.299 2002/04/01 04:35:38 tgl Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.300 2002/04/05 11:56:53 momjian Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -203,6 +203,7 @@ static bool set_name_needs_quotes(const char *name);
203203
from_clause, from_list, opt_array_bounds, qualified_name_list,
204204
any_name, any_name_list, expr_list, dotted_name, attrs,
205205
target_list, update_target_list, insert_column_list,
206+
insert_target_list,
206207
def_list, opt_indirection, group_clause, TriggerFuncArgs,
207208
select_limit, opt_select_limit
208209

@@ -263,7 +264,7 @@ static bool set_name_needs_quotes(const char *name);
263264
%type <node> table_ref
264265
%type <jexpr> joined_table
265266
%type <range> relation_expr
266-
%type <target> target_el, update_target_el
267+
%type <target> target_el, insert_target_el, update_target_el
267268

268269
%type <typnam> Typename, SimpleTypename, ConstTypename
269270
GenericType, Numeric, Character, ConstDatetime, ConstInterval, Bit
@@ -3504,7 +3505,7 @@ InsertStmt: INSERT INTO qualified_name insert_rest
35043505
}
35053506
;
35063507

3507-
insert_rest: VALUES '(' target_list ')'
3508+
insert_rest: VALUES '(' insert_target_list ')'
35083509
{
35093510
$$ = makeNode(InsertStmt);
35103511
$$->cols = NIL;
@@ -3525,7 +3526,7 @@ insert_rest: VALUES '(' target_list ')'
35253526
$$->targetList = NIL;
35263527
$$->selectStmt = $1;
35273528
}
3528-
| '(' insert_column_list ')' VALUES '(' target_list ')'
3529+
| '(' insert_column_list ')' VALUES '(' insert_target_list ')'
35293530
{
35303531
$$ = makeNode(InsertStmt);
35313532
$$->cols = $2;
@@ -5244,7 +5245,6 @@ c_expr: columnref
52445245
s->val.type = T_String;
52455246
s->val.val.str = "now";
52465247
s->typename = makeTypeName(xlateSqlType("text"));
5247-
52485248
d = makeTypeName(xlateSqlType("timetz"));
52495249
if (($3 < 0) || ($3 > 13))
52505250
elog(ERROR,"CURRENT_TIME(%d) precision must be between %d and %d",
@@ -5721,6 +5721,23 @@ update_target_el: ColId opt_indirection '=' a_expr
57215721
}
57225722
;
57235723

5724+
insert_target_list: insert_target_list ',' insert_target_el
5725+
{ $$ = lappend($1, $3); }
5726+
| insert_target_el
5727+
{ $$ = makeList1($1); }
5728+
;
5729+
5730+
insert_target_el: target_el { $$ = $1; }
5731+
| DEFAULT {
5732+
InsertDefault *def = makeNode(InsertDefault);
5733+
$$ = makeNode(ResTarget);
5734+
$$->name = NULL;
5735+
$$->indirection = NULL;
5736+
$$->val = (Node *)def;
5737+
}
5738+
;
5739+
5740+
57245741
/*****************************************************************************
57255742
*
57265743
* Names and constants

src/backend/parser/parse_target.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/parser/parse_target.c,v 1.81 2002/04/02 08:51:52 inoue Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_target.c,v 1.82 2002/04/05 11:56:53 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -175,9 +175,19 @@ transformTargetList(ParseState *pstate, List *targetlist)
175175
false));
176176
}
177177
}
178+
else if (IsA(res->val, InsertDefault))
179+
{
180+
InsertDefault *newnode = makeNode(InsertDefault);
181+
182+
/*
183+
* If this is a DEFAULT element, we make a junk entry
184+
* which will get dropped on return to transformInsertStmt().
185+
*/
186+
p_target = lappend(p_target, newnode);
187+
}
178188
else
179189
{
180-
/* Everything else but ColumnRef */
190+
/* Everything else but ColumnRef and InsertDefault */
181191
p_target = lappend(p_target,
182192
transformTargetEntry(pstate,
183193
res->val,

src/include/nodes/nodes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: nodes.h,v 1.103 2002/03/22 02:56:36 tgl Exp $
10+
* $Id: nodes.h,v 1.104 2002/04/05 11:56:54 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -229,6 +229,7 @@ typedef enum NodeTag
229229
T_PrivGrantee,
230230
T_FuncWithArgs,
231231
T_PrivTarget,
232+
T_InsertDefault,
232233

233234
/*
234235
* TAGS FOR FUNCTION-CALL CONTEXT AND RESULTINFO NODES (see fmgr.h)

src/include/nodes/parsenodes.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: parsenodes.h,v 1.167 2002/04/01 04:35:40 tgl Exp $
10+
* $Id: parsenodes.h,v 1.168 2002/04/05 11:56:54 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -359,6 +359,14 @@ typedef struct ResTarget
359359
* assign */
360360
} ResTarget;
361361

362+
/*
363+
* Empty node used as a marker for Default Columns
364+
*/
365+
typedef struct InsertDefault
366+
{
367+
NodeTag type;
368+
} InsertDefault;
369+
362370
/*
363371
* SortGroupBy - for ORDER BY clause
364372
*/

src/test/regress/expected/insert.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--
2+
-- insert with DEFAULT in the target_list
3+
--
4+
create table inserttest (col1 int4, col2 int4 NOT NULL, col3 text default 'testing');
5+
insert into inserttest (col1, col2, col3) values (DEFAULT, DEFAULT, DEFAULT);
6+
ERROR: ExecAppend: Fail to add null value in not null attribute col2
7+
insert into inserttest (col2, col3) values (3, DEFAULT);
8+
insert into inserttest (col1, col2, col3) values (DEFAULT, 5, DEFAULT);
9+
insert into inserttest values (DEFAULT, 5, 'test');
10+
insert into inserttest values (DEFAULT, 7);
11+
select * from inserttest;
12+
col1 | col2 | col3
13+
------+------+---------
14+
| 3 | testing
15+
| 5 | testing
16+
| 5 | test
17+
| 7 | testing
18+
(4 rows)
19+
20+
drop table inserttest;

src/test/regress/parallel_schedule

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ test: horology
2121
# ----------
2222
# These four each depend on the previous one
2323
# ----------
24+
test: insert
2425
test: create_function_1
2526
test: create_type
2627
test: create_table

src/test/regress/serial_schedule

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Header: /cvsroot/pgsql/src/test/regress/serial_schedule,v 1.8 2002/03/19 02:18:24 momjian Exp $
1+
# $Header: /cvsroot/pgsql/src/test/regress/serial_schedule,v 1.9 2002/04/05 11:56:55 momjian Exp $
22
# This should probably be in an order similar to parallel_schedule.
33
test: boolean
44
test: char
@@ -37,6 +37,7 @@ test: type_sanity
3737
test: opr_sanity
3838
test: geometry
3939
test: horology
40+
test: insert
4041
test: create_function_1
4142
test: create_type
4243
test: create_table

src/test/regress/sql/insert.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--
2+
-- insert with DEFAULT in the target_list
3+
--
4+
create table inserttest (col1 int4, col2 int4 NOT NULL, col3 text default 'testing');
5+
insert into inserttest (col1, col2, col3) values (DEFAULT, DEFAULT, DEFAULT);
6+
insert into inserttest (col2, col3) values (3, DEFAULT);
7+
insert into inserttest (col1, col2, col3) values (DEFAULT, 5, DEFAULT);
8+
insert into inserttest values (DEFAULT, 5, 'test');
9+
insert into inserttest values (DEFAULT, 7);
10+
11+
select * from inserttest;
12+
drop table inserttest;

0 commit comments

Comments
 (0)