Code from Radiobox/gorp:query-plans