simplify query builder functions
Improve generated SQL and tests