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

order_by with distinct generates wrong query #318

Closed
achempion opened this issue Apr 15, 2021 · 3 comments
Closed

order_by with distinct generates wrong query #318

achempion opened this issue Apr 15, 2021 · 3 comments

Comments

@achempion
Copy link

Environment

  • Elixir version (elixir -v):
    elixir:1.11.2-alpine
  • Database and version (PostgreSQL 9.4, MongoDB 3.2, etc.):
    postgres:13.1-alpine
  • Ecto version (mix deps):
    3.5.5
  • Database adapter and version (mix deps):
    postgres 0.15.8
  • Operating system:
    linux alpine

Sample 1

Current behavior

from(i in Import, distinct: i.id, order_by: [desc: i.id]) |> Repo.all

generates

SELECT DISTINCT ON (i0."id") i0."id",  i0."inserted_at", i0."updated_at" FROM "imports" AS i0 ORDER BY i0."id", i0."id" DESC

Expected behavior

would generate

SELECT DISTINCT ON (i0."id") i0."id",  i0."inserted_at", i0."updated_at" FROM "imports" AS i0 ORDER BY i0."id" DESC

Sample 2

Current behavior

from(i in Import, distinct: [desc: i.id]) |> Repo.all

generates

SELECT DISTINCT ON (i0."id") i0."id", i0."inserted_at", i0."updated_at" FROM "imports" AS i0

Expected behavior

would generate

SELECT DISTINCT ON (i0."id") i0."id", i0."inserted_at", i0."updated_at" FROM "imports" AS i0 ORDER BY i0."id" DESC

Sample 3

Current behavior

from(i in Import, distinct: [desc: i.id], order_by: [desc: i.id]) |> Repo.all

generates

SELECT DISTINCT ON (i0."id") i0."id", i0."inserted_at", i0."updated_at" FROM "imports" AS i0 ORDER BY i0."id" DESC, i0."id" DESC

Expected behavior

would generate

SELECT DISTINCT ON (i0."id") i0."id", i0."inserted_at", i0."updated_at" FROM "imports" AS i0 ORDER BY i0."id" DESC

would generate section contains the same query in every example

@josevalim
Copy link
Member

Sample 1 and Sample 2 behaviours are documented - and therefore I would not change it.

Sample 3 is the correct way to go about things, if you want to enforce both order by and distinct - but we need to remove the duplicate.

@achempion
Copy link
Author

achempion commented Apr 15, 2021

It seems non obvious to me that on Sample 2 we ignore distinct sorting option if order_by option is empty.

@dorian-marchal
Copy link

dorian-marchal commented Jul 29, 2021

The distinct version of the sort order seems to have been added to avoid repeating columns in both distinct and order_by (cf. #35 (comment)).

But we now have to repeat both the columns and the sort orders, which is unexpected.
What do you think?

Maybe this should be allowed?

Current behavior

from(i in Import, distinct: [desc: i.id], order_by: []) |> Repo.all

generates

SELECT DISTINCT ON (i0."id") i0."id",  i0."inserted_at", i0."updated_at" FROM "imports" AS i0

Expected behavior

would generate

SELECT DISTINCT ON (i0."id") i0."id",  i0."inserted_at", i0."updated_at" FROM "imports" AS i0 ORDER BY i0."id" DESC

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

No branches or pull requests

3 participants