improved typecasting for aggregate db functions