forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
fe/fe-core/src/main/java/org/apache/doris/nereids/commonCTE/CteExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
|
||
} |
88 changes: 88 additions & 0 deletions
88
fe/fe-core/src/main/java/org/apache/doris/nereids/commonCTE/SignatureVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
|
70 changes: 70 additions & 0 deletions
70
fe/fe-core/src/main/java/org/apache/doris/nereids/commonCTE/TableSignature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |