Skip to content

Commit

Permalink
fix repeat compute equalset
Browse files Browse the repository at this point in the history
  • Loading branch information
feiniaofeiafei committed Feb 11, 2025
1 parent 853e99b commit 4767bf5
Show file tree
Hide file tree
Showing 4 changed files with 32 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 @@ -197,6 +199,15 @@ public void computeUniform(DataTrait.Builder builder) {
@Override
public void computeEqualSet(DataTrait.Builder builder) {
builder.addEqualSet(child().getLogicalProperties().getTrait());
Set<Expression> common = getCommonGroupingSetExpressions();
Set<Slot> slots = new HashSet<>();
for (Expression expr : common) {
if (!(expr instanceof Slot)) {
return;
}
slots.add((Slot) expr);
}
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 @@ -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 4767bf5

Please sign in to comment.