[MDEV-14032] SEC_TO_TIME executes side effect two times Created: 2017-10-09  Updated: 2018-08-09  Resolved: 2018-08-09

Status: Closed
Project: MariaDB Server
Component/s: OTHER
Affects Version/s: 5.5, 10.0, 10.1, 10.2, 10.3
Fix Version/s: 10.4.0

Type: Bug Priority: Major
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-13972 crash in Item_func_sec_to_time::get_date Closed

 Description   

Item_func_sec_to_time::get_date() executes val_str() to print warnings. So value methods for args[0] are executed two times:
1. val_int() or val_decimal() in get_deconds(), to get the "sec + sec_part" pair.
2. val_str(), to generate a warning, in case of overflow.

This looks wrong, especially for functions with side effect.

Example 1:

I run this script:

SET @a=10000000;
SELECT SEC_TO_TIME(@a:=@a+1);
SELECT @a;

It returns 838:59:59 from the first SELECT, with a warning as expected. But then the second SELECT returns a wrong result:

+----------+
| @a       |
+----------+
| 10000002 |
+----------+

The expected result should be 10000001.

Example 2:

SELECT SEC_TO_TIME(LAST_VALUE(SLEEP(0.33),10000000));

+-----------------------------------------------+
| SEC_TO_TIME(LAST_VALUE(SLEEP(0.33),10000000)) |
+-----------------------------------------------+
| 838:59:59                                     |
+-----------------------------------------------+
1 row in set, 1 warning (0.66 sec)

Notice, it says 0.66 sec in the query statistics. The expected timing should be 0.33 sec, as in SLEEP.

Example 3:

DROP TABLE IF EXISTS t1;
DROP FUNCTION IF EXISTS f1;
CREATE TABLE t1 (a TEXT);
DELIMITER $$
CREATE FUNCTION f1() RETURNS INT
BEGIN
  INSERT INTO t1 VALUES ('f1 was called');
  RETURN 10000000;
END;
$$
DELIMITER ;
SELECT SEC_TO_TIME(f1());
SELECT * FROM t1;

+---------------+
| a             |
+---------------+
| f1 was called |
| f1 was called |
+---------------+

Notice, the INSERT happened two times. The expected behavior would be to do INSERT only once.


Generated at Thu Feb 08 08:10:23 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.