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

Bug in nth_value() window function for NULL input (SQLancer) #12320

Open
2010YOUY01 opened this issue Sep 4, 2024 · 2 comments · May be fixed by #12676
Open

Bug in nth_value() window function for NULL input (SQLancer) #12320

2010YOUY01 opened this issue Sep 4, 2024 · 2 comments · May be fixed by #12676
Assignees
Labels
bug Something isn't working

Comments

@2010YOUY01
Copy link
Contributor

Describe the bug

There are two unexcepted behavior in nth_value() window function

  1. When NULL literal is used in the second arg, it returned Execution error: Expected an integer value
    The expected behavior is to evaluate all rows to NULL, because NULL should be treated as a missing integer value

  2. When second arg is col*NULL, it returned another unexpected internal error

More details can be found in the reproducer in datafusion-cli (Compiled from latest main, commit hash 6bbad7e, with cargo run)

DataFusion CLI v41.0.0
> create table t1(v1 int, v2 int);
insert into t1 values (1,1), (1,2),(1,3),(2,1),(2,2);

> select v1, nth_value(v2, null) over (partition by v1 order by v2) from t1;

Execution error: Expected an integer value

> select v1, nth_value(v2, v2*null) over (partition by v1 order by v2) from t1;
Internal error: Cannot convert Int64(NULL) to i64.
This was likely caused by a bug in DataFusion's code and we would welcome that you file an bug report in our issue tracker

Expected behavior in DuckDB (output in postgres is the same)

D select v1, nth_value(v2, null) over (partition by v1 order by v2) from t1;
┌───────┬────────────────────────────────────────────────────────┐
│  v1   │ nth_value(v2, NULL) OVER (PARTITION BY v1 ORDER BY v2) │
│ int32 │                         int32                          │
├───────┼────────────────────────────────────────────────────────┤
│     2 │                                                        │
│     2 │                                                        │
│     1 │                                                        │
│     1 │                                                        │
│     1 │                                                        │
└───────┴────────────────────────────────────────────────────────┘

D select v1, nth_value(v2, v2*null) over (partition by v1 order by v2) from t1;
┌───────┬───────────────────────────────────────────────────────────────┐
│  v1   │ nth_value(v2, (v2 * NULL)) OVER (PARTITION BY v1 ORDER BY v2) │
│ int32 │                             int32                             │
├───────┼───────────────────────────────────────────────────────────────┤
│     2 │                                                               │
│     2 │                                                               │
│     1 │                                                               │
│     1 │                                                               │
│     1 │                                                               │
└───────┴───────────────────────────────────────────────────────────────┘

To Reproduce

No response

Expected behavior

No response

Additional context

Found by SQLancer #11030

@2010YOUY01 2010YOUY01 added the bug Something isn't working label Sep 4, 2024
@thinh2
Copy link
Contributor

thinh2 commented Sep 4, 2024

take

@dharanad
Copy link
Contributor

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants