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

Document ordering by an alias #3828

Merged
merged 1 commit into from
Feb 2, 2022
Merged

Conversation

nathanl
Copy link
Contributor

@nathanl nathanl commented Feb 1, 2022

No description provided.

@josevalim
Copy link
Member

Wait, did we add this feature?! :D

@josevalim
Copy link
Member

Ok, so I double checked and this feature does not work. It works for subqueries exclusively but it means we shouldn't document it. Of course we could make it more general purpose and have this case work but we would need to address that first. :)

@nathanl
Copy link
Contributor Author

nathanl commented Feb 2, 2022

I'm confused. 😄 I have a working demo at https://github.com/nathanl/people_query_demo

The relevant code is:

today = Date.utc_today()

base_query =
  from(person in Person,
    select: %{
      name: person.name,
      lifespan:
        fragment(
          # If person is alive, calculate lifespan until today
          "COALESCE(?, ?) - ? AS lifespan",
          person.death,
          ^today,
          person.birth
        )
    }
  )

base_query
|> order_by([{:asc, fragment("lifespan")}])
|> People.Repo.all()
|> IO.inspect(label: "ascending by lifespan ")

base_query
|> order_by([{:desc, fragment("lifespan")}])
|> People.Repo.all()
|> IO.inspect(label: "descending by lifespan ")

which logs:

[debug] QUERY OK source="people" db=1.0ms decode=0.6ms queue=31.4ms idle=0.0ms
SELECT p0."name", COALESCE(p0."death", $1) - p0."birth" AS lifespan FROM "people" AS p0 ORDER BY lifespan [~D[2022-02-02]]
ascending by lifespan : [
  %{lifespan: 763, name: "C"},
  %{lifespan: 25780, name: "B"},
  %{lifespan: 27545, name: "A"}
]
descending by lifespan : [
  %{lifespan: 27545, name: "A"},
  %{lifespan: 25780, name: "B"},
  %{lifespan: 763, name: "C"}
]
[debug] QUERY OK source="people" db=0.9ms queue=1.2ms idle=18.7ms
SELECT p0."name", COALESCE(p0."death", $1) - p0."birth" AS lifespan FROM "people" AS p0 ORDER BY lifespan DESC [~D[2022-02-02]]

Were you saying that ⬆️ is not supported, or am I misunderstanding?

@josevalim
Copy link
Member

Oh, I am so sorry. I thought the name was coming from the map key! But it is coming from the fragment. Doh!

@josevalim josevalim merged commit c149d96 into elixir-ecto:master Feb 2, 2022
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@nathanl
Copy link
Contributor Author

nathanl commented Feb 2, 2022

Hmmm. If the example confused you, that's not good 😬 I'll do a follow-up PR to make it clearer.

@nathanl nathanl deleted the order_by_alias branch February 2, 2022 16:36
@nathanl nathanl mentioned this pull request Feb 2, 2022
@josevalim
Copy link
Member

@nathanl it confused me because I know how the internals work, it is definitely on me, but clarifications are always welcome!

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.

2 participants