Re: BUG #8150: NULL emements lost when casting result of unnest() - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #8150: NULL emements lost when casting result of unnest()
Date
Msg-id 16834.1368285224@sss.pgh.pa.us
Whole thread Raw
In response to BUG #8150: NULL emements lost when casting result of unnest()  (brandstetter@falter.at)
Responses Re: BUG #8150: NULL emements lost when casting result of unnest()  ("Erik Rijkers" <er@xs4all.nl>)
List pgsql-bugs
brandstetter@falter.at writes:
> SELECT unnest('{1,NULL,4}'::int[])::int8;
>  i
> ---
>  1
>  4

Hm ... this seems to be a very old oversight in ExecMakeFunctionResult:
when it's dealing with a set-valued function argument, if the function
is strict and the particular input value is NULL, it sets the isDone
flag to ExprEndResult, ie, empty-set result.  I think this is the right
thing if the current function returns set; but for a non-set-returning
function, what we ought to get is a scalar NULL result not an empty set.
Various other code paths including ExecMakeTableFunctionResult appear to
get this right.

The attached patch fixes it.

This is another case where I'm not too sure if we ought to back-patch.
The current behavior is clearly wrong, but perhaps some application
out there will be unhappy if we change it in back branches?

            regards, tom lane

diff --git a/src/backend/executor/execQual.c b/src/backend/executor/execQual.c
index 4ea0cbadadbc14614d5e8182ec1da0ded631ee0f..c86a8456424df20e304f55c6de2628fa4f89db1f 100644
*** a/src/backend/executor/execQual.c
--- b/src/backend/executor/execQual.c
*************** restart:
*** 1801,1812 ****
                  pgstat_end_function_usage(&fcusage,
                                          rsinfo.isDone != ExprMultipleResult);
              }
!             else
              {
                  result = (Datum) 0;
                  *isNull = true;
                  *isDone = ExprEndResult;
              }

              /* Which protocol does function want to use? */
              if (rsinfo.returnMode == SFRM_ValuePerCall)
--- 1801,1820 ----
                  pgstat_end_function_usage(&fcusage,
                                          rsinfo.isDone != ExprMultipleResult);
              }
!             else if (fcache->func.fn_retset)
              {
+                 /* for a strict SRF, result is an empty set */
                  result = (Datum) 0;
                  *isNull = true;
                  *isDone = ExprEndResult;
              }
+             else
+             {
+                 /* for a strict non-SRF, result is a NULL */
+                 result = (Datum) 0;
+                 *isNull = true;
+                 *isDone = ExprSingleResult;
+             }

              /* Which protocol does function want to use? */
              if (rsinfo.returnMode == SFRM_ValuePerCall)

pgsql-bugs by date:

Previous
From: brandstetter@falter.at
Date:
Subject: BUG #8150: NULL emements lost when casting result of unnest()
Next
From: "Erik Rijkers"
Date:
Subject: Re: BUG #8150: NULL emements lost when casting result of unnest()