After upgrading from 6.1.2 to 7.1.1, executing SQL reports "index out of range [0] with length 0"

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 从6.1.2升级到7.1.1后,执行SQL报index out of range [0] with length 0

| username: Zealot

[TiDB Usage Environment] Production Environment
[TiDB Version] 7.1.1

[Encountered Problem: Problem Phenomenon and Impact]
SQL:

select d.*,group_concat(wi.wc_person_name) Talent Selection
from (
         select suser.realName                                         Owner,
                base.id                                                baseid,
                luser.realName                                         Entry Person,
                resuser.realName                                       Applicant,
                sdept.d4 Region,
                sdept.d5 Branch,
                concat_ws('/', sdept.d4, sdept.d5, sdept.d6, sdept.d7) Department,
                contract_no,
                price                                                  Contract Amount,
                kickback                                               Consultation Fee,
                FROM_UNIXTIME(contract_signing_time)                   Signing Time,
                case
                    when base.state = 2
                        then 'Signed'
                    when base.state = 3
                        then 'Contract in Execution'
                    end as                                             Contract Status,
                ent_name                                               Company Name,
                ent_linkman                                            Contact Person,
                config.name                                            Operation Status,
                config2.name                                           Fund Verification Status,
                FROM_UNIXTIME(xxf.create_time)                         Application Time,
                #        xxf.state                                                                                       Operation Status,
                 xxf.transfer_price                                     Performance Transfer Amount,

                (select sum(transfer_price)
                 from kl_conm_account
                 where qconmid = base.id
                   and state in (12, 14)
                   and conmid = 0)                                     Total Amount Recorded,

                base.id
         from ods_oa.kl_conm_base base
                  left join ods_oa.kl_users suser on base.ouid = suser.ID
                  left join ods_oa.kl_users luser on base.uid = luser.ID
                  left join ods_oa.dept_ext sdept on sdept.id = suser.DEPARTMENTID
                  left join kl_conm_ycenterprise qy on qy.conmid = base.id
                  left join kl_conm_account_xxf xxf on xxf.conmid = base.id
                  left join kl_conm_account_result res on res.cid = base.id and is_zx = 2
                  left join kl_status_config config on config.id = xxf.state
                  left join kl_status_config config2 on config2.id = entry_status
                  left join ods_oa.kl_users resuser on resuser.id = xxf.uid
         where suser.realName in (
             select Owner
             from (
                      select suser.realName Owner
                      from ods_oa.kl_conm_base base
                               left join ods_oa.kl_users suser on base.ouid = suser.ID
                               left join kl_conm_account_xxf xxf on xxf.conmid = base.id
                      where contract_no like '%Q%'
                        and xxf.transfer_price > 0

                      group by contract_no
                  ) t
             group by t.Owner
             having count(t.Owner) > 1
         )
           and contract_no like '%Q%'
           and xxf.transfer_price > 0

         group by contract_no, suser.realName
         order by suser.realName) d
         left join kl_conm_ycenterprise_withcert wi on wi.conmid = d.baseid
group by d.contract_no, Owner
order by Owner

If I reduce the number of fields in d., the error will not occur, but if the number of fields in d. reaches a certain number, it will report index out of range again.

| username: ShawnYan | Original post link

Please provide the table definition, and we will try to reproduce it.

| username: zhanggame1 | Original post link

Does this error also occur when using the MySQL client? Check if there is anything written in the TiDB logs.

| username: Zealot | Original post link

The previous version was fine, here is the log:

[2023/08/14 08:54:28.753 +08:00] [ERROR] [aggregate.go:458] [“parallel hash aggregation panicked”] [error=“runtime error: index out of range [0] with length 0”] [stack=“github.com/pingcap/tidb/executor.recoveryHashAgg\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/executor/aggregate.go:458\ngithub.com/pingcap/tidb/executor.(*HashAggPartialWorker).run.func1\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/executor/aggregate.go:466\nruntime.gopanic\n\t/usr/local/go/src/runtime/panic.go:884\nruntime.goPanicIndex\n\t/usr/local/go/src/runtime/panic.go:113\ngithub.com/pingcap/tidb/util/chunk.(*Column).IsNull\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/util/chunk/column.go:169\ngithub.com/pingcap/tidb/util/chunk.Row.IsNull\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/util/chunk/row.go:218\ngithub.com/pingcap/tidb/expression.(*Column).EvalString\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/expression/column.go:439\ngithub.com/pingcap/tidb/executor/aggfuncs.(*groupConcat).UpdatePartialResult\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/executor/aggfuncs/func_group_concat.go:134\ngithub.com/pingcap/tidb/executor.(*HashAggPartialWorker).updatePartialResult\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/executor/aggregate.go:526\ngithub.com/pingcap/tidb/executor.(*HashAggPartialWorker).run\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/executor/aggregate.go:487”]

| username: Zealot | Original post link

This is not very convenient to provide, here is the log:

[2023/08/14 08:54:28.753 +08:00] [ERROR] [aggregate.go:458] [“parallel hash aggregation panicked”] [error=“runtime error: index out of range [0] with length 0”] [stack=“github.com/pingcap/tidb/executor.recoveryHashAgg\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/executor/aggregate.go:458\ngithub.com/pingcap/tidb/executor.(*HashAggPartialWorker).run.func1\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/executor/aggregate.go:466\nruntime.gopanic\n\t/usr/local/go/src/runtime/panic.go:884\nruntime.goPanicIndex\n\t/usr/local/go/src/runtime/panic.go:113\ngithub.com/pingcap/tidb/util/chunk.(*Column).IsNull\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/util/chunk/column.go:169\ngithub.com/pingcap/tidb/util/chunk.Row.IsNull\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/util/chunk/row.go:218\ngithub.com/pingcap/tidb/expression.(*Column).EvalString\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/expression/column.go:439\ngithub.com/pingcap/tidb/executor/aggfuncs.(*groupConcat).UpdatePartialResult\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/executor/aggfuncs/func_group_concat.go:134\ngithub.com/pingcap/tidb/executor.(*HashAggPartialWorker).updatePartialResult\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/executor/aggregate.go:526\ngithub.com/pingcap/tidb/executor.(*HashAggPartialWorker).run\n\t/home/jenkins/agent/workspace/build-common/go/src/github.com/pingcap/tidb/executor/aggregate.go:487”]

| username: Zealot | Original post link

Currently, I have modified the SQL and it can run now. I moved the left join kl_conm_ycenterprise_withcert wi on wi.conmid = d.baseid into the subquery and associated it with kl_conm_base. Using group_concat(distinct wc_person_name) outside allows it to be queried.

select d.*, group_concat(distinct wc_person_name) as Talent Selection
from (
         select suser.realName as Owner,
                base.id as baseid,
                luser.realName as Entry Person,
                resuser.realName as Applicant,
                sdept.d4 as Region,
                sdept.d5 as Branch,
                concat_ws('/', sdept.d4, sdept.d5, sdept.d6, sdept.d7) as Department,
                contract_no,
                price as Contract Amount,
                kickback as Consulting Fee,
                FROM_UNIXTIME(contract_signing_time) as Signing Time,
                case
                    when base.state = 2 then 'Signed'
                    when base.state = 3 then 'Contract in Execution'
                end as Contract Status,
                ent_name as Enterprise Name,
                ent_linkman as Contact Person,
                config.name as Operation Status,
                config2.name as Fund Verification Status,
                FROM_UNIXTIME(xxf.create_time) as Application Time,
                xxf.transfer_price as Performance Transfer Amount,
                (select sum(transfer_price)
                 from kl_conm_account
                 where qconmid = base.id
                   and state in (12, 14)
                   and conmid = 0) as Total Accounted Amount,
                base.id,
                wi.wc_person_name
         from ods_oa.kl_conm_base base
                  left join ods_oa.kl_users suser on base.ouid = suser.ID
                  left join ods_oa.kl_users luser on base.uid = luser.ID
                  left join ods_oa.dept_ext sdept on sdept.id = suser.DEPARTMENTID
                  left join kl_conm_ycenterprise qy on qy.conmid = base.id
                  left join kl_conm_account_xxf xxf on xxf.conmid = base.id
                  left join kl_conm_account_result res on res.cid = base.id and is_zx = 2
                  left join kl_status_config config on config.id = xxf.state
                  left join kl_status_config config2 on config2.id = entry_status
                  left join ods_oa.kl_users resuser on resuser.id = xxf.uid
                  left join kl_conm_ycenterprise_withcert wi on wi.conmid = base.id
         where suser.realName in (
             select Owner
             from (
                      select suser.realName as Owner
                      from ods_oa.kl_conm_base base
                               left join ods_oa.kl_users suser on base.ouid = suser.ID
                               left join kl_conm_account_xxf xxf on xxf.conmid = base.id
                      where contract_no like '%Q%'
                        and xxf.transfer_price > 0
                      group by contract_no
                  ) t
             group by t.Owner
             having count(t.Owner) > 1
         )
           and contract_no like '%Q%'
           and xxf.transfer_price > 0
         group by contract_no, suser.realName
         order by suser.realName
) d
group by d.contract_no, Owner
order by Owner
| username: cassblanca | Original post link

Is it possible not to use Chinese as an alias?

| username: WalterWj | Original post link

It feels unexpected. Please provide feedback in the feedback section.

| username: ShawnYan | Original post link

It’s not that we really need your original DDL and query, but rather suggest providing a minimal test case. This will make it easier for everyone to further help you identify and locate the issue.

| username: redgame | Original post link

If modifying the SQL allows it to run, then the result set of the subquery is smaller. Is there a performance bottleneck?

| username: 有猫万事足 | Original post link

Feels like this bug.

| username: ljluestc | Original post link

SELECT d.*, GROUP_CONCAT(DISTINCT wc_person_name) AS Talent Selection
FROM (
    SELECT
        suser.realName AS Owner,
        base.id AS baseid,
        luser.realName AS Entry Person,
        resuser.realName AS Applicant,
        sdept.d4 AS Region,
        sdept.d5 AS Branch,
        CONCAT_WS('/', sdept.d4, sdept.d5, sdept.d6, sdept.d7) AS Department,
        contract_no,
        price AS Contract Amount,
        kickback AS Consulting Fee,
        FROM_UNIXTIME(contract_signing_time) AS Signing Time,
        CASE
            WHEN base.state = 2 THEN 'Signed'
            WHEN base.state = 3 THEN 'Contract in Execution'
        END AS Contract Status,
        ent_name AS Company Name,
        ent_linkman AS Contact Person,
        config.name AS Operation Status,
        config2.name AS Fund Verification Status,
        FROM_UNIXTIME(xxf.create_time) AS Application Time,
        xxf.transfer_price AS Performance Transfer Amount,
        (
            SELECT SUM(transfer_price)
            FROM kl_conm_account
            WHERE qconmid = base.id
                AND state IN (12, 14)
                AND conmid = 0
        ) AS Total Accounted Amount,
        base.id,
        wi.wc_person_name
    FROM ods_oa.kl_conm_base base
    LEFT JOIN ods_oa.kl_users suser ON base.ouid = suser.ID
    LEFT JOIN ods_oa.kl_users luser ON base.uid = luser.ID
    LEFT JOIN ods_oa.dept_ext sdept ON sdept.id = suser.DEPARTMENTID
    LEFT JOIN kl_conm_ycenterprise qy ON qy.conmid = base.id
    LEFT JOIN kl_conm_account_xxf xxf ON xxf.conmid = base.id
    LEFT JOIN kl_conm_account_result res ON res.cid = base.id AND is_zx = 2
    LEFT JOIN kl_status_config config ON config.id = xxf.state
    LEFT JOIN kl_status_config config2 ON config2.id = entry_status
    LEFT JOIN ods_oa.kl_users resuser ON resuser.id = xxf.uid
    LEFT JOIN kl_conm_ycenterprise_withcert wi ON wi.conmid = base.id
    WHERE suser.realName IN (
        SELECT Owner
        FROM (
            SELECT suser.realName Owner
            FROM ods_oa.kl_conm_base base
            LEFT JOIN ods_oa.kl_users suser ON base.ouid = suser.ID
            LEFT JOIN kl_conm_account_xxf xxf ON xxf.conmid = base.id
            WHERE contract_no LIKE '%Q%'
                AND xxf.transfer_price > 0
            GROUP BY contract_no
        ) t
        GROUP BY t.Owner
        HAVING COUNT(t.Owner) > 1
    )
    AND contract_no LIKE '%Q%'
    AND xxf.transfer_price > 0
    GROUP BY contract_no, suser.realName
) d
GROUP BY d.contract_no, Owner
ORDER BY Owner;
| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.