Skip to content
Closed
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 @@ -27,7 +27,6 @@
import org.elasticsearch.xpack.esql.plan.logical.UnaryPlan;
import org.elasticsearch.xpack.esql.plan.logical.UnionAll;
import org.elasticsearch.xpack.esql.plan.logical.join.InlineJoin;
import org.elasticsearch.xpack.esql.plan.logical.join.StubRelation;
import org.elasticsearch.xpack.esql.plan.logical.local.LocalRelation;
import org.elasticsearch.xpack.esql.plan.logical.local.LocalSupplier;
import org.elasticsearch.xpack.esql.planner.PlannerUtils;
Expand All @@ -39,7 +38,6 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.elasticsearch.xpack.esql.expression.NamedExpressions.mergeOutputExpressions;
import static org.elasticsearch.xpack.esql.optimizer.rules.logical.PruneEmptyPlans.skipPlan;

/**
Expand Down Expand Up @@ -79,10 +77,10 @@ private static LogicalPlan pruneColumns(LogicalPlan plan, AttributeSet.Builder u
do {
recheck.set(false);
p = switch (p) {
case Aggregate agg -> pruneColumnsInAggregate(agg, used, inlineJoin);
case InlineJoin inj -> pruneColumnsInInlineJoinRight(inj, used, recheck);
case Aggregate agg -> pruneColumnsInAggregate(agg, used, false);
case InlineJoin inj -> pruneColumnsInInlineJoin(inj, used, recheck);
case Eval eval -> pruneColumnsInEval(eval, used, recheck);
case Project project -> inlineJoin ? pruneColumnsInProject(project, used, recheck) : p;
case Project project -> pruneColumnsInProject(project, used, recheck);
case EsRelation esr -> pruneColumnsInEsRelation(esr, used);
case Fork fork -> {
forkPresent.set(true);
Expand Down Expand Up @@ -139,33 +137,35 @@ private static LogicalPlan pruneColumnsInAggregate(Aggregate aggregate, Attribut
return p;
}

private static LogicalPlan pruneColumnsInInlineJoinRight(InlineJoin ij, AttributeSet.Builder used, Holder<Boolean> recheck) {
private static LogicalPlan pruneColumnsInInlineJoin(InlineJoin ij, AttributeSet.Builder used, Holder<Boolean> recheck) {
LogicalPlan p = ij;

var right = pruneColumns(ij.right(), used, true);
if (right.output().isEmpty() || isLocalEmptyRelation(right)) {
used.addAll(ij.references());

var right = pruneColumns(ij.right(), used, false);
if (right.outputSet().subtract(ij.references()).isEmpty() || isLocalEmptyRelation(right)) {
p = pruneRightSideAndProject(ij);
recheck.set(true);
} else if (right != ij.right()) {
if (right.anyMatch(plan -> plan instanceof Aggregate) == false) {// there is no aggregation on the right side anymore
if (right instanceof StubRelation) {// right is just a StubRelation, meaning nothing is needed from the right side
p = pruneRightSideAndProject(ij);
} else {
// if the right has no aggregation anymore, but it still has some other plans (evals, projects),
// we keep those and integrate them into the main plan. The InlineJoin is also replaced entirely.
p = InlineJoin.replaceStub(ij.left(), right);
p = new Project(ij.source(), p, mergeOutputExpressions(p.output(), ij.left().output()));
}
} else {
// if the right side has been updated, replace it
p = ij.replaceRight(right);
}
recheck.set(true);
// if (right.anyMatch(plan -> plan instanceof Aggregate) == false) {// there is no aggregation on the right side anymore
// if (right instanceof StubRelation) {// right is just a StubRelation, meaning nothing is needed from the right side
// p = pruneRightSideAndProject(ij);
// } else {
// // if the right has no aggregation anymore, but it still has some other plans (evals, projects),
// // we keep those and integrate them into the main plan. The InlineJoin is also replaced entirely.
// p = InlineJoin.replaceStub(ij.left(), right);
// p = new Project(ij.source(), p, mergeOutputExpressions(p.output(), ij.left().output()));
// }
// } else {
// // if the right side has been updated, replace it
// p = ij.replaceRight(right);
// }
// recheck.set(true);
}

if (recheck.get() == false) {
used.addAll(p.references());
}
// if (recheck.get() == false) {
// used.addAll(p.references());
// }

return p;
}
Expand Down Expand Up @@ -204,7 +204,7 @@ private static LogicalPlan pruneColumnsInProject(Project project, AttributeSet.B

var remaining = pruneUnusedAndAddReferences(project.projections(), used);
if (remaining != null) {
p = remaining.isEmpty() ? emptyLocalRelation(project) : new Project(project.source(), project.child(), remaining);
p = new Project(project.source(), project.child(), remaining);
recheck.set(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ protected final ExecutionInfo executeWithInfo(TreeType plan) {

if (tf.hasChanged()) {
hasChanged = true;
if (changeLog.isTraceEnabled()) {
changeLog.trace("Rule {} applied with change\n{}", rule, NodeUtils.diffString(tf.before, tf.after));
if (true) {
changeLog.info("Rule {} applied with change\n{}", rule, NodeUtils.diffString(tf.before, tf.after));
}
} else {
if (log.isTraceEnabled()) {
Expand Down