-
Notifications
You must be signed in to change notification settings - Fork 386
Chainable unions #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chainable unions #118
Conversation
|
I also just got this rocking with ActiveRelation over at jroes/rails@06fec10. If this looks good, I could potentially port the code over to master and make a pull request to rails as well. |
|
I tried to tackle that same problem, along with creating a union method on Relation, I implemented with the same idea. I don't think of (SELECT * FROM table1 LIMIT 10) UNION (SELECT * FROM table2 LIMIT 10) LIMIT 10The previous query would merge top results from table1 first, then add non duplicates top results from table2, then limit the overall results to 10. I rather think of Unions as a toplevel construct, having multiple subqueries, rather that what you made, ie. a select query having multiple subqueries to link to, which prevents the first query from having its own |
Depends on rails/arel#118. The accepted implementation could act as a unions list, or a new top-level object, which would change this patch proposal.
Depends on rails/arel#118. The accepted implementation could act as a unions list, or a new top-level object, which would change this patch proposal.
|
Hi Jonathan Do you know when /if this pull request will be merged? |
|
I think @ofavre has a better handle on this than I do, my vote is to go his direction. |
|
Here is my past work on this issue, before I stumbled upon this pull request. |
|
According to the SQL 2003 standard, we should be able to express sequence of Maybe What if I think I'll go that direction. Any thoughts? especially on the second paragraph? |
|
I wish I was qualified to answer your questions Olivier. In our project I had to work around this issue by creating a wrapper class around multiple scopes. I then have an apply method which just builds up the query using string interpolation and calling to_sql on each scope applying all the intersects to the base scope first and then the unions and then the excludes and calling .find_by_sql. It works for our needs for now. It would however be nice to be able to return an active relation though. My implementation would lead me to believe that it is a basic implementation of you last paragraph. i.e. a wrapper class around multiple selects. Sorry I can't help more. |
|
Failing test: https://gist.github.com/calebthompson/5377038 |
|
Updated @ofavre's awesome changeset for 4-0-stable in this branch in case anybody is interested! @ofavre, @jroes: Do you think it would make sense to pull-request that? It seems to work really well... |
|
Definitely! On Wed, Jan 15, 2014 at 1:34 PM, James Sanders notifications@github.comwrote:
|
|
Due to lack of time, I was actually stuck with an old version of Ruby and could not rebase on up-to-date versions. |
Fixes rails#118 Fixes rails#98 Conflicts: lib/arel/nodes/and.rb lib/arel/nodes/select_statement.rb lib/arel/visitors/mssql.rb lib/arel/visitors/to_sql.rb Conflicts: lib/arel/nodes/and.rb lib/arel/nodes/binary.rb lib/arel/predications.rb lib/arel/select_manager.rb lib/arel/visitors/depth_first.rb lib/arel/visitors/mssql.rb lib/arel/visitors/to_sql.rb test/visitors/test_mysql.rb test/visitors/test_oracle.rb test/visitors/test_to_sql.rb
Fixes rails#118 Fixes rails#98 Conflicts: lib/arel/nodes/and.rb lib/arel/nodes/select_statement.rb lib/arel/visitors/mssql.rb lib/arel/visitors/to_sql.rb Conflicts: lib/arel/nodes/and.rb lib/arel/nodes/binary.rb lib/arel/predications.rb lib/arel/select_manager.rb lib/arel/visitors/depth_first.rb lib/arel/visitors/mssql.rb lib/arel/visitors/to_sql.rb test/visitors/test_mysql.rb test/visitors/test_oracle.rb test/visitors/test_to_sql.rb
I'm working on a solution to the chainable unions problem. See also #98
I ended up making
UnionaUnarynode. I could see a case for it being binary, but the solution seemed simpler this way.I think I'm pretty close here, but I've got a single failing test for
WITH RECURSIVE. I believe this is because it expects that a union will always be enclosed within parentheses, but I don't know if that's necessarily valid.Let me know if I'm going down the right path?