Commit graph

54 commits

Author SHA1 Message Date
Monty
5a759d31f7 Changing field::field_name and Item::name to LEX_CSTRING
Benefits of this patch:
- Removed a lot of calls to strlen(), especially for field_string
- Strings generated by parser are now const strings, less chance of
  accidently changing a string
- Removed a lot of calls with LEX_STRING as parameter (changed to pointer)
- More uniform code
- Item::name_length was not kept up to date. Now fixed
- Several bugs found and fixed (Access to null pointers,
  access of freed memory, wrong arguments to printf like functions)
- Removed a lot of casts from (const char*) to (char*)

Changes:
- This caused some ABI changes
  - lex_string_set now uses LEX_CSTRING
  - Some fucntions are now taking const char* instead of char*
- Create_field::change and after changed to LEX_CSTRING
- handler::connect_string, comment and engine_name() changed to LEX_CSTRING
- Checked printf() related calls to find bugs. Found and fixed several
  errors in old code.
- A lot of changes from LEX_STRING to LEX_CSTRING, especially related to
  parsing and events.
- Some changes from LEX_STRING and LEX_STRING & to LEX_CSTRING*
- Some changes for char* to const char*
- Added printf argument checking for my_snprintf()
- Introduced null_clex_str, star_clex_string, temp_lex_str to simplify
  code
- Added item_empty_name and item_used_name to be able to distingush between
  items that was given an empty name and items that was not given a name
  This is used in sql_yacc.yy to know when to give an item a name.
- select table_name."*' is not anymore same as table_name.*
- removed not used function Item::rename()
- Added comparision of item->name_length before some calls to
  my_strcasecmp() to speed up comparison
- Moved Item_sp_variable::make_field() from item.h to item.cc
- Some minimal code changes to avoid copying to const char *
- Fixed wrong error message in wsrep_mysql_parse()
- Fixed wrong code in find_field_in_natural_join() where real_item() was
  set when it shouldn't
- ER_ERROR_ON_RENAME was used with extra arguments.
- Removed some (wrong) ER_OUTOFMEMORY, as alloc_root will already
  give the error.

TODO:
- Check possible unsafe casts in plugin/auth_examples/qa_auth_interface.c
- Change code to not modify LEX_CSTRING for database name
  (as part of lower_case_table_names)
2017-04-23 22:35:46 +03:00
Alexander Barkov
48c59f394b MDEV-12338 Split Item_type_holder::get_real_type() into virtual Item::real_type_handler() 2017-03-24 17:52:55 +04: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
20aae56efa Fixed bug mdev-10660.
The method Item_sum::print did not print opening '(' after the name
of simple window functions (like rank, dense_rank etc).
As a result the view definitions with such window functions
were formed invalid in .frm files.
2017-02-03 15:50:25 -08: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
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
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
9a5930bcdf Implement first_value and last_value as window functions
Currently the implementation doesn't support removal, thus the
computation is performed by running over the window frame again.
2016-09-24 15:12:34 +02:00
Vicențiu Ciorbaru
2857ff3c98 MDEV-10815: Window Function Expressions Wrong Results
Fix window function expressions such as win_func() <operator> expr.
The problem was found in 2 places.
First, when we have complex expressions containing window functions, we
can only compute their final value _after_ we have computed the window
function's values. These values must be stored within the temporary
table that we are using, before sending them off.
This is done by performing an extra copy_funcs call before the final
end_send() call.

Second, such expressions need to have their inner arguments,
changed such that the references within those arguments point to fields within
the temporary table.
Ex: sum(t.a) over (order by t.b) + sum(t.a) over (order by t.b)
Before this fix, t.a pointed to the original table's a field. In order
to compute the sum function's value correctly, it needs to point to the
copy of this field inside the temp table.
This is done by calling split_sum_func for each argument in the
expression in turn.

The win.test results have also been updated as they contained wrong
values for such a use case.
2016-09-24 15:12:34 +02:00
Vicențiu Ciorbaru
54b81ac57f Window functions fail with --ps-protocol
When running with --ps protocol, window functions cleanup method may
be called twice. Enforce proper cleanup by setting pointers to NULL
after deletion. This prevents double deletion on second cleanup.
2016-09-12 18:50:47 +02: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
Igor Babaev
3fb4f9bb93 Merge branch '10.2-mdev9197-cons' of github.com:shagalla/server
into branch 10.2-mdev9197.
2016-08-31 16:16:54 -07:00
Galina Shalygina
eb2c147475 The consolidated patch for mdev-9197. 2016-08-23 00:39:12 +03:00
Sergei Golubchik
0a056c9b53 better ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED 2016-06-30 11:43:02 +02:00
Sergei Golubchik
ed77ee1aab cleanup: change Item::walk() to take void* not uchar*
and remove all related casts to uchar*
also remove a couple of unused methods
2016-06-30 11:43:02 +02: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
Vicențiu Ciorbaru
31fb045c12 Make ntile use args[0] for it's argument.
Don't hold a separate pointer as args[0] changes during fix_fields /
split_sum_func.
2016-04-13 11:26:58 +02:00
Vicențiu Ciorbaru
3dd08a11de Fix another bug in dense_rank.
When ordering by a column and partitioning by another, we must reset the
peer_tracker for dense_rank, regardless if the value for the order
column changes or not.

Example:
select a, b, dense_rank() over (partition by b order by a)
a  |  b  |  dense_rank
----------------------
1  | p1  |           1
2  | p1  |           2
2  | p2  |           1   // Here, without this fix we returned 0.
2  | p2  |           2   // And 1 here.
2016-04-13 10:39:06 +02:00
Vicențiu Ciorbaru
419c925069 Fix dense_rank returning minimum rank of 2 when using null columns.
The bug was caused by a weird behaviour in test_if_group_changed, not
returning true when testing for the first time after initializing
the Cached_item list.
2016-04-11 10:46:58 +02:00
Sergei Petrunia
cb002d3479 Window functions: make "ORDER BY window_func" work
- When window functions are present, JOIN::simple_order should be set
  to FALSE. (Otherwise, the optimizer may attempt to do a "pre-sorting"
  on the first join_tab. Which can work in some cases, but generally
  isn't)

- filesort tries to only read table fields that it requires. Window
  function requires its temp.table field.  In order to pass this info
  to filesort, added an implementation of Item_window_func::
  register_field_in_read_map.
2016-04-08 03:21:25 +03:00
Vicențiu Ciorbaru
7c9cfa07ad Fix ntile to work with --ps-protocol 2016-04-06 12:17:04 +03:00
Vicențiu Ciorbaru
960b221c0b Convert ntile to work with expressions as parameters. 2016-04-04 22:04:18 +03: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
a5d300814c Remove no longer used Window_context class. 2016-04-04 22:04:18 +03:00
Igor Babaev
162ea7c0cf Fix a problem in code for percent_rank().
The problem popped up when running win_percent_cume.test
with --ps-protocol.
2016-04-01 15:29:45 -07: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
6ad9ac212c Encapsulate use of List<Cached_item> in a Group_bound_tracker. 2016-03-24 03:23:14 +03:00
Sergei Petrunia
c7a60de78c Code cleanup 2016-03-24 03:08:43 +03:00
Sergei Petrunia
39d3cdbf22 Encapsulate the switching between different return values in Item_window_func 2016-03-24 02:57:03 +03:00
Sergei Petrunia
d8b8b5affa Fix a PS re-execution problem and code cleanup
- Make Item_XXX::cleanup() clean List<Cached_item> orderby_fields.
  (Items survive across PS re-executions. Cached_item don't, because
   they keep pointers to fix_field'ed items, etc)
- Move List<Cached_item> out into Group_bound_tracker.
2016-03-24 02:09:17 +03:00
Igor Babaev
602e15a0cb Changed the base class for Item_window_func from Item_result_field to
Item_func_or_sum.
Implemented method update_used_tables for class Item_findow_func.
Added the flag Item::with_window_func.
Made sure that window functions could be used only in SELECT list
and ORDER BY clause.
Added test cases that checked different illegal placements of
window functions.
2016-03-21 20:00:05 -07:00
Igor Babaev
a74e8d36dd For some window functions an order list must be present. 2016-03-18 10:52:02 -07:00
Igor Babaev
a197c6bb68 Prohibit using window functions of some types with
window frames in full accordance with the SQL standard.
2016-03-18 00:33:53 -07:00
Vicențiu Ciorbaru
aa74fef25e Fix 2 more bugs in item_windowfunc.
1. Item_windowfunc must check the nullness of the field if it returns the
   value from the result_field.
2. The decimal value must come from the window_func instead of the
   result_field when not fetching values from the result_field.

Added a more extensive test case that highlights if these bugs occur.
The 2 select statements check behaviour for decimal and real typed
fields respectively.
2016-03-16 01:42:45 +02:00
Vicențiu Ciorbaru
333ac13be5 Make sure to return the result value when calling Item_windowfunc::val_real()
This caused Item_windowfunc to return garbage.
2016-03-16 01:09:53 +02:00
Sergei Petrunia
833304313d Continuation of "Implemented a counter within Item_sum_sum" a few commits before
Query result had 0 where it should have had NULLs:
- Make Item_window_func::val* functions honor NULL-handling conventions:
  1. set null_value to indicate whether we've returned a NULL value
  2. val_str and val_decimal should return NULL pointer when they're
     returning SQL NULL value.

Fix assertion failure when sending results to network.
- The assert was due to window func returing SQL NULL despite having
  maybe_null=false
- Fixed by settting Item_window_func::maybe_null correctly in fix_fields
2016-03-16 01:26:39 +03:00
Vicențiu Ciorbaru
b579a626cf Implement percent_rank window function 2016-03-03 18:48:34 +02:00
Vicențiu Ciorbaru
f638ffef2c Convert if statements to switch case. 2016-03-03 18:48:34 +02:00
Sergei Petrunia
1cc6fd1f00 Remove comments that may be confusing 2016-03-02 14:01:43 +03:00
Sergei Petrunia
d290a66553 MDEV-9634: Window function produces incorrect value
- Item_sum_count::remove() should check if the argument's value is NULL.
- Window Function item must have its Item_window_func::split_sum_func
  called,
- and it must call split_sum_func for aggregate's arguments (see the
  comment near Item_window_func::split_sum_func for explanation why)
2016-02-26 02:08:45 +03:00
Igor Babaev
0c6d753708 Fixed a problems in the parser.
Resolved window names.
Checked some constraints for window frames.
Added test cases for window name resolution.
2016-02-19 23:20:09 -08:00
Sergei Petrunia
be15858245 MDEV-9526: Compute Aggregate functions as window functions
- Add temporary code: clone_read_record() clones READ_RECORD structure,
  as long as it is used for reading filesort() result that fits into
  memory.
- Add frame bounds for ROWS-type frames. ROWS n PRECEDING|FOLLOWING,
  ROWS UNBOUNDED PRECEDING|FOLLOWING, CURRENT ROW are supported.
- Add Item_sum_count::remove() which allows "streaming" computation
  of COUNT() over a moving frame.
2016-02-18 01:25:26 +03:00
Vicențiu Ciorbaru
0c223a96c1 Fix review comments. 2016-02-16 00:43:57 +02:00
Vicențiu Ciorbaru
9a673e0a88 Implement DENSE_RANK function. 2016-02-16 00:22:12 +02:00
Sergei Petrunia
d8a20d4db7 Post-merge fixes. win.test passes but further cleanup is needed. 2016-02-14 21:00:05 +03:00
Sergei Petrunia
64ab10ffff Initial implementation of RANK() window function 2016-02-14 11:30:36 +03:00