Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WINDOW clause gives wrong results with negative RANGE PRECEDING #10885

Closed
1 task done
soerenwolfers opened this issue Feb 27, 2024 · 2 comments · Fixed by #11390
Closed
1 task done

WINDOW clause gives wrong results with negative RANGE PRECEDING #10885

soerenwolfers opened this issue Feb 27, 2024 · 2 comments · Fixed by #11390
Assignees

Comments

@soerenwolfers
Copy link
Contributor

What happens?

WINDOW clause gives wrong results with negative RANGE PRECEDING, as if the offset was capped below at zero.

It should instead either throw (like PostgreSQL15, SQLite3.39 and ClickHouse do) or give the "correct" result (like duckdb does for ROWS PRECEDING, ROWS FOLLOWING, and RANGE FOLLOWING)

To Reproduce

CREATE OR REPLACE TABLE test(i INTEGER, v FLOAT);
INSERT INTO test VALUES (0, 1), (1, 2), (2, 3),;
SELECT i, v, sum(v) OVER (ORDER BY i RANGE BETWEEN -1 PRECEDING AND 1 FOLLOWING) FROM test

prints

┌───────┬───────┬─────────────────────────────────────────────────────────────────────┐
│   i   │   v   │ sum(v) OVER (ORDER BY i RANGE BETWEEN -1 PRECEDING AND 1 FOLLOWING) │
│ int32 │ float │                               double                                │
├───────┼───────┼─────────────────────────────────────────────────────────────────────┤
│     0 │   1.0 │                                                                 3.0 │
│     1 │   2.0 │                                                                 5.0 │
│     2 │   3.0 │                                                                 3.0 │
└───────┴───────┴─────────────────────────────────────────────────────────────────────┘

but should print same as

SELECT i, v, sum(v) OVER (ORDER BY i ROWS BETWEEN -1 PRECEDING AND 1 FOLLOWING) FROM test
┌───────┬───────┬────────────────────────────────────────────────────────────────────┐
│   i   │   v   │ sum(v) OVER (ORDER BY i ROWS BETWEEN -1 PRECEDING AND 1 FOLLOWING) │
│ int32 │ float │                               double                               │
├───────┼───────┼────────────────────────────────────────────────────────────────────┤
│     0 │   1.0 │                                                                2.0 │
│     1 │   2.0 │                                                                3.0 │
│     2 │   3.0 │                                                               NULL │
└───────┴───────┴────────────────────────────────────────────────────────────────────┘

OS:

Linux

DuckDB Version:

0.10.1

DuckDB Client:

Python

Full Name:

Soeren Wolfers

Affiliation:

G-Research

Have you tried this on the latest nightly build?

I have tested with a nightly build

Have you tried the steps to reproduce? Do they include all relevant data and configuration? Does the issue you report still appear there?

  • Yes, I have
@hawkfish
Copy link
Contributor

Not a duplicate of #10809

@hawkfish
Copy link
Contributor

We clamp preceding range values to precede the current row, so this is currently expected behaviour. It is obscure, however and we may decide throw at some point.

@hawkfish hawkfish self-assigned this Feb 28, 2024
hawkfish pushed a commit to hawkfish/duckdb that referenced this issue Mar 27, 2024
Detect and trap negative range boundaries to match PG behaviour.

fixes: duckdb#10885
fixes: duckdblabs/duckdb-internal#1389
Mytherin added a commit that referenced this issue Mar 28, 2024
ccfelius pushed a commit to ccfelius/duckdb that referenced this issue Apr 6, 2024
Detect and trap negative range boundaries to match PG behaviour.

fixes: duckdb#10885
fixes: duckdblabs/duckdb-internal#1389
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants