docs/design/2021-03-01-pipelined-window-functions.md
This document proposes to support executing window functions in a pipelined manner.
The current WF implementation materialized a whole partition before processing it, and if a partition is too large, it will cause TiDB OOM. One particular example is seen in issue/18444 where the whole table is processed as a single partition in order to get a row number for the paging scenario, while the alternative solution using user variable could significantly decrease the memory usage.
As the cause is clear, we aim to pipeline the calculation of some of the window function, which means the window function executor will return data as soon as possible before the whole partition is consumed. After this design is implemented, the evaluation of RN WF will not cause the whole partition to be materialized, instead, it can be processed in a pipelined manner in the whole executor pipeline, that’s why we call it pipelining.
The current window function implementation is like this (with a focus on processing RN):
aggWindowProcessor, as the MySQL document pointed out.Standard SQL specifies that window functions that operate on the entire partition should have no frame clause. MySQL permits a frame clause for such functions but ignores it. These functions use the entire partition even if a frame is specified:
After carefully examining the source code, we provide the following solution, which is based on unifying windowProcessor, and then pipeline it, so that RN function as well as many other WF currently using sliding windows can be pipelined.
However, we could see it as the sliding window is the whole partition for each row
We need to modify the executor build to support this:
This feature will decrease memory consumption for executing window function.
Pipelining won't cause any compatibility issue.
All implemented by PR23022.