Unexpected result when composing aggregations with `case` expressions

Đang mở
#5,175 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 5 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
rust, sql
Lĩnh vực
backend, compilers, databases

Hướng nghiên cứu

Start by reproducing the PRQL input and generated SQL from the issue with the compiler, targeting ClickHouse. Then trace how aggregation expressions containing case are lowered into SQL and why this produces an intermediate CTE; compare the output with the expected SQL. Done when the example generates valid SQL with the aggregation composed in the final SELECT.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug
What happened?

Hi,

I am experiencing a bug where prql generates an invalid SQL because there is a case expression inside a sum. Let me demonstrate.

PRQL input
prql target:sql.clickhouse

from `t`.`table`
group {(group_variable)} (aggregate {
  `new_variable` =  (sum (case [(var | as int) < 10 => 1, true => 0])) * 100 / (count null)
})
SQL output
WITH table_0 AS (
  SELECT
    group_variable,
    COALESCE(
      SUM(
        CASE
          WHEN CAST(var AS int) < 10 THEN 1
          ELSE 0
        END
      ),
      0
    ) AS _expr_0,
    var
  FROM
    t.`table`
)
SELECT
  group_variable,
  (_expr_0 * 100 / COUNT(*)) AS new_variable
FROM
  table_0
GROUP BY
  group_variable

-- Generated by PRQL compiler version:0.13.3 (https://prql-lang.org)
Expected SQL output
SELECT
  group_variable,
  (COALESCE(SUM(
        CASE
          WHEN CAST(var AS int) < 10 THEN 1
          ELSE 0
        END
   ), 0) * 100 / COUNT(*)) AS new_variable
FROM
  t.`table`
GROUP BY
  group_variable
MVCE confirmation
  • Minimal example
  • New issue
Anything else?

For some reason the system breaks the expression into a intermediary CTE for the sum when the case expr exists.

Ngôn ngữ chính
Rust
Star
10.9k
Fork
261
Merge trung bình
4 ngày 7 giờ
Pull request đã merge (30 ngày)
77

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của PRQL/prql

Tất cả issue của PRQL/prql

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.