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

DISTINCT ON does not ensure columns exist #4432

Open
2 tasks done
syko opened this issue Apr 30, 2024 · 0 comments
Open
2 tasks done

DISTINCT ON does not ensure columns exist #4432

syko opened this issue Apr 30, 2024 · 0 comments
Labels
bug Invalid compiler output or panic

Comments

@syko
Copy link

syko commented Apr 30, 2024

What happened?

It's hard to tell whether it's a problem with DISTINCT ON or some other mechanic a but a certain combination of steps causes an issue where the columns for a DISTINCT ON and ORDER BY clause do not exist:

  1. group by something
  2. take something, which produces a new CTE
  3. have a second take 1, which produces a DISTINCT ON
  4. have a select in the end which forces specific columns in the produced initial SELECT (no SELECT *)

PRQL input

prql target:sql.postgres

from src
group {grouped_field} (
  sort {sort_1}
  take 2..3
  sort {sort_2}
  take 1
)
select {
  foo
}

SQL output

WITH table_0 AS (
  SELECT
    foo,
    ROW_NUMBER() OVER (
      PARTITION BY grouped_field
      ORDER BY
        sort_1
    ) AS _expr_0
  FROM
    src
)
SELECT
  DISTINCT ON (grouped_field) foo -- MISSING grouped_field
FROM
  table_0
WHERE
  _expr_0 BETWEEN 2 AND 3
ORDER BY
  grouped_field, -- MISSING
  sort_2 -- MISSING

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

Expected SQL output

WITH table_0 AS (
  SELECT
    foo,
    grouped_field,
    sort_2,
    ROW_NUMBER() OVER (
      PARTITION BY grouped_field
      ORDER BY
        sort_1
    ) AS _expr_1
  FROM
    src
)
SELECT
  DISTINCT ON (grouped_field) foo
FROM
  table_0
WHERE
  _expr_0 BETWEEN 2 AND 3
ORDER BY
  grouped_field,
  sort_2

MVCE confirmation

  • Minimal example
  • New issue

Anything else?

No response

@syko syko added the bug Invalid compiler output or panic label Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Invalid compiler output or panic
Projects
None yet
Development

No branches or pull requests

1 participant