Commit graph

48 commits

Author SHA1 Message Date
Marko Mäkelä
24c9785a67 Merge 10.2 into bb-10.2-ext 2017-12-01 15:35:16 +02:00
Vicențiu Ciorbaru
8cee2f136d MDEV-13384: "window" seems like a reserved column name but it's not listed as one
Window is a reserved keyword according to SQL Standard 2016. However, we
can make the grammar slightly flexible by allowing WINDOW keyword everywhere
except table aliases. Change yacc grammar to separate between all keywords
and table_alias keywords.
2017-11-29 09:39:31 +02:00
Marko Mäkelä
fc3b1a7d2f Merge 10.2 into bb-10.2-ext 2017-09-20 17:47:49 +03:00
Vicențiu Ciorbaru
a1e589be9b MDEV-13354: Server crashes in find_field_in_tables upon PS with window function and subquery
When creating an Item_direct_view_ref we were setting the
Name_resolution_context based on TABLE_LIST::view member variable.
However, for derived tables this member is NULL. To not set a wrong
context, if TABLE_LIST::view is empty, use THD::lex instead.
2017-09-19 15:14:10 +03:00
Vicențiu Ciorbaru
33209350f7 MDEV-13649: Server crashes in set_field_to_null_with_conversions or in Field::set_notnull
Item_cond did not correctly propagate with_window_func flag in the Item
tree. Without it, we would not call Item::split_sum_func correctly and the
window function's result_field would remain NULL.
2017-09-19 15:14:10 +03:00
Vicențiu Ciorbaru
02eda36e4e MDEV-13358: FIRST_V throw SQL Fehler (1292): Incorrect datetime value
This is backport of 25ad623d64 for 10.2.

The issue is similar to the one from MDEV-13240. Item::save_in_field()
returns an error during tmp table population in a create table from select query
as we try to save an empty string as a date value when
force_return_blank is set to true for window functions.

    MDEV-13240 Wrong warning with MAX(datetime_field) OVER (...)

    The problem resided in Item_window_func implementation,
    and it was revealed by bb-10.2-ext specific changes:

    Item_window_func::save_in_field() works differently in bb-10.2-ext vs 10.2:

    - 10.2 goes through val_str()
    - bb-10.2-ext goes through get_date(), due to Type_handler related changes.
      get_date() tries to convert empty string to DATETIME, hence the warning.

    During a discussion with Vicentiu, it was decided to fix
    Item_window_func::val_xxx() to return NULL
    (instead of an "empty" value, such as 0 for numbers and '' for strings)
    when force_return_blank is set.
2017-09-19 15:14:10 +03:00
Alexander Barkov
988a9daa94 Merge remote-tracking branch 'origin/10.2' into bb-10.2-ext
Conflicts:
	mysql-test/r/func_json.result
	mysql-test/r/win.result
	mysql-test/t/func_json.test
	mysql-test/t/win.test
	sql/share/errmsg-utf8.txt
	storage/rocksdb/ha_rocksdb.cc
	storage/rocksdb/mysql-test/rocksdb/r/tbl_opt_data_index_dir.result
2017-08-07 21:35:34 +04:00
Sergei Petrunia
0b30ce4f31 MDEV-13374: Server crashes in first_linear_tab / st_select_lex::set_explain_type
- Support first_linear_tab() traversal for degenerate joins
2017-08-07 16:04:38 +03:00
Alexander Barkov
4fc1f2fb75 Merge remote-tracking branch 'origin/10.2' into bb-10.2-ext 2017-07-22 22:48:45 +04:00
Alexander Barkov
23290e42e3 Merge commit 'e2afdb1ee430cb9d030aeeedc85eb903cda5e5d1' into bb-10.2-ext 2017-07-21 23:38:30 +04:00
Sergei Petrunia
2a1035b004 MDEV-13351: Server crashes in st_select_lex::set_explain_type upon UNION with window function
Make st_select_lex::set_explain_type() take into account that JOIN_TABs
it is traversing may be also post-join aggregation JOIN_TABs (which
have pos_in_table_list=NULL, etc).
2017-07-21 20:09:19 +03:00
Sergei Petrunia
e2afdb1ee4 MDEV-13344: Server crashes in in AGGR_OP::put_record on subquery
Add a testcase
2017-07-21 19:06:01 +03:00
Sergei Petrunia
17fc288b30 MDEV-13352: Server crashes in st_join_table::remove_duplicates
Do not run the window function computation step when the select
produces no rows (zero_result_cause!=NULL).
This may cause reads from uninitialized memory.

We still need to run the window function computation step when
the output includes just one row  (for example
SELECT MAX(col), RANK() OVER (...) FROM t1 WHERE 1=0).

This fix also resolves an issue with queries with window functions
producing an output row where should be none, like in
SELECT ROW_NUMBER() FROM t1 WHERE 1=0.

Updated a few test results in the existing tests to reflect this.
2017-07-21 13:53:58 +03:00
Alexander Barkov
8b2c7c9444 Merge remote-tracking branch 'origin/10.2' into bb-10.2-ext 2017-07-07 12:43:10 +04:00
Vicențiu Ciorbaru
4f93c732d5 MDEV-13189: Window functions crash when using INTERVAL
Interval function makes use of Item_row. Item_row did not correctly mark
with_window_func flag according to its arguments. Fix it by making
Item_row aware of this flag.
2017-07-05 15:40:25 +03:00
Alexander Barkov
5c0df0e4a8 Merge remote-tracking branch 'origin/10.2' into bb-10.2-ext 2017-07-04 15:31:25 +04:00
Alexander Barkov
25ad623d64 MDEV-13240 Wrong warning with MAX(datetime_field) OVER (...)
The problem resided in Item_window_func implementation,
and it was revealed by bb-10.2-ext specific changes:

Item_window_func::save_in_field() works differently in bb-10.2-ext vs 10.2:

- 10.2 goes through val_str()
- bb-10.2-ext goes through get_date(), due to Type_handler related changes.
  get_date() tries to convert empty string to DATETIME, hence the warning.

During a discussion with Vicentiu, it was decided to fix
Item_window_func::val_xxx() to return NULL
(instead of an "empty" value, such as 0 for numbers and '' for strings)
when force_return_blank is set.
2017-07-04 14:14:30 +04:00
Vicențiu Ciorbaru
31ba0fa48d MDEV-12851: Case with window functions query crashes server
The "is null" function performs one operation which no other Item_func
does, which is to update used tables during fix_length_and_dec().

This however can not be performed before window functions have had a
chance to resolve their order by and partition by definitions, which
happens after the initial setup_fields call. Consequently, do not call
Item_func_isnull update_used_tables during fix_length_and_dec().

There was another issue detected once the crash was resolved.
Because window functions did not implement is_null() method, we would
end up returning bad results for "is null" and "is not null" functions.
Implemented is_null() method for Item_windowfunc.
2017-06-28 19:21:31 +03:00
Vicențiu Ciorbaru
23edc7c88f MDEV-13186: main.win failure post MDEV-12336
During statement preparation st_order::item gets set to a value in
ref_ptr_array. During statement execution we were overriding that value,
causing subsequent checks for window functions to return true.

Whenever we do any setting from ref_ptr_array, make sure to always
store the value in all_fields as well.

For function items containing window functions, as MDEV-12336 has
discovered, we don't need to create a separate Item_direct_ref or
Item_aggregate_ref as they will be computed directly from the top-level
item once the window function argument columns are computed.
2017-06-28 15:23:36 +03:00
Igor Babaev
ce8ee7d90b Fixed the bug mdev-11990.
The usage of windows functions when all tables were optimized away
by min/max optimization were not supported. As result a result,
the queries that used window functions with min/max aggregation
over the whole table returned wrong result sets.
The patch fixed this problem.
2017-05-03 13:50:35 -07:00
Igor Babaev
4ebdef2bcd Fixed bug mdev-12336.
The function Item::split_sum_func2() incorrectly processed the function
items with window functions that were not window functions themselfes
and were used as arguments of other functions.
2017-03-29 08:46:00 -07:00
Igor Babaev
f381e73f7d Fixed bug mdev-11907.
With the current design the function copy_funcs() should ignore
the items with window functions from the array **func_ptr.
2017-03-29 08:46:00 -07:00
Igor Babaev
37925c6ccc Fixed bug mdev-9924.
Supported queries with window functions when GROUP BY could be
optimized away.
2017-02-15 22:41:45 -08:00
Vicențiu Ciorbaru
eb54d86b58 MDEV-10700: 10.2.2 windowing function returns incorrect result
This bug is fixed by MDEV-10092. Add test case to check for regressions.
2017-02-15 14:09:27 +02:00
Vicențiu Ciorbaru
d06a44e027 MDEV-11868: min ( distinct ) over ( ) returns wrong value
The bug was not visible in current HEAD. Introduced test case to catch
regressions. Also improve error messages regarding distinct usage in
window functions.
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
Igor Babaev
d35aea5407 Fixed bugs mdev-12051, mdev-10885.
These are different bugs, but the fixing code is the same:
if window functions are used over implicit grouping then
now the execution should follow the general path calling
the function set in JOIN::first_select.
2017-02-12 15:50:14 -08:00
Igor Babaev
5593458062 Fixed bug mdev-12015.
Corrected an assertion in JOIN::create_postjoin_aggr_table():
JOIN::join_tab[0] can be the first aggregation table if
the query uses window functions.
2017-02-10 17:21:45 -08:00
Igor Babaev
78b5e8d6ca Fixed bug mdev-11745.
Due to this bug many queries that contained a window function
with MIN/MAX aggregation returned wrong results.

Calculation of a MIN/MAX aggregate function uses cache objects
and a comparator object that are created and set up in
Item_sum_hybrid::fix_fields () by a call of Item_sum_hybrid::setup_hybrid().
The latter binds the objects to the first argument of the
MIN/MAX function. Meanwhile window function perform aggregation
over fields of a temporary table. So binding must be done rather to
these fields. The earliest moment when setup the objects used in
MIN/max functions can be done is after all calls of the method
split_sum_func().

This patch introduces this late setup, but only for aggregate
functions used in window functions.
Probably it makes sense to use this late setup for all MIN/MAX
objects.
2017-02-09 19:34:01 -08:00
Igor Babaev
c16c9e8e76 Fixed bug mdev-11999.
This patch complements the patch for bug 11138.
Without this patch some table-less queries with window functions
could cause crashes due to a memory overwrite.
2017-02-06 22:12:53 -08: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
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
fd7accabbb Fixed bug mdev-9923.
Partition and order lists of a window specification cannot
use constant integer to refer to select list elements.
2017-02-03 00:10:36 -08:00
Igor Babaev
5606f878bf Fixed bug mdev-11594.
Using window functions over results of implicit groupings
required special handling in JOIN::make_aggr_tables_info.
The patch made sure that the result of implicit grouping
was written into a temporary table properly.
2017-02-02 18:01:35 -08:00
Igor Babaev
8481c70ede Fixed bug mdev-11867.
If a window function with aggregation is over the result
set of a grouping query then the argument of the aggregate
function from the window function is allowed to be an
aggregate function itself.
2017-02-01 19:15:28 -08:00
Igor Babaev
9073f9fd7d Fixed bug mdev-9976.
This bug happens due to a conflict in the construct window_spec.
(win_ref conflicts with the non-reserved key word ROWS).
The standard SQL-2003 says that ROWS is a reserved key word.
Made this key word reserved in our grammar and removed
the conflict.
2017-01-31 20:37:26 -08:00
Igor Babaev
81c1abe8cf Fixed bug mdev-10875.
Now, after the implementation of mdev-8646
"Re-engineer the code for post-join operations"
create_sort_index() can be called for subqueries.
2017-01-31 20:37:25 -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
29b227c335 Cleanup win testcase to always be deterministic
Also remove some whitespace
2016-09-24 15:12:34 +02:00
Vicențiu Ciorbaru
5cb568786a MDEV-10669: Crash in SELECT with window function used
Make sure to call split_sum_func on all items that contain window
functions, so that all the column references are set up correctly.
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
Igor Babaev
457f3b9941 Added the test case for bug mdev-9941 that was fixed some time ago. 2016-09-23 14:42:12 -07: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
be2b833c42 It is now possible to remove values multiple times from window functions
This can happen with degenerate frame definitions that are always empty.
2016-09-09 18:53:01 +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
Galina Shalygina
be1d06c8a5 Merge branch '10.2' into 10.2-mdev9864 2016-05-08 23:04:41 +03:00