pingcap / pingcap/tidb

`CHECK INDEX` statement meets runtime error

Open
#62,461 0 comments 0 reactions 0 assignees View on GitHub
component/ddl impact/panic severity/major type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

`CHECK INDEX` statement meets runtime error. It can be reproduced on the docker image `pingcap/tidb:nightly`.

### 1. Minimal reproduce step (Required)

```sql
CREATE TABLE t (c1 INT PRIMARY KEY, c2 JSON, INDEX i ((CAST(c2->'$.array' AS UNSIGNED ARRAY))));
INSERT INTO t (c1, c2) VALUES (1, '{"name": "John", "age": 30}');
ADMIN CHECK INDEX t i (500, 600);
```

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

No runtime error.

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

```sql
mysql> ADMIN CHECK INDEX t i (500, 600);
ERROR 1105 (HY000): runtime error: index out of range [3] with length 3
```

Logs:
```
[2025/07/16 14:12:00.800 +00:00] [INFO] [conn.go:1190] ["command dispatched failed"] [conn=2097160] [session_alias=] [connInfo="id:2097160, addr:172.17.0.1:36354 status:10, collation:utf8mb4_0900_ai_ci, user:root"] [command=Query] [status="inTxn:0, autocommit:1"]
[sql="ADMIN CHECK INDEX t i (500, 600)"] [txn_mode=PESSIMISTIC] [timestamp=459453266866733056]
runtime error: index out of range [3] with length 3
github.com/pingcap/errors.Trace
/root/go/pkg/mod/github.com/pingcap/errors@v0.11.5-0.20250523034308-74f78ae071ee/juju_adaptor.go:15
github.com/pingcap/tidb/pkg/util.GetRecoverError
/workspace/source/tidb/pkg/util/util.go:312
github.com/pingcap/tidb/pkg/executor/internal/exec.Next.func1
/workspace/source/tidb/pkg/executor/internal/exec/executor.go:444
runtime.gopanic
/root/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.11.linux-amd64/src/runtime/panic.go:791
runtime.goPanicIndex
/root/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.11.linux-amd64/src/runtime/panic.go:115
encoding/binary.littleEndian.Uint32
/root/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.11.linux-amd64/src/encoding/binary/binary.go:87
github.com/pingcap/tidb/pkg/util/chunk.(*Codec).decodeColumn
/workspace/source/tidb/pkg/util/chunk/codec.go:111
github.com/pingcap/tidb/pkg/util/chunk.(*Codec).DecodeToChunk
/workspace/source/tidb/pkg/util/chunk/codec.go:98
github.com/pingcap/tidb/pkg/util/chunk.(*Decoder).Reset
/workspace/source/tidb/pkg/util/chunk/codec.go:262
github.com/pingcap/tidb/pkg/distsql.(*selectResult).readFromChunk
/workspace/source/tidb/pkg/distsql/select_result.go:493
github.com/pingcap/tidb/pkg/distsql.(*selectResult).Next
/workspace/source/tidb/pkg/distsql/select_result.go:436
github.com/pingcap/tidb/pkg/executor.(*CheckIndexRangeExec).Next
/workspace/source/tidb/pkg/executor/admin.go:72
github.com/pingcap/tidb/pkg/executor/internal/exec.Next
/workspace/source/tidb/pkg/executor/internal/exec/executor.go:460
github.com/pingcap/tidb/pkg/executor.(*ExecStmt).next
/workspace/source/tidb/pkg/executor/adapter.go:1322
github.com/pingcap/tidb/pkg/executor.(*recordSet).Next
/workspace/source/tidb/pkg/executor/adapter.go:173
github.com/pingcap/tidb/pkg/server/internal/resultset.(*tidbResultSet).Next
/workspace/source/tidb/pkg/server/internal/resultset/resultset.go:72
github.com/pingcap/tidb/pkg/server.(*clientConn).writeChunks
/workspace/source/tidb/pkg/server/conn.go:2339
github.com/pingcap/tidb/pkg/server.(*clientConn).writeResultSet
/workspace/source/tidb/pkg/server/conn.go:2282
github.com/pingcap/tidb/pkg/server.(*clientConn).handleStmt
/workspace/source/tidb/pkg/server/conn.go:2085
github.com/pingcap/tidb/pkg/server.(*clientConn).handleQuery
/workspace/source/tidb/pkg/server/conn.go:1794
github.com/pingcap/tidb/pkg/server.(*clientConn).dispatch
/workspace/source/tidb/pkg/server/conn.go:1384
github.com/pingcap/tidb/pkg/server.(*clientConn).Run
/workspace/source/tidb/pkg/server/conn.go:1153
github.com/pingcap/tidb/pkg/server.(*Server).onConn
/workspace/source/tidb/pkg/server/server.go:740
runtime.goexit
/root/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.11.linux-amd64/src/runtime/asm_amd64.s:1700
```

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

```
Release Version: v9.0.0-beta.2.pre-118-g6ec4810
Edition: Community
Git Commit Hash: 6ec48101cae75529682a0f9fa8c16cbc42f246d3
Git Branch: HEAD
UTC Build Time: 2025-07-16 11:00:02
GoVersion: go1.23.11
Race Enabled: false
Check Table Before Drop: false
Store: unistore
Kernel Type: Classic
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the ADMIN CHECK INDEX sequence on the nightly Docker image, then inspect pkg/util/chunk/codec.go around decodeColumn and pkg/executor/admin.go around CheckIndexRangeExec.Next. Trace how the index result is decoded and add a regression test for the reported JSON multi-valued index case. Done means the statement completes without the runtime error.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.