On Tue, Jul 29, 2025 at 9:47 AM David Rowley <dgrowle...@gmail.com> wrote: > You should be pushing the qual to the lowest level that it's valid to > evaluate it at. We do this already for HAVING quals where those will > effectively be "transferred" to the WHERE clause when it's valid to do > so. I'd expect the same for QUALIFY.
Yeah, we should have the same kind of optimization for a QUALIFY clause as we do for HAVING - pushing it down to WHERE when possible. One condition for doing this is that the QUALIFY clause does not reference any columns that are not present in the window PARTITION BY clauses. There may be other conditions under which we can have other optimizations, such as pushing down a QUALIFY clause as run conditions. I think we should at a minimum inherit all existing optimizations for window functions used in subqueries; otherwise, it will be difficult to convince others to accept this patch. Thanks Richard