Skip to content

Commit

Permalink
[nereids] group by key elimination
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongjian.xzj authored and zhongjian.xzj committed Feb 2, 2024
1 parent a4a0c7d commit 2766391
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@

package org.apache.doris.nereids.properties;

import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;

import com.google.common.collect.ImmutableSet;

public class ExprFdItem extends FdItem {
private ImmutableSet<NamedExpression> childExprs;
private ImmutableSet<SlotReference> childExprs;

public ExprFdItem(ImmutableSet<NamedExpression> parentExprs, boolean isUnique,
ImmutableSet<NamedExpression> childExprs) {
public ExprFdItem(ImmutableSet<SlotReference> parentExprs, boolean isUnique,
ImmutableSet<SlotReference> childExprs) {
super(parentExprs, isUnique, false);
this.childExprs = ImmutableSet.copyOf(childExprs);
}

@Override
public boolean checkExprInChild(Expression slot, LogicalProject project) {
public boolean checkExprInChild(SlotReference slot, LogicalPlan childPlan) {
return childExprs.contains(slot);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,28 @@

package org.apache.doris.nereids.properties;

import org.apache.doris.analysis.SlotRef;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.Slot;

import com.google.common.collect.ImmutableSet;
import org.apache.doris.catalog.Table;
import org.apache.doris.nereids.trees.expressions.SlotReference;


public class FdFactory {

public static final FdFactory INSTANCE = new FdFactory();

public TableFdItem createTableFdItem(ImmutableSet<NamedExpression> parentExprs, boolean isUnique,
public TableFdItem createTableFdItem(ImmutableSet<SlotReference> parentExprs, boolean isUnique,
boolean isCandidate, ImmutableSet<TableIf> tableIds) {
TableFdItem fdItem = new TableFdItem(parentExprs, isUnique, isCandidate, tableIds);
return fdItem;
}

public ExprFdItem createExprFdItem(ImmutableSet<NamedExpression> parentExprs, boolean isUnique,
ImmutableSet<NamedExpression> childExprs) {
public ExprFdItem createExprFdItem(ImmutableSet<SlotReference> parentExprs, boolean isUnique,
ImmutableSet<SlotReference> childExprs) {
ExprFdItem fdItem = new ExprFdItem(parentExprs, isUnique, childExprs);
return fdItem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@

package org.apache.doris.nereids.properties;

import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;

import com.google.common.collect.ImmutableSet;


public class FdItem {
private ImmutableSet<NamedExpression> parentExprs;
private ImmutableSet<SlotReference> parentExprs;

boolean isUnique;

boolean isCandidate;

public FdItem(ImmutableSet<NamedExpression> parentExprs, boolean isUnique, boolean isCandidate) {
public FdItem(ImmutableSet<SlotReference> parentExprs, boolean isUnique, boolean isCandidate) {
this.parentExprs = ImmutableSet.copyOf(parentExprs);
this.isUnique = isUnique;
this.isCandidate = isCandidate;
Expand All @@ -53,15 +54,11 @@ public void setUnique(boolean isUnique) {
this.isUnique = isUnique;
}

public ImmutableSet<NamedExpression> getParentExprs() {
public ImmutableSet<SlotReference> getParentExprs() {
return parentExprs;
}

public void setParentExprs(ImmutableSet<NamedExpression> parentExprs) {
this.parentExprs = ImmutableSet.copyOf(parentExprs);
}

public boolean checkExprInChild(Expression slot, LogicalProject project) {
public boolean checkExprInChild(SlotReference slot, LogicalPlan childPlan) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

import org.apache.doris.catalog.TableIf;
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;

import com.google.common.collect.ImmutableSet;
Expand All @@ -33,37 +33,38 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


public class TableFdItem extends FdItem {

private ImmutableSet<TableIf> childTables;

public TableFdItem(ImmutableSet<NamedExpression> parentExprs, boolean isUnique,
public TableFdItem(ImmutableSet<SlotReference> parentExprs, boolean isUnique,
boolean isCandidate, ImmutableSet<TableIf> childTables) {
super(parentExprs, isUnique, isCandidate);
this.childTables = ImmutableSet.copyOf(childTables);
}

@Override
public boolean checkExprInChild(Expression slot, LogicalProject project) {
NamedExpression slotInProject = null;
List<NamedExpression> projectList = project.getProjects();
for (NamedExpression expr : projectList) {
if (expr.getExprId().equals(((SlotReference)slot).getExprId())) {
slotInProject = expr;
public boolean checkExprInChild(SlotReference slot, LogicalPlan childPlan) {
NamedExpression slotInChild = null;
List<NamedExpression> exprList = ((LogicalProject) childPlan).getProjects();
for (NamedExpression expr : exprList) {
if (expr.getExprId().equals(slot.getExprId())) {
slotInChild = expr;
break;
}
}
if (slotInProject != null) {
if (slotInChild != null) {
Set<Slot> slotSet = new HashSet<>();
if (slotInProject instanceof Alias) {
slotSet = ((Alias) slotInProject).getInputSlots();
if (slotInChild instanceof Alias) {
slotSet = ((Alias) slotInChild).getInputSlots();
} else {
slotSet.add((Slot)slotInProject);
slotSet.add((SlotReference) slotInChild);
}
// get table list from slotSet
Set<TableIf> tableSets = getTableIds(slotSet, project);
Set<TableIf> tableSets = getTableIds(slotSet, childPlan);
if (childTables.containsAll(tableSets)) {
return true;
} else {
Expand All @@ -74,20 +75,20 @@ public boolean checkExprInChild(Expression slot, LogicalProject project) {
}
}

private Set<TableIf> getTableIds(Set<Slot> slotSet, LogicalProject project) {
private Set<TableIf> getTableIds(Set<Slot> slotSet, LogicalPlan project) {
List<LogicalCatalogRelation> tableList = getTableListUnderProject(project);
Set<TableIf> resultSet = new HashSet<>();
for (Slot slot : slotSet) {
for (LogicalCatalogRelation table : tableList) {
if (table.getOutputExprIds().contains(((SlotReference)slot).getExprId())) {
if (table.getOutputExprIds().contains(slot.getExprId())) {
resultSet.add(table.getTable());
}
}
}
return resultSet;
}

private List<LogicalCatalogRelation> getTableListUnderProject(LogicalProject project) {
private List<LogicalCatalogRelation> getTableListUnderProject(LogicalPlan project) {
List<LogicalCatalogRelation> tableLists = new ArrayList<>();
tableLists.addAll((Collection<? extends LogicalCatalogRelation>) project
.collect(LogicalCatalogRelation.class::isInstance));
Expand Down
Loading

0 comments on commit 2766391

Please sign in to comment.