Skip to content

Commit

Permalink
[fix](mtmv) Related partition exclude null generate column when incre…
Browse files Browse the repository at this point in the history
…ment build materialized view (#28855)

Infer partition column by materialized view partition column, exclude null generate column in join when increment build materialized view
  • Loading branch information
seawinde authored Dec 23, 2023
1 parent dfbf082 commit 37777dc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.WindowExpression;
import org.apache.doris.nereids.trees.plans.JoinType;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation;
Expand All @@ -45,6 +46,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
* The common util for materialized view
Expand Down Expand Up @@ -175,6 +177,28 @@ public Void visitLogicalFilter(LogicalFilter<? extends Plan> filter, IncrementCh
@Override
public Void visitLogicalJoin(LogicalJoin<? extends Plan, ? extends Plan> join,
IncrementCheckerContext context) {
Plan left = join.child(0);
Set<Column> leftColumnSet = left.getOutputSet().stream()
.filter(slot -> slot instanceof SlotReference
&& slot.isColumnFromTable())
.map(slot -> ((SlotReference) slot).getColumn().get())
.collect(Collectors.toSet());
boolean useLeft = leftColumnSet.contains(context.getMvPartitionColumn().getColumn().get());
JoinType joinType = join.getJoinType();
if (joinType.isInnerJoin() || joinType.isCrossJoin()) {
context.setPctPossible(true);
} else if (joinType.isLeftJoin()
|| joinType.isLefSemiJoin()
|| joinType.isLeftAntiJoin()) {
context.setPctPossible(useLeft);
} else if (joinType.isRightJoin()
|| joinType.isRightAntiJoin()
|| joinType.isRightSemiJoin()) {
context.setPctPossible(!useLeft);
} else {
// un supported join type
context.setPctPossible(false);
}
return visit(join, context);
}

Expand Down Expand Up @@ -272,6 +296,7 @@ private static final class IncrementCheckerContext {
private boolean pctPossible = true;
private TableIf relatedTable;
private Column relatedTableColumn;
private boolean joinNullGenerateSide;

public IncrementCheckerContext(SlotReference mvPartitionColumn) {
this.mvPartitionColumn = mvPartitionColumn;
Expand Down Expand Up @@ -304,6 +329,14 @@ public Column getRelatedTableColumn() {
public void setRelatedTableColumn(Column relatedTableColumn) {
this.relatedTableColumn = relatedTableColumn;
}

public boolean isJoinNullGenerateSide() {
return joinNullGenerateSide;
}

public void setJoinNullGenerateSide(boolean joinNullGenerateSide) {
this.joinNullGenerateSide = joinNullGenerateSide;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void getRelatedTableInfoUseRightTest() {
+ " (select * from "
+ " lineitem "
+ " where L_SHIPDATE in ('2017-01-30')) t1 "
+ "left join "
+ "right join "
+ " (select * from "
+ " orders "
+ " where O_ORDERDATE in ('2017-01-30')) t2 "
Expand All @@ -207,6 +207,33 @@ public void getRelatedTableInfoUseRightTest() {
});
}

@Test
public void getRelatedTableInfoUseNullGenerateSideTest() {
PlanChecker.from(connectContext)
.checkExplain("SELECT t1.L_SHIPDATE, t2.O_ORDERDATE, t1.L_QUANTITY, t2.O_ORDERSTATUS, "
+ "count(distinct case when t1.L_SUPPKEY > 0 then t2.O_ORDERSTATUS else null end) as cnt_1 "
+ "from "
+ " (select * from "
+ " lineitem "
+ " where L_SHIPDATE in ('2017-01-30')) t1 "
+ "left join "
+ " (select * from "
+ " orders "
+ " where O_ORDERDATE in ('2017-01-30')) t2 "
+ "on t1.L_ORDERKEY = t2.O_ORDERKEY "
+ "group by "
+ "t1.L_SHIPDATE, "
+ "t2.O_ORDERDATE, "
+ "t1.L_QUANTITY, "
+ "t2.O_ORDERSTATUS;",
nereidsPlanner -> {
Plan rewrittenPlan = nereidsPlanner.getRewrittenPlan();
Optional<RelatedTableInfo> relatedTableInfo =
MaterializedViewUtils.getRelatedTableInfo("o_orderdate", rewrittenPlan);
Assertions.assertFalse(relatedTableInfo.isPresent());
});
}

@Test
public void getRelatedTableInfoTestWithoutPartitionTest() {
PlanChecker.from(connectContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ suite("inner_join") {
sql """ DROP MATERIALIZED VIEW IF EXISTS mv6_0"""


// filter inside + inner + right
// filter inside + left + right
def mv7_0 = "select l_shipdate, o_orderdate, l_partkey, l_suppkey " +
"from lineitem " +
"inner join (select * from orders where o_orderdate = '2023-12-08') t2 " +
Expand Down

0 comments on commit 37777dc

Please sign in to comment.