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

Repo.aggregate: wrap query into a subquery if it has combinations #3703

Conversation

smartepsh
Copy link
Contributor

Environment

  • Elixir version (elixir -v): 1.12.2
  • Database and version (PostgreSQL 9.4, MongoDB 3.2, etc.): 13.3
  • Ecto version (mix deps): master
  • Database adapter and version (mix deps): postgrex 0.15.10
  • Operating system: mac

Current behavior

Repo.aggregate/3 may throw an exception if query has combinations.

  supplier_query = Supplier |> select([s], s.city)
  query = Customer |> select([c], c.city) |> union(^supplier_query)

  Repo.aggregate(query, :count, :city)

  # The query will be something like 
  # Ecto.Query<from c in Customer, union: (from s in Supplier, select: s.city), select: count(c.city)>

The code above will count the Customer first , then union the count with citys in supplier_query, not count the union result.

Expected behavior

Aggregate the union result.

I wrap the query into a subquery if it has combinations .

@smartepsh smartepsh force-pushed the smartepsh/wrap-in-subquery-when-aggregate-has-combinations branch from a0b89f1 to 4d6da2b Compare August 6, 2021 06:14
@josevalim josevalim merged commit cc75abd into elixir-ecto:master Aug 6, 2021
@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.

3 participants