[MDEV-9727] Window functions: datetime arithmetic for RANGE-type frames Created: 2016-03-14  Updated: 2021-10-14

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

Type: Task Priority: Major
Reporter: Sergei Petrunia Assignee: Vicențiu Ciorbaru
Resolution: Unresolved Votes: 5
Labels: None

Issue Links:
Blocks
blocks MCOL-644 MariaDB 10.2 WF Add RANGE BETWEEN wit... Closed
PartOf
is part of MDEV-6115 window functions as in the SQL standard Closed
is part of MDEV-12987 complete window function support for ... Open
Relates
relates to MDEV-9676 RANGE-type frames for window functions Closed
relates to MDEV-24937 window functions over unbounded RANGE... Closed

 Description   

The standard specifies that RANGE-type frame bounds support date[time] arithmetics. That is, one can write queries like this:

SUM(number) OVER (PARTITION BY col1 
                  ORDER BY date_col
                  RANGE BETWEEN INTERVAL 1 MONTH PRECEDING 
                  AND INTERVAL 1 MONTH FOLLOWING)

MariaDB only supports numeric types there. The reason for the limitation is that interval is not a first-class datatype in MySQL codebase.

  • Interval is not parsed to an Item object
  • One cannot use addition/subtraction to add an interval to a date.

  • MDEV-9676 uses Item_func_plus/Item_func_minus, which allow range bounds to use any numeric datatype. This doesn't include datetime, though.

The building blocks for date+interval arithmetics are already there:

SELECT DATE_ADD(date_col, INTERVAL 10 DAY) 
SELECT DATE_SUB(date_col, INTERVAL 5 YEAR) 

That is,

  • the parser is able to parse interval values.
  • there is code to do addition/subtraction.

This task is to support interval arithmetics in window functions.



 Comments   
Comment by Zvi Landsman [ 2020-06-14 ]

resolving time to unix timestamp/seconds works:

SUM(number) OVER (PARTITION BY col1 
                                   order by unix_timestamp(date_col) 
                                   range between 2592000 preceding 
                                   and 2592000 following)

Generated at Thu Feb 08 07:36:52 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.