Skip to content

Commit

Permalink
table-signature
Browse files Browse the repository at this point in the history
  • Loading branch information
englefly committed Dec 17, 2024
1 parent 86c3c76 commit 296fa02
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.doris.common.util.DebugUtil;
import org.apache.doris.mysql.FieldInfo;
import org.apache.doris.nereids.CascadesContext.Lock;
import org.apache.doris.nereids.commonCTE.CteExtractor;
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.glue.LogicalPlanAdapter;
import org.apache.doris.nereids.glue.translator.PhysicalPlanTranslator;
Expand Down Expand Up @@ -59,6 +60,7 @@
import org.apache.doris.nereids.trees.plans.distribute.DistributePlanner;
import org.apache.doris.nereids.trees.plans.distribute.DistributedPlan;
import org.apache.doris.nereids.trees.plans.distribute.FragmentIdMapping;
import org.apache.doris.nereids.trees.plans.logical.AbstractLogicalPlan;
import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
Expand Down Expand Up @@ -284,6 +286,8 @@ protected Plan planWithoutLock(

setRuntimeFilterWaitTimeByTableRowCountAndType();

extractCommonCTE();

optimize();
if (statementContext.getConnectContext().getExecutor() != null) {
statementContext.getConnectContext().getExecutor().getSummaryProfile().setNereidsOptimizeTime();
Expand Down Expand Up @@ -320,6 +324,11 @@ protected LogicalPlan preprocess(LogicalPlan logicalPlan) {
return new PlanPreprocessors(statementContext).process(logicalPlan);
}

private void extractCommonCTE() {
CteExtractor commonCTE = new CteExtractor((AbstractLogicalPlan) cascadesContext.getRewritePlan());
commonCTE.execute();
}

/**
* compute rf wait time according to max table row count, if wait time is not default value
* olap:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.apache.doris.nereids.commonCTE;

import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.AbstractLogicalPlan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class CteExtractor {
private AbstractLogicalPlan plan;
private Map<Plan, TableSignature> anchorToSignature = new HashMap<>();
private Map<TableSignature, List<Plan>> signatureToAnchorList = new HashMap<>();

public CteExtractor(AbstractLogicalPlan plan) {
this.plan = plan;
}

public AbstractLogicalPlan execute() {
sign();
return plan;
}

private void sign() {
SignatureVisitor visitor = new SignatureVisitor();
visitor.visit(plan, anchorToSignature);
extract();
}

private void extract() {
List<TableSignature> a = anchorToSignature.values().stream().collect(Collectors.toList());


for (Plan plan : anchorToSignature.keySet()) {
TableSignature signature = anchorToSignature.get(plan);
List<Plan> plans = signatureToAnchorList.computeIfAbsent(signature, key -> new ArrayList<>());
plans.add(plan);
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.apache.doris.nereids.commonCTE;

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;
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
import org.apache.doris.nereids.trees.plans.visitor.DefaultPlanVisitor;

import com.google.common.collect.ImmutableSet;

import java.util.Map;

public class SignatureVisitor extends DefaultPlanVisitor <TableSignature, Map<Plan, TableSignature>>{

@Override
public TableSignature visitLogicalCatalogRelation(LogicalCatalogRelation relation,
Map<Plan, TableSignature> signatureMap) {
TableSignature signature = new TableSignature(true, false,
ImmutableSet.of(relation.getTable().getId()));
signatureMap.put(relation, signature);
return signature;
}

@Override
public TableSignature visitLogicalFilter(LogicalFilter<? extends Plan> filter,
Map<Plan, TableSignature> signatureMap) {
TableSignature childSignature = filter.child().accept(this, signatureMap);
if (filter.child() instanceof LogicalAggregate) {
return TableSignature.EMPTY;
}
signatureMap.put(filter, childSignature);
return childSignature;
}

@Override
public TableSignature visitLogicalJoin(LogicalJoin<? extends Plan, ? extends Plan> join,
Map<Plan, TableSignature> signatureMap) {
TableSignature signature = TableSignature.EMPTY;
TableSignature leftSignature = join.left().accept(this, signatureMap);

TableSignature rightSignature = join.right().accept(this, signatureMap);

if (leftSignature != TableSignature.EMPTY && rightSignature != TableSignature.EMPTY) {
signature = new TableSignature(true,
leftSignature.isContainsAggregation() || rightSignature.isContainsAggregation(),
new ImmutableSet.Builder()
.addAll(leftSignature.getTableIds())
.addAll(rightSignature.getTableIds())
.build());
signatureMap.put(join, signature);
}
return signature;
}

@Override
public TableSignature visitLogicalAggregate(LogicalAggregate<? extends Plan> aggregate,
Map<Plan, TableSignature> signatureMap) {
TableSignature signature = TableSignature.EMPTY;
TableSignature childSignature = aggregate.child().accept(this, signatureMap);
if (childSignature != TableSignature.EMPTY) {
signature = childSignature.withContainsAggregation(true);
signatureMap.put(aggregate, signature);
}
return signature;
}

@Override
public TableSignature visitLogicalProject(LogicalProject<? extends Plan> project,
Map<Plan, TableSignature> signatureMap) {
TableSignature childSignature = project.child().accept(this, signatureMap);
if (childSignature != TableSignature.EMPTY) {
signatureMap.put(project, childSignature);
}
return childSignature;
}

@Override
public TableSignature visit(Plan plan, Map<Plan, TableSignature> signatureMap) {
for (Plan child : plan.children()) {
child.accept(this, signatureMap);
}
return TableSignature.EMPTY;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.apache.doris.nereids.commonCTE;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import java.util.List;
import java.util.Set;

public class TableSignature {
private final boolean isSPJG;
private final boolean containsAggregation;
private final Set<Long> tableIds;

public static TableSignature EMPTY = new TableSignature(false, false, ImmutableSet.of());

public TableSignature(boolean isSPJG, boolean containsAggregation , Set<Long> tableIds) {
this.isSPJG = isSPJG;
this.tableIds = ImmutableSet.copyOf(tableIds);
this.containsAggregation = containsAggregation;
}

public boolean isSPJG() {
return isSPJG;
}

public boolean isContainsAggregation() {
return containsAggregation;
}

public Set<Long> getTableIds() {
return tableIds;
}

public TableSignature withContainsAggregation(boolean containsAggregation) {
return new TableSignature(isSPJG, containsAggregation, tableIds);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (isSPJG) {
sb.append("SPJG ");
}
if (containsAggregation) {
sb.append("AGG ");
}
if (tableIds != null && !tableIds.isEmpty()) {
sb.append(tableIds);
}
return sb.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TableSignature that = (TableSignature) o;
return isSPJG == that.isSPJG && containsAggregation == that.containsAggregation
&& tableIds.equals(that.tableIds);
}

@Override
public int hashCode() {
return getClass().hashCode() + tableIds.hashCode();
}
}

0 comments on commit 296fa02

Please sign in to comment.