!3791 修复更新视图字段为default错误的问题

Merge pull request !3791 from chenxiaobin/fixUpdatableViewDefault
This commit is contained in:
opengauss_bot
2023-07-20 02:21:30 +00:00
committed by Gitee
5 changed files with 62 additions and 3 deletions

View File

@ -791,7 +791,7 @@ static List* rewriteTargetListIU(List* targetList, CmdType commandType, Relation
ereport(DEBUG2, (errmodule(MOD_PARSER), errcode(ERRCODE_LOG),
errmsg("default column \"%s\" is effectively NULL, and hence omitted.",
NameStr(att_tup->attname))));
} else {
} else if (target_relation->rd_rel->relkind != RELKIND_VIEW) {
new_expr = (Node*)makeConst(att_tup->atttypid,
-1,
att_tup->attcollation,
@ -936,7 +936,7 @@ static void rewriteTargetListMutilUpdate(Query* parsetree, List* rtable, List* r
new_tle = NULL;
} else if (applyDefault) {
Node* new_expr = build_column_default(target_relation, attrno, true);
if (new_expr == NULL) {
if (new_expr == NULL && target_relation->rd_rel->relkind != RELKIND_VIEW) {
new_expr = (Node*)makeConst(att_tup->atttypid,
-1,
att_tup->attcollation,
@ -948,7 +948,9 @@ static void rewriteTargetListMutilUpdate(Query* parsetree, List* rtable, List* r
new_expr = coerce_to_domain(
new_expr, InvalidOid, -1, att_tup->atttypid, COERCE_IMPLICIT_CAST, -1, false, false);
}
new_tle = makeTargetEntry((Expr*)new_expr, attrno, pstrdup(NameStr(att_tup->attname)), false);
if (new_expr != NULL) {
new_tle = makeTargetEntry((Expr*)new_expr, attrno, pstrdup(NameStr(att_tup->attname)), false);
}
new_tle->rtindex = result_relation;
}

View File

@ -1326,6 +1326,29 @@ select * from t_t_mutil_t3;
1 | 6
(2 rows)
alter table t_t_mutil_t1 alter column col2 set default 999;
update t_t_mutil_t1 a,multiview2 b,t_t_mutil_t3 c set a.col2 = default, b.col2 = 5,c.col2 = 6 where a.col1 = b.col1 and a.col1 = c.col1;
select * from t_t_mutil_t1;
col1 | col2
------+------
1 | 999
1 | 999
(2 rows)
select * from t_t_mutil_t2;
col1 | col2 | col3
------+------+------
1 | 5 |
1 | 5 |
(2 rows)
select * from t_t_mutil_t3;
col1 | col2
------+------
1 | 6
1 | 6
(2 rows)
-- left join
update multiview1 a left join multiview2 b on a.col1=b.col1 set a.col2=7,b.col2=8;
select * from t_t_mutil_t1;

View File

@ -2472,5 +2472,23 @@ explain (costs off) UPDATE ro_view1 SET a = a + b;
UPDATE ro_view1 SET a = a + b;
ERROR: new row violates WITH CHECK OPTION for view "ro_view1"
DETAIL: Failing row contains (10, 2).
create table default_t (a int, b varchar default 'table default');
insert into default_t values (1, 'dec');
create view default_v as select b, a from default_t;
update default_v set b = default where a = 1;
select * from default_v;
b | a
---------------+---
table default | 1
(1 row)
alter view default_v alter column b set default 'view default';
update default_v set b = default where a = 1;
select * from default_v;
b | a
--------------+---
view default | 1
(1 row)
\c regression
drop database updatable_views_db;

View File

@ -593,6 +593,11 @@ update t_t_mutil_t1 a,multiview2 b,t_t_mutil_t3 c set a.col2 = 4, b.col2 = 5,c.c
select * from t_t_mutil_t1;
select * from t_t_mutil_t2;
select * from t_t_mutil_t3;
alter table t_t_mutil_t1 alter column col2 set default 999;
update t_t_mutil_t1 a,multiview2 b,t_t_mutil_t3 c set a.col2 = default, b.col2 = 5,c.col2 = 6 where a.col1 = b.col1 and a.col1 = c.col1;
select * from t_t_mutil_t1;
select * from t_t_mutil_t2;
select * from t_t_mutil_t3;
-- left join
update multiview1 a left join multiview2 b on a.col1=b.col1 set a.col2=7,b.col2=8;
select * from t_t_mutil_t1;

View File

@ -1264,5 +1264,16 @@ set enable_bitmapscan = off;
explain (costs off) UPDATE ro_view1 SET a = a + b;
UPDATE ro_view1 SET a = a + b;
create table default_t (a int, b varchar default 'table default');
insert into default_t values (1, 'dec');
create view default_v as select b, a from default_t;
update default_v set b = default where a = 1;
select * from default_v;
alter view default_v alter column b set default 'view default';
update default_v set b = default where a = 1;
select * from default_v;
\c regression
drop database updatable_views_db;