}
/*
- * Build a list of the acceptable GROUP BY expressions for use by
- * substitute_grouped_columns().
+ * Build a list of the acceptable GROUP BY expressions to save in the
+ * RTE_GROUP RTE, and for use by substitute_grouped_columns().
*
* We get the TLE, not just the expr, because GROUPING wants to know the
* sortgroupref.
groupClauses = lappend(groupClauses, expr);
}
+ /*
+ * If there are any acceptable GROUP BY expressions, build an RTE and
+ * nsitem for the result of the grouping step. (It's important to do this
+ * before flattening join alias vars in groupClauses, because the RTE
+ * should preserve any alias vars that were in the input.)
+ */
+ if (groupClauses)
+ {
+ pstate->p_grouping_nsitem =
+ addRangeTableEntryForGroup(pstate, groupClauses);
+
+ /* Set qry->rtable again in case it was previously NIL */
+ qry->rtable = pstate->p_rtable;
+ /* Mark the Query as having RTE_GROUP RTE */
+ qry->hasGroupRTE = true;
+ }
+
/*
* If there are join alias vars involved, we have to flatten them to the
* underlying vars, so that aliased and unaliased vars will be correctly
}
}
- /*
- * If there are any acceptable GROUP BY expressions, build an RTE and
- * nsitem for the result of the grouping step.
- */
- if (groupClauses)
- {
- pstate->p_grouping_nsitem =
- addRangeTableEntryForGroup(pstate, groupClauses);
-
- /* Set qry->rtable again in case it was previously NIL */
- qry->rtable = pstate->p_rtable;
- /* Mark the Query as having RTE_GROUP RTE */
- qry->hasGroupRTE = true;
- }
-
/*
* Replace grouped variables in the targetlist and HAVING clause with Vars
* that reference the RTE_GROUP RTE. Emit an error message if we find any
----+-------
(0 rows)
+-- check that we preserve join alias in GROUP BY expressions
+create temp view v1 as
+select f1::int from t1 left join t2 using (f1) group by f1;
+select pg_get_viewdef('v1'::regclass);
+ pg_get_viewdef
+-------------------------------
+ SELECT (f1)::integer AS f1 +
+ FROM (t1 +
+ LEFT JOIN t2 USING (f1))+
+ GROUP BY f1;
+(1 row)
+
+drop view v1;
drop table t1, t2;
--
-- Test planner's selection of pathkeys for ORDER BY aggregates