Unexpected result when composing aggregations with `case` expressions

オープン
#5,175 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
rust, sql

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Rust
スター
10.9k
フォーク
261
平均マージ
3日 20時間
マージ済み PR(30日)
85

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

PRQL/prql のほかの issue

PRQL/prql の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。