Skip to content

Grouping by column derived from switch function generates invalid SQL #1564

Closed

Description

@mihkelambre

Repoducable in playground.

# from playground
from tracks
select [
  avg_length = average milliseconds,
  length = milliseconds,
  category = switch [
    length > avg_length -> 'long',
    true -> 'short'
  ]
]
group [category] (
  aggregate (
    count
  )
)

Grouping by a column derived from switch function (that uses window functions) generates SQL that with window functions in group by.

SELECT
  CASE
    WHEN milliseconds > AVG(milliseconds) OVER () THEN 'long'
    ELSE 'short'
  END AS category,
  COUNT(*)
FROM
  tracks
GROUP BY
  CASE
    WHEN milliseconds > AVG(milliseconds) OVER () THEN 'long'
    ELSE 'short'
  END

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

Error thrown:

ERROR:  window functions are not allowed in GROUP BY

Activity

  1. added
    bug
    on Jan 18, 2023
  2. max-sixty commented on Jan 18, 2023

    @max-sixty
    Member

    Thanks a lot @mihkelambre , indeed a bug...

    Here's a slightly smaller repro

    # from playground
    from tracks
    select [
      category = switch [
        length > avg_length -> 'long'
      ]
    ]
    group [category] (
      aggregate (count)
    )
    SELECT
      CASE
        WHEN length > avg_length THEN 'long'
        ELSE NULL
      END AS category,
      COUNT(*)
    FROM
      tracks
    GROUP BY
      CASE
        WHEN length > avg_length THEN 'long'
        ELSE NULL
      END

    Interestingly if there's a function rather than a switch then it does use the variable name — here avg_length.

    # from playground
    from tracks
    select avg_length = (average milliseconds)
    group [avg_length] (
      aggregate count
    )
    WITH table_1 AS (
      SELECT
        AVG(milliseconds) OVER () AS avg_length,
        milliseconds
      FROM
        tracks
    )
    SELECT
      avg_length,
      COUNT(*)
    FROM
      table_1
    GROUP BY
      avg_length
  3. snth commented on Jan 18, 2023

    @snth
    Member

    @max-sixty Your first example fails for a different reason:

    Error: Binder Error: Referenced column "length" not found in FROM clause!
    

    which seems accurate as length hasn't been defined in that example.


    The problem seems to be that with the switch it doesn't recognise that it needs to generate the table_1 CTE for the GROUP BY.

    I'm curious why in the second example it includes milliseconds as a column in the CTE when it's not needed? @aljazerzen ?

  4. max-sixty commented on Jan 19, 2023

    @max-sixty
    Member

    @max-sixty Your first example fails for a different reason:

    Error: Binder Error: Referenced column "length" not found in FROM clause!
    

    which seems accurate as length hasn't been defined in that example.

    I don't get this error... Do you get that in the playground?

  5. snth commented on Jan 19, 2023

    @snth
    Member

    Yes. Might be in the output.arrow tab. On mobile rn.

  6. aljazerzen commented on Jan 19, 2023

    @aljazerzen
    Member

    I like these type of bugs, where we already have a framework for solving them.

    If the issue with length persist, feel free to reopen.

  7. snth commented on Jan 19, 2023

    @snth
    Member

    The following is a minimal example which has the same semantics as the first example and still works in output.arrow:

    from tracks
        select category = switch [
            milliseconds > (avg milliseconds) -> 'long'
        ]
    group category (aggregate count)
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