Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/backend/gpopt/translate/CTranslatorUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ CTranslatorUtils::GetColumnAttnosForGroupBy(
case GROUPING_SET_EMPTY:
{
col_attnos_arr_current = GPOS_NEW(mp) CBitSetArray(mp);
CBitSet *bset = GPOS_NEW(mp) CBitSet(mp);
CBitSet *bset = GPOS_NEW(mp) CBitSet(mp, num_cols);
Comment thread
yjhjstz marked this conversation as resolved.
col_attnos_arr_current->Append(bset);
break;
}
Expand Down Expand Up @@ -1113,11 +1113,12 @@ CTranslatorUtils::CreateGroupingSetsForRollup(CMemoryPool *mp,
GPOS_ASSERT(grouping_set->kind == GROUPING_SET_ROLLUP);
CBitSetArray *col_attnos_arr = GPOS_NEW(mp) CBitSetArray(mp);
ListCell *lc = nullptr;
CBitSet *current_result = GPOS_NEW(mp) CBitSet(mp);

CBitSet *current_result = GPOS_NEW(mp) CBitSet(mp, num_cols);
// Maintaining the order of grouping sets is essential because the
// UnionAll operator matches each child's distribution with the
// distribution of the first child
col_attnos_arr->Append(GPOS_NEW(mp) CBitSet(mp));
col_attnos_arr->Append(GPOS_NEW(mp) CBitSet(mp, num_cols));
ForEach(lc, grouping_set->content)
{
GroupingSet *gs_current = (GroupingSet *) lfirst(lc);
Expand Down Expand Up @@ -1152,8 +1153,9 @@ CTranslatorUtils::CreateGroupingSetsForCube(CMemoryPool *mp,
GPOS_ASSERT(grouping_set->kind == GROUPING_SET_CUBE);
CBitSetArray *col_attnos_arr = GPOS_NEW(mp) CBitSetArray(mp);

// add an empty set
col_attnos_arr->Append(GPOS_NEW(mp) CBitSet(mp));
// add an empty set — vec_size must match what CreateAttnoSetForGroupingSet
// produces (num_cols), otherwise Union below leaves misaligned links.
col_attnos_arr->Append(GPOS_NEW(mp) CBitSet(mp, num_cols));

ListCell *lc = nullptr;
ForEach(lc, grouping_set->content)
Expand Down
6 changes: 6 additions & 0 deletions src/backend/gporca/libgpos/src/common/CBitSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ CBitSet::ExchangeClear(ULONG pos)
void
CBitSet::Union(const CBitSet *pbsOther)
{
GPOS_ASSERT(m_vector_size == pbsOther->m_vector_size);

CBitSetLink *bsl = nullptr;
CBitSetLink *bsl_other = nullptr;

Expand Down Expand Up @@ -425,6 +427,10 @@ CBitSet::Intersection(const CBitSet *pbsOther)
return;
}

// See CBitSet::Union: link offsets depend on m_vector_size, so mixing
// bitsets with different sizes makes FindLinkByOffset miss links.
GPOS_ASSERT(m_vector_size == pbsOther->m_vector_size);

CBitSetLink *bsl_other = nullptr;
CBitSetLink *bsl = m_bsllist.First();

Expand Down
28 changes: 28 additions & 0 deletions src/test/regress/expected/groupingsets.out
Original file line number Diff line number Diff line change
Expand Up @@ -2470,4 +2470,32 @@ group by rollup (a,b) order by a;
| | 6
(8 rows)

-- ORCA: rollup over a derived-expression group alias with a target-list SRF.
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;
g | ab
---+----
1 | 2
1 | 4
1 |
1 |
2 | 4
2 |
(6 rows)

-- Same shape with cube(): exercises additional grouping-set combinations.
select generate_series(1, a) g, a+b ab
from (values (1,1),(2,2)) t(a,b)
group by cube(a, ab) order by 1,2;
g | ab
---+----
1 | 2
1 | 4
1 |
1 |
2 | 4
2 |
(6 rows)

-- end
28 changes: 28 additions & 0 deletions src/test/regress/expected/groupingsets_optimizer.out
Original file line number Diff line number Diff line change
Expand Up @@ -2645,4 +2645,32 @@ group by rollup (a,b) order by a;
| | 6
(8 rows)

-- ORCA: rollup over a derived-expression group alias with a target-list SRF.
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;
Comment thread
yjhjstz marked this conversation as resolved.
g | ab
---+----
1 | 2
1 | 4
1 |
1 |
2 | 4
2 |
(6 rows)

-- Same shape with cube(): exercises additional grouping-set combinations.
select generate_series(1, a) g, a+b ab
from (values (1,1),(2,2)) t(a,b)
group by cube(a, ab) order by 1,2;
g | ab
---+----
1 | 2
1 | 4
1 |
1 |
2 | 4
2 |
(6 rows)

-- end
10 changes: 10 additions & 0 deletions src/test/regress/sql/groupingsets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -723,4 +723,14 @@ select a, b, rank(b) within group (order by b nulls last)
from (values (1,1),(1,4),(1,5),(3,1),(3,2)) v(a,b)
group by rollup (a,b) order by a;

-- ORCA: rollup over a derived-expression group alias with a target-list SRF.
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;

-- Same shape with cube(): exercises additional grouping-set combinations.
select generate_series(1, a) g, a+b ab
from (values (1,1),(2,2)) t(a,b)
group by cube(a, ab) order by 1,2;

-- end
Loading