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

Issue on query compilation when filtering on combined aggregates #5130

Open
2 tasks done
matiastoro opened this issue Jan 29, 2025 · 0 comments
Open
2 tasks done

Issue on query compilation when filtering on combined aggregates #5130

matiastoro opened this issue Jan 29, 2025 · 0 comments
Labels
bug Invalid compiler output or panic

Comments

@matiastoro
Copy link

matiastoro commented Jan 29, 2025

What happened?

PRQL compiler version:0.13.3

This query:

from invoices
group billing_city (
  aggregate{
    sum_c = (sum(customer_id)) + (sum(customer_id))
  }
)

generates sql query:

SELECT
  billing_city,
  COALESCE(SUM(customer_id), 0) + COALESCE(SUM(customer_id), 0) AS sum_c
FROM
  invoices
GROUP BY
  billing_city

but after adding a filter at the end:

from invoices
group billing_city (
  aggregate{
    sum_c = (sum(customer_id)) + (sum(customer_id))
  }
)
filter sum_c>0

then the resulting query does not compile, as the group by should be done in table_0 inner query, right?

WITH table_0 AS (
  SELECT
    billing_city,
    COALESCE(SUM(customer_id), 0) AS _expr_0,
    COALESCE(SUM(customer_id), 0) AS _expr_1
  FROM
    invoices
)
SELECT
  billing_city,
  _expr_0 + _expr_1 AS sum_c
FROM
  table_0
GROUP BY
  billing_city
HAVING
  _expr_0 + _expr_1 > 0

I noticed that this only happens when you aggregate by combining two different aggregation functions.

PRQL input

from invoices
group billing_city (
  aggregate{
    sum_c = (sum(customer_id)) + (sum(customer_id))
  }
)
filter sum_c>0

SQL output

WITH table_0 AS (
  SELECT
    billing_city,
    COALESCE(SUM(customer_id), 0) AS _expr_0,
    COALESCE(SUM(customer_id), 0) AS _expr_1
  FROM
    invoices
)
SELECT
  billing_city,
  _expr_0 + _expr_1 AS sum_c
FROM
  table_0
GROUP BY
  billing_city
HAVING
  _expr_0 + _expr_1 > 0

Expected SQL output

SELECT
  billing_city,
  COALESCE(SUM(customer_id), 0) + COALESCE(SUM(customer_id), 0),
FROM
  invoices
GROUP BY
  billing_city
HAVING
  COALESCE(SUM(customer_id), 0) + COALESCE(SUM(customer_id), 0) > 0

MVCE confirmation

  • Minimal example
  • New issue

Anything else?

No response

@matiastoro matiastoro added the bug Invalid compiler output or panic label Jan 29, 2025
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