[MDEV-24937] window functions over unbounded RANGE fails on not numeric type Created: 2021-02-22  Updated: 2022-02-12  Resolved: 2022-02-12

Status: Closed
Project: MariaDB Server
Component/s: Optimizer - Window functions
Affects Version/s: 10.3.27
Fix Version/s: N/A

Type: Bug Priority: Minor
Reporter: Daniel Black Assignee: Vicențiu Ciorbaru
Resolution: Won't Fix Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-9727 Window functions: datetime arithmetic... Open

 Description   

create table ticks 
(SYMBOL varchar(50), `DATE` Date, PRICE int);
insert into ticks values
("AAPL","2021-02-21",10),
("TSLA","2021-02-21",800),
("AMZN","2021-02-21",90),
("AAPL","2021-02-20",17),
("TSLA","2021-02-20",900),
("AMZN","2021-02-20",105),
("AAPL","2021-02-19",5),
("TSLA","2021-02-19",960),
("AMZN","2021-02-19",80);
 
select distinct symbol, first_value(price) over w as first, last_value(price) over w as last
from ticks
window w as (partition by symbol order by date range between UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING)

"Numeric datatype is required for RANGE-type frame"

https://dbfiddle.uk/?rdbms=mariadb_10.3&fiddle=b5818fc67035bb95bf7ad49663f456b4

Works in MySQL-8.0.23 as if RANGE was replace with ROWS.



 Comments   
Comment by Vicențiu Ciorbaru [ 2021-07-07 ]

This is a limitation of current RANGE implementation. It will be fixed when MDEV-9727 is also fixed.

Comment by Daniel Black [ 2022-02-11 ]

While MDEV-9727 will fix this, as UNBOUNDED won't look at the type or the value, maybe this could still be done.

Comment by Vicențiu Ciorbaru [ 2022-02-11 ]

I do not want to introduce a hack to only allow RANGE type frames, regardless of data type, if UNBOUNDED PRECEDING & UNBOUNDED FOLLOWING is present on the respective ends of the window frame. This would create queries like this that work:

select symbol, first_value(price) over w as first, last_value(price) over w as last
from ticks
window w as (partition by symbol order by date_2 range between UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING)

and queries like this that do not work:

select symbol, first_value(price) over w as first, last_value(price) over w as last
from ticks
window w as (partition by symbol order by date_2 range between UNBOUNDED PRECEDING and CURRENT ROW)

This would set confusing expectations for users and we'd also have a very peculiar documentation problem that needs to always be maintained as long as that version is supported.
Documentation would have to read something like:
"For RANGE type frames, starting with MariaDB 10.9, it works for all data types when the window bounds are UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING".

We should just get MDEV-9727 implemented instead, not waste time with hacks.

Comment by Daniel Black [ 2022-02-12 ]

Ok.

Generated at Thu Feb 08 09:33:50 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.