Skip to content
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

Support subqueries in order_by, group_by, distinct, and windows #4417

Merged
merged 13 commits into from
May 26, 2024

Conversation

zachdaniel
Copy link
Contributor

@zachdaniel zachdaniel commented May 16, 2024

So this seems to work without too much change required, but I do have one main misgiving: This requires a modification to Ecto.QueryExpr, which AFAIK is used in other places that won't be anticipate having subqueries in them.

I've got a test that shows this working as-is with ecto_sql: elixir-ecto/ecto_sql#607

@josevalim
Copy link
Member

So this seems to work without too much change required, but I do have one main misgiving: This requires a modification to Ecto.QueryExpr, which AFAIK is used in other places that won't be anticipate having subqueries in them.

Let's create a new struct called ByExpr and use the opportunity to support subqueries in both group by and order by?

@zachdaniel
Copy link
Contributor Author

Happy to do that, only concern with doing that will be anywhere that was explicitly matching on QueryExpr, but we can suss those out as necessary. Let me give it a try and see if anything breaks :)

lib/ecto/query/planner.ex Outdated Show resolved Hide resolved
@zachdaniel
Copy link
Contributor Author

zachdaniel commented May 16, 2024

Alright, made some adjustments. There are three outstanding issues:

  1. now this does require changes in ecto_sql. I'm working on those changes. They will have to be updated in tandem for this to work.
  2. I might need some guidance on what to do with the subqueries/aliases present when building windows. I can't say for sure, but it seems like it might just need more updates in ecto_sql.
  3. will need to add tests for subqueries in group_by and (when we figure it out) windows.

If we're good to move forward with this, then I will flesh out the tests both here and in ecto_sql.

@zachdaniel zachdaniel changed the title support subqueries in order by Support subqueries in order_by or group_by May 16, 2024
@zachdaniel
Copy link
Contributor Author

zachdaniel commented May 20, 2024

Working on wrapping this up today. I'm ending up doing windows support because windows uses the same code that group_by uses, so adding subquery support to that naturally flows on to adding it for windows (w/ some extra bits required to make that work).

The big thing I'm realizing based on your comment about testing non-dynamic cases is that I definitely haven't handled the compile time builder side of things. I'm going to try to do that as well, but I'm not sure if I'll be able to pull it off w/o some tips. I'll follow up and we can see how far I get 👍

@zachdaniel zachdaniel changed the title Support subqueries in order_by or group_by Support subqueries in order_by, group_by, distinct, and windows May 20, 2024
@zachdaniel
Copy link
Contributor Author

Still have some equivalent tests to write in ecto_sql. I'm curious if the non-dynamic/dynamic split is important to test in ecto_sql considering that it's tested here? Regardless, still need to test each query building scenario.

@zachdaniel zachdaniel force-pushed the subqueries-in-order-by branch 2 times, most recently from 494cb6c to e15b707 Compare May 20, 2024 13:24
@zachdaniel
Copy link
Contributor Author

Just commenting to point out that this should be ready to review. I can't see (for some reason) how or why the integration tests failed, but I'm assuming that it is because there are required changes in ecto_sql for this to work. Can we ship them simultaneously to address that? Do I need to find a way to make them compatible?

@greg-rychlewski
Copy link
Member

@zachdaniel Normally we have the PR in ecto_sql point here, then if the integration tests pass there we know we are good and leave this one alone.

@zachdaniel
Copy link
Contributor Author

@greg-rychlewski like I'd update the mix.exs to use this branch as a dependency?

@greg-rychlewski
Copy link
Member

yeah exactly

@zachdaniel
Copy link
Contributor Author

Done :) its building now.

lib/ecto/query.ex Outdated Show resolved Hide resolved
zachdaniel and others added 9 commits May 25, 2024 16:31
Co-authored-by: José Valim <jose.valim@gmail.com>
Co-authored-by: José Valim <jose.valim@gmail.com>
Co-authored-by: José Valim <jose.valim@gmail.com>
Co-authored-by: José Valim <jose.valim@gmail.com>
Co-authored-by: José Valim <jose.valim@gmail.com>
Co-authored-by: José Valim <jose.valim@gmail.com>
Co-authored-by: José Valim <jose.valim@gmail.com>
Co-authored-by: José Valim <jose.valim@gmail.com>
Co-authored-by: José Valim <jose.valim@gmail.com>
Copy link
Member

@josevalim josevalim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me. Any other comments @greg-rychlewski? :)

@greg-rychlewski
Copy link
Member

looks good to me :)

@josevalim josevalim merged commit bfcf86f into elixir-ecto:master May 26, 2024
3 of 6 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants