WIP - begin crazy sql rewrite