Description:
With derived_condition_pushdown=on (the default), an outer WHERE predicate on
a column of a view or derived table is moved below the window functions when
the column is in every PARTITION BY, and below GROUP BY when the column is a
grouping column. That is only correct if the predicate has the same value for
all rows that the partition or group puts together. A predicate that is
finer than the column's collation does not: on a column with a case- or
accent-insensitive collation (the default utf8mb4_0900_ai_ci, any _ci
collation), the common case-sensitive lookup
customer = 'Ann' COLLATE utf8mb4_bin
(also BINARY customer = 'Ann', HEX(customer) = ..., LIKE BINARY) removes
'ann' and 'ANN' from the partition or group before the aggregates are
computed.
- Window view: every 'Ann' order reports customer_total 40 and
customer_orders 2, although the customer ('Ann', 'ann', 'ANN') has 4
orders and a total of 100, which is what the same query returns with the
NO_DERIVED_CONDITION_PUSHDOWN hint. Filtering the rows of a window result
cannot change the window values of the rows that remain.
- GROUP BY view: the filtered query returns (n, total) = (2, 40), a row the
view does not contain (its rows are ('Ann', 4, 100) and ('Bob', 2, 12));
with the hint it returns ('Ann', 4, 100).
There is no error or warning. CREATE TABLE ... SELECT and INSERT ... SELECT
store the wrong totals, and a DELETE driven by the window count deletes an
order of a customer who has four (below: "delete the order of customers with
a single order", restricted to the exact spelling 'ANN', deletes order 4).
The pushdown code already knows the rule for non-deterministic predicates:
the comment in Item_func::check_column_in_window_functions() says that
pushing them "may result in eliminating rows which would have otherwise
contributed to aggregations". A comparison in a finer collation has the same
effect.
Bug #121040 (Verified) reports this kind of predicate for pushdown into the
query blocks of a set operation (EXCEPT/INTERSECT, whose de-duplication then
picks another representative). The GROUP BY and window cases here go through
push_past_group_by() and push_past_window_functions(), so a fix limited to set
operations would not cover them.
How to repeat:
-- run with: mysql -uroot --table < file
CREATE DATABASE m209;
USE m209;
SET NAMES utf8mb4;
-- default collation utf8mb4_0900_ai_ci: 'Ann', 'ann' and 'ANN' are the same customer
CREATE TABLE orders (id INT PRIMARY KEY, customer VARCHAR(20), amount INT);
INSERT INTO orders VALUES (1,'Ann',10), (2,'ann',20), (3,'Ann',30), (4,'ANN',40), (5,'Bob',5), (6,'bob',7);
CREATE VIEW order_shares AS
SELECT id, customer, amount,
SUM(amount) OVER (PARTITION BY customer) AS customer_total,
COUNT(*) OVER (PARTITION BY customer) AS customer_orders
FROM orders;
CREATE VIEW customer_totals AS
SELECT customer, COUNT(*) AS n, SUM(amount) AS total FROM orders GROUP BY customer;
SELECT * FROM order_shares ORDER BY id;
SELECT * FROM customer_totals ORDER BY customer;
-- 1. window view, case-sensitive filter on the PARTITION BY column
SELECT id, customer_total, customer_orders FROM order_shares
WHERE customer = 'Ann' COLLATE utf8mb4_bin ORDER BY id;
SELECT /*+ NO_DERIVED_CONDITION_PUSHDOWN(order_shares) */ id, customer_total, customer_orders
FROM order_shares WHERE customer = 'Ann' COLLATE utf8mb4_bin ORDER BY id;
EXPLAIN FORMAT=TREE SELECT id, customer_total FROM order_shares WHERE customer = 'Ann' COLLATE utf8mb4_bin;
-- 2. GROUP BY view, the same filter on the GROUP BY column
SELECT customer, n, total FROM customer_totals WHERE customer = 'Ann' COLLATE utf8mb4_bin;
SELECT /*+ NO_DERIVED_CONDITION_PUSHDOWN(customer_totals) */ customer, n, total
FROM customer_totals WHERE customer = 'Ann' COLLATE utf8mb4_bin;
SELECT customer, n, total FROM customer_totals WHERE BINARY customer = 'Ann';
-- 3. persisted
CREATE TABLE ann_report AS
SELECT id, customer_total FROM order_shares WHERE customer = 'Ann' COLLATE utf8mb4_bin;
SELECT * FROM ann_report ORDER BY id;
CREATE TABLE invoice (customer VARCHAR(20), n INT, total INT);
INSERT INTO invoice SELECT customer, n, total FROM customer_totals WHERE customer = 'Ann' COLLATE utf8mb4_bin;
SELECT * FROM invoice;
-- clean-up: delete the order of a customer who has a single order, for the exact spelling 'ANN'
DELETE FROM orders WHERE id IN (SELECT id FROM (SELECT id FROM order_shares
WHERE customer = 'ANN' COLLATE utf8mb4_bin AND customer_orders = 1) d);
SELECT id FROM orders ORDER BY id;
Result on 26.10.0 (8.4.13 gives the same):
order_shares: every Ann/ann/ANN order has customer_total 100, customer_orders 4;
Bob/bob orders 12, 2
customer_totals: ('Ann', 4, 100), ('Bob', 2, 12)
1. window view, default: (1, 40, 2), (3, 40, 2) <- wrong
with NO_DERIVED_CONDITION_PUSHDOWN: (1, 100, 4), (3, 100, 4)
EXPLAIN: the filter is below both window aggregates
-> Table scan on order_shares (cost=1.81..3.96 rows=6)
-> Materialize (cost=1.38..1.38 rows=6)
-> Window aggregate with buffering: count(0) OVER (PARTITION BY orders.customer ) (cost=0 rows=6)
-> Table scan on <temporary> (cost=1.81..3.96 rows=6)
-> Temporary table (cost=1.38..1.38 rows=6)
-> Window aggregate with buffering: sum(orders.amount) OVER (PARTITION BY orders.customer ) (cost=0 rows=6)
-> Sort: orders.customer (cost=0.85 rows=6)
-> Filter: (orders.customer = <cache>(('Ann' collate utf8mb4_bin))) (cost=0.85 rows=6)
-> Table scan on orders (cost=0.85 rows=6)
2. GROUP BY view, default: ('Ann', 2, 40) <- not a row of the view
with NO_DERIVED_CONDITION_PUSHDOWN: ('Ann', 4, 100)
WHERE BINARY customer = 'Ann': ('Ann', 2, 40) <- not a row of the view
3. ann_report: (1, 40), (3, 40) <- expected (1, 100), (3, 100)
invoice: ('Ann', 2, 40) <- expected ('Ann', 4, 100)
orders after the DELETE: 1, 2, 3, 5, 6 <- order 4 deleted; the customer has 4 orders
Expected: the results with the NO_DERIVED_CONDITION_PUSHDOWN hint (the
predicate evaluated on the view's rows): (1, 100, 4), (3, 100, 4);
('Ann', 4, 100); ann_report (1, 100), (3, 100); invoice ('Ann', 4, 100);
the DELETE deletes nothing.
Suggested fix:
Condition_pushdown::push_past_window_functions() and push_past_group_by()
move the parts of the condition whose columns are all PARTITION BY / GROUP BY
columns below the windowing / grouping:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/sq...
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/sq...
The tests are syntactic: Item_field::check_column_in_window_functions() and
Item_field::check_column_in_group_by() only check that the column is one of
the partitioning / grouping expressions, and the Item_func versions only
refuse non-deterministic functions:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/it...
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/it...
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/it...
A predicate should be moved below the grouping only if it is constant on the
grouping's equality classes: every comparison that involves the column uses
the column's own collation, and the column is not passed to a function that
looks at its bytes or length (HEX, LENGTH, BINARY / CAST ... AS BINARY,
COLLATE). Otherwise the predicate should stay above (in the outer query or
the derived table's HAVING after the aggregation).
Workaround: the NO_DERIVED_CONDITION_PUSHDOWN hint, or
optimizer_switch='derived_condition_pushdown=off'.
Description: With derived_condition_pushdown=on (the default), an outer WHERE predicate on a column of a view or derived table is moved below the window functions when the column is in every PARTITION BY, and below GROUP BY when the column is a grouping column. That is only correct if the predicate has the same value for all rows that the partition or group puts together. A predicate that is finer than the column's collation does not: on a column with a case- or accent-insensitive collation (the default utf8mb4_0900_ai_ci, any _ci collation), the common case-sensitive lookup customer = 'Ann' COLLATE utf8mb4_bin (also BINARY customer = 'Ann', HEX(customer) = ..., LIKE BINARY) removes 'ann' and 'ANN' from the partition or group before the aggregates are computed. - Window view: every 'Ann' order reports customer_total 40 and customer_orders 2, although the customer ('Ann', 'ann', 'ANN') has 4 orders and a total of 100, which is what the same query returns with the NO_DERIVED_CONDITION_PUSHDOWN hint. Filtering the rows of a window result cannot change the window values of the rows that remain. - GROUP BY view: the filtered query returns (n, total) = (2, 40), a row the view does not contain (its rows are ('Ann', 4, 100) and ('Bob', 2, 12)); with the hint it returns ('Ann', 4, 100). There is no error or warning. CREATE TABLE ... SELECT and INSERT ... SELECT store the wrong totals, and a DELETE driven by the window count deletes an order of a customer who has four (below: "delete the order of customers with a single order", restricted to the exact spelling 'ANN', deletes order 4). The pushdown code already knows the rule for non-deterministic predicates: the comment in Item_func::check_column_in_window_functions() says that pushing them "may result in eliminating rows which would have otherwise contributed to aggregations". A comparison in a finer collation has the same effect. Bug #121040 (Verified) reports this kind of predicate for pushdown into the query blocks of a set operation (EXCEPT/INTERSECT, whose de-duplication then picks another representative). The GROUP BY and window cases here go through push_past_group_by() and push_past_window_functions(), so a fix limited to set operations would not cover them. How to repeat: -- run with: mysql -uroot --table < file CREATE DATABASE m209; USE m209; SET NAMES utf8mb4; -- default collation utf8mb4_0900_ai_ci: 'Ann', 'ann' and 'ANN' are the same customer CREATE TABLE orders (id INT PRIMARY KEY, customer VARCHAR(20), amount INT); INSERT INTO orders VALUES (1,'Ann',10), (2,'ann',20), (3,'Ann',30), (4,'ANN',40), (5,'Bob',5), (6,'bob',7); CREATE VIEW order_shares AS SELECT id, customer, amount, SUM(amount) OVER (PARTITION BY customer) AS customer_total, COUNT(*) OVER (PARTITION BY customer) AS customer_orders FROM orders; CREATE VIEW customer_totals AS SELECT customer, COUNT(*) AS n, SUM(amount) AS total FROM orders GROUP BY customer; SELECT * FROM order_shares ORDER BY id; SELECT * FROM customer_totals ORDER BY customer; -- 1. window view, case-sensitive filter on the PARTITION BY column SELECT id, customer_total, customer_orders FROM order_shares WHERE customer = 'Ann' COLLATE utf8mb4_bin ORDER BY id; SELECT /*+ NO_DERIVED_CONDITION_PUSHDOWN(order_shares) */ id, customer_total, customer_orders FROM order_shares WHERE customer = 'Ann' COLLATE utf8mb4_bin ORDER BY id; EXPLAIN FORMAT=TREE SELECT id, customer_total FROM order_shares WHERE customer = 'Ann' COLLATE utf8mb4_bin; -- 2. GROUP BY view, the same filter on the GROUP BY column SELECT customer, n, total FROM customer_totals WHERE customer = 'Ann' COLLATE utf8mb4_bin; SELECT /*+ NO_DERIVED_CONDITION_PUSHDOWN(customer_totals) */ customer, n, total FROM customer_totals WHERE customer = 'Ann' COLLATE utf8mb4_bin; SELECT customer, n, total FROM customer_totals WHERE BINARY customer = 'Ann'; -- 3. persisted CREATE TABLE ann_report AS SELECT id, customer_total FROM order_shares WHERE customer = 'Ann' COLLATE utf8mb4_bin; SELECT * FROM ann_report ORDER BY id; CREATE TABLE invoice (customer VARCHAR(20), n INT, total INT); INSERT INTO invoice SELECT customer, n, total FROM customer_totals WHERE customer = 'Ann' COLLATE utf8mb4_bin; SELECT * FROM invoice; -- clean-up: delete the order of a customer who has a single order, for the exact spelling 'ANN' DELETE FROM orders WHERE id IN (SELECT id FROM (SELECT id FROM order_shares WHERE customer = 'ANN' COLLATE utf8mb4_bin AND customer_orders = 1) d); SELECT id FROM orders ORDER BY id; Result on 26.10.0 (8.4.13 gives the same): order_shares: every Ann/ann/ANN order has customer_total 100, customer_orders 4; Bob/bob orders 12, 2 customer_totals: ('Ann', 4, 100), ('Bob', 2, 12) 1. window view, default: (1, 40, 2), (3, 40, 2) <- wrong with NO_DERIVED_CONDITION_PUSHDOWN: (1, 100, 4), (3, 100, 4) EXPLAIN: the filter is below both window aggregates -> Table scan on order_shares (cost=1.81..3.96 rows=6) -> Materialize (cost=1.38..1.38 rows=6) -> Window aggregate with buffering: count(0) OVER (PARTITION BY orders.customer ) (cost=0 rows=6) -> Table scan on <temporary> (cost=1.81..3.96 rows=6) -> Temporary table (cost=1.38..1.38 rows=6) -> Window aggregate with buffering: sum(orders.amount) OVER (PARTITION BY orders.customer ) (cost=0 rows=6) -> Sort: orders.customer (cost=0.85 rows=6) -> Filter: (orders.customer = <cache>(('Ann' collate utf8mb4_bin))) (cost=0.85 rows=6) -> Table scan on orders (cost=0.85 rows=6) 2. GROUP BY view, default: ('Ann', 2, 40) <- not a row of the view with NO_DERIVED_CONDITION_PUSHDOWN: ('Ann', 4, 100) WHERE BINARY customer = 'Ann': ('Ann', 2, 40) <- not a row of the view 3. ann_report: (1, 40), (3, 40) <- expected (1, 100), (3, 100) invoice: ('Ann', 2, 40) <- expected ('Ann', 4, 100) orders after the DELETE: 1, 2, 3, 5, 6 <- order 4 deleted; the customer has 4 orders Expected: the results with the NO_DERIVED_CONDITION_PUSHDOWN hint (the predicate evaluated on the view's rows): (1, 100, 4), (3, 100, 4); ('Ann', 4, 100); ann_report (1, 100), (3, 100); invoice ('Ann', 4, 100); the DELETE deletes nothing. Suggested fix: Condition_pushdown::push_past_window_functions() and push_past_group_by() move the parts of the condition whose columns are all PARTITION BY / GROUP BY columns below the windowing / grouping: https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/sq... https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/sq... The tests are syntactic: Item_field::check_column_in_window_functions() and Item_field::check_column_in_group_by() only check that the column is one of the partitioning / grouping expressions, and the Item_func versions only refuse non-deterministic functions: https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/it... https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/it... https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/it... A predicate should be moved below the grouping only if it is constant on the grouping's equality classes: every comparison that involves the column uses the column's own collation, and the column is not passed to a function that looks at its bytes or length (HEX, LENGTH, BINARY / CAST ... AS BINARY, COLLATE). Otherwise the predicate should stay above (in the outer query or the derived table's HAVING after the aggregation). Workaround: the NO_DERIVED_CONDITION_PUSHDOWN hint, or optimizer_switch='derived_condition_pushdown=off'.