Commit graph

97 commits

Author SHA1 Message Date
Vicențiu Ciorbaru
d474642254 MDEV-10092: Server crashes in in ha_heap::rnd_pos / Table_read_cursor::get_next
The bug was caused by several issues.
2 problems in seek_io_cache. Due to wrong offsets used, we would end up
seeking way too much (first change), or over the intended seek point
(second change). Fixing it requires correctly detecting available data
in buffer (first change), and not using "IO_SIZE alligned" reads. The
second is needed because _my_b_cache_read adjusts the pos_in_file itself
based on read_pos and read_end. Pretending buffer is empty when we want
to force a read will aleviate this problem.

Secondly, the big-table cursors didn't repect the interface definitions
of always returning the rownumber that Table_read_cursor::fetch() would activate.

At the same time, next(), prev() and move_to() should not perform any
row activation.
2017-02-15 14:09:27 +02:00
Vicențiu Ciorbaru
9fe9fb68ac MDEV-10859: Wrong result of aggregate window function in query with HAVING and no ORDER BY
Window functions need to be computed after applying the HAVING clause.
An optimization that we have for regular, non-window function, cases is
to apply having only during sending of the rows to the client. This
allows rows that should be filtered from the temporary table used to
store aggregation results to be stored there.

This behaviour is undesireable for window functions, as we have to
compute window functions on the result-set after HAVING is applied.
Storing extra rows in the table leads to wrong values as the frame
bounds might capture those -to be filtered afterwards- rows.
2017-02-15 14:09:27 +02:00
Vicențiu Ciorbaru
5bf338435a MDEV-11746: Wrong result upon using FIRST_VALUE with a window frame
The same approach is needed for LAST_VALUE, otherwise the LAST_VALUE sum
functions are not cleared correctly. Now LAST_VALUE behaves as NTH_VALUE
with 0 offset, only that the frame that it is examining is the bottom bound,
not the top bound.
2017-02-14 07:46:58 +02:00
Vicențiu Ciorbaru
57341852b5 MDEV-11746: Wrong result upon using FIRST_VALUE with a window frame
Reimplement FIRST_VALUE to act as NTH_VALUE with 0 offset. The previous
implementation was flawed when the window frame would remove values.
2017-02-14 07:46:58 +02:00
Igor Babaev
e51b015fc3 Fixed bug mdev-11138.
Supported usage of expressions with window functions
in SELECTs without tables.
2017-02-04 21:51:40 -08:00
Igor Babaev
6681a4998e Post-review addition to the fix for mdev-10868. 2016-09-30 17:40:42 -07:00
Igor Babaev
903f34c7a9 Fixed bug mdev-10868.
There was no implementation of the virtual method print()
for the Item_window_func class. As a result for a view
containing window function an invalid view definition could
be written in the frm file. When a query that refers to
this view was executed a syntax error was reported.
2016-09-30 17:40:40 -07:00
Vicențiu Ciorbaru
de2175e96a Window functions can have an empty over clause
Make sure we select one window function if we have an empty over clause.
2016-09-24 15:25:00 +02:00
Vicențiu Ciorbaru
8b95e7eb44 Make sure to call Rowid_seq_cursor::next to not face infinite recursion 2016-09-24 15:12:34 +02:00
Sergei Petrunia
047963922c MDEV-9736: Window functions: multiple cursors to read filesort result
Add support for having multiple IO_CACHEs with type=READ_CACHE to share
the file they are reading from.
Each IO_CACHE keeps its own in-memory buffer. When doing a read or seek
operation on the file, it notifies other IO_CACHEs that the file position
has been changed.

Make Rowid_seq_cursor use cloned IO_CACHE when reading filesort result.
2016-09-24 15:12:34 +02:00
Vicențiu Ciorbaru
6e4015727a Clean up nth_value
Implement nth_value correctly and add a test case for it.
2016-09-24 15:12:34 +02:00
Vicențiu Ciorbaru
53cf265b3b Implement LEAD and LAG and NTH_VALUE functions
Refactour out (into a copy for now) the logic of Item_sum_hybrid, to
allow for multiple arguments. It does not contain the comparator
members. The result is the class Item_sum_hybrid_simple.

LEAD and LAG make use of this Item to store previous rows in a chache.
It also helps in specifying the field type. Currently LEAD/LAG do not
support default values.

NTH_VALUE behaves identical to LEAD and LAG, except that the starting
position cursor is placed on the top of the frame instead of the current
row.
2016-09-24 15:12:34 +02:00
Vicențiu Ciorbaru
3dd3a5da0e MDEV-9935: Window functions: assertion failure with empty OVER () clause
Make window functions work with an empty over clause by forcing
a sort on the first column of the current join_tab. This is a temporary
fix until we get window functions to work with big tables.
2016-09-24 15:12:34 +02:00
Vicențiu Ciorbaru
e992464f27 Update Frame_positional_cursor to also take an optional bound
The positional cursor now fetches rows based on the positional
cursor and an offset (if present). It will fetch rows, based on the
offset, only if the required position is not out of bounds.
2016-09-24 15:12:34 +02:00
Vicențiu Ciorbaru
872c0b4225 Allow first/last value functions to have frame definitions
Add a test case to check this behavior.
2016-09-24 15:12:34 +02:00
Vicențiu Ciorbaru
a2bafbabe2 Make first_value and last_value computation efficient
With clever use of partition bounds, we only need to add one row to the
items at a time. This way we remove the need to "reset" the item and run
through the full partition again.
2016-09-24 15:12:34 +02:00
Vicențiu Ciorbaru
00bf18e2bf Move table record writing outside of loop
We can set values in the record buffer first and only perform one table
write call at the end. No need to write to file every time one column is
updated.

Also, remove unused method from Table_read_cursor.
2016-09-24 15:12:34 +02:00
Vicențiu Ciorbaru
1c72441364 Frame bounds using FOLLOWING or PRECEDING can have 0 as cardinal value
This makes them behave exactly like CURRENT ROW. Standard specifies
unsigned integer, which includes the value 0.

Expand the win_min_max test to include this kind of frame definitions.
2016-09-24 15:09:39 +02:00
Igor Babaev
4872ec6177 Fixed bug mdev-10874.
In some cases the method Window_funcs_sort::setup() did
not build the sequence of sorting keys correctly.
2016-09-23 14:23:17 -07:00
Igor Babaev
10aa3936a7 Fixed bug mdev-10842.
In some cases the function compare_order_elements() erroneously
returned CMP_EQ for not equal elements.
2016-09-20 19:56:05 -07:00
Vicențiu Ciorbaru
14690c70c1 Enable almost all aggregate functions for window functions 2016-09-09 18:33:29 +03:00
Vicențiu Ciorbaru
dfd3be928d Make cursor implementation uniform
Cursors now report their current row number as the boundary of the
partition. This is used by Frame_scan_cursor to compute aggregate
functions that do not support removal.
2016-09-09 18:33:29 +03:00
Vicențiu Ciorbaru
ffed20c563 Extend Frame_cursor to report the current row it is pointing at
Added an extra virtual method to the Frame_cursor class to allow cursors
to report the row number to which they are pointing.
2016-09-09 18:32:35 +03:00
Vicențiu Ciorbaru
e174b13465 Add a method to disable the automatic adding of values from cursors
Currently cursors automatically add values to the sum functions they
manage. There are use cases when we just want to figure out the frame
boundaries, without actually adding/removing values from them.
2016-09-09 18:32:35 +03:00
Vicențiu Ciorbaru
3ba867be89 Convert Partition_read_cursor to inherit from Table_read_cursor
The 'IS A' relation is more appropriate for Partition_read_cursor. This
also helps with accessing methods available only to Table_read_cursor.
2016-09-09 18:32:35 +03:00
Vicențiu Ciorbaru
1adc3fab23 MDEV-10097: Assertion `count > 0' failed in Item_sum_sum::add_helper(bool)
When specifying a RANGE type frame that exceeds the partition size, both
for the top and bottom cursors we end up removing more rows than added
to the aggregate function. This happens because our TOP range cursor,
which removes values from the aggregate function, would be allowed to breach
partition boundaries, while the BOTTOM range cursor would not.

To prevent this from happening, force the TOP range cursor to only move
within the current partition, as does the BOTTOM range cursor.
2016-09-09 18:32:35 +03:00
Vicențiu Ciorbaru
23e8b508a0 MDEV-10059: Compute window functions with same sorting criteria simultaneously
Perform only one table scan for each window function present. We do this
by keeping keeping cursors for each window function frame bound and
running them for each function for every row.
2016-09-09 18:32:35 +03:00
Monty
4dc5075860 Fixed compiler warnings and test failures found by buildbot
Fixed ccfilter to detect errors where the column is included in the error message
2016-06-24 02:25:14 +03:00
Monty
fabeab7819 Cleanups
- Avoid some realloc() during startup
- Ensure that file_key_management_plugin frees it's memory early, even if
  it's linked statically.
- Fixed compiler warnings from unused variables and missing destructors
- Fixed wrong indentation
2016-04-28 16:36:02 +03:00
Sergei Petrunia
a81e711a06 MDEV-9925: Wrong result with aggregate function as a window function
Make Frame_range_current_row_bottom to take into account partition bounds.

Other partition bounds that could potentially hit the end of partition are
Frame_range_n_bottom, Frame_n_rows_following, Frame_unbounded_following,
and they all had end-of-partition protection.

To simplify the code, factored out end-of-partition checks into
class Partition_read_cursor.
2016-04-15 20:40:01 +03:00
Igor Babaev
5ff4b21e02 Fixed bug mdev-9897.
This bug revealed a serious problem: if the same partition list
was used in two window specifications then the temporary table created
to calculate window functions contained fields for two identical
partitions. This problem was fixed as well.
2016-04-14 00:47:28 -07:00
Sergei Petrunia
0f62eee2f8 Fix compiler warning 2016-04-11 23:59:51 +02:00
Sergei Petrunia
da7c5e3b85 MDEV-9895: Assertion `n_rows > 0' failed in Frame_cursor* get_frame_cursor
n=0 in "ROWS 0 PRECEDING" is valid, add handling for it:
- Adjust the assert
- Bottom bound of 'ROW 0 PRECEDING' is actually looking at the current
  row, that is, it needs to process partition's first row directly in
  Frame_n_rows_preceding::next_partition().
- Added testcases
2016-04-10 16:24:04 +02:00
Sergei Petrunia
2905b2feb6 Window functions: return error if aggregate is not supported as window functions 2016-04-10 11:28:33 +02:00
Sergei Petrunia
306de8a927 MDEV-9877: Window functions: wrong sort criteria is used
" The sort order for the sub-sequence of window functions starting
from the element marked by SORTORDER_CHANGE_FLAG up to the next
element marked by SORTORDER_CHANGE_FLAG must be taken from the
last element of the sub-sequence (not from the first one)."
2016-04-06 23:02:31 +03:00
Sergei Petrunia
2efabf81f1 MDEV-9847: Window functions: crash with big_tables=1
- Move filesort's sort_positions argument into class Filesort.
- Make window function code construct Filesort with sort_positions=true.
2016-04-06 20:34:23 +03:00
Sergei Petrunia
5b85d0a75b Window functions: Better class names
As discussed on the call:
- s/Window_funcs_computation_step/Window_funcs_computation/g
- s/Window_func_sort/Window_funcs_sort/g
2016-04-06 18:24:11 +03:00
Sergei Petrunia
9bd194b1b7 MDEV-9848: Window functions: reuse sorting and/or scanning
- Rename Window_funcs_computation to Window_funcs_computation_step
- Introduce Window_func_sort which invokes filesort and then
  invokes computation of all window functions that use this ordering.
- Expose Window functions' sort operations in EXPLAIN|ANALYZE FORMAT=JSON
2016-04-05 19:10:44 +03:00
Igor Babaev
e30bd913ae Fixed a problem with setting wrong flags for ordering in the code
of compare_window_funcs_by_window_specs().
2016-04-04 15:24:27 -07:00
Vicențiu Ciorbaru
be3902fceb Implement ntile window function.
The current implementation does not allow for a dynamic expression
within the sum function's parameter.
2016-04-04 22:04:18 +03:00
Vicențiu Ciorbaru
629f9feabe Fix post review comments regarding the usage of List<>. 2016-04-04 22:04:18 +03:00
Igor Babaev
0b89c61413 Fixed a problem with --ps-protocol.
Window names has to be resolved only once.
2016-04-01 14:05:51 -07:00
Igor Babaev
2e4bd4407e The implementation of the template bubble_sort assumed
that the call-back comparison function returns a positive
number when arg1 < arg2, and a negative number when arg1 > arg2.
This is not in line with other implementation of sorting
algorithm.
Changed bubble_sort: now a negative result from the comparison
function means that arg1 < arg2, and positive result means
that arg1 > arg2.
Changed accordingly all call-back functions that are used as
parameters in the call of bubble_sort.

Added a test case to check the proper sorting of window functions.
2016-04-01 12:00:54 -07:00
Igor Babaev
c9ff5cfbfd Fixed a crash in compare_window_frames().
The function did not take into account the case when only one of
of the pointers to the compared frames is NULL.
2016-03-30 12:43:57 -07:00
Igor Babaev
3450c2da02 Added sorting window function to minimize the number of scans
of the temporary table needed to compute them.
2016-03-30 02:07:48 -07:00
Vicențiu Ciorbaru
de35787a83 Merge branch 'cume_dist' into bb-10.2-mdev9543 2016-03-28 22:52:18 +03:00
Vicențiu Ciorbaru
3544fe0144 Implemented cume_dist function.
Also fixed a bug in row_counts detection, when partition changes.
2016-03-28 22:51:42 +03:00
Vicențiu Ciorbaru
d40d68f236 Convert percent_rank to work with cursors
The percent_rank function now is compatible with the cursor algorithm.
We no longer need a special implementation for it to work.
2016-03-28 22:51:42 +03:00
Vicențiu Ciorbaru
bf18dac08f Lay the groundwork for variable number of cursors.
Instead of relying solely on top bound and bottom bound cursors,
now we create a list of cursors that are iterated over.
2016-03-28 22:51:42 +03:00
Sergei Petrunia
4fe6fbbb63 Merge branch 'bb-10.2-mdev9543' of github.com:MariaDB/server into bb-10.2-mdev9543 2016-03-28 22:19:55 +03:00