diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/optimizer/prep/preptlist.c | 34 | ||||
-rw-r--r-- | src/backend/parser/parse_coerce.c | 37 | ||||
-rw-r--r-- | src/backend/rewrite/rewriteHandler.c | 42 | ||||
-rw-r--r-- | src/backend/rewrite/rewriteManip.c | 30 | ||||
-rw-r--r-- | src/include/parser/parse_coerce.h | 3 |
5 files changed, 80 insertions, 66 deletions
diff --git a/src/backend/optimizer/prep/preptlist.c b/src/backend/optimizer/prep/preptlist.c index 7c439d1947c..c2a77503d4b 100644 --- a/src/backend/optimizer/prep/preptlist.c +++ b/src/backend/optimizer/prep/preptlist.c @@ -46,7 +46,8 @@ #include "parser/parsetree.h" #include "utils/rel.h" -static List *expand_insert_targetlist(List *tlist, Relation rel); +static List *expand_insert_targetlist(PlannerInfo *root, List *tlist, + Relation rel); /* @@ -102,7 +103,7 @@ preprocess_targetlist(PlannerInfo *root) */ tlist = parse->targetList; if (command_type == CMD_INSERT) - tlist = expand_insert_targetlist(tlist, target_relation); + tlist = expand_insert_targetlist(root, tlist, target_relation); else if (command_type == CMD_UPDATE) root->update_colnos = extract_update_targetlist_colnos(tlist); @@ -148,7 +149,8 @@ preprocess_targetlist(PlannerInfo *root) ListCell *l2; if (action->commandType == CMD_INSERT) - action->targetList = expand_insert_targetlist(action->targetList, + action->targetList = expand_insert_targetlist(root, + action->targetList, target_relation); else if (action->commandType == CMD_UPDATE) action->updateColnos = @@ -376,7 +378,7 @@ extract_update_targetlist_colnos(List *tlist) * but now this code is only applied to INSERT targetlists. */ static List * -expand_insert_targetlist(List *tlist, Relation rel) +expand_insert_targetlist(PlannerInfo *root, List *tlist, Relation rel) { List *new_tlist = NIL; ListCell *tlist_item; @@ -430,26 +432,18 @@ expand_insert_targetlist(List *tlist, Relation rel) * confuse code comparing the finished plan to the target * relation, however. */ - Oid atttype = att_tup->atttypid; - Oid attcollation = att_tup->attcollation; Node *new_expr; if (!att_tup->attisdropped) { - new_expr = (Node *) makeConst(atttype, - -1, - attcollation, - att_tup->attlen, - (Datum) 0, - true, /* isnull */ - att_tup->attbyval); - new_expr = coerce_to_domain(new_expr, - InvalidOid, -1, - atttype, - COERCION_IMPLICIT, - COERCE_IMPLICIT_CAST, - -1, - false); + new_expr = coerce_null_to_domain(att_tup->atttypid, + att_tup->atttypmod, + att_tup->attcollation, + att_tup->attlen, + att_tup->attbyval); + /* Must run expression preprocessing on any non-const nodes */ + if (!IsA(new_expr, Const)) + new_expr = eval_const_expressions(root, new_expr); } else { diff --git a/src/backend/parser/parse_coerce.c b/src/backend/parser/parse_coerce.c index a6f4f2fc28d..4723c960d0e 100644 --- a/src/backend/parser/parse_coerce.c +++ b/src/backend/parser/parse_coerce.c @@ -1264,6 +1264,43 @@ coerce_to_specific_type(ParseState *pstate, Node *node, } /* + * coerce_null_to_domain() + * Build a NULL constant, then wrap it in CoerceToDomain + * if the desired type is a domain type. This allows any + * NOT NULL domain constraint to be enforced at runtime. + */ +Node * +coerce_null_to_domain(Oid typid, int32 typmod, Oid collation, + int typlen, bool typbyval) +{ + Node *result; + Oid baseTypeId; + int32 baseTypeMod = typmod; + + /* + * The constant must appear to have the domain's base type/typmod, else + * coerce_to_domain() will apply a length coercion which is useless. + */ + baseTypeId = getBaseTypeAndTypmod(typid, &baseTypeMod); + result = (Node *) makeConst(baseTypeId, + baseTypeMod, + collation, + typlen, + (Datum) 0, + true, /* isnull */ + typbyval); + if (typid != baseTypeId) + result = coerce_to_domain(result, + baseTypeId, baseTypeMod, + typid, + COERCION_IMPLICIT, + COERCE_IMPLICIT_CAST, + -1, + false); + return result; +} + +/* * parser_coercion_errposition - report coercion error location, if possible * * We prefer to point at the coercion request (CAST, ::, etc) if possible; diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c index b74f2acc327..847edcfa90e 100644 --- a/src/backend/rewrite/rewriteHandler.c +++ b/src/backend/rewrite/rewriteHandler.c @@ -1008,23 +1008,11 @@ rewriteTargetListIU(List *targetList, if (commandType == CMD_INSERT) new_tle = NULL; else - { - new_expr = (Node *) makeConst(att_tup->atttypid, - -1, - att_tup->attcollation, - att_tup->attlen, - (Datum) 0, - true, /* isnull */ - att_tup->attbyval); - /* this is to catch a NOT NULL domain constraint */ - new_expr = coerce_to_domain(new_expr, - InvalidOid, -1, - att_tup->atttypid, - COERCION_IMPLICIT, - COERCE_IMPLICIT_CAST, - -1, - false); - } + new_expr = coerce_null_to_domain(att_tup->atttypid, + att_tup->atttypmod, + att_tup->attcollation, + att_tup->attlen, + att_tup->attbyval); } if (new_expr) @@ -1572,21 +1560,11 @@ rewriteValuesRTE(Query *parsetree, RangeTblEntry *rte, int rti, continue; } - new_expr = (Node *) makeConst(att_tup->atttypid, - -1, - att_tup->attcollation, - att_tup->attlen, - (Datum) 0, - true, /* isnull */ - att_tup->attbyval); - /* this is to catch a NOT NULL domain constraint */ - new_expr = coerce_to_domain(new_expr, - InvalidOid, -1, - att_tup->atttypid, - COERCION_IMPLICIT, - COERCE_IMPLICIT_CAST, - -1, - false); + new_expr = coerce_null_to_domain(att_tup->atttypid, + att_tup->atttypmod, + att_tup->attcollation, + att_tup->attlen, + att_tup->attbyval); } newList = lappend(newList, new_expr); } diff --git a/src/backend/rewrite/rewriteManip.c b/src/backend/rewrite/rewriteManip.c index bca11500e9e..a115b217c91 100644 --- a/src/backend/rewrite/rewriteManip.c +++ b/src/backend/rewrite/rewriteManip.c @@ -22,6 +22,7 @@ #include "parser/parse_relation.h" #include "parser/parsetree.h" #include "rewrite/rewriteManip.h" +#include "utils/lsyscache.h" typedef struct @@ -1802,20 +1803,21 @@ ReplaceVarsFromTargetList_callback(Var *var, return (Node *) var; case REPLACEVARS_SUBSTITUTE_NULL: - - /* - * If Var is of domain type, we should add a CoerceToDomain - * node, in case there is a NOT NULL domain constraint. - */ - return coerce_to_domain((Node *) makeNullConst(var->vartype, - var->vartypmod, - var->varcollid), - InvalidOid, -1, - var->vartype, - COERCION_IMPLICIT, - COERCE_IMPLICIT_CAST, - -1, - false); + { + /* + * If Var is of domain type, we must add a CoerceToDomain + * node, in case there is a NOT NULL domain constraint. + */ + int16 vartyplen; + bool vartypbyval; + + get_typlenbyval(var->vartype, &vartyplen, &vartypbyval); + return coerce_null_to_domain(var->vartype, + var->vartypmod, + var->varcollid, + vartyplen, + vartypbyval); + } } elog(ERROR, "could not find replacement targetlist entry for attno %d", var->varattno); diff --git a/src/include/parser/parse_coerce.h b/src/include/parser/parse_coerce.h index cfded2a8e80..8d775c72c59 100644 --- a/src/include/parser/parse_coerce.h +++ b/src/include/parser/parse_coerce.h @@ -63,6 +63,9 @@ extern Node *coerce_to_specific_type_typmod(ParseState *pstate, Node *node, Oid targetTypeId, int32 targetTypmod, const char *constructName); +extern Node *coerce_null_to_domain(Oid typid, int32 typmod, Oid collation, + int typlen, bool typbyval); + extern int parser_coercion_errposition(ParseState *pstate, int coerce_location, Node *input_expr); |