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
fix: Be more lenient in interpreting input args for builtin window functions (#11199)
* fix: Be more lenient in interpreting input args for builtin window functions
The built-in window functions Lag, Lead, NthValue, Ntile
accept integer arguments. However while they should
allow any integers, currently as they just use
ScalarValue's try_from to convert into an i64, they
actually only accept i64s. Any other argument, e.g. an i32,
would be converted into a None and ignored.
Before - lag and lead would silently ignore the argument, ntile and nth_value would fail:
```
> SELECT id, lead(id, -1) OVER (ORDER BY id) AS correct, lead(id, arrow_cast(-1,'Int32')) OVER (ORDER BY id) as wrong from (values (1), (2)) as tbl(id);
+----+---------+-------+
| id | correct | wrong |
+----+---------+-------+
| 1 | | 2 |
| 2 | 1 | |
+----+---------+-------+
> SELECT id, lag(id, -1) OVER (ORDER BY id) AS correct, lag(id, arrow_cast(-1,'Int32')) OVER (ORDER BY id) as wrong from (values (1), (2)) as tbl(id);
+----+---------+-------+
| id | correct | wrong |
+----+---------+-------+
| 1 | 2 | |
| 2 | | 1 |
+----+---------+-------+
> SELECT id, nth_value(id, 2) OVER (ORDER BY id) AS correct, nth_value(id, arrow_cast(2,'Int32')) OVER (ORDER BY id) as corrected from (values (1), (2)) as tbl(id);
Execution error: Internal("Cannot convert Int32(2) to i64")
> SELECT id, ntile(2) OVER (ORDER BY id) AS correct, ntile(arrow_cast(2,'Int32')) OVER (ORDER BY id) as corrected from (values (1), (2)) as tbl(id);
Internal error: Cannot convert Int32(2) 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
```
After - all four produce expected results:
```
SELECT id, lead(id, -1) OVER (ORDER BY id) AS correct, lead(id, arrow_cast(-1,'Int32')) OVER (ORDER BY id) as corrected from (values (1), (2)) as tbl(id)
+----+---------+-----------+
| id | correct | corrected |
+----+---------+-----------+
| 1 | | |
| 2 | 1 | 1 |
+----+---------+-----------+
SELECT id, lag(id, -1) OVER (ORDER BY id) AS correct, lag(id, arrow_cast(-1,'Int32')) OVER (ORDER BY id) as corrected from (values (1), (2)) as tbl(id)
+----+---------+-----------+
| id | correct | corrected |
+----+---------+-----------+
| 1 | 2 | 2 |
| 2 | | |
+----+---------+-----------+
SELECT id, nth_value(id, 2) OVER (ORDER BY id) AS correct, nth_value(id, arrow_cast(2,'Int32')) OVER (ORDER BY id) as corrected from (values (1), (2)) as tbl(id)
+----+---------+-----------+
| id | correct | corrected |
+----+---------+-----------+
| 1 | | |
| 2 | 2 | 2 |
+----+---------+-----------+
SELECT id, ntile(2) OVER (ORDER BY id) AS correct, ntile(arrow_cast(2,'Int32')) OVER (ORDER BY id) as corrected from (values (1), (2)) as tbl(id)
+----+---------+-----------+
| id | correct | corrected |
+----+---------+-----------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
+----+---------+-----------+
```
* cleanup
* make lead/lag throw if arg is invalid, check that the arg is int before casting, add tests
* return unsigned handling to ntile and move tests to sqllogictests window.slt
* remove unused import
0 commit comments