Skip to content

Aggregate doesn't respect union's names #3579

Done

Description

@avikam

What happened?

Group aggregation after append doesn't preserve column alias (nor order) which leads to broken aggregations

PRQL input

prql target:sql.snowflake

let shape = p -> (
  p
  select {
    interaction_day = s"DATE_TRUNC('day', ts)",
    user_id = s"substr(user, length('u:') + 1)",
  }
)

from table1
shape
append (from  table2 | shape)
group { user_id } (
  aggregate {
    daily_interaction = count interaction_day
  }
)

SQL output

WITH table_0 AS (
  SELECT
    DATE_TRUNC('day', ts) AS interaction_day,
    substr(user, length('u:') + 1) AS user_id
  FROM
    table2
),
table_1 AS (
  SELECT
    substr(user, length('u:') + 1) AS user_id,
    DATE_TRUNC('day', ts) AS _expr_0
  FROM
    table1
  UNION
  ALL
  SELECT
    *
  FROM
    table_0
)
SELECT
  user_id,
  COUNT(*) AS daily_interaction
FROM
  table_1
GROUP BY
  user_id

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

Expected SQL output

WITH table_0 AS (
  SELECT
    DATE_TRUNC('day', ts) AS interaction_day,
    substr(user, length('u:') + 1) AS user_id
  FROM
    table2
),
table_1 AS (
  SELECT
    substr(user, length('u:') + 1) AS user_id,
    DATE_TRUNC('day', ts) AS interaction_day
  FROM
    table1
  UNION
  ALL
  SELECT
    *
  FROM
    table_0
)
SELECT
  user_id,
  COUNT(*) AS daily_interaction
FROM
  table_1
GROUP BY
  user_id

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

MVCE confirmation

  • Minimal example
  • New issue

Anything else?

No response

Activity

  1. added
    bug
    on Sep 27, 2023
  2. max-sixty commented on Sep 27, 2023

    @max-sixty
    Member

    Thanks @avikam .

    (For others looking — the error is DATE_TRUNC('day', ts) AS _expr_0)

  3. snth commented on Sep 28, 2023

    @snth
    Member

    The naming of the column as _expr_0 is one error. The other is that the order of the columns in the first part of table_1 before the UNION (as was pointed out in the original report).

    I believe the Expected SQL Output should actually be:

    WITH table_0 AS (
      SELECT
        DATE_TRUNC('day', ts) AS interaction_day,
        substr(user, length('u:') + 1) AS user_id
      FROM
        table2
    ),
    table_1 AS (
      SELECT
        DATE_TRUNC('day', ts) AS interaction_day,
        substr(user, length('u:') + 1) AS user_id
      FROM
        table1
      UNION
      ALL
      SELECT
        *
      FROM
        table_0
    )
    SELECT
      user_id,
      COUNT(*) AS daily_interaction
    FROM
      table_1
    GROUP BY
      user_id
    
    -- Generated by PRQL compiler version:0.9.5 (https://prql-lang.org)
  4. avikam commented on Sep 30, 2023

    @avikam
    Author

    yeah it might be two separate issues.

    the order issue can be reproduced with:

    from tbl1
    select { y, x }
    append (
        from tbl2
        select { y, x }
    )
    group { x } (
      aggregate {
        tot = count y
      }
    )
    

    which currently transpiles as

    WITH table_0 AS (
      SELECT
        y,
        x
      FROM
        tbl2
    ),
    table_1 AS (
      SELECT
        x,
        y
      FROM
        tbl1
      UNION
      ALL
      SELECT
        *
      FROM
        table_0
    )
    SELECT
      x,
      COUNT(*) AS tot
    FROM
      table_1
    GROUP BY
      x
    
  5. avikam commented on Oct 2, 2023

    @avikam
    Author

    looking more into it, i think the ordering issue can be reproduced even without an aggregation. Consider this example:

    from tbl1
    select { x, y }
    append (from tbl2 | select { x, y } )
    select { y, x}
    

    This pipeline sequence is not split, and the last select dominates the projection of tbl1

    select y, x from tbl1 union all select * from table_0 -- table_0 is a correct CTE

    the reason i think it is related to the first issue, is that in that case there is a Union followed by Select, Aggregate which does introduce a split. Then anchor_split appends a projection transform, with columns that their order is determined by the Aggregate's partition and compute arguments. When these are not in the same order as the first select, we get a similar behavior.

    all of that should be taken with a grain of salt, as this all is very speculative.

  6. prql-bot commented on Oct 7, 2026

    @prql-bot
    Collaborator

    On current main (0.13.14-263-gc945ca81) both reported problems produce valid SQL.

    • The original query compiles to a UNION ALL whose arms each select only substr(user, length('u:') + 1) AS "user_id". interaction_day is pruned because count compiles to COUNT(*), so the arms line up and the SELECT * arm is gone.
    • The column-order repro (select { y, x } on both sides, then group { x }) selects x, y in the same order in both arms. With max y in place of count y, the outer query reads MAX(y), which is correct.

    One cosmetic difference is left. When the s-string column is actually used (e.g. adding m = max interaction_day), the first arm still aliases it "_expr_0" while the second uses "interaction_day". UNION ALL takes names from the first arm, and the outer query reads MAX("_expr_0"), so the result is correct, just less readable.

    Output with m = max interaction_day added
    WITH
    "table_1" AS (
      SELECT
        substr(user, length('u:') + 1) AS "user_id",
        DATE_TRUNC('day', ts) AS "_expr_0"
      FROM
        "table1"
      UNION ALL
      SELECT
        substr(user, length('u:') + 1) AS "user_id",
        DATE_TRUNC('day', ts) AS "interaction_day"
      FROM
        "table2"
    )
    SELECT
      "user_id",
      COUNT(*) AS "daily_interaction",
      MAX("_expr_0") AS "m"
    FROM
      "table_1"
    GROUP BY
      "user_id"

    This may be ready to close, unless the _expr_0 naming should be tracked here.

  7. max-sixty commented on Nov 28, 2026

    @max-sixty
    Member

    Closing as completed: on main, the union's arms keep their columns aligned, and an aggregation after append reads the right names.

    This was written by Claude Code on behalf of max-sixty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions