Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e0c433c

Browse files
committed
Change CREATE TABLE so that column default expressions coming from different
inheritance parent tables are compared using equal(), instead of doing strcmp() on the nodeToString representation. The old implementation was always a tad cheesy, and it finally fails completely as of 8.4, now that the node tree might contain syntax location information. equal() knows it's supposed to ignore those fields, but strcmp() hardly can. Per recent report from Scott Ribe.
1 parent 051168b commit e0c433c

File tree

8 files changed

+50
-25
lines changed

8 files changed

+50
-25
lines changed

src/backend/commands/tablecmds.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.300 2009/10/05 19:24:37 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.301 2009/10/06 00:55:26 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -493,7 +493,7 @@ DefineRelation(CreateStmt *stmt, char relkind)
493493
cooked->contype = CONSTR_DEFAULT;
494494
cooked->name = NULL;
495495
cooked->attnum = attnum;
496-
cooked->expr = stringToNode(colDef->cooked_default);
496+
cooked->expr = colDef->cooked_default;
497497
cooked->is_local = true; /* not used for defaults */
498498
cooked->inhcount = 0; /* ditto */
499499
cookedDefaults = lappend(cookedDefaults, cooked);
@@ -1168,8 +1168,8 @@ MergeAttributes(List *schema, List *supers, bool istemp,
11681168
List *constraints = NIL;
11691169
int parentsWithOids = 0;
11701170
bool have_bogus_defaults = false;
1171-
char *bogus_marker = "Bogus!"; /* marks conflicting defaults */
11721171
int child_attno;
1172+
static Node bogus_marker = { 0 }; /* marks conflicting defaults */
11731173

11741174
/*
11751175
* Check for and reject tables with too many columns. We perform this
@@ -1353,7 +1353,7 @@ MergeAttributes(List *schema, List *supers, bool istemp,
13531353
*/
13541354
if (attribute->atthasdef)
13551355
{
1356-
char *this_default = NULL;
1356+
Node *this_default = NULL;
13571357
AttrDefault *attrdef;
13581358
int i;
13591359

@@ -1364,7 +1364,7 @@ MergeAttributes(List *schema, List *supers, bool istemp,
13641364
{
13651365
if (attrdef[i].adnum == parent_attno)
13661366
{
1367-
this_default = attrdef[i].adbin;
1367+
this_default = stringToNode(attrdef[i].adbin);
13681368
break;
13691369
}
13701370
}
@@ -1382,10 +1382,10 @@ MergeAttributes(List *schema, List *supers, bool istemp,
13821382
*/
13831383
Assert(def->raw_default == NULL);
13841384
if (def->cooked_default == NULL)
1385-
def->cooked_default = pstrdup(this_default);
1386-
else if (strcmp(def->cooked_default, this_default) != 0)
1385+
def->cooked_default = this_default;
1386+
else if (!equal(def->cooked_default, this_default))
13871387
{
1388-
def->cooked_default = bogus_marker;
1388+
def->cooked_default = &bogus_marker;
13891389
have_bogus_defaults = true;
13901390
}
13911391
}
@@ -1524,7 +1524,7 @@ MergeAttributes(List *schema, List *supers, bool istemp,
15241524
{
15251525
ColumnDef *def = lfirst(entry);
15261526

1527-
if (def->cooked_default == bogus_marker)
1527+
if (def->cooked_default == &bogus_marker)
15281528
ereport(ERROR,
15291529
(errcode(ERRCODE_INVALID_COLUMN_DEFINITION),
15301530
errmsg("column \"%s\" inherits conflicting default values",

src/backend/nodes/copyfuncs.c

Lines changed: 2 additions & 2 deletions
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-
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.439 2009/10/05 19:24:38 tgl Exp $
18+
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.440 2009/10/06 00:55:26 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -2054,7 +2054,7 @@ _copyColumnDef(ColumnDef *from)
20542054
COPY_SCALAR_FIELD(is_local);
20552055
COPY_SCALAR_FIELD(is_not_null);
20562056
COPY_NODE_FIELD(raw_default);
2057-
COPY_STRING_FIELD(cooked_default);
2057+
COPY_NODE_FIELD(cooked_default);
20582058
COPY_NODE_FIELD(constraints);
20592059

20602060
return newnode;

src/backend/nodes/equalfuncs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* Portions Copyright (c) 1994, Regents of the University of California
2323
*
2424
* IDENTIFICATION
25-
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.362 2009/10/05 19:24:38 tgl Exp $
25+
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.363 2009/10/06 00:55:26 tgl Exp $
2626
*
2727
*-------------------------------------------------------------------------
2828
*/
@@ -2072,7 +2072,7 @@ _equalColumnDef(ColumnDef *a, ColumnDef *b)
20722072
COMPARE_SCALAR_FIELD(is_local);
20732073
COMPARE_SCALAR_FIELD(is_not_null);
20742074
COMPARE_NODE_FIELD(raw_default);
2075-
COMPARE_STRING_FIELD(cooked_default);
2075+
COMPARE_NODE_FIELD(cooked_default);
20762076
COMPARE_NODE_FIELD(constraints);
20772077

20782078
return true;

src/backend/nodes/outfuncs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.364 2009/09/17 20:49:28 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.365 2009/10/06 00:55:26 tgl Exp $
1212
*
1313
* NOTES
1414
* Every node type that can appear in stored rules' parsetrees *must*
@@ -1849,7 +1849,7 @@ _outColumnDef(StringInfo str, ColumnDef *node)
18491849
WRITE_BOOL_FIELD(is_local);
18501850
WRITE_BOOL_FIELD(is_not_null);
18511851
WRITE_NODE_FIELD(raw_default);
1852-
WRITE_STRING_FIELD(cooked_default);
1852+
WRITE_NODE_FIELD(cooked_default);
18531853
WRITE_NODE_FIELD(constraints);
18541854
}
18551855

src/backend/parser/parse_utilcmd.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
2020
* Portions Copyright (c) 1994, Regents of the University of California
2121
*
22-
* $PostgreSQL: pgsql/src/backend/parser/parse_utilcmd.c,v 2.25 2009/07/30 02:45:37 tgl Exp $
22+
* $PostgreSQL: pgsql/src/backend/parser/parse_utilcmd.c,v 2.26 2009/10/06 00:55:26 tgl Exp $
2323
*
2424
*-------------------------------------------------------------------------
2525
*/
@@ -644,7 +644,7 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
644644
*/
645645
if (attribute->atthasdef && including_defaults)
646646
{
647-
char *this_default = NULL;
647+
Node *this_default = NULL;
648648
AttrDefault *attrdef;
649649
int i;
650650

@@ -655,7 +655,7 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
655655
{
656656
if (attrdef[i].adnum == parent_attno)
657657
{
658-
this_default = attrdef[i].adbin;
658+
this_default = stringToNode(attrdef[i].adbin);
659659
break;
660660
}
661661
}
@@ -666,7 +666,7 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt,
666666
* but it can't; so default is ready to apply to child.
667667
*/
668668

669-
def->cooked_default = pstrdup(this_default);
669+
def->cooked_default = this_default;
670670
}
671671
}
672672

src/include/nodes/parsenodes.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
1414
* Portions Copyright (c) 1994, Regents of the University of California
1515
*
16-
* $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.403 2009/10/05 19:24:48 tgl Exp $
16+
* $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.404 2009/10/06 00:55:26 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -443,10 +443,9 @@ typedef struct RangeFunction
443443
*
444444
* If the column has a default value, we may have the value expression
445445
* in either "raw" form (an untransformed parse tree) or "cooked" form
446-
* (the nodeToString representation of an executable expression tree),
447-
* depending on how this ColumnDef node was created (by parsing, or by
448-
* inheritance from an existing relation). We should never have both
449-
* in the same node!
446+
* (a post-parse-analysis, executable expression tree), depending on
447+
* how this ColumnDef node was created (by parsing, or by inheritance
448+
* from an existing relation). We should never have both in the same node!
450449
*
451450
* The constraints list may contain a CONSTR_DEFAULT item in a raw
452451
* parsetree produced by gram.y, but transformCreateStmt will remove
@@ -462,7 +461,7 @@ typedef struct ColumnDef
462461
bool is_local; /* column has local (non-inherited) def'n */
463462
bool is_not_null; /* NOT NULL constraint specified? */
464463
Node *raw_default; /* default value (untransformed parse tree) */
465-
char *cooked_default; /* nodeToString representation */
464+
Node *cooked_default; /* default value (transformed expr tree) */
466465
List *constraints; /* other constraints on column */
467466
} ColumnDef;
468467

src/test/regress/expected/inherit.out

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,21 @@ order by 1,2;
571571
bar2 | 4 | 4
572572
(8 rows)
573573

574+
/* Test multiple inheritance of column defaults */
575+
CREATE TABLE firstparent (tomorrow date default now()::date + 1);
576+
CREATE TABLE secondparent (tomorrow date default now() :: date + 1);
577+
CREATE TABLE jointchild () INHERITS (firstparent, secondparent); -- ok
578+
NOTICE: merging multiple inherited definitions of column "tomorrow"
579+
CREATE TABLE thirdparent (tomorrow date default now()::date - 1);
580+
CREATE TABLE otherchild () INHERITS (firstparent, thirdparent); -- not ok
581+
NOTICE: merging multiple inherited definitions of column "tomorrow"
582+
ERROR: column "tomorrow" inherits conflicting default values
583+
HINT: To resolve the conflict, specify a default explicitly.
584+
CREATE TABLE otherchild (tomorrow date default now())
585+
INHERITS (firstparent, thirdparent); -- ok, child resolves ambiguous default
586+
NOTICE: merging multiple inherited definitions of column "tomorrow"
587+
NOTICE: merging column "tomorrow" with inherited definition
588+
DROP TABLE firstparent, secondparent, jointchild, thirdparent, otherchild;
574589
/* Test inheritance of structure (LIKE) */
575590
CREATE TABLE inhx (xx text DEFAULT 'text');
576591
/*

src/test/regress/sql/inherit.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ update bar set f2 = f2 + 100 where f1 in (select f1 from foo);
121121
SELECT relname, bar.* FROM bar, pg_class where bar.tableoid = pg_class.oid
122122
order by 1,2;
123123

124+
/* Test multiple inheritance of column defaults */
125+
126+
CREATE TABLE firstparent (tomorrow date default now()::date + 1);
127+
CREATE TABLE secondparent (tomorrow date default now() :: date + 1);
128+
CREATE TABLE jointchild () INHERITS (firstparent, secondparent); -- ok
129+
CREATE TABLE thirdparent (tomorrow date default now()::date - 1);
130+
CREATE TABLE otherchild () INHERITS (firstparent, thirdparent); -- not ok
131+
CREATE TABLE otherchild (tomorrow date default now())
132+
INHERITS (firstparent, thirdparent); -- ok, child resolves ambiguous default
133+
134+
DROP TABLE firstparent, secondparent, jointchild, thirdparent, otherchild;
124135

125136
/* Test inheritance of structure (LIKE) */
126137
CREATE TABLE inhx (xx text DEFAULT 'text');

0 commit comments

Comments
 (0)