Bug #100670 Undescribed behavior of INTERVAL function with NULL

Submitted: 28 Aug 2020 6:51 Modified: 28 Aug 2020 7:10
Reporter: Yushan ZHANG Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:8.0.21, 5.7.31, 5.6.48 OS:Any
Assigned to: CPU Architecture:Any
Tags: documentation, interval, null

[28 Aug 2020 6:51] Yushan ZHANG
Description:
Documentation says:

````
Returns 0 if N < N1, 1 if N < N2 and so on or -1 if N is NULL. All arguments are treated as integers. It is required that N1 < N2 < N3 < ... < Nn for this function to work correctly. This is because a binary search is used (very fast).
````

However, it doesn't describe the result when N1...Nx are nulls:

-- -1 < 1 is correct, however `null < -1 < 1` is incorrect
mysql> select interval( -1, null, 1);
+------------------------+                                                                                                                                                                                                                                                                                                                                                                                                                                                                | interval( -1, null, 1) |                                                                                                                                                                                                                                            +------------------------+                                                                                                                                                                                                                                            |                      1 |                                                                                                                                                                                                                                            +------------------------+                                                                                                                                                                                                                                            
1 row in set (0.00 sec)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
mysql> select interval( -1, null, null);                                                                                                                                                                                                                            +---------------------------+                                                                                                                                                                                                                                         
| interval( -1, null, null) |                                                                                                                                                                                                                                         +---------------------------+                                                                                                                                                                                                                                         
|                         2 |                                                                                                                                                                                                                                         +---------------------------+                                                                                                                                                                                                                                         
1 row in set (0.00 sec)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
mysql> select interval( -1, null, null, null);                                                                                                                                                                                                                        +---------------------------------+                                                                                                                                                                                                                                   
| interval( -1, null, null, null) |                                                                                                                                                                                                                                   
+---------------------------------+
|                               3 |
+---------------------------------+
1 row in set (0.00 sec)

I would expect the second and third query to return NULL as the result.

How to repeat:
select interval( -1, null, 1);
select interval( -1, null, null);   
select interval( -1, null, null, null);  

Suggested fix:
I think either of the following would be acceptable:

1. update the documentation for INTERVAL
2. let the function return NULL when the args are all nulls

I have a quick check on the function source code, it seems it uses literal `<=` to do the comparison.
[28 Aug 2020 7:10] MySQL Verification Team
Hello Yushan ZHANG,

Thank you for the report and feedback.

regards,
Umesh

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.