Skip to content

Commit 0697143

Browse files
committed
Fix assorted fallout from IS [NOT] NULL patch.
Commits 4452000 et al established semantics for NullTest.argisrow that are a bit different from its initial conception: rather than being merely a cache of whether we've determined the input to have composite type, the flag now has the further meaning that we should apply field-by-field testing as per the standard's definition of IS [NOT] NULL. If argisrow is false and yet the input has composite type, the construct instead has the semantics of IS [NOT] DISTINCT FROM NULL. Update the comments in primnodes.h to clarify this, and fix ruleutils.c and deparse.c to print such cases correctly. In the case of ruleutils.c, this merely results in cosmetic changes in EXPLAIN output, since the case can't currently arise in stored rules. However, it represents a live bug for deparse.c, which would formerly have sent a remote query that had semantics different from the local behavior. (From the user's standpoint, this means that testing a remote nested-composite column for null-ness could have had unexpected recursive behavior much like that fixed in 4452000.) In a related but somewhat independent fix, make plancat.c set argisrow to false in all NullTest expressions constructed to represent "attnotnull" constructs. Since attnotnull is actually enforced as a simple null-value check, this is a more accurate representation of the semantics; we were previously overpromising what it meant for composite columns, which might possibly lead to incorrect planner optimizations. (It seems that what the SQL spec expects a NOT NULL constraint to mean is an IS NOT NULL test, so arguably we are violating the spec and should fix attnotnull to do the other thing. If we ever do, this part should get reverted.) Back-patch, same as the previous commit. Discussion: <10682.1469566308@sss.pgh.pa.us>
1 parent 5220562 commit 0697143

File tree

5 files changed

+77
-20
lines changed

5 files changed

+77
-20
lines changed

contrib/postgres_fdw/deparse.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,10 +1799,27 @@ deparseNullTest(NullTest *node, deparse_expr_cxt *context)
17991799

18001800
appendStringInfoChar(buf, '(');
18011801
deparseExpr(node->arg, context);
1802-
if (node->nulltesttype == IS_NULL)
1803-
appendStringInfoString(buf, " IS NULL)");
1802+
1803+
/*
1804+
* For scalar inputs, we prefer to print as IS [NOT] NULL, which is
1805+
* shorter and traditional. If it's a rowtype input but we're applying a
1806+
* scalar test, must print IS [NOT] DISTINCT FROM NULL to be semantically
1807+
* correct.
1808+
*/
1809+
if (node->argisrow || !type_is_rowtype(exprType((Node *) node->arg)))
1810+
{
1811+
if (node->nulltesttype == IS_NULL)
1812+
appendStringInfoString(buf, " IS NULL)");
1813+
else
1814+
appendStringInfoString(buf, " IS NOT NULL)");
1815+
}
18041816
else
1805-
appendStringInfoString(buf, " IS NOT NULL)");
1817+
{
1818+
if (node->nulltesttype == IS_NULL)
1819+
appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL)");
1820+
else
1821+
appendStringInfoString(buf, " IS DISTINCT FROM NULL)");
1822+
}
18061823
}
18071824

18081825
/*

src/backend/optimizer/util/plancat.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,13 @@ get_relation_constraints(PlannerInfo *root,
719719
att->attcollation,
720720
0);
721721
ntest->nulltesttype = IS_NOT_NULL;
722-
ntest->argisrow = type_is_rowtype(att->atttypid);
722+
723+
/*
724+
* argisrow=false is correct even for a composite column,
725+
* because attnotnull does not represent a SQL-spec IS NOT
726+
* NULL test in such a case, just IS DISTINCT FROM NULL.
727+
*/
728+
ntest->argisrow = false;
723729
result = lappend(result, ntest);
724730
}
725731
}

src/backend/utils/adt/ruleutils.c

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7405,17 +7405,43 @@ get_rule_expr(Node *node, deparse_context *context,
74057405
if (!PRETTY_PAREN(context))
74067406
appendStringInfoChar(buf, '(');
74077407
get_rule_expr_paren((Node *) ntest->arg, context, true, node);
7408-
switch (ntest->nulltesttype)
7408+
7409+
/*
7410+
* For scalar inputs, we prefer to print as IS [NOT] NULL,
7411+
* which is shorter and traditional. If it's a rowtype input
7412+
* but we're applying a scalar test, must print IS [NOT]
7413+
* DISTINCT FROM NULL to be semantically correct.
7414+
*/
7415+
if (ntest->argisrow ||
7416+
!type_is_rowtype(exprType((Node *) ntest->arg)))
74097417
{
7410-
case IS_NULL:
7411-
appendStringInfo(buf, " IS NULL");
7412-
break;
7413-
case IS_NOT_NULL:
7414-
appendStringInfo(buf, " IS NOT NULL");
7415-
break;
7416-
default:
7417-
elog(ERROR, "unrecognized nulltesttype: %d",
7418-
(int) ntest->nulltesttype);
7418+
switch (ntest->nulltesttype)
7419+
{
7420+
case IS_NULL:
7421+
appendStringInfo(buf, " IS NULL");
7422+
break;
7423+
case IS_NOT_NULL:
7424+
appendStringInfo(buf, " IS NOT NULL");
7425+
break;
7426+
default:
7427+
elog(ERROR, "unrecognized nulltesttype: %d",
7428+
(int) ntest->nulltesttype);
7429+
}
7430+
}
7431+
else
7432+
{
7433+
switch (ntest->nulltesttype)
7434+
{
7435+
case IS_NULL:
7436+
appendStringInfo(buf, " IS NOT DISTINCT FROM NULL");
7437+
break;
7438+
case IS_NOT_NULL:
7439+
appendStringInfo(buf, " IS DISTINCT FROM NULL");
7440+
break;
7441+
default:
7442+
elog(ERROR, "unrecognized nulltesttype: %d",
7443+
(int) ntest->nulltesttype);
7444+
}
74197445
}
74207446
if (!PRETTY_PAREN(context))
74217447
appendStringInfoChar(buf, ')');

src/include/nodes/primnodes.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,8 +1007,16 @@ typedef struct XmlExpr
10071007
* NullTest represents the operation of testing a value for NULLness.
10081008
* The appropriate test is performed and returned as a boolean Datum.
10091009
*
1010-
* NOTE: the semantics of this for rowtype inputs are noticeably different
1011-
* from the scalar case. We provide an "argisrow" flag to reflect that.
1010+
* When argisrow is false, this simply represents a test for the null value.
1011+
*
1012+
* When argisrow is true, the input expression must yield a rowtype, and
1013+
* the node implements "row IS [NOT] NULL" per the SQL standard. This
1014+
* includes checking individual fields for NULLness when the row datum
1015+
* itself isn't NULL.
1016+
*
1017+
* NOTE: the combination of a rowtype input and argisrow==false does NOT
1018+
* correspond to the SQL notation "row IS [NOT] NULL"; instead, this case
1019+
* represents the SQL notation "row IS [NOT] DISTINCT FROM NULL".
10121020
* ----------------
10131021
*/
10141022

@@ -1022,7 +1030,7 @@ typedef struct NullTest
10221030
Expr xpr;
10231031
Expr *arg; /* input expression */
10241032
NullTestType nulltesttype; /* IS NULL, IS NOT NULL */
1025-
bool argisrow; /* T if input is of a composite type */
1033+
bool argisrow; /* T to perform field-by-field null checks */
10261034
} NullTest;
10271035

10281036
/*

src/test/regress/expected/rowtypes.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,10 @@ explain (verbose, costs off)
664664
select r, r is null as isnull, r is not null as isnotnull
665665
from (values (1,row(1,2)), (1,row(null,null)), (1,null),
666666
(null,row(1,2)), (null,row(null,null)), (null,null) ) r(a,b);
667-
QUERY PLAN
668-
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
667+
QUERY PLAN
668+
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
669669
Values Scan on "*VALUES*"
670-
Output: ROW("*VALUES*".column1, "*VALUES*".column2), (("*VALUES*".column1 IS NULL) AND ("*VALUES*".column2 IS NULL)), (("*VALUES*".column1 IS NOT NULL) AND ("*VALUES*".column2 IS NOT NULL))
670+
Output: ROW("*VALUES*".column1, "*VALUES*".column2), (("*VALUES*".column1 IS NULL) AND ("*VALUES*".column2 IS NOT DISTINCT FROM NULL)), (("*VALUES*".column1 IS NOT NULL) AND ("*VALUES*".column2 IS DISTINCT FROM NULL))
671671
(2 rows)
672672

673673
select r, r is null as isnull, r is not null as isnotnull

0 commit comments

Comments
 (0)