Hi Tomas,

Thanks for looking at this, and for cross-checking v4 against your
test suite.

> The one thing this v4 fix does not handle is RETURNING with
> null-rejecting domains, which is violating strictness (as it turns
> NULL into an ERROR).

You're right, and it's worse than a missed case: v4 even had a test
asserting that this gets inlined.  My reasoning was that with NULL /
UNKNOWN / ERROR behaviors the result is "either NULL or an error", but
of course an error is not NULL -- the STRICT function returns NULL
without evaluating its body, while the inlined expression raises.  With
a NOT NULL domain, both the default NULL ON ERROR ("could not coerce ON
ERROR expression (NULL) to the RETURNING type") and ERROR ON ERROR fail
for a NULL context item, for both the inlining and the outer-join
pull-up paths.

Attached v5 adopts your check as-is:

    if (jsexpr->use_json_coercion &&
        get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN)
        return true;

so the rule is now: a JsonExpr is non-strict if it coerces its result
to a domain via json_populate_type(), if it has PASSING arguments, or
if either ON EMPTY / ON ERROR is anything other than NULL, UNKNOWN or
ERROR; otherwise its inputs are checked as usual.  Like yours, it
doesn't look at DomainHasConstraints(), which also keeps it independent
of constraints added after planning.

> Expressions that stop being inlined in a STRICT SQL function are for
> example: [...]

Agreed, the list is longer than "narrow".  For a back-patched fix I'd
still prefer the simple rule: it only looks at node fields, and it
errs on the side of not inlining, which seems the right trade-off
given that (as you say) STRICT SQL wrappers around JSON queries are
probably not that common.

Regards,
Zhenglong

Attachment: v5-0001-Fix-planner-s-strictness-check-for-JsonExpr.patch
Description: Binary data

Reply via email to