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
WITH table_0 AS(SELECT
tbl1.*FROM
tbl1
)SELECT
table_0.*FROM
table_0
WHEREAVG(bar)OVER()>3
... which uses window function in WHERE, which is not allowed. The window function should have been applied in first CTE and used only in SELECT in the second CTE.
I know average does not make sense in filter, but there could be lag or rank instead.
How does it even know to turn the average bar into a window function? Is there a rule that "naked" reductions (i.e. not inside an aggregate) are turned into window functions?
Yes there is an implicit rule :D It first started as "function calls in select and derive are window functions", but during implementation I generalized to "all functions that take column as input and are not in aggregate are window functions".
This also applies to filter and other transforms, which I haven't even thought about, but now it does make some sense. We have to make an explicit decision about it. What would you expect would happen here?
One option is to infer the type of the function from typing. If the result is compared to a scalar, then the function must surly also return scalar. This would be harder to implement, though.
About the first CTE above: it's needed because WHERE cannot contain window functions, but only columns from existing table. So we need first CTE for window function and the second for where clause.
I just don't have any intuition as to what I expect it to produce.
from tbl1
filter (average bar)>3
compiles to
from tbl1
filter (average bar) >3
WITH table_1 AS (
SELECT*,
AVG(bar) OVER () AS _expr_0
FROM
tbl1
)
SELECT*FROM
table_1
WHERE
_expr_0 >3
In order to test this I ran the following in Postgres:
with tbl1(bar) as (
values (1), (2), (3), (4), (5)
),
table_1 AS (
SELECT*,
AVG(bar) OVER () AS _expr_0
FROM
tbl1
)
SELECT*FROM
table_1
WHERE
_expr_0 >3
which produces no rows.
To make it easier to see what's going on I changed this to:
from tbl1
derive (average bar)
and then ran
with tbl1(bar) as (
values (1), (2), (3), (4), (5)
)
SELECT*,
AVG(bar) OVER ()
FROM
tbl1
So clearly the frame of the window function is the whole table in each case. I thought the default frame was BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW but it turns out that's only in the presence of an ORDER BY qualifier.
In order to try and get a running average I tried
from tbl1
sort bar
derive (average bar)
which compiles to
SELECT*,
AVG(bar) OVER (
ORDER BY
bar ROWS BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING
)
FROM
tbl1
ORDER BY
bar
which on the same data as above produces the same output.
It's good that this is consistent and the output doesn't change depending on whether the pipeline includes a sort previously or not.
Finally, in order to get a running (expanding) average, you need
from tbl1
window expanding:true (
derive (average bar))
which compiles to
SELECT*,
AVG(bar) OVER (
ROWS BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW
)
FROM
tbl1
These results are of course all perfectly logical and great, I was just a little thrown off by the initial example since it produces either all rows or none, depending on what the global average is.
A slightly modified example which shows how amazingly useful this feature is, is the following:
from tbl1
filter bar >(average bar)
which compiles to
WITH table_1 AS (
SELECT*,
bar,
AVG(bar) OVER () AS _expr_0
FROM
tbl1
)
SELECT*FROM
table_1
WHERE
bar > _expr_0
and returns all rows with values greater than the global average, i.e. for my example dataset
4 3.0000000000000000
5 3.0000000000000000
NOTE: Unfortunately the last example doesn't actually work as is because bar is also included in * so Postgres complains and I had to amend the query to
WITH table_1 AS (
SELECT*,
AVG(bar) OVER () AS _expr_0
FROM
tbl1
)
SELECT*FROM
table_1
WHERE
bar > _expr_0
Activity
snth commentedon Sep 22, 2022
How does it even know to turn the
average barinto a window function? Is there a rule that "naked" reductions (i.e. not inside an aggregate) are turned into window functions?Why does it produce the first CTE in this case?
aljazerzen commentedon Sep 24, 2022
Yes there is an implicit rule :D It first started as "function calls in select and derive are window functions", but during implementation I generalized to "all functions that take column as input and are not in aggregate are window functions".
This also applies to
filterand other transforms, which I haven't even thought about, but now it does make some sense. We have to make an explicit decision about it. What would you expect would happen here?One option is to infer the type of the function from typing. If the result is compared to a scalar, then the function must surly also return scalar. This would be harder to implement, though.
About the first CTE above: it's needed because
WHEREcannot contain window functions, but only columns from existing table. So we need first CTE for window function and the second for where clause.aljazerzen commentedon Dec 1, 2022
Closed by #593
snth commentedon Dec 1, 2022
This is amazing!
I just don't have any intuition as to what I expect it to produce.
compiles to
In order to test this I ran the following in Postgres:
which produces no rows.
To make it easier to see what's going on I changed this to:
and then ran
which produced
So clearly the frame of the window function is the whole table in each case. I thought the default frame was
BETWEEN UNBOUNDED PRECEDING AND CURRENT ROWbut it turns out that's only in the presence of anORDER BYqualifier.In order to try and get a running average I tried
which compiles to
which on the same data as above produces the same output.
It's good that this is consistent and the output doesn't change depending on whether the pipeline includes a sort previously or not.
Finally, in order to get a running (expanding) average, you need
which compiles to
and on my example data produces
These results are of course all perfectly logical and great, I was just a little thrown off by the initial example since it produces either all rows or none, depending on what the global average is.
A slightly modified example which shows how amazingly useful this feature is, is the following:
which compiles to
and returns all rows with values greater than the global average, i.e. for my example dataset
NOTE: Unfortunately the last example doesn't actually work as is because
baris also included in*so Postgres complains and I had to amend the query toaljazerzen commentedon Dec 1, 2022
Yeah, the example does not make sense - it would if I had used rank, row number or applied a rolling window as you did.
The bug in your note is also fixed now, I think.
mklopets commentedon Dec 3, 2022
One of these examples would also be awesome to have in the book (I like the
bar > (average bar)one a lot!