prql icon indicating copy to clipboard operation
prql copied to clipboard

Aggregating on group key got messed with multiple CTEs

Open echou opened this issue 1 year ago • 5 comments
trafficstars

What happened?

The following prql fails on 0.11.4 but works on 0.10.1.

PRQL input

from foo
derive b = a + 1
filter true          # ensure a CTE

derive c = b + 1
filter true          # ensure another CTE

group {c} (
  aggregate {
    n = (average c)
  }
)

SQL output

WITH table_1 AS (
  SELECT
    c AS _expr_0,
    a + 1 AS _expr_1
  FROM
    foo
),
table_0 AS (
  SELECT
    _expr_1 + 1 AS c,
    _expr_0
  FROM
    table_1
  WHERE
    true
)
SELECT
  c,
  AVG(_expr_0) AS n
FROM
  table_0
WHERE
  true
GROUP BY
  c

Expected SQL output

WITH table_1 AS (
  SELECT
    a + 1 AS _expr_1
  FROM
    foo
),
table_0 AS (
  SELECT
    _expr_1 + 1 AS c,
    _expr_0
  FROM
    table_1
  WHERE
    true
)
SELECT
  c,
  AVG(_expr_0) AS n
FROM
  table_0
WHERE
  true
GROUP BY
  c

MVCE confirmation

  • [X] Minimal example
  • [ ] New issue

Anything else?

No response

echou avatar Feb 26 '24 08:02 echou

Expected sql is wrong. just ignore it.

echou avatar Feb 26 '24 08:02 echou

The error is: "c" is a derived column but appears as a real column in the first CTE

echou avatar Feb 26 '24 08:02 echou

Thanks for the issue. This does look wrong.

We're rethinking how columns are resolved, so this may get pushed into that effort.

max-sixty avatar Mar 04 '24 19:03 max-sixty

Any plan to solve this bug?

echou avatar Sep 10 '24 01:09 echou

Any plan to solve this bug?

We're rethinking how columns are resolved, so this may get pushed into that effort.

The effort is taking a bit longer than hoped; the bounds are fairly wide given it's no one's day job...

max-sixty avatar Sep 10 '24 06:09 max-sixty