Skip to content

Window functions in filter, sort & join #806

Closed

Description

@aljazerzen
from tbl1
filter (average bar) > 3

... produces:

WITH table_0 AS (
  SELECT
    tbl1.*
  FROM
    tbl1
)
SELECT
  table_0.*
FROM
  table_0
WHERE
  AVG(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.

Activity

  1. added
    bug
    on Jul 11, 2022
  2. snth commented on Sep 22, 2022

    @snth
    Member

    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?

    Why does it produce the first CTE in this case?

  3. aljazerzen commented on Sep 24, 2022

    @aljazerzen
    MemberAuthor

    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.

  4. aljazerzen commented on Dec 1, 2022

    @aljazerzen
    MemberAuthor

    Closed by #593

    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
  5. snth commented on Dec 1, 2022

    @snth
    Member

    This is amazing!

    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

    which produced

    1	3.0000000000000000
    2	3.0000000000000000
    3	3.0000000000000000
    4	3.0000000000000000
    5	3.0000000000000000
    

    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

    and on my example data produces

    1	1.00000000000000000000
    2	1.5000000000000000
    3	2.0000000000000000
    4	2.5000000000000000
    5	3.0000000000000000
    

    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
  6. aljazerzen commented on Dec 1, 2022

    @aljazerzen
    MemberAuthor

    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.

  7. mklopets commented on Dec 3, 2022

    @mklopets
    Collaborator

    One of these examples would also be awesome to have in the book (I like the bar > (average bar) one a lot!

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

    bugcompiler

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions