Bug #121844 Hypergraph optimizer: IN-subquery LooseScan dedups by the inner column's collation/type, not the comparison's

Submitted: 20 Nov 1:10
Reporter: Ke Han Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:26.10.0 (trunk 06bcb0eb0ad) OS:Any
Assigned to: CPU Architecture:Any

[20 Nov 1:10] Ke Han
Description:
With hypergraph_optimizer=on, a semijoin such as outer_col IN (SELECT
inner_col ...) can be planned as an inner join over the inner rows with
duplicates removed on inner_col ("Nested loop inner join (LooseScan)" over
"Remove duplicates from input grouped on <inner_col>"). The duplicate removal
compares inner_col with inner_col's own collation and type, but the join
condition compares outer_col = inner_col with the comparison's collation and
type. When the two notions of equality differ, the rewrite is not
equivalent:

(a) Coarser inner column: accounts.code is utf8mb4_bin, revoked.code has the
    default collation utf8mb4_0900_ai_ci. The comparison uses utf8mb4_bin
    (the manual: "For an operation with operands from the same character set
    but that mix a _bin collation and a _ci or _cs collation, the _bin
    collation is used"), so 'ab' and 'AB' are different values and both
    match. The duplicate removal merges 'ab' and 'AB' (and 'cd' and 'CD')
    into one row each, so the join finds only the accounts spelled like the
    kept row: 500 of the 1000 matching accounts.

(b) Finer inner column: o2.n is INT and i2.s is VARCHAR, so the comparison is
    numeric (cast(o2.n as double) = cast(i2.s as double) in the plan) and
    '1', '1.0' and '01' all equal 1. The duplicate removal keeps the three
    strings, so every outer row with n = 1 is returned three times
    (16 rows instead of 8).

The classic optimizer returns the right rows for the same statements, and
so does the hypergraph optimizer with loosescan=off. There is no error or
warning. CREATE TABLE ... SELECT and INSERT ... SELECT store the wrong rows
(500 of the 1000 matching accounts below). The same happens with
EXISTS (SELECT 1 FROM revoked r WHERE r.code = a.code).

How to repeat:
-- run with: mysql -uroot --table < file
CREATE DATABASE m208;
USE m208;
SET SESSION cte_max_recursion_depth = 100000;
-- case-sensitive account codes
CREATE TABLE accounts (id INT PRIMARY KEY, code VARCHAR(10) COLLATE utf8mb4_bin, KEY (code));
INSERT INTO accounts WITH RECURSIVE s(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM s WHERE n < 2000)
  SELECT n, ELT(1 + n % 8, 'ab', 'AB', 'Ab', 'cd', 'CD', 'ef', CONCAT('z', n), CONCAT('Z', n)) FROM s;
-- revoked codes, default collation (utf8mb4_0900_ai_ci)
CREATE TABLE revoked (id INT PRIMARY KEY, code VARCHAR(10), KEY (code));
INSERT INTO revoked WITH RECURSIVE s(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM s WHERE n < 300)
  SELECT n, ELT(1 + n % 4, 'ab', 'AB', 'cd', 'CD') FROM s;
-- numeric comparison of an INT with VARCHAR values '1', '1.0', '01', '2'
CREATE TABLE o2 (id INT PRIMARY KEY, n INT, KEY (n));
CREATE TABLE i2 (id INT PRIMARY KEY, s VARCHAR(10), KEY (s));
INSERT INTO o2 WITH RECURSIVE s(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM s WHERE n < 2000)
  SELECT n, n % 500 FROM s;
INSERT INTO i2 WITH RECURSIVE s(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM s WHERE n < 300)
  SELECT n, ELT(1 + n % 4, '1', '1.0', '01', '2') FROM s;
ANALYZE TABLE accounts, revoked, o2, i2;

-- classic optimizer (default)
SELECT COUNT(*), SUM(id) FROM accounts WHERE code IN (SELECT code FROM revoked);
SELECT code, COUNT(*) FROM accounts WHERE code IN (SELECT code FROM revoked) GROUP BY code ORDER BY code;
SELECT COUNT(*), SUM(id) FROM o2 WHERE n IN (SELECT s FROM i2);

SET SESSION optimizer_switch = 'hypergraph_optimizer=on';
SELECT COUNT(*), SUM(id) FROM accounts WHERE code IN (SELECT code FROM revoked);
SELECT code, COUNT(*) FROM accounts WHERE code IN (SELECT code FROM revoked) GROUP BY code ORDER BY code;
EXPLAIN FORMAT=TREE SELECT COUNT(*), SUM(id) FROM accounts WHERE code IN (SELECT code FROM revoked);
CREATE TABLE revoked_accounts AS SELECT id, code FROM accounts WHERE code IN (SELECT code FROM revoked);
SELECT COUNT(*) FROM revoked_accounts;
SELECT COUNT(*), SUM(id) FROM o2 WHERE n IN (SELECT s FROM i2);
SELECT id, COUNT(*) FROM o2 WHERE n IN (SELECT s FROM i2) GROUP BY id HAVING COUNT(*) > 1 ORDER BY id;
EXPLAIN FORMAT=TREE SELECT COUNT(*), SUM(id) FROM o2 WHERE n IN (SELECT s FROM i2);

Result on 26.10.0:
  classic optimizer:
    accounts IN:            COUNT 1000, SUM 1000000
    per code:               AB 250, CD 250, ab 250, cd 250
    o2 IN:                  COUNT 8, SUM 6012
  hypergraph_optimizer=on:
    accounts IN:            COUNT 500, SUM 499000            <- wrong
    per code:               AB 250, cd 250                   <- 'ab' and 'CD' accounts missing
    EXPLAIN:
      -> Aggregate: count(0), sum(accounts.id)  (cost=1526..1526 rows=1)
          -> Nested loop inner join (LooseScan)  (cost=0.562..1125 rows=2000)
              -> Remove duplicates from input grouped on revoked.`code`  (cost=93.1..186 rows=2)
                  -> Covering index scan on revoked using code  (cost=0.391..117 rows=300)
              -> Filter: (accounts.`code` = revoked.`code`)  (cost=0.938..469 rows=500)
                  -> Covering index lookup on accounts using code (code = revoked.`code`)  (cost=0.412..412 rows=1000)
    revoked_accounts:       500 rows                         <- 1000 expected
    o2 IN:                  COUNT 16, SUM 12020              <- wrong
    rows returned 3 times:  id 1, 501, 1001, 1501
    EXPLAIN:
      -> Aggregate: count(0), sum(o2.id)  (cost=1364..1364 rows=1)
          -> Nested loop inner join (LooseScan)  (cost=0.481..963 rows=2000)
              -> Remove duplicates from input grouped on i2.s  (cost=46.6..186 rows=4)
                  -> Covering index scan on i2 using s  (cost=0.391..117 rows=300)
              -> Filter: (cast(o2.n as double) = cast(i2.s as double))  (cost=0.388..194 rows=500)
                  -> Covering index lookup on o2 using n (n = i2.s)  (cost=0.331..165 rows=500)

Expected: the classic optimizer's results (1000 / 1000000 with all four
spellings, 1000 rows in revoked_accounts; 8 / 6012 with no row returned twice);
the hypergraph optimizer with optimizer_switch 'hypergraph_optimizer=on,
loosescan=off' returns these.

Suggested fix:
The grouping used for the semijoin-to-inner-join rewrite is built from the
bare inner argument of each equijoin condition, without looking at how the
condition compares:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/jo...
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/jo...
DeduplicateForSemijoin() then removes duplicates on these items
(NewRemoveDuplicatesAccessPath(), or a sort with remove_duplicates), which
compares them with their own collation and type:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/jo...
(called for hash joins and nested-loop joins at lines 5482 and 6192).
The rewrite is only valid when equality of the inner item under its own
collation and type is the same as the equality of the join condition. Not
grouping on an equijoin condition whose comparison collation or comparison
type differs from the inner item's (keeping a semijoin strategy for it), or
deduplicating on the comparison's semantics, would fix both cases.

Workaround: SET optimizer_switch = 'hypergraph_optimizer=on,loosescan=off'
(or the classic optimizer).

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.042 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.