Skip to content

aggregate clause after append clause #3184

Closed

Description

@YuxiLing

What happened?

PRQL compiler version:0.9.2

This issue is similar to an existing one, #2680. But I'm not sure if the root causes of those two issues are the same. So I open a new one.

I found that the aggregate clause also affects the translation of the append clause.

In the following example, assuming tb1 and tb2 have the same columns, we append tb2 to tb1, and get the minimum c0. But in the SQL code, it selects the target column before the union operation. Then the union operation fails.

error message from the database: "each UNION query must have the same number of columns"

PRQL input

from tb1
append tb2
aggregate {new_col1 = min c0}

SQL output

WITH table_0 AS (
  SELECT
    c0
  FROM
    tb1
  UNION
  ALL
  SELECT
    *
  FROM
    tb2
)
SELECT
  MIN(c0) AS new_col1
FROM
  table_0

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

Expected SQL output

WITH table_0 AS (
  SELECT
    c0
  FROM
    tb1
  UNION
  ALL
  SELECT
    c0
  FROM
    tb2
)
SELECT
  MIN(c0) AS new_col1
FROM
  table_0

MVCE confirmation

  • Minimal example
  • New issue

Anything else?

No response

Activity

  1. added
    bug
    on Jul 31, 2023
  2. max-sixty commented on Jul 31, 2023

    @max-sixty
    Member

    Yes. I have spent less time on the append-like table operations, so I'll defer to others.

    I note that writing it out more explicitly works:

    let x = (
      from tb2
      select c0
    )
    
    from tb1
    select c0
    append x
    WITH x AS (
      SELECT
        c0
      FROM
        tb2
    )
    SELECT
      c0
    FROM
      tb1
    UNION
    ALL
    SELECT
      *
    FROM
      x
    
    -- Generated by PRQL compiler version:0.9.2 (https://prql-lang.org)

    Though we have quite poor error messages (which also don't show in the playground) if anything is forgotten:

    let x = (
      from tb2
      select c0
    )
    
    from tb1
    # select c0
    append x
    Error: Unknown name
    
  3. richb-hanover commented on Feb 4, 2024

    @richb-hanover
    Contributor

    I think this can be closed: compiler version 0.11.1 gives exactly this SQL output

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