Skip to content

Commit 1e2f96f

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 884aec4 commit 1e2f96f

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
@@ -1806,10 +1806,27 @@ deparseNullTest(NullTest *node, deparse_expr_cxt *context)
18061806

18071807
appendStringInfoChar(buf, '(');
18081808
deparseExpr(node->arg, context);
1809-
if (node->nulltesttype == IS_NULL)
1810-
appendStringInfoString(buf, " IS NULL)");
1809+
1810+
/*
1811+
* For scalar inputs, we prefer to print as IS [NOT] NULL, which is
1812+
* shorter and traditional. If it's a rowtype input but we're applying a
1813+
* scalar test, must print IS [NOT] DISTINCT FROM NULL to be semantically
1814+
* correct.
1815+
*/
1816+
if (node->argisrow || !type_is_rowtype(exprType((Node *) node->arg)))
1817+
{
1818+
if (node->nulltesttype == IS_NULL)
1819+
appendStringInfoString(buf, " IS NULL)");
1820+
else
1821+
appendStringInfoString(buf, " IS NOT NULL)");
1822+
}
18111823
else
1812-
appendStringInfoString(buf, " IS NOT NULL)");
1824+
{
1825+
if (node->nulltesttype == IS_NULL)
1826+
appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL)");
1827+
else
1828+
appendStringInfoString(buf, " IS DISTINCT FROM NULL)");
1829+
}
18131830
}
18141831

18151832
/*

src/backend/optimizer/util/plancat.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,13 @@ get_relation_constraints(PlannerInfo *root,
10901090
att->attcollation,
10911091
0);
10921092
ntest->nulltesttype = IS_NOT_NULL;
1093-
ntest->argisrow = type_is_rowtype(att->atttypid);
1093+
1094+
/*
1095+
* argisrow=false is correct even for a composite column,
1096+
* because attnotnull does not represent a SQL-spec IS NOT
1097+
* NULL test in such a case, just IS DISTINCT FROM NULL.
1098+
*/
1099+
ntest->argisrow = false;
10941100
ntest->location = -1;
10951101
result = lappend(result, ntest);
10961102
}

src/backend/utils/adt/ruleutils.c

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7890,17 +7890,43 @@ get_rule_expr(Node *node, deparse_context *context,
78907890
if (!PRETTY_PAREN(context))
78917891
appendStringInfoChar(buf, '(');
78927892
get_rule_expr_paren((Node *) ntest->arg, context, true, node);
7893-
switch (ntest->nulltesttype)
7893+
7894+
/*
7895+
* For scalar inputs, we prefer to print as IS [NOT] NULL,
7896+
* which is shorter and traditional. If it's a rowtype input
7897+
* but we're applying a scalar test, must print IS [NOT]
7898+
* DISTINCT FROM NULL to be semantically correct.
7899+
*/
7900+
if (ntest->argisrow ||
7901+
!type_is_rowtype(exprType((Node *) ntest->arg)))
78947902
{
7895-
case IS_NULL:
7896-
appendStringInfoString(buf, " IS NULL");
7897-
break;
7898-
case IS_NOT_NULL:
7899-
appendStringInfoString(buf, " IS NOT NULL");
7900-
break;
7901-
default:
7902-
elog(ERROR, "unrecognized nulltesttype: %d",
7903-
(int) ntest->nulltesttype);
7903+
switch (ntest->nulltesttype)
7904+
{
7905+
case IS_NULL:
7906+
appendStringInfoString(buf, " IS NULL");
7907+
break;
7908+
case IS_NOT_NULL:
7909+
appendStringInfoString(buf, " IS NOT NULL");
7910+
break;
7911+
default:
7912+
elog(ERROR, "unrecognized nulltesttype: %d",
7913+
(int) ntest->nulltesttype);
7914+
}
7915+
}
7916+
else
7917+
{
7918+
switch (ntest->nulltesttype)
7919+
{
7920+
case IS_NULL:
7921+
appendStringInfoString(buf, " IS NOT DISTINCT FROM NULL");
7922+
break;
7923+
case IS_NOT_NULL:
7924+
appendStringInfoString(buf, " IS DISTINCT FROM NULL");
7925+
break;
7926+
default:
7927+
elog(ERROR, "unrecognized nulltesttype: %d",
7928+
(int) ntest->nulltesttype);
7929+
}
79047930
}
79057931
if (!PRETTY_PAREN(context))
79067932
appendStringInfoChar(buf, ')');

src/include/nodes/primnodes.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,8 +1076,16 @@ typedef struct XmlExpr
10761076
* NullTest represents the operation of testing a value for NULLness.
10771077
* The appropriate test is performed and returned as a boolean Datum.
10781078
*
1079-
* NOTE: the semantics of this for rowtype inputs are noticeably different
1080-
* from the scalar case. We provide an "argisrow" flag to reflect that.
1079+
* When argisrow is false, this simply represents a test for the null value.
1080+
*
1081+
* When argisrow is true, the input expression must yield a rowtype, and
1082+
* the node implements "row IS [NOT] NULL" per the SQL standard. This
1083+
* includes checking individual fields for NULLness when the row datum
1084+
* itself isn't NULL.
1085+
*
1086+
* NOTE: the combination of a rowtype input and argisrow==false does NOT
1087+
* correspond to the SQL notation "row IS [NOT] NULL"; instead, this case
1088+
* represents the SQL notation "row IS [NOT] DISTINCT FROM NULL".
10811089
* ----------------
10821090
*/
10831091

@@ -1091,7 +1099,7 @@ typedef struct NullTest
10911099
Expr xpr;
10921100
Expr *arg; /* input expression */
10931101
NullTestType nulltesttype; /* IS NULL, IS NOT NULL */
1094-
bool argisrow; /* T if input is of a composite type */
1102+
bool argisrow; /* T to perform field-by-field null checks */
10951103
int location; /* token location, or -1 if unknown */
10961104
} NullTest;
10971105

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)