Skip to content

WINDOW order by changes row order of table for no reason #20015

Description

@Djfe

What happens?

I have two different WINDOWs in my select statement. If I order window2 then it orders the full table for some reason placing rows with a NULL value at the end before calculating window1: row_number() OVER () despite OVER () guaranteeing preservation of row order according to:
https://duckdb.org/docs/stable/sql/dialect/order_preservation#clauses

Sure I could do this in two steps, row_number() first, put it in a nested select statement and then do my calculations for window2 but that seems like a workaround for a bug.

To Reproduce

WITH test_data AS (
    SELECT ['', 'P diff. (psi)', '0', '1', '2', '3', '4'] AS str_list
)
SELECT
    row_number() OVER () AS row,
    TRY_CAST(str_col AS DOUBLE) AS elapsed_time,
    elapsed_time - LAG(elapsed_time) OVER (ORDER BY elapsed_time ASC) AS interval_s
FROM (
    SELECT
        UNNEST(str_list) AS str_col,
    FROM test_data
)

VS

WITH test_data AS (
    SELECT ['', 'P diff. (psi)', '0', '1', '2', '3', '4'] AS str_list
)
SELECT
    row_number() OVER () AS row,
    TRY_CAST(str_col AS DOUBLE) AS elapsed_time,
    elapsed_time - LAG(elapsed_time) OVER () AS interval_s
FROM (
    SELECT
        UNNEST(str_list) AS str_col,
    FROM test_data
)

They have a different row column/row order for some reason.

OS:

Windows 10 Pro 64bit

DuckDB Version:

1.4.2

DuckDB Client:

Python

Hardware:

Intel Core i3-6006U, 8GB RAM, integrated GPU

Full Name:

Felix Baumann

Affiliation:

student

Did you include all relevant configuration (e.g., CPU architecture, Linux distribution) to reproduce the issue?

  • Yes, I have

Did you include all code required to reproduce the issue?

  • Yes, I have

Did you include all relevant data sets for reproducing the issue?

Yes

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions