8000 Fix old oversight in const-simplification of COALESCE() expressions. · danielcode/postgres@faea083 · GitHub
[go: up one dir, main page]

Skip to content

Commit faea083

Browse files
committed
Fix old oversight in const-simplification of COALESCE() expressions.
Once we have found a non-null constant argument, there is no need to examine additional arguments of the COALESCE. The previous coding got it right only if the constant was in the first argument position; otherwise it tried to simplify following arguments too, leading to unexpected behavior like this: regression=# select coalesce(f1, 42, 1/0) from int4_tbl; ERROR: division by zero It's a minor corner case, but a bug is a bug, so back-patch all the way.
1 parent f5cf3ce commit faea083

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/backend/optimizer/util/clauses.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1774,14 +1774,18 @@ eval_const_expressions_mutator(Node *node,
17741774
/*
17751775
* We can remove null constants from the list. For a non-null
17761776
* constant, if it has not been preceded by any other
1777-
* non-null-constant expressions then that is the result.
1777+
* non-null-constant expressions then it is the result. Otherwise,
1778+
* it's the next argument, but we can drop following arguments
1779+
* since they will never be reached.
17781780
*/
17791781
if (IsA(e, Const))
17801782
{
17811783
if (((Const *) e)->constisnull)
17821784
continue; /* drop null constant */
17831785
if (newargs == NIL)
17841786
return e; /* first expr */
1787+
newargs = lappend(newargs, e);
1788+
break;
17851789
}
17861790
newargs = lappend(newargs, e);
17871791
}

0 commit comments

Comments
 (0)
0