Details
- 
    Task 
- 
    Status: Closed (View Workflow)
- 
    Major 
- 
    Resolution: Fixed
Description
Spec draft
Place in the optimizer
Window functions are evaluated on the result set after the WHERE, GROUP BY and HAVING have been applied.
- If we are operating after a temptable-based grouping operation, we can read its output (and apply HAVING on the fly)
- if we are operating after a Join operation, or non-temptable based grouping, we will need to store the output of previous operation in a temptable and then work from there.
Basic idea about operation
Let's start with one window function:
| func_name(arg) OVER (PARTITION BY part_expr  ORDER BY order_expr  $window_spec) | 
Window function is evaluated for each row of the result set. It is a function of row's partition, we need to look at the rows of the resultset that are ordered according to order_expr. $window_spec specifies at which rows we need to look. Sometimes it's certain preceding rows, sometimes it's certain following rows (and sometimes it's both?)
Execution can be done as follows:
- sort resultset rows by (part_expr, order_expr).
- scan each partition (either in reverse or forward order) and compute the value of window function on the fly.
Unresolved questions:
- How to handle the context. If a window function depends on preceding and following rows, how do we keep track of those?
- Whether we should use files or temp.tables for storing the sorted resultset (we will always have to store it, because filesort doesn't support pull-based reading of its resultset)
Multiple window functions
A query may use multiple window functions which use different PARTITION BY / ORDER BY expressions:
| SELECT  | 
|    window_func1 OVER (PARTITION BY part_expr1 ORDER BY order_expr1) | 
|    window_func2 OVER (PARTITION BY part_expr2 ORDER BY order_expr2) | 
| FROM ... WHERE ... etc | 
The query should be evaluated as follows:
- sort the result according to part_expr1/order_expr1, compute values of window_func1
- sort the result according to part_expr2/order_expr2, compute values of window_func2
Links
- a tutorial http://sqlschool.modeanalytics.com/advanced/window-functions.html
- SQL standard draft: see 5CD2-02-Foundation-2006-01.pdf
	- pg. 62 - 4.15.3 window functions
- pg. 204 - <window function>
 
Attachments
Issue Links
- includes
- 
                    MDEV-9198 Window functions: parser, error checking, and name resolution -         
- Closed
 
-         
- 
                    MDEV-9204 Window functions: locate the place where to put window function execution calls -         
- Closed
 
-         
- 
                    MDEV-9525 Window functions: Support for aggregate_func(DISTINCT ...) OVER (...) -         
- Open
 
-         
- 
                    MDEV-9526 Compute Aggregate functions as window functions -         
- Closed
 
-         
- 
                    MDEV-9676 RANGE-type frames for window functions -         
- Closed
 
-         
- 
                    MDEV-9719 Query with window function crashes with --ps-protocol -         
- Closed
 
-         
- 
                    MDEV-9724 Window functions: Frame Exclusion support -         
- Closed
 
-         
- 
                    MDEV-9727 Window functions: datetime arithmetic for RANGE-type frames -         
- Open
 
-         
- 
                    MDEV-9736 Window functions: multiple cursors to read filesort result -         
- Open
 
-         
- 
                    MDEV-9740 Window functions: catch invalid window frame specs -         
- Closed
 
-         
- 
                    MDEV-9741 Window functions: support being in a subquery -         
- Stalled
 
-         
- 
                    MDEV-9746 Window functions: CUME_DIST function -         
- Closed
 
-         
- 
                    MDEV-9753 Window functions: reject window frame definition where it is not allowed -         
- Closed
 
-         
- 
                    MDEV-9754 Window functions: assertion failure in with --ps-protocol -         
- Closed
 
-         
- 
                    MDEV-9755 Buildbot shows a crash in JOIN::make_aggr_tables_info() on windows -         
- Closed
 
-         
- 
                    MDEV-9780 Window functions: interplay between window function and other constructs -         
- Closed
 
-         
- 
                    MDEV-9787 Window functions: HAVING and GROUP BY -         
- Closed
 
-         
- 
                    MDEV-9841 Window functions: embedded server fails to start due to error code conflicts -         
- Closed
 
-         
- 
                    MDEV-9847 Window functions: crash with big_tables=1 -         
- Closed
 
-         
- 
                    MDEV-9848 Window functions: reuse sorting and/or scanning between window functions -         
- Open
 
-         
- 
                    MDEV-9877 Window functions: wrong sort criteria is used -         
- Closed
 
-         
- 
                    MDEV-9892 Window functions: add a counter -         
- Closed
 
-         
- 
                    MDEV-9897 Window functions: crash when ORDER BY clause uses an expression -         
- Closed
 
-         
- 
                    MDEV-9911 NTILE must return an error when parameter is not stable -         
- Closed
 
-         
- 
                    MDEV-9935 Window functions: assertion failure with empty OVER() clause -         
- Closed
 
-         
- 
                    MDEV-10023 Window functions: big window frame handling -         
- Closed
 
-         
- 
                    MDEV-17191 Named WINDOW in window functions -         
- Closed
 
-         
- is blocked by
- 
                    MDEV-8646 Re-engineer the code for post-join operations -         
- Closed
 
-         
- 
                    MDEV-9543 Parsing, name resolution and optimization for window functions. -         
- Closed
 
-         
- relates to
- 
                    MDEV-8091 Simple window functions -         
- Closed
 
-         
- 
                    MDEV-9894 Assertion `0' failed in Window_func_runner::setup -         
- Closed
 
-         
- 
                    MDEV-9895 Assertion `n_rows > 0' failed in Frame_cursor* get_frame_cursor -         
- Closed
 
-         
- 
                    MDEV-5535 cannot reopen temporary table -         
- Closed
 
-         
- 
                    MDEV-10855 Window functions: condition pushdown through the PARTITION BY clause -         
- Closed
 
-