Fix assignment to array of domain over composite, redux.
authorTom Lane
Sat, 15 Apr 2023 16:01:39 +0000 (12:01 -0400)
committerTom Lane
Sat, 15 Apr 2023 16:01:39 +0000 (12:01 -0400)
Commit 3e310d837 taught isAssignmentIndirectionExpr() to look through
CoerceToDomain nodes.  That's not sufficient, because since commit
04fe805a1 it's been possible for the planner to simplify
CoerceToDomain to RelabelType when the domain has no constraints
to enforce.  So we need to look through RelabelType too.

Per bug #17897 from Alexander Lakhin.  Although 3e310d837 was
back-patched to v11, it seems sufficient to apply this change
to v12 and later, since 04fe805a1 came in in v12.

Dmitry Dolgov

Discussion: https://postgr.es/m/17897-4216c546c3874044@postgresql.org

src/backend/executor/execExpr.c
src/test/regress/expected/domain.out
src/test/regress/sql/domain.sql

index c32dbd6e63fdf0d21a9476c96a3b911ca6768058..8b64ba8aefef2d3fd72e9c45465748bd955c0984 100644 (file)
@@ -2854,8 +2854,8 @@ ExecInitSubscriptingRef(ExprEvalStep *scratch, SubscriptingRef *sbsref,
  * trees in which each level of assignment has its own CaseTestExpr, and the
  * recursive structure appears within the newvals or refassgnexpr field.
  * There is an exception, though: if the array is an array-of-domain, we will
- * have a CoerceToDomain as the refassgnexpr, and we need to be able to look
- * through that.
+ * have a CoerceToDomain or RelabelType as the refassgnexpr, and we need to
+ * be able to look through that.
  */
 static bool
 isAssignmentIndirectionExpr(Expr *expr)
@@ -2882,6 +2882,12 @@ isAssignmentIndirectionExpr(Expr *expr)
 
        return isAssignmentIndirectionExpr(cd->arg);
    }
+   else if (IsA(expr, RelabelType))
+   {
+       RelabelType *r = (RelabelType *) expr;
+
+       return isAssignmentIndirectionExpr(r->arg);
+   }
    return false;
 }
 
index 93dc31ff1ca1f692d6c11231933ec8ac0690a3ad..3aa20f8b8f730c5b3ced6410f41a20156be65e5f 100644 (file)
@@ -562,6 +562,15 @@ table dcomptable;
  {"(1,5)"}
 (1 row)
 
+-- if there's no constraints, a different code path is taken:
+alter domain dcomptype drop constraint dcomptype_check;
+update dcomptable set f1[1].cf1 = -1;  -- now ok
+table dcomptable;
+     f1     
+------------
+ {"(-1,5)"}
+(1 row)
+
 drop table dcomptable;
 drop type comptype cascade;
 NOTICE:  drop cascades to type dcomptype
index d19e2bdf0c99b9db0ecd99ed51c3b2cba5ab1ac4..3e875018ea3556cb3f1bdeecb326b0cfd1dee4cb 100644 (file)
@@ -289,6 +289,10 @@ table dcomptable;
 update dcomptable set f1[1].cf1 = -1;  -- fail
 update dcomptable set f1[1].cf1 = 1;
 table dcomptable;
+-- if there's no constraints, a different code path is taken:
+alter domain dcomptype drop constraint dcomptype_check;
+update dcomptable set f1[1].cf1 = -1;  -- now ok
+table dcomptable;
 
 drop table dcomptable;
 drop type comptype cascade;