prql icon indicating copy to clipboard operation
prql copied to clipboard

Sorting disappears with empty group

Open syko opened this issue 11 months ago • 2 comments

What happened?

It seems grouping by nothing is otherwise a valid thing to do, except it makes an inner sort do nothing.

PRQL input

from foo
group {} (
  sort a
  take 1
)

SQL output

SELECT
  *
FROM
  foo
LIMIT
  1

-- Generated by PRQL compiler version:0.13.2 (https://prql-lang.org)

Expected SQL output

SELECT
  *
FROM
  foo
ORDER BY
  a
LIMIT
  1

MVCE confirmation

  • [x] Minimal example
  • [x] New issue

Anything else?

No response

syko avatar Jan 20 '25 15:01 syko

I think this may actually be by design. I recently read through the discussion in #1363 and this comment suggests that using group, even if it is empty, will reset (remove) sorting.

https://github.com/PRQL/prql/issues/1363#issuecomment-1368528636

Were you thinking of a use case where you expected sorting to be preserved through an empty group?

kgutwin avatar Jan 21 '25 14:01 kgutwin

Ok I see part of it might be by design but if the sorting is private only to the group then I'd say the output should be a combination of CTEs where the sorting still affects the take 1 within the grouping but leaves the final sorting of the relation unaffected.

Our use case is that our prql is dynamically generated so grouping by nothing is just an edge case that we have to handle manually, which is not unreasonable, but from the way aggregations and things are seemingly unaffected by an empty group, we got the impression that group {} is basically noop.

syko avatar Jan 24 '25 12:01 syko