Skip to content

Commit

Permalink
[fix](nereids) fix LogicalRepeat compute equalset (#47737)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

Issue Number: close #xxx

Related PR: #xxx

Problem Summary:
This PR addresses an issue where LogicalRepeat should not propagate
equalset because, in scenarios involving group by cube(a, b) with a = b,
LogicalRepeat generates four rows (e.g., when a = b = 3, the rows are
(3,3), (3,null), (null,3), and (null,null)), which breaks the equalset
relationship a = b. To resolve this, the equalset slots are now retained
only when they are part of the common set of LogicalRepeat's group by
keys. This ensures the integrity of equalset is maintained during group
by cube operations, preventing incorrect query results.
  • Loading branch information
feiniaofeiafei authored Feb 21, 2025
1 parent 5a7454e commit 2dd9689
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ public void pruneSlots(Set<Slot> outputSlots) {
fdDgBuilder.removeNotContain(outputSlots);
}

public void pruneEqualSetSlots(Set<Slot> outputSlots) {
equalSetBuilder.removeNotContain(outputSlots);
}

public void replaceUniformBy(Map<Slot, Slot> replaceMap) {
uniformSet.replace(replaceMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

/**
* LogicalRepeat.
Expand Down Expand Up @@ -196,7 +198,16 @@ public void computeUniform(DataTrait.Builder builder) {

@Override
public void computeEqualSet(DataTrait.Builder builder) {
Set<Expression> common = getCommonGroupingSetExpressions();
Set<Slot> slots = new HashSet<>();
for (Expression expr : common) {
if (!(expr instanceof Slot)) {
return;
}
slots.add((Slot) expr);
}
builder.addEqualSet(child().getLogicalProperties().getTrait());
builder.pruneEqualSetSlots(slots);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected void runBeforeAll() throws Exception {
+ "UNIQUE KEY(id)\n"
+ "distributed by hash(id) buckets 10\n"
+ "properties('replication_num' = '1');");
createTable("create table test.eli_gbk_t(a int, b int) distributed by hash(a) properties('replication_num'='1');");
connectContext.setDatabase("test");
connectContext.getSessionVariable().setDisableNereidsRules("PRUNE_EMPTY_PARTITION");
}
Expand Down Expand Up @@ -179,4 +180,18 @@ void testEliminateByEqual() {
agg.getGroupByExpressions().size() == 1
&& agg.getGroupByExpressions().get(0).toSql().equals("name")));
}

@Test
void testRepeatEliminateByEqual() {
PlanChecker.from(connectContext)
.analyze("select count(1) from (select a,b from eli_gbk_t where a=b group by grouping sets((a,b),(b,a))) t group by a,b;")
.rewrite()
.printlnTree()
.matches(logicalAggregate().when(agg -> agg.getGroupByExpressions().size() == 1));
PlanChecker.from(connectContext)
.analyze("select count(1) from (select a,b from eli_gbk_t where a=b group by cube(a,b)) t group by a,b;")
.rewrite()
.printlnTree()
.matches(logicalAggregate().when(agg -> agg.getGroupByExpressions().size() == 2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@
1
1

-- !grouping_equalset --
1
1
1
1

-- !grouping_equalset_can_eliminate --
2

Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,6 @@ suite("eliminate_gby_key") {
sql "create table eli_gbk_t(a int, b int) distributed by hash(a) properties('replication_num'='1');"
sql "insert into eli_gbk_t values(1,1),(2,1),(3,1);"
qt_grouping """select count(1) from (select b as k, a k3, sum(b) as sum_k1 from eli_gbk_t where b=1 group by cube(k,a)) t group by k,k3 order by 1"""
qt_grouping_equalset """select count(1) from (select a,b from eli_gbk_t where a=b group by cube(a,b)) t group by a,b;"""
qt_grouping_equalset_can_eliminate """select count(1) from (select a,b from eli_gbk_t where a=b group by grouping sets((a,b),(b,a))) t group by a,b;"""
}

0 comments on commit 2dd9689

Please sign in to comment.