nkallen/arel — Issue #10: non-join query expressions incorrectly implemented
Author: nyarly
Created: 2012-02-17 23:48:29+00:00
State: open

As it stands, UNION, INTERSECT and EXCEPT are implemented and Binary nodes on two ASTs - but the SQL spec and (at least Postgres's) implementations treat them more like a sub-select. Check out:

http://www.postgresql.org/docs/9.1/static/sql-select.html

and

http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt (page 195)

Specifically, the only correct way to limit a union is like:

SELECT * FROM table {} UNION SELECT * FROM table LIMIT 100

LIMIT isn't valid (although it's accepted) at {} and it should apply to the overall query, not the second tree.

Regardless, SelectManager#{union,intersect,except} pass in two ASTs to the Binary nodes, so limits get removed anyway.