You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Grouping by column derived from switch function generates invalid SQL #1564
# 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
# 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 (
SELECTAVG(milliseconds) OVER () AS avg_length,
milliseconds
FROM
tracks
)
SELECT
avg_length,
COUNT(*)
FROM
table_1
GROUP BY
avg_length
Activity
max-sixty commentedon Jan 18, 2023
Thanks a lot @mihkelambre , indeed a bug...
Here's a slightly smaller repro
Interestingly if there's a function rather than a
switchthen it does use the variable name — hereavg_length.snth commentedon Jan 18, 2023
@max-sixty Your first example fails for a different reason:
which seems accurate as
lengthhasn't been defined in that example.The problem seems to be that with the
switchit doesn't recognise that it needs to generate thetable_1CTE for the GROUP BY.I'm curious why in the second example it includes
millisecondsas a column in the CTE when it's not needed? @aljazerzen ?max-sixty commentedon Jan 19, 2023
I don't get this error... Do you get that in the playground?
snth commentedon Jan 19, 2023
Yes. Might be in the output.arrow tab. On mobile rn.
aljazerzen commentedon Jan 19, 2023
I like these type of bugs, where we already have a framework for solving them.
If the issue with
lengthpersist, feel free to reopen.snth commentedon Jan 19, 2023
The following is a minimal example which has the same semantics as the first example and still works in output.arrow: