pingcap / pingcap/tidb

Wrong result of LEAST function in TiDB with MySQL

Open
#58,927 2 comments 0 reactions 0 assignees View on GitHub
affects-8.5 severity/minor sig/execution type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)

```sql
DROP TABLE IF EXISTS t0;
CREATE TABLE IF NOT EXISTS t0(c0 TINYINT(200) ZEROFILL PRIMARY KEY UNIQUE) ;
INSERT IGNORE INTO t0(c0) VALUES(-149547112);
SELECT SUM(t0.c0) FROM t0 WHERE LEAST(t0.c0, "0.95");
```

### 2. What did you expect to see? (Required)

see the following case:

The MySQL manual clarifies that If the arguments comprise a mix of numbers and strings, they are compared as strings.

https://dev.mysql.com/doc/refman/9.1/en/comparison-operators.html#function_least

### 3. What did you see instead (Required)

```sql
// MySQL 8.4.1
MySQL [test]> select LEAST(t0.c0, "0.95") from t0;
+----------------------+
| LEAST(t0.c0, "0.95") |
+----------------------+
| 0.95 |
+----------------------+
1 row in set (0.00 sec)

MySQL [test]> SELECT SUM(t0.c0) FROM t0 WHERE LEAST(t0.c0, "0.95");
+------------+
| SUM(t0.c0) |
+------------+
| 0 |
+------------+
1 row in set (0.00 sec)

// TiDB
mysql> select LEAST(t0.c0, "0.95") from t0;
+----------------------+
| LEAST(t0.c0, "0.95") |
+----------------------+
| 0 |
+----------------------+
1 row in set (0.04 sec)

mysql> SELECT SUM(t0.c0) FROM t0 WHERE LEAST(t0.c0, "0.95");
+------------+
| SUM(t0.c0) |
+------------+
| NULL |
+------------+
1 row in set (0.04 sec)
```

### 4. What is your TiDB version? (Required)

TiDB v8.5.0

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.