[MDEV-23525] Wrong result of MIN(time_expr) and MAX(time_expr) with GROUP BY Created: 2020-08-21  Updated: 2023-07-19  Resolved: 2020-08-22

Status: Closed
Project: MariaDB Server
Component/s: Data types, Temporal Types
Affects Version/s: 10.1, 10.2, 10.3, 10.4, 10.5
Fix Version/s: 10.4.16, 10.5.7

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

Issue Links:
Problem/Incident
causes MDEV-23838 Possibly wrong result or Assertion `0... Closed
Relates
relates to MDEV-23537 Comparison with temporal columns is s... Closed
relates to MDEV-23551 Performance degratation in temporal l... Closed

 Description   

I run this script:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (i INT, d TIME);
INSERT INTO t1 VALUES (1,'10:20:30');
INSERT INTO t1 VALUES (1,'100:20:20');
SELECT MIN(d), MAX(d) FROM t1;
SELECT i, MIN(d), MAX(d) FROM t1 GROUP BY i;

The first SELECT returns:

+----------+-----------+
| MIN(d)   | MAX(d)    |
+----------+-----------+
| 10:20:30 | 100:20:20 |
+----------+-----------+

Looks correct.

The second SELECT returns:

+------+-----------+----------+
| i    | MIN(d)    | MAX(d)   |
+------+-----------+----------+
|    1 | 100:20:20 | 10:20:30 |
+------+-----------+----------+

Looks wrong. The MIN and MAX values swapped.

The problem happens in this code:

void Item_sum_min_max::update_field()
{
...
    switch (Item_sum_min_max::type_handler()->cmp_type()) {
    case STRING_RESULT:
    case TIME_RESULT:
      min_max_update_str_field();

Notice, it compares TIME values in text format.



 Comments   
Comment by Alexander Barkov [ 2020-08-22 ]

Benchmarks:

TIME(0)

# Create tables:
# t3 with 10 records
# t2 with 1000 records
 
DROP TABLE IF EXISTS t3,t2;
CREATE TABLE t3 (a INT);
INSERT INTO t3 VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
CREATE TABLE t2 AS SELECT 1 FROM t3 t3a, t3 t3b, t3 t3c;
 
# Create table t1 with 1 million records
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (id INT, a TIME) ENGINE=HEAP;
INSERT INTO t1 SELECT 1, '10:10:10' FROM t2,t2 t2a;
SELECT id, MAX(a) FROM t1 GROUP BY id;
 
# My80: 0.153
# 10.3: 0.108
# 10.4: 0.093 (fixed)

TIME(6)

# Create tables:
# t3 with 10 records
# t2 with 1000 records
 
DROP TABLE IF EXISTS t3,t2;
CREATE TABLE t3 (a INT);
INSERT INTO t3 VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
CREATE TABLE t2 AS SELECT 1 FROM t3 t3a, t3 t3b, t3 t3c;
 
# Create table t1 with 1 million records
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (id INT, a TIME(6)) ENGINE=HEAP;
INSERT INTO t1 SELECT 1, '10:10:10.999999' FROM t2,t2 t2a;
SELECT id, MAX(a) FROM t1 GROUP BY id;
 
# My80: 0.154
# 10.3: 0.135
# 10.4: 0.093 (fixed)

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