Skip to content

[Bug] Wrong result on ROLLUP/CUBE when a SortGroupRef exceeds num_cols #1753

@yjhjstz

Description

@yjhjstz

Apache Cloudberry version

main branch

What happened

When ORCA translates a GROUP BY ROLLUP(...) / CUBE(...) / GROUPING SETS () query, columns can be silently NULL-ed out in the wrong grouping levels, producing incorrect query
results — no error, no crash, just wrong output.

What you think should happen instead

Reproduction

-- Apache Cloudberry / GPDB, ORCA enabled (default)
SET optimizer = on;

SELECT generate_series(1, a) g, a+b ab
  FROM (VALUES (1,1),(2,2)) t(a,b)
  GROUP BY ROLLUP(a, ab)
  ORDER BY 1, 2;

Actual (buggy) result with ORCA

 g | ab
---+----
(0 rows)

Expected result (matches PostgreSQL planner, SET optimizer = off)

 g | ab
---+----
 1 |  2
 1 |  4
 1 |
 1 |
 2 |  4
 2 |
(6 rows)

### How to reproduce

RT

### Operating System

centos9

### Anything else

_No response_

### Are you willing to submit PR?

- [x] Yes, I am willing to submit a PR!

### Code of Conduct

- [x] I agree to follow this project's [Code of Conduct](https://github.com/apache/cloudberry/blob/main/CODE_OF_CONDUCT.md).

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: BugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions