Bug #121970 SQL_BIG_RESULT GROUP BY repeats stored-function side effects

Submitted: 28 Nov 10:16
Reporter: QiFan Liu Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:9.7.2 OS:Linux
Assigned to: CPU Architecture:x86
Tags: GROUP BY, hypergraph optimizer, SQL_BIG_RESULT, stored function

[28 Nov 10:16] QiFan Liu
Description:
With the hypergraph optimizer enabled, SQL_BIG_RESULT causes a stored function used as a GROUP BY expression to be evaluated 11 times for four input rows. Each evaluation inserts a row into a log table, so the query produces duplicated persistent side effects.

The query still returns the correct groups: 1, 2, 3.

The observed call arguments are:

1,1,2,3,1,1,2,1,3,2,3

Using SQL_SMALL_RESULT with the same data and function returns the same groups but evaluates the function four times, with arguments:

1,1,2,3

How to repeat:
CREATE DATABASE db1;
USE db1;

SET SESSION optimizer_switch='hypergraph_optimizer=on,hash_set_operations=off';

CREATE TABLE call_log (
  seq INT AUTO_INCREMENT PRIMARY KEY,
  arg_value INT
);
CREATE TABLE t1 (x INT);
INSERT INTO t1 VALUES (1), (1), (2), (3);

DELIMITER //
CREATE FUNCTION f1(p INT)
RETURNS INT DETERMINISTIC MODIFIES SQL DATA
BEGIN
  INSERT INTO call_log(arg_value) VALUES (p);
  RETURN p;
END//
DELIMITER ;

SELECT SQL_SMALL_RESULT f1(x) AS f
FROM t1
GROUP BY f;
-- returns 1, 2, 3

SELECT COUNT(*) AS function_calls,
       GROUP_CONCAT(arg_value ORDER BY seq) AS call_arguments
FROM call_log;
-- returns: 4 | 1,1,2,3

TRUNCATE TABLE call_log;

SELECT SQL_BIG_RESULT f1(x) AS f
FROM t1
GROUP BY f;
-- returns 1, 2, 3

SELECT COUNT(*) AS function_calls,
       GROUP_CONCAT(arg_value ORDER BY seq) AS call_arguments
FROM call_log;
-- actual:   11 | 1,1,2,3,1,1,2,1,3,2,3
-- expected:  4 | 1,1,2,3

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

Page generated in 0.049 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.