The SQL
select sum(sum(a)) from myt1 group by a;
This note focuses only on sum(sum(a))
and it's about how postgres rejects the sql above.
Notes
sum(sum(a))
| | |
| | \_ inner most argument, Var node
| \_ function call
\_ function call
Function call is handled by function transformFuncCall
. Here's overview how tranformFuncCall
works:
- transform arguments first
- if it's an aggregate function, transform aggregate call.
- check_agg_arguments
For sum(sum(a))
, the recursive way is like this:
FuncCall
FuncCall
AggCall
check_agg_arguments
AggCall
check_agg_arguments
Here's the detail,
sum(sum(a))
is a FuncCall, handle its arguementssum(a)
.sum(a)
is a FuncCall, handle its argumentsa
.- check the arguments
a
, is's a simpleVar
node, that's fine. sum(a)
is an AggCall, check_agg_arguments ofsum(a)
. AVar
node, that's fine.sum(sum(a))
is an AggCall, check_agg_arguments ofsum(sum(a))
, which issum(a)
. Sincesum(a)
is an AggCall, postgres rejects this sql.