Skip to content

select distinct produces unexpected result #53726

@r33s3n6

Description

@r33s3n6

1. Minimal reproduce step (Required)

create table t1 (c1 int primary key);
insert into t1 (c1) values (575932053), (-258025139);

SELECT DISTINCT
  cast(c1 as decimal) as c3,
  cast(c1 as signed) as c4
FROM t1;

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

The two columns should be same, in MySQL:

mysql> SELECT DISTINCT 
    ->   cast(c1 as decimal) as c3,
    ->   cast(c1 as signed) as c4
    -> FROM t1;
+------------+------------+
| c3         | c4         |
+------------+------------+
| -258025139 | -258025139 |
|  575932053 |  575932053 |
+------------+------------+
2 rows in set (0.00 sec)

3. What did you see instead (Required)

But TiDB outputs strange number.

mysql> SELECT DISTINCT 
    ->   cast(c1 as decimal) as c3,
    ->   cast(c1 as signed) as c4
    -> FROM t1;
+------------+---------------------+
| c3         | c4                  |
+------------+---------------------+
| -258025139 | 1108209533567631369 |
|  575932053 |                   0 |
+------------+---------------------+
2 rows in set (0.01 sec)

After removing distinct, the result is correct:

mysql> SELECT
    ->   cast(c1 as decimal) as c3,
    ->   cast(c1 as signed) as c4
    -> FROM t1;
+------------+------------+
| c3         | c4         |
+------------+------------+
| -258025139 | -258025139 |
|  575932053 |  575932053 |
+------------+------------+
2 rows in set (0.00 sec)

4. What is your TiDB version? (Required)

Release Version: v8.1.0
Edition: Community
Git Commit Hash: 945d07c5d5c7a1ae212f6013adfb187f2de24b23
Git Branch: HEAD
UTC Build Time: 2024-05-21 03:51:57
GoVersion: go1.21.10
Race Enabled: false
Check Table Before Drop: false
Store: tikv

topology:

distributed.yaml:

global:
  user: "tidb"
  ssh_port: 22
  deploy_dir: "/tidb-deploy"
  data_dir: "/tidb-data"

pd_servers:
  - host: 10.0.2.31

tidb_servers:
  - host: 10.0.2.21

tikv_servers:
  - host: 10.0.2.11
  - host: 10.0.2.12
  - host: 10.0.2.13

monitoring_servers:
  - host: 10.0.2.8

grafana_servers:
  - host: 10.0.2.8

alertmanager_servers:
  - host: 10.0.2.8

tiflash_servers:
  - host: 10.0.2.32

about us

We are the BASS team from the School of Cyber Science and Technology at Beihang University. Our main focus is on system software security, operating systems, and program analysis research, as well as the development of automated program testing frameworks for detecting software defects. Using our self-developed database vulnerability testing tool, we have identified the above-mentioned vulnerabilities in TiDB that may lead to database logic error.

Metadata

Metadata

Assignees

Labels

affects-6.5This bug affects the 6.5.x(LTS) versions.affects-7.1This bug affects the 7.1.x(LTS) versions.affects-7.5This bug affects the 7.5.x(LTS) versions.affects-8.1This bug affects the 8.1.x(LTS) versions.impact/wrong-resultseverity/criticalsig/plannerSIG: Plannertype/bugThe issue is confirmed as a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions