# [KB]关于 TiDB 的 SQL 包含窗口函数和 limit 时报错：index out of range[0] with length-0

## Description

在 SQL 中包含 窗口函数 并且包含 limit 语句，会出现 Painc 问题

报错信息： `index out of range[0] with length 0`

* 相关 Schema:

```sql
CREATE TABLE `table1` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
  `code` varchar(32) NOT NULL,
  `bill_type` int(11) NOT NULL,
  `relase_code` varchar(32) DEFAULT NULL,
  `vin` varchar(32) NOT NULL,
  `status` int(11) NOT NULL DEFAULT '10',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `is_delete` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
) ENGINE=InnoDB DEFAULT CHARSET=utf8
```

* 相关 SQL：

```sql
select relase_code,vin,status,bill_type,
row_number() over(partition by `code` order by create_time desc,id desc) num
from table1
where is_delete = 0
limit 10;
```

* TiDB log 报错

```log
ERROR] [conn.go:736] ["connection running loop panic"] [conn=1782177] [lastSQL="select relase_code,vin,status,
```

## Root Cause

```
create table t(a int);
insert into t values(1);
select count(a) f1, row_number() over (order by count(a)) as f2 from t limit 1;
```

Select 语句出错根因，外部 limit的错误列修剪，导致它的子句有更多的列，从而产生了一个错误的执行计划。 这个问题是由 #20288 引起的，当试图对limit进行内联投影时，它将limit从baseLogicalPlan变为logicalSchemaProducer。

问题 Issus: <https://github.com/pingcap/tidb/issues/25344>

修复 PR：<https://github.com/pingcap/tidb/pull/25345>

## Diagnostic Steps

* 需要提供相关问题 SQL

* 需要提供相关 SQL 涉及的 Schema

* 需要提供相关 TiDB Server 的 log 信息，关键字 GetBytes ，有类似如下日志

```log
[err="runtime error: index out of range [0] with length 0"]
[stack="goroutine 703942231 [running]:
github.com/pingcap/tidb/server.(*clientConn).Run.func1(0x4012d20, 0xc236f0ab70, 0xc163056100)
/home/jenkins/agent/workspace/idb_multi_branch_v5.0.2-20210813/go/src/github.com/pingcap/tidb/server/conn.go:734 +0xee
panic(0x37a6d20, 0xc21a8963e0)
/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/pingcap/tidb/server.(*clientConn).writeResultset.func1(0xc16d21f100, 0x4012d20, 0xc0fc9fa390, 0xc163056100)
/home/jenkins/agent/workspace/idb_multi_branch_v5.0.2-20210813/go/src/github.com/pingcap/tidb/server/conn.go:1749 +0x4e2
panic(0x37a6d20, 0xc21a8963e0)
/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/pingcap/tidb/util/chunk.(*Column).GetBytes(...)
/home/jenkins/agent/workspace/idb_multi_branch_v5.0.2-20210813/go/src/github.com/pingcap/tidb/util/chunk/column.go:539
github.com/pingcap/tidb/util/chunk.Row.GetBytes(...)
/home/jenkins/agent/workspace/idb_multi_branch_v5.0.2-20210813/go/src/github.com/pingcap/tidb/util/chunk/row.go:77
github.com/pingcap/tidb/server.dumpTextRow(0xc0100d2000, 0x4, 0x400, 0xc079658040, 0x8, 0x8, 0xc07bdd2820, 0x0, 0xc16d21f018, 0x1360016, ...)
/home/jenkins/agent/workspace/idb_multi_branch_v5.0.2-20210813/go/src/github.com/pingcap/tidb/server/util.go:327 +0x1896
github.com/pingcap/tidb/server.(*clientConn).writeChunks(0xc163056100, 0x4012d20, 0xc0fc9fa390, 0x4033420, 0xc07bdd2780, 0xc10002f000, 0x1e025c6, 0x3685da0, 0x0)
/home/jenkins/agent/workspace/idb_multi_branch_v5.0.2-20210813/go/src/github.com/pingcap/tidb/server/conn.go:1840 +0x427
github.com/pingcap/tidb/server.(*clientConn).writeResultset(0xc163056100, 0x4012d20, 0xc0fc9fa390, 0x4033420, 0xc07bdd2780, 0x23400, 0x0, 0x0, 0x0, 0x0)
/home/jenkins/agent/workspace/idb_multi_branch_v5.0.2-20210813/go/src/github.com/pingcap/tidb/server/conn.go:1762 +0x201
github.com/pingcap/tidb/server.(*clientConn).handleStmt(0xc163056100, 0x4012d20, 0xc0fc9fa390, 0x4039900, 0xc7cf84f500, 0x5faa160, 0x0, 0x0, 0x1, 0x0, ...)
/home/jenkins/agent/workspace/idb_multi_branch_v5.0.2-20210813/go/src/github.com/pingcap/tidb/server/conn.go:1656 +0x242
github.com/pingcap/tidb/server.(*clientConn).handleQuery(0xc163056100, 0x4012c60, 0xc187e9f1c0, 0xc12dc4ba01, 0xcf, 0x0, 0x0)
/home/jenkins/agent/workspace/idb_multi_branch_v5.0.2-20210813/go/src/github.com/pingcap/tidb/server/conn.go:1503 +0x44f
github.com/pingcap/tidb/server.(*clientConn).dispatch(0xc163056100, 0x4012c60, 0xc187e9f1c0, 0xc12dc4ba00, 0xd0, 0xcf, 0x0, 0x0)、
/home/jenkins/agent/workspace/idb_multi_branch_v5.0.2-20210813/go/src/github.com/pingcap/tidb/server/conn.go:1037 +0x6f2
github.com/pingcap/tidb/server.(*clientConn).Run(0xc163056100, 0x4012d20, 0xc236f0ab70)
/home/jenkins/agent/workspace/idb_multi_branch_v5.0.2-20210813/go/src/github.com/pingcap/tidb/server/conn.go:795 +0x293
github.com/pingcap/tidb/server.(*Server).onConn(0xc00314b6c0, 0xc163056100)
/home/jenkins/agent/workspace/idb_multi_branch_v5.0.2-20210813/go/src/github.com/pingcap/tidb/server/server.go:477 +0xb1b
created by github.com/pingcap/tidb/server.(*Server).Run
/home/jenkins/agent/workspace/idb_multi_branch_v5.0.2-20210813/go/src/github.com/pingcap/tidb/server/server.go:380 +0x8a5
"]
```

## Resolution

修复版本 v5.0.3,v5.1.1,v5.2.0 及以上版本
修复 PR：<https://github.com/pingcap/tidb/pull/25345>

> **注意**
>  
> 在 V5.0.2 在 SQL 中包含 开窗函数并且包含 limit 语句，会出现 Painc 问题

## Workaround

修改SQL 去掉 Limit关键字、或者去掉开窗函数。再进行查询。此操作对结果集会有改变，需要进行确认

## Affect Version(optional)

v5.0.0,v5.0.1,v5.0.2,v5.1.0
