Skip to content

[Bug]: GROUPING() in ORDER BY fails with column does not exist #25287

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

I searched existing issues for GROUPING ORDER BY column does not exist rollup and did not find a duplicate.

MatrixOne version

Verified on latest main at 200bfa7e0efb (fix: improve skills SKILL.md compatibility format (#25271)).

MySQL version

Local MySQL 8.3.0.

Reproduce steps

drop database if exists repro_grouping_order_by;
create database repro_grouping_order_by;
use repro_grouping_order_by;

create table t(region varchar(8), product varchar(8), amount int);
insert into t values
  (null, 'tea', 10),
  (null, 'tea', 20),
  ('east', null, 5),
  ('east', 'coffee', 7),
  ('east', 'tea', null),
  ('west', 'tea', 3);

select if(grouping(region), 'ROLLUP', if(region is null, 'NULL', region)) as region_label,
       grouping(region) as grp_region,
       if(grouping(product), 'ROLLUP', if(product is null, 'NULL', product)) as product_label,
       grouping(product) as grp_product,
       count(*) as count_all,
       count(amount) as count_amount,
       sum(amount) as sum_amount
from t
group by region, product with rollup
order by grouping(region), region_label, grouping(product), product_label;

Expected behavior

MySQL executes successfully and allows GROUPING() in ORDER BY for a WITH ROLLUP query.

Actual behavior

MatrixOne errors:

ERROR 20301 (HY000): invalid input: column region does not exist

Additional information

The same grouped result can be produced in MatrixOne if GROUPING(region) and GROUPING(product) are selected as aliases and those aliases are used for sorting. The issue appears specific to resolving GROUPING() directly inside ORDER BY.

Activity

  1. added
    kind/bug
    severity/s1
    on Jul 1, 2026
  2. added
    severity/s0
    and removed
    severity/s1
    on Jul 1, 2026
  3. added theissue type on Jul 1, 2026
  4. added this to the MO-202607 milestone on Jul 1, 2026
  5. added
    ai-light
    on Jul 1, 2026
  6. Ariznawlll commented on Jul 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 验证结果:通过

    测试版本

    • MatrixOne:main@cd8e175349932ccf4be8d388080a0c3310e538ac
    • 测试方式:真实 MatrixOne SQL + planner 定向测试

    黑盒测试过程

    按照 issue 原始数据创建包含普通值、NULL 和聚合 NULL 的表,执行:

    SELECT IF(GROUPING(region), 'ROLLUP',
              IF(region IS NULL, 'NULL', region)) AS region_label,
           GROUPING(region) AS grp_region,
           IF(GROUPING(product), 'ROLLUP',
              IF(product IS NULL, 'NULL', product)) AS product_label,
           GROUPING(product) AS grp_product,
           COUNT(*) AS count_all,
           COUNT(amount) AS count_amount,
           SUM(amount) AS sum_amount
    FROM grouping_source
    GROUP BY region, product WITH ROLLUP
    ORDER BY GROUPING(region), region_label,
             GROUPING(product), product_label;

    另外扩展验证 IF(GROUPING(region),1,0) 嵌套表达式直接用于 ORDER BY。

    黑盒实际结果

    • 原始 SQL 正常返回 9 行 ROLLUP 结果;
    • 普通 NULL 分组与 ROLLUP NULL 可通过 GROUPING() 正确区分;
    • grand total 为 count_all=6、count_amount=5、sum_amount=45;
    • 嵌套 IF(GROUPING(...)) 排序同样返回正确结果;
    • 两组 SQL 连续执行 3 轮,3/3 通过且输出一致;
    • 未再出现 column region does not exist。

    planner 定向测试

    执行 GROUPING() 直接排序、星号展开、select alias、prepared/reentrant、alias shadowing、二元表达式和嵌套表达式等 8 组用例,每组连续运行 3 轮,全部通过。

    结论

    最新 main 已能在 WITH ROLLUP 查询的 ORDER BY 中正确解析并执行 GROUPING(),原问题已修复。

  7. Ariznawlll commented on Jul 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    motr 黑盒回归

    该问题已加入 matrixorigin/motr#66。

    回归覆盖 issue 原始 GROUPING() + ORDER BY + WITH ROLLUP 查询,以及 IF(GROUPING(...)) 嵌套排序表达式。

    在 MatrixOne main@cd8e175349932ccf4be8d388080a0c3310e538ac 上录制后连续执行 3 轮,共 24 条 SQL,全部通过且输出一致。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-lightkind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions