8.0.11-TiDB-v7.5.0版本 执行SQL报错:runtime error: index out of range [1] with length 0

版本:8.0.11-TiDB-v7.5.0
环境:测试环境

SQL只是为了拼接分区表的一些语句,用的都是系统表

提供一个表结构:

CREATE TABLE `advertise_timing_order` (
  `ruleUid` char(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '定时规则数据唯一编码',
  `matching` char(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '匹配对象: campaign=活动,adgroup=广告组,keyword=关键词,target=定位',
  `state` int NOT NULL DEFAULT '0' COMMENT '开启暂停10=开启20=暂停',
  `matchUid` char(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '匹配对象 唯一ID',
  `time` datetime NOT NULL COMMENT '修改时间',
  `user` char(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '操作用户',
  PRIMARY KEY (`ruleUid`,`matchUid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='亚马逊广告-定时任务已注册任务'
/*!50100 PARTITION BY KEY (ruleUid)
PARTITIONS 251 */;

报错 SQL:

SELECT
                    `data`.TABLE_NAME,
                    CONCAT(
                        'PARTITION BY ',
                        `data`.PARTITION_METHOD,
                        '(',
                        `data`.PARTITION_EXPRESSION,
                        ') PARTITIONS ',
                        MAX(`data`.PARTITION_ORDINAL_POSITION),
                        `data`.sub_var,
                        ' (',
                        GROUP_CONCAT(                                                           /*拼出分区主体*/
                            CONCAT(
                                'PARTITION `',
                                REPLACE(`data`.PARTITION_NAME, '`', '``'),
                                '`',
                                IF(
                                    `data`.PARTITION_METHOD = 'RANGE',
                                    CONCAT(' VALUES LESS THAN (', `data`.PARTITION_DESCRIPTION, ')'),
                                    IF(
                                        `data`.PARTITION_METHOD = 'LIST',
                                        CONCAT(' VALUES IN (', `data`.PARTITION_DESCRIPTION, ')'),
                                        ''
                                    )
                                ),
                                IFNULL(
                                    `data`.sub_body,                                            /*子分区*/
                                    CONCAT(
                                        ' COMMENT="',
                                        REPLACE(`data`.PARTITION_COMMENT, '"', '\\"'),
                                        '"'
                                    )
                                )
                            )
                            ORDER BY `data`.PARTITION_ORDINAL_POSITION
                            SEPARATOR ', '
                        ),
                        ')'
                    ) PARTITION_SQL,
                    CONCAT(
                        `data`.PARTITION_EXPRESSION, ',', 
                        IFNULL(`data`.SUBPARTITION_EXPRESSION, '')
                    ) COLUMNS_COMPARE                                                           /*字段变化证明*/
                FROM
                    (SELECT
                    IF(
                        ISNULL(`PARTITIONS`.SUBPARTITION_ORDINAL_POSITION),
                        '',
                        CONCAT(
                            ' SUBPARTITION BY ',
                            `PARTITIONS`.SUBPARTITION_METHOD,
                            '(',
                            `PARTITIONS`.SUBPARTITION_EXPRESSION,
                            ') SUBPARTITIONS ',
                            MAX(`PARTITIONS`.SUBPARTITION_ORDINAL_POSITION)
                        )
                    ) `sub_var`,                                                                /*子分区说明*/
                    IF(
                        ISNULL(`PARTITIONS`.SUBPARTITION_ORDINAL_POSITION),
                        NULL,
                        CONCAT(
                            ' (',
                            GROUP_CONCAT(
                                CONCAT(
                                    'SUBPARTITION `',
                                    REPLACE(`PARTITIONS`.SUBPARTITION_NAME, '`', '``'),
                                    '` COMMENT="',
                                    REPLACE(`PARTITIONS`.PARTITION_COMMENT, '"', '\\"'),
                                    '"'
                                )
                                ORDER BY `PARTITIONS`.SUBPARTITION_ORDINAL_POSITION
                                SEPARATOR ', '
                            ),
                            ')'
                        )
                    ) `sub_body`,                                                               /*子分区主体*/
                    `PARTITIONS`.TABLE_NAME,
                    `PARTITIONS`.PARTITION_NAME,
                    `PARTITIONS`.PARTITION_ORDINAL_POSITION,
                    `PARTITIONS`.PARTITION_METHOD,
                    `PARTITIONS`.PARTITION_EXPRESSION,
                    `PARTITIONS`.PARTITION_DESCRIPTION,
                    `PARTITIONS`.PARTITION_COMMENT,
                    `PARTITIONS`.SUBPARTITION_EXPRESSION
                FROM
                    `information_schema`.`PARTITIONS`
                WHERE
                    `PARTITIONS`.TABLE_SCHEMA='test'
                AND `PARTITIONS`.TABLE_NAME IN ('advertise_timing_order')
                AND `PARTITIONS`.PARTITION_ORDINAL_POSITION IS NOT NULL
                GROUP BY
                    `PARTITIONS`.TABLE_NAME, `PARTITIONS`.PARTITION_NAME) `data`
                GROUP BY
                    `data`.TABLE_NAME

奇怪的是:注释掉GROUP_CONCAT里面的order by 执行又没问题,SQL如下:

SELECT
                    `data`.TABLE_NAME,
                    CONCAT(
                        'PARTITION BY ',
                        `data`.PARTITION_METHOD,
                        '(',
                        `data`.PARTITION_EXPRESSION,
                        ') PARTITIONS ',
                        MAX(`data`.PARTITION_ORDINAL_POSITION),
                        `data`.sub_var,
                        ' (',
                        GROUP_CONCAT(                                                           /*拼出分区主体*/
                            CONCAT(
                                'PARTITION `',
                                REPLACE(`data`.PARTITION_NAME, '`', '``'),
                                '`',
                                IF(
                                    `data`.PARTITION_METHOD = 'RANGE',
                                    CONCAT(' VALUES LESS THAN (', `data`.PARTITION_DESCRIPTION, ')'),
                                    IF(
                                        `data`.PARTITION_METHOD = 'LIST',
                                        CONCAT(' VALUES IN (', `data`.PARTITION_DESCRIPTION, ')'),
                                        ''
                                    )
                                ),
                                IFNULL(
                                    `data`.sub_body,                                            /*子分区*/
                                    CONCAT(
                                        ' COMMENT="',
                                        REPLACE(`data`.PARTITION_COMMENT, '"', '\\"'),
                                        '"'
                                    )
                                )
                            )
                            #ORDER BY `data`.PARTITION_ORDINAL_POSITION
                            SEPARATOR ', '
                        ),
                        ')'
                    ) PARTITION_SQL,
                    CONCAT(
                        `data`.PARTITION_EXPRESSION, ',', 
                        IFNULL(`data`.SUBPARTITION_EXPRESSION, '')
                    ) COLUMNS_COMPARE                                                           /*字段变化证明*/
                FROM
                    (SELECT
                    IF(
                        ISNULL(`PARTITIONS`.SUBPARTITION_ORDINAL_POSITION),
                        '',
                        CONCAT(
                            ' SUBPARTITION BY ',
                            `PARTITIONS`.SUBPARTITION_METHOD,
                            '(',
                            `PARTITIONS`.SUBPARTITION_EXPRESSION,
                            ') SUBPARTITIONS ',
                            MAX(`PARTITIONS`.SUBPARTITION_ORDINAL_POSITION)
                        )
                    ) `sub_var`,                                                                /*子分区说明*/
                    IF(
                        ISNULL(`PARTITIONS`.SUBPARTITION_ORDINAL_POSITION),
                        NULL,
                        CONCAT(
                            ' (',
                            GROUP_CONCAT(
                                CONCAT(
                                    'SUBPARTITION `',
                                    REPLACE(`PARTITIONS`.SUBPARTITION_NAME, '`', '``'),
                                    '` COMMENT="',
                                    REPLACE(`PARTITIONS`.PARTITION_COMMENT, '"', '\\"'),
                                    '"'
                                )
                                ORDER BY `PARTITIONS`.SUBPARTITION_ORDINAL_POSITION
                                SEPARATOR ', '
                            ),
                            ')'
                        )
                    ) `sub_body`,                                                               /*子分区主体*/
                    `PARTITIONS`.TABLE_NAME,
                    `PARTITIONS`.PARTITION_NAME,
                    `PARTITIONS`.PARTITION_ORDINAL_POSITION,
                    `PARTITIONS`.PARTITION_METHOD,
                    `PARTITIONS`.PARTITION_EXPRESSION,
                    `PARTITIONS`.PARTITION_DESCRIPTION,
                    `PARTITIONS`.PARTITION_COMMENT,
                    `PARTITIONS`.SUBPARTITION_EXPRESSION
                FROM
                    `information_schema`.`PARTITIONS`
                WHERE
                    `PARTITIONS`.TABLE_SCHEMA='test'
                AND `PARTITIONS`.TABLE_NAME IN ('advertise_timing_order')
                AND `PARTITIONS`.PARTITION_ORDINAL_POSITION IS NOT NULL
                GROUP BY
                    `PARTITIONS`.TABLE_NAME, `PARTITIONS`.PARTITION_NAME) `data`
                GROUP BY
                    `data`.TABLE_NAME

同时注释部分其他SQL,执行也不报错:

SELECT
                   # `data`.TABLE_NAME,
                   # CONCAT(
                   #     'PARTITION BY ',
                   #     `data`.PARTITION_METHOD,
                   #     '(',
                   #     `data`.PARTITION_EXPRESSION,
                   #     ') PARTITIONS ',
                   #     MAX(`data`.PARTITION_ORDINAL_POSITION),
                   #     `data`.sub_var,
                   #     ' (',
                        GROUP_CONCAT(                                                           /*拼出分区主体*/
                            CONCAT(
                                'PARTITION `',
                                REPLACE(`data`.PARTITION_NAME, '`', '``'),
                                '`',
                                IF(
                                    `data`.PARTITION_METHOD = 'RANGE',
                                    CONCAT(' VALUES LESS THAN (', `data`.PARTITION_DESCRIPTION, ')'),
                                    IF(
                                        `data`.PARTITION_METHOD = 'LIST',
                                        CONCAT(' VALUES IN (', `data`.PARTITION_DESCRIPTION, ')'),
                                        ''
                                    )
                                ),
                                IFNULL(
                                    `data`.sub_body,                                            /*子分区*/
                                    CONCAT(
                                        ' COMMENT="',
                                        REPLACE(`data`.PARTITION_COMMENT, '"', '\\"'),
                                        '"'
                                    )
                                )
                            )
                            ORDER BY `data`.PARTITION_ORDINAL_POSITION
                            SEPARATOR ', '
                        ) aa
                    #    ')'
                    #) PARTITION_SQL,
                    #CONCAT(
                    #    `data`.PARTITION_EXPRESSION, ',', 
                    #    IFNULL(`data`.SUBPARTITION_EXPRESSION, '')
                    #) COLUMNS_COMPARE                                                           /*字段变化证明*/
                FROM
                    (SELECT
                    IF(
                        ISNULL(`PARTITIONS`.SUBPARTITION_ORDINAL_POSITION),
                        '',
                        CONCAT(
                            ' SUBPARTITION BY ',
                            `PARTITIONS`.SUBPARTITION_METHOD,
                            '(',
                            `PARTITIONS`.SUBPARTITION_EXPRESSION,
                            ') SUBPARTITIONS ',
                            MAX(`PARTITIONS`.SUBPARTITION_ORDINAL_POSITION)
                        )
                    ) `sub_var`,                                                                /*子分区说明*/
                    IF(
                        ISNULL(`PARTITIONS`.SUBPARTITION_ORDINAL_POSITION),
                        NULL,
                        CONCAT(
                            ' (',
                            GROUP_CONCAT(
                                CONCAT(
                                    'SUBPARTITION `',
                                    REPLACE(`PARTITIONS`.SUBPARTITION_NAME, '`', '``'),
                                    '` COMMENT="',
                                    REPLACE(`PARTITIONS`.PARTITION_COMMENT, '"', '\\"'),
                                    '"'
                                )
                                ORDER BY `PARTITIONS`.SUBPARTITION_ORDINAL_POSITION
                                SEPARATOR ', '
                            ),
                            ')'
                        )
                    ) `sub_body`,                                                               /*子分区主体*/
                    `PARTITIONS`.TABLE_NAME,
                    `PARTITIONS`.PARTITION_NAME,
                    `PARTITIONS`.PARTITION_ORDINAL_POSITION,
                    `PARTITIONS`.PARTITION_METHOD,
                    `PARTITIONS`.PARTITION_EXPRESSION,
                    `PARTITIONS`.PARTITION_DESCRIPTION,
                    `PARTITIONS`.PARTITION_COMMENT,
                    `PARTITIONS`.SUBPARTITION_EXPRESSION
                FROM
                    `information_schema`.`PARTITIONS`
                WHERE
                    `PARTITIONS`.TABLE_SCHEMA='test'
                AND `PARTITIONS`.TABLE_NAME IN ('advertise_timing_order')
                AND `PARTITIONS`.PARTITION_ORDINAL_POSITION IS NOT NULL
                GROUP BY
                    `PARTITIONS`.TABLE_NAME, `PARTITIONS`.PARTITION_NAME) `data`
                GROUP BY
                    `data`.TABLE_NAME

请问这是踩到BUG了 ?

如果子查询返回空结果集,当尝试访问结果集中的第二列时可能会出现此错误。请确保子查询返回的结果集不为空。

SELECT
  data.TABLE_NAME,
  CONCAT(
    'PARTITION BY ',
    data.PARTITION_METHOD,
    '(',
    data.PARTITION_EXPRESSION,
    ') PARTITIONS ',
    MAX(data.PARTITION_ORDINAL_POSITION),
    data.sub_var,
    ' (',
    GROUP_CONCAT(
      CONCAT(
        'PARTITION ',
        REPLACE(data.PARTITION_NAME, '`', ''),
        '`',
        IF(data.PARTITION_METHOD = 'RANGE', CONCAT(' VALUES LESS THAN (', data.PARTITION_DESCRIPTION, ')'), 
          IF(data.PARTITION_METHOD = 'LIST', CONCAT(' VALUES IN (', data.PARTITION_DESCRIPTION, ')'), '')
        ),
        IFNULL(data.sub_body, CONCAT(' COMMENT="', REPLACE(data.PARTITION_COMMENT, '"', '\\"'), '"'))
      ) 
      ORDER BY data.PARTITION_ORDINAL_POSITION SEPARATOR ', '
    ),
    ')'
  ) AS PARTITION_SQL,
  CONCAT(data.PARTITION_EXPRESSION, ',', IFNULL(data.SUBPARTITION_EXPRESSION, '')) AS COLUMNS_COMPARE
FROM (
  SELECT
    IF(ISNULL(PARTITIONS.SUBPARTITION_ORDINAL_POSITION), '', CONCAT(
      ' SUBPARTITION BY ',
      PARTITIONS.SUBPARTITION_METHOD,
      '(',
      PARTITIONS.SUBPARTITION_EXPRESSION,
      ') SUBPARTITIONS ',
      MAX(PARTITIONS.SUBPARTITION_ORDINAL_POSITION)
    )) AS sub_var,
    IF(ISNULL(PARTITIONS.SUBPARTITION_ORDINAL_POSITION), NULL, CONCAT(
      ' (',
      GROUP_CONCAT(
        CONCAT(
          'SUBPARTITION `', 
          REPLACE(PARTITIONS.SUBPARTITION_NAME, '`', ''),
          '`',
          ' COMMENT="', 
          REPLACE(PARTITIONS.PARTITION_COMMENT, '"', '\\"'), 
          '"'
        ) 
        ORDER BY PARTITIONS.SUBPARTITION_ORDINAL_POSITION SEPARATOR ', '
      ),
      ')'
    )) AS sub_body,
    PARTITIONS.TABLE_NAME,
    PARTITIONS.PARTITION_NAME,
    PARTITIONS.PARTITION_ORDINAL_POSITION,
    PARTITIONS.PARTITION_METHOD,
    PARTITIONS.PARTITION_EXPRESSION,
    PARTITIONS.PARTITION_DESCRIPTION,
    PARTITIONS.PARTITION_COMMENT,
    PARTITIONS.SUBPARTITION_EXPRESSION
  FROM information_schema.PARTITIONS
  WHERE 
    PARTITIONS.TABLE_SCHEMA = 'am' AND
    PARTITIONS.TABLE_NAME IN ('') AND
    PARTITIONS.PARTITION_ORDINAL_POSITION IS NOT NULL
  GROUP BY PARTITIONS.TABLE_NAME, PARTITIONS.PARTITION_NAME
) data
GROUP BY data.TABLE_NAME;

试试这个。

子查询即使返回到不是空的结果集,也是会报错的,你提供的哪个SQL还是会报错


会不会是因为兼容性问题?

已反馈给研发!

可能得让研发看下究竟是什么问题了

这个是代码问题,索引越界了
但是你为啥要拼表结构呢,直接show create table不行了吗

感觉应该是bug把

https://github.com/pingcap/tidb/issues/49986 一个问题, master 上已修复

sql_mode是怎样的?之前mysql也遇到过这样的错误。检查sql_mode或者改写下sql。

那就只能让官方技术看看是否是sql超长导致的。

+1 我也碰到这个问题了。

你们怎么处理的?

此话题已在最后回复的 60 天后被自动关闭。不再允许新回复。