rails/arel — Issue #101: Correct implementation of non-joiry query expressions
Author: nyarly
Created: 2012-02-17 23:51:04+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.