8000 CREATE DOMAIN ... DEFAULT NULL failed because gram.y special-cases DE… · commandprompt/postgres@cbe8af8 · GitHub
[go: up one dir, main page]

Skip to content

Commit cbe8af8

Browse files
committed
CREATE DOMAIN ... DEFAULT NULL failed because gram.y special-cases DEFAULT
NULL and DefineDomain didn't. Bug goes all the way back to original coding of domains. Per bug #3396 from Sergey Burladyan.
1 parent 72cbfa4 commit cbe8af8

File tree

1 file changed

+41
-30
lines changed

1 file changed

+41
-30
lines changed

src/backend/commands/typecmds.c

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/typecmds.c,v 1.15 2002/09/21 18:39:25 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/typecmds.c,v 1.15.2.1 2007/06/20 18:16:30 tgl Exp $
1212
*
1313
* DESCRIPTION
1414
* The "DefineFoo" routines take the parse tree and pick out the
@@ -397,6 +397,7 @@ DefineDomain(CreateDomainStmt *stmt)
397397
Node *defaultExpr = NULL;
398398
char *defaultValue = NULL;
399399
char *defaultValueBin = NULL;
400+
bool saw_default = false;
400401
bool typNotNull = false;
401402
bool nullDefined = false;
402403
Oid basetypelem;
@@ -493,42 +494,52 @@ DefineDomain(CreateDomainStmt *stmt)
493494
foreach(listptr, schema)
494495
{
495496
Constraint *colDef = lfirst(listptr);
496-
ParseState *pstate;
497497

498498
switch (colDef->contype)
499499
{
500+
case CONSTR_DEFAULT:
500501
/*
501502
* The inherited default value may be overridden by the
502-
* user with the DEFAULT <expr> statement.
503-
*
504-
* We have to search the entire constraint tree returned as
505-
* we don't want to cook or fiddle too much.
503+
* user with the DEFAULT <expr> clause ... but only once.
506504
*/
507-
case CONSTR_DEFAULT:
508-
if (defaultExpr)
505+
if (saw_default)
509506
elog(ERROR, "CREATE DOMAIN has multiple DEFAULT expressions");
510-
/* Create a dummy ParseState for transformExpr */
511-
pstate = make_parsestate(NULL);
512-
513-
/*
514-
* Cook the colDef->raw_expr into an expression. Note:
515-
* Name is strictly for error message
516-
*/
517-
defaultExpr = cookDefault(pstate, colDef->raw_expr,
518-
basetypeoid,
519-
stmt->typename->typmod,
520-
domainName);
521-
522-
/*
523-
* Expression must be stored as a nodeToString result, but
524-
* we also require a valid textual representation (mainly
525-
* to make life easier for pg_dump).
526-
*/
527-
defaultValue = deparse_expression(defaultExpr,
528-
deparse_context_for(domainName,
529-
InvalidOid),
530-
false, false);
531-
defaultValueBin = nodeToString(defaultExpr);
507+
saw_default = true;
508+
509+
if (colDef->raw_expr)
510+
{
511+
ParseState *pstate;
512+
513+
/* Create a dummy ParseState for transformExpr */
514+
pstate = make_parsestate(NULL);
515+
516+
/*
517+
* Cook the colDef->raw_expr into an expression.
518+
* Note: name is strictly for error message
519+
*/
520+
defaultExpr = cookDefault(pstate, colDef->raw_expr,
521+
basetypeoid,
522+
stmt->typename->typmod,
523+
domainName);
524+
525+
/*
526+
* Expression must be stored as a nodeToString result, but
527+
* we also require a valid textual representation (mainly
528+
* to make life easier for pg_dump).
529+
*/
530+
defaultValue =
531+
deparse_expression(defaultExpr,
532+
deparse_context_for(domainName,
533+
InvalidOid),
534+
false, false);
535+
defaultValueBin = nodeToString(defaultExpr);
536+
}
537+
else
538+
{
539+
/* DEFAULT NULL is same as not having a default */
540+
defaultValue = NULL;
541+
defaultValueBin = NULL;
542+
}
532543
break;
533544

534545
/*

0 commit comments

Comments
 (0)
0