Logic for aggregate functions.