pingcap / pingcap/tidb

`GROUP BY` returns different results in prepared statement and normal SELECT query

Open
#62,606 0 comments 0 reactions 0 assignees View on GitHub
sig/planner 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)

The following test case contains two equivalent queries, one is a normal SELECT query and another one is a prepared statement. However, they have different results. The normal SELECT query returns 2, while the prepared statement returns 1. Moreover, if I remove the `PRIMARY KEY` keyword, these two queries will have the same results.

```
CREATE TABLE t0(c0 BOOL PRIMARY KEY);
INSERT INTO t0 VALUES (true), (false);
SELECT count(*) FROM t0 WHERE 1 GROUP BY DATE_FORMAT(t0.c0, t0.c0);
SET @b = 1;
PREPARE prepare_query FROM 'SELECT count(*) FROM t0 WHERE ? GROUP BY DATE_FORMAT(t0.c0, t0.c0)';
EXECUTE prepare_query USING @b;
```

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

The prepared statement should return the same result with the normal SELECT query.

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

The prepared statement returns a different result from the normal SELECT query.

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

tidb_version()
Release Version: v9.0.0-beta.2.pre-149-g96f5396\nEdition: Community\nGit Commit Hash: 96f5396be12ae6c5bf440bd22b1e0bec532aff9e\nGit Branch: HEAD\nUTC Build Time: 2025-07-21 20:48:44\nGoVersion: go1.23.11\nRace Enabled: false\nCheck Table Before Drop: false\nStore: tikv\nKernel Type: Classic

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.