Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
import org.apache.doris.nereids.trees.plans.logical.LogicalUnion;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;

import java.util.ArrayList;
Expand Down Expand Up @@ -198,101 +200,105 @@ private Plan pushThroughUnion(MatchingContext<LogicalProject<LogicalUnion>> ctx)
if (!ctx.connectContext.getSessionVariable().enablePruneNestedColumns) {
return ctx.root;
}
LogicalProject<LogicalUnion> project = ctx.root;
return pushThroughUnion(ctx.root, ctx.statementContext);
}

@VisibleForTesting
static Plan pushThroughUnion(LogicalProject<LogicalUnion> project, StatementContext statementContext) {
LogicalUnion union = project.child();
PushdownProjectHelper pushdownProjectHelper
= new PushdownProjectHelper(ctx.statementContext, project);

= new PushdownProjectHelper(statementContext, project);
Pair<Boolean, List<NamedExpression>> pushProjects
= pushdownProjectHelper.pushDownExpressions(project.getProjects());
if (pushProjects.first) {
List<NamedExpression> unionOutputs = union.getOutputs();
Map<Slot, Integer> slotToColumnIndex = new LinkedHashMap<>();
for (int i = 0; i < unionOutputs.size(); i++) {
NamedExpression output = unionOutputs.get(i);
slotToColumnIndex.put(output.toSlot(), i);
}
if (!pushProjects.first) {
return project;
}
List<NamedExpression> unionOutputs = union.getOutputs();
Map<Slot, Integer> slotToColumnIndex = new LinkedHashMap<>();
for (int i = 0; i < unionOutputs.size(); i++) {
NamedExpression output = unionOutputs.get(i);
slotToColumnIndex.put(output.toSlot(), i);
}

Collection<NamedExpression> pushDownProjections
= pushdownProjectHelper.childToPushDownProjects.values();
List<Plan> newChildren = new ArrayList<>();
List<List<SlotReference>> newChildrenOutputs = new ArrayList<>();
for (Plan child : union.children()) {
List<NamedExpression> pushedOutput = replaceSlot(
ctx.statementContext,
pushDownProjections,
slot -> {
Integer sourceColumnIndex = slotToColumnIndex.get(slot);
if (sourceColumnIndex != null) {
return child.getOutput().get(sourceColumnIndex).toSlot();
}
return slot;
List<NamedExpression> pushDownProjections
= Lists.newArrayList(pushdownProjectHelper.childToPushDownProjects.values());
List<Plan> newChildren = new ArrayList<>();
List<List<SlotReference>> newChildrenOutputs = new ArrayList<>();
for (int i = 0; i < union.arity(); i++) {
List<SlotReference> regulatorOutput = union.getRegularChildOutput(i);
List<NamedExpression> pushedOutput = replaceSlot(
statementContext,
pushDownProjections,
slot -> {
Integer sourceColumnIndex = slotToColumnIndex.get(slot);
if (sourceColumnIndex != null) {
return regulatorOutput.get(sourceColumnIndex).toSlot();
}
);

LogicalProject<Plan> newChild = new LogicalProject<>(
ImmutableList.<NamedExpression>builder()
.addAll(child.getOutput())
.addAll(pushedOutput)
.build(),
child
);

newChildrenOutputs.add((List) newChild.getOutput());
newChildren.add(newChild);
}
return slot;
}
);

for (List<NamedExpression> originConstantExprs : union.getConstantExprsList()) {
List<NamedExpression> pushedOutput = replaceSlot(
ctx.statementContext,
pushDownProjections,
slot -> {
Integer sourceColumnIndex = slotToColumnIndex.get(slot);
if (sourceColumnIndex != null) {
return originConstantExprs.get(sourceColumnIndex).toSlot();
}
return slot;
LogicalProject<Plan> newChild = new LogicalProject<>(
ImmutableList.<NamedExpression>builder()
.addAll(regulatorOutput)
.addAll(pushedOutput)
.build(),
union.child(i)
);

newChildrenOutputs.add((List) newChild.getOutput());
newChildren.add(newChild);
}

for (List<NamedExpression> originConstantExprs : union.getConstantExprsList()) {
List<NamedExpression> pushedOutput = replaceSlot(
statementContext,
pushDownProjections,
slot -> {
Integer sourceColumnIndex = slotToColumnIndex.get(slot);
if (sourceColumnIndex != null) {
return originConstantExprs.get(sourceColumnIndex).toSlot();
}
);

LogicalOneRowRelation originOneRowRelation = new LogicalOneRowRelation(
ctx.statementContext.getNextRelationId(),
originConstantExprs
);

LogicalProject<Plan> newChild = new LogicalProject<>(
ImmutableList.<NamedExpression>builder()
.addAll(originOneRowRelation.getOutput())
.addAll(pushedOutput)
.build(),
originOneRowRelation
);

newChildrenOutputs.add((List) newChild.getOutput());
newChildren.add(newChild);
}
return slot;
}
);

List<NamedExpression> newUnionOutputs = new ArrayList<>(union.getOutputs());
for (NamedExpression projection : pushDownProjections) {
newUnionOutputs.add(projection.toSlot());
}
LogicalOneRowRelation originOneRowRelation = new LogicalOneRowRelation(
statementContext.getNextRelationId(),
originConstantExprs
);

return new LogicalProject<>(
pushProjects.second,
new LogicalUnion(
union.getQualifier(),
newUnionOutputs,
newChildrenOutputs,
ImmutableList.of(),
union.hasPushedFilter(),
newChildren
)
LogicalProject<Plan> newChild = new LogicalProject<>(
ImmutableList.<NamedExpression>builder()
.addAll(originOneRowRelation.getOutput())
.addAll(pushedOutput)
.build(),
originOneRowRelation
);

newChildrenOutputs.add((List) newChild.getOutput());
newChildren.add(newChild);
}
return project;

List<NamedExpression> newUnionOutputs = new ArrayList<>(union.getOutputs());
for (NamedExpression projection : pushDownProjections) {
newUnionOutputs.add(projection.toSlot());
}

return new LogicalProject<>(
pushProjects.second,
new LogicalUnion(
union.getQualifier(),
newUnionOutputs,
newChildrenOutputs,
ImmutableList.of(),
union.hasPushedFilter(),
newChildren
)
);
}

private List<NamedExpression> replaceSlot(
private static List<NamedExpression> replaceSlot(
StatementContext statementContext,
Collection<NamedExpression> pushDownProjections,
Function<Slot, Slot> slotReplace) {
Expand Down
Loading
Loading