Re: pgsql: Teach expr_is_nonnullable() to handle more expression types
Re: pgsql: Teach expr_is_nonnullable() to handle more expression types
On Wed, Dec 24, 2025 at 4:02 AM Richard Guo wrote: > Teach expr_is_nonnullable() to handle more expression types Due to this commit, expr_is_nonnullable() claims that "IS DISTINCT FROM never returns NULL", but execExprInterp.c's implementation for EEOP_DISTINCT disagrees; when neither argument is NULL, it returns null if the underlying operator does. Two Claude-written test cases illustrating the problem are attached. -- Robert Haas Databricks
Re: pgsql: Teach expr_is_nonnullable() to handle more expression types
От:
Richard Guo <guofenglinux@gmail.com>
Дата:
On Tue, Oct 6, 2026 at 11:50 PM Robert Haas wrote: > Due to this commit, expr_is_nonnullable() claims that "IS DISTINCT > FROM never returns NULL", but execExprInterp.c's implementation for > EEOP_DISTINCT disagrees; when neither argument is NULL, it returns > null if the underlying operator does. Two Claude-written test cases > illustrating the problem are attached. Right, thanks for the report. This needs an equality operator that returns NULL for two non-null inputs, which I think is rare in practice, but nothing forbids it, so I agree we should fix it. I think we can just remove the T_DistinctExpr case from expr_is_nonnullable(). An alternative is that we keep it, but only when the operator is a btree equality operator, as such an operator must not return NULL for non-null inputs. This keeps the optimization for nearly all real-world cases. What do you think? - Richard
Re: pgsql: Teach expr_is_nonnullable() to handle more expression types
От:
Robert Haas <robertmhaas@gmail.com>
Дата:
On Tue, Oct 6, 2026 at 9:30 PM Richard Guo wrote: > I think we can just remove the T_DistinctExpr case from > expr_is_nonnullable(). An alternative is that we keep it, but only > when the operator is a btree equality operator, as such an operator > must not return NULL for non-null inputs. This keeps the optimization > for nearly all real-world cases. I'm not sure. expr_is_nonnullable() doesn't look like an appealing place to do being catalog lookups. I see that var_is_nonnullable() does do catalog lookups when source == NOTNULL_SOURCE_CATALOG, which strikes me as not great (and why does it test has_subclass() before rte->relkind?!). But I'd be inclined to use a conditional rule only if we can base it on information that we already have cached in some planner data structure, and not if we'd have to look it up in the catalog. -- Robert Haas Databricks
Re: pgsql: Teach expr_is_nonnullable() to handle more expression types
От:
Richard Guo <guofenglinux@gmail.com>
Дата:
On Thu, Oct 8, 2026 at 10:49 PM Robert Haas wrote: > But I'd be inclined to use a conditional rule only if > we can base it on information that we already have cached in some > planner data structure, and not if we'd have to look it up in the > catalog. We don't have that cached, so I've pushed a fix that just removes the case. It also moves the relkind test ahead of has_subclass(). - Richard