diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index bbc2c7e71f4..e7aafc89700 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -2484,6 +2484,10 @@ create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path) Oid *ordOperators; Oid *ordCollations; ListCell *lc; + List *defineVariableList = NIL; + List *filteredDefineClause = NIL; + RPRPattern *compiledPattern = NULL; + /* * Choice of tlist here is motivated by the fact that WindowAgg will be @@ -2535,50 +2539,45 @@ create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path) } /* Build RPR pattern and filter defineClause */ + if (wc->rpPattern) { - List *defineVariableList = NIL; - List *filteredDefineClause = NIL; - RPRPattern *compiledPattern = NULL; - - if (wc->rpPattern) - { - List *patternVars; + List *patternVars; - /* - * Filter defineClause to include only variables used in PATTERN. - * This eliminates unnecessary DEFINE evaluations at runtime. - */ - patternVars = collectPatternVariables(wc->rpPattern); - filteredDefineClause = filterDefineClause(wc->defineClause, - patternVars, - &defineVariableList); - - compiledPattern = buildRPRPattern(wc->rpPattern, - defineVariableList, - wc->rpSkipTo, - wc->frameOptions); - } - - /* And finally we can make the WindowAgg node */ - plan = make_windowagg(tlist, - wc, - partNumCols, - partColIdx, - partOperators, - partCollations, - ordNumCols, - ordColIdx, - ordOperators, - ordCollations, - best_path->runCondition, - wc->rpSkipTo, - compiledPattern, - filteredDefineClause, - best_path->qual, - best_path->topwindow, - subplan); + /* + * Filter defineClause to include only variables used in PATTERN. This + * eliminates unnecessary DEFINE evaluations at runtime. + */ + patternVars = collectPatternVariables(wc->rpPattern); + filteredDefineClause = filterDefineClause(wc->defineClause, + patternVars, + &defineVariableList); + + /* Compile and optimize RPR patterns */ + compiledPattern = buildRPRPattern(wc->rpPattern, + defineVariableList, + wc->rpSkipTo, + wc->frameOptions); } + /* And finally we can make the WindowAgg node */ + plan = make_windowagg(tlist, + wc, + partNumCols, + partColIdx, + partOperators, + partCollations, + ordNumCols, + ordColIdx, + ordOperators, + ordCollations, + best_path->runCondition, + wc->rpSkipTo, + compiledPattern, + filteredDefineClause, + best_path->qual, + best_path->topwindow, + subplan); + copy_generic_path_info(&plan->plan, (Path *) best_path); return plan;