2008/12/20 Tom Lane <t...@sss.pgh.pa.us>: > "Hitoshi Harada" <umi.tan...@gmail.com> writes: >> Here's the patch, including latest function default args. Regards, > > The comments added to planner.c contain various claims that setrefs.c > will fix up window function references, but I see no code related to > that in setrefs.c. Please explain. >
The Window node handles only expressions that contain corresponding window functions. Since more than one Window nodes may be put in a Plan, the window functions in the upper node are not evaluated in the lower node but tlist should not forget it exists in the lower. For examples, SQL: SELECT row_number() OVER (ORDER BY salary) AS r1, row_number() OVER (ORDER BY depname) AS r2 FROM empsalary; The final Plan: -Window (upper) pull r1 (Var) eval r2 (WindowFuncExpr) -Sort by depname -Window (lower) eval r1 (WindowFunctionExpr) r2 as NULL (Const) -Sort by salary -Scan empsalary And it is possible for window_tlist() to change upper's r1 to Var but delegating it to set_upper_references() seems right to do.If we simply delegate all the nodes to set_upper_reference(), r2 would also be evaluated in lower node, so we put null const in the lower and put actual evaluation in the upper. So window_preprocess and window_tlist do things needed for set_upper_reference to fix up all the expressions correctly, that's why no code is added to setrefs.c. This part is hard to describe in english (or even in my japanese) so if you are still lost, I'll add more explanation. Regards, -- Hitoshi Harada -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers