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 @@ -790,33 +790,33 @@ public PlanNode visitDelete(DeleteNode node, RewriteContext<Set<VariableReferenc
@Override
public PlanNode visitUnion(UnionNode node, RewriteContext<Set<VariableReferenceExpression>> context)
{
ListMultimap<VariableReferenceExpression, VariableReferenceExpression> rewrittenVariableMapping = rewriteSetOperationVariableMapping(node, context);
ListMultimap<VariableReferenceExpression, VariableReferenceExpression> rewrittenVariableMapping = rewriteSetOperationVariableMapping(node, context, true);
ImmutableList<PlanNode> rewrittenSubPlans = rewriteSetOperationSubPlans(node, context, rewrittenVariableMapping);
return new UnionNode(node.getSourceLocation(), node.getId(), node.getStatsEquivalentPlanNode(), rewrittenSubPlans, ImmutableList.copyOf(rewrittenVariableMapping.keySet()), fromListMultimap(rewrittenVariableMapping));
}

@Override
public PlanNode visitIntersect(IntersectNode node, RewriteContext<Set<VariableReferenceExpression>> context)
{
ListMultimap<VariableReferenceExpression, VariableReferenceExpression> rewrittenVariableMapping = rewriteSetOperationVariableMapping(node, context);
ListMultimap<VariableReferenceExpression, VariableReferenceExpression> rewrittenVariableMapping = rewriteSetOperationVariableMapping(node, context, false);
Copy link
Copy Markdown
Member

@jaystarshot jaystarshot Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit- The method is called rewriteSetOperationVariableMapping, so Instead of changing the method to not prune, can we use inline and exit early where it should be false?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rewriteSetOperationVariableMapping is doing two things, 1) prune variables 2) rewrite Map<Variable, List> to ListMultimap<Variable, Variable>. For the intersect and except node, it does not need to be pruned. Ideally we do not even need to rewrite to ListMultimap here, however it needs to change function rewriteSetOperationSubPlans as well, which seems too much change for this fix. After some thought I found the current way to be with minimum change in code and more direct in intention, i.e. skip pruning for these two nodes.

Copy link
Copy Markdown
Member

@jaystarshot jaystarshot Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way we can also do is like this

 @Override
        public PlanNode visitIntersect(IntersectNode node, RewriteContext<Set<VariableReferenceExpression>> context)
        {

            Set<VariableReferenceExpression> expectedInputs = new HashSet<>(context.get());
            expectedInputs.addAll(node.getOutputVariables());
            ListMultimap<VariableReferenceExpression, VariableReferenceExpression> rewrittenVariableMapping = rewriteSetOperationVariableMapping(node, expectedInputs);
            ImmutableList<PlanNode> rewrittenSubPlans = rewriteSetOperationSubPlans(node, context, rewrittenVariableMapping);
            return new IntersectNode(node.getSourceLocation(), node.getId(), rewrittenSubPlans, ImmutableList.copyOf(rewrittenVariableMapping.keySet()), fromListMultimap(rewrittenVariableMapping));
        }

Basically add intersects output as expectedInputs and refactor rewriteSetOperationVariableMapping function to take Set as the input. I think this is more in line logically with this optimizer.
Not blocking the current approach just adding a discussion point

ImmutableList<PlanNode> rewrittenSubPlans = rewriteSetOperationSubPlans(node, context, rewrittenVariableMapping);
return new IntersectNode(node.getSourceLocation(), node.getId(), node.getStatsEquivalentPlanNode(), rewrittenSubPlans, ImmutableList.copyOf(rewrittenVariableMapping.keySet()), fromListMultimap(rewrittenVariableMapping));
}

@Override
public PlanNode visitExcept(ExceptNode node, RewriteContext<Set<VariableReferenceExpression>> context)
{
ListMultimap<VariableReferenceExpression, VariableReferenceExpression> rewrittenVariableMapping = rewriteSetOperationVariableMapping(node, context);
ListMultimap<VariableReferenceExpression, VariableReferenceExpression> rewrittenVariableMapping = rewriteSetOperationVariableMapping(node, context, false);
ImmutableList<PlanNode> rewrittenSubPlans = rewriteSetOperationSubPlans(node, context, rewrittenVariableMapping);
return new ExceptNode(node.getSourceLocation(), node.getId(), node.getStatsEquivalentPlanNode(), rewrittenSubPlans, ImmutableList.copyOf(rewrittenVariableMapping.keySet()), fromListMultimap(rewrittenVariableMapping));
}

private ListMultimap<VariableReferenceExpression, VariableReferenceExpression> rewriteSetOperationVariableMapping(SetOperationNode node, RewriteContext<Set<VariableReferenceExpression>> context)
private ListMultimap<VariableReferenceExpression, VariableReferenceExpression> rewriteSetOperationVariableMapping(SetOperationNode node, RewriteContext<Set<VariableReferenceExpression>> context, boolean pruneUnreferencedOutput)
{
// Find out which output variables we need to keep
ImmutableListMultimap.Builder<VariableReferenceExpression, VariableReferenceExpression> rewrittenVariableMappingBuilder = ImmutableListMultimap.builder();
for (VariableReferenceExpression variable : node.getOutputVariables()) {
if (context.get().contains(variable)) {
if (context.get().contains(variable) || !pruneUnreferencedOutput) {
rewrittenVariableMappingBuilder.putAll(
variable,
node.getVariableMapping().get(variable));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import com.facebook.presto.spi.plan.Ordering;
import com.facebook.presto.spi.plan.OrderingScheme;
import com.facebook.presto.spi.relation.CallExpression;
import com.facebook.presto.spi.relation.VariableReferenceExpression;
import com.facebook.presto.sql.planner.assertions.OptimizerAssert;
import com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest;
import com.facebook.presto.sql.planner.iterative.rule.test.RuleTester;
import com.facebook.presto.sql.planner.plan.WindowNode;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import org.testng.annotations.Test;

Expand All @@ -32,6 +34,8 @@
import static com.facebook.presto.common.type.BigintType.BIGINT;
import static com.facebook.presto.common.type.VarcharType.VARCHAR;
import static com.facebook.presto.metadata.MetadataManager.createTestMetadataManager;
import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.except;
import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.intersect;
import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.output;
import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.project;
import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.values;
Expand Down Expand Up @@ -89,6 +93,52 @@ public void windowNodePruning()
values("user_uuid")))));
}

@Test
public void testIntersectNodePruning()
{
assertRuleApplication()
.on(p ->
p.output(ImmutableList.of("regionkey"), ImmutableList.of(p.variable("regionkey_16")),
p.project(Assignments.of(p.variable("regionkey_16"), p.variable("regionkey_16")),
p.intersect(
ImmutableListMultimap.<VariableReferenceExpression, VariableReferenceExpression>builder()
.putAll(p.variable("nationkey_15"), p.variable("nationkey"), p.variable("regionkey_6"))
.putAll(p.variable("regionkey_16"), p.variable("regionkey"), p.variable("regionkey_6"))
.build(),
ImmutableList.of(
p.values(p.variable("nationkey"), p.variable("regionkey")),
p.values(p.variable("regionkey_6")))))))
.matches(
output(
project(
intersect(
values("nationkey", "regionkey"),
values("regionkey_6")))));
}

@Test
public void testExceptNodePruning()
{
assertRuleApplication()
.on(p ->
p.output(ImmutableList.of("regionkey"), ImmutableList.of(p.variable("regionkey_16")),
p.project(Assignments.of(p.variable("regionkey_16"), p.variable("regionkey_16")),
p.except(
ImmutableListMultimap.<VariableReferenceExpression, VariableReferenceExpression>builder()
.putAll(p.variable("nationkey_15"), p.variable("nationkey"), p.variable("regionkey_6"))
.putAll(p.variable("regionkey_16"), p.variable("regionkey"), p.variable("regionkey_6"))
.build(),
ImmutableList.of(
p.values(p.variable("nationkey"), p.variable("regionkey")),
p.values(p.variable("regionkey_6")))))))
.matches(
output(
project(
except(
values("nationkey", "regionkey"),
values("regionkey_6")))));
}

private OptimizerAssert assertRuleApplication()
{
RuleTester tester = tester();
Expand Down