[feature](nereids) add session var: dump_nereids_memo (#17666)

* dump_nereids_memo

* print groupexpr id
This commit is contained in:
minghong
2023-03-11 13:40:15 +08:00
committed by GitHub
parent 3231fab8c2
commit d7cb5cf3db
5 changed files with 50 additions and 15 deletions

View File

@ -61,6 +61,7 @@ import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
@ -180,10 +181,23 @@ public class NereidsPlanner extends Planner {
optimize();
//print memo before choose plan.
//if chooseNthPlan failed, we could get memo to debug
if (ConnectContext.get().getSessionVariable().isDumpNereidsMemo()) {
String memo = cascadesContext.getMemo().toString();
LOG.info(memo);
}
int nth = ConnectContext.get().getSessionVariable().getNthOptimizedPlan();
PhysicalPlan physicalPlan = chooseNthPlan(getRoot(), requireProperties, nth);
physicalPlan = postProcess(physicalPlan);
if (ConnectContext.get().getSessionVariable().isDumpNereidsMemo()) {
String tree = physicalPlan.treeString();
LOG.info(tree);
}
if (explainLevel == ExplainLevel.OPTIMIZED_PLAN || explainLevel == ExplainLevel.ALL_PLAN) {
optimizedPlan = physicalPlan;
}
@ -293,8 +307,8 @@ public class NereidsPlanner extends Planner {
if (!(plan instanceof PhysicalPlan)) {
throw new AnalysisException("Result plan must be PhysicalPlan");
}
// TODO: set (logical and physical)properties/statistics/... for physicalPlan.
// add groupExpression to plan so that we could print group id in plan.treeString()
plan = plan.withGroupExpression(Optional.of(groupExpression));
PhysicalPlan physicalPlan = ((PhysicalPlan) plan).withPhysicalPropertiesAndStats(
groupExpression.getOutputProperties(physicalProperties),
groupExpression.getOwnerGroup().getStatistics());

View File

@ -397,13 +397,13 @@ public class Group {
@Override
public String toString() {
StringBuilder str = new StringBuilder("Group[" + groupId + "]\n");
str.append("logical expressions:\n");
str.append(" logical expressions:\n");
for (GroupExpression logicalExpression : logicalExpressions) {
str.append(" ").append(logicalExpression).append("\n");
str.append(" ").append(logicalExpression).append("\n");
}
str.append("physical expressions:\n");
str.append(" physical expressions:\n");
for (GroupExpression physicalExpression : physicalExpressions) {
str.append(" ").append(physicalExpression).append("\n");
str.append(" ").append(physicalExpression).append("\n");
}
return str.toString();
}

View File

@ -36,6 +36,7 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.text.DecimalFormat;
import java.util.BitSet;
import java.util.List;
import java.util.Map;
@ -318,8 +319,9 @@ public class GroupExpression {
builder.append("#").append(ownerGroup.getGroupId().asInt());
}
builder.append(" cost=").append((long) cost);
DecimalFormat decimalFormat = new DecimalFormat();
decimalFormat.setGroupingSize(3);
builder.append(" cost=").append(decimalFormat.format((long) cost));
builder.append(" estRows=").append(estOutputRowCount);
builder.append(" (plan=").append(plan.toString()).append(") children=[");
for (Group group : children) {

View File

@ -693,7 +693,7 @@ public class Memo {
StringBuilder builder = new StringBuilder();
builder.append("root:").append(getRoot()).append("\n");
for (Group group : groups.values()) {
builder.append(group).append("\n");
builder.append("\n\n").append(group);
builder.append(" stats=").append(group.getStatistics()).append("\n");
StatsDeriveResult stats = group.getStatistics();
if (stats != null && !group.getLogicalExpressions().isEmpty()
@ -702,12 +702,18 @@ public class Memo {
builder.append(" ").append(e.getKey()).append(":").append(e.getValue()).append("\n");
}
}
for (GroupExpression groupExpression : group.getLogicalExpressions()) {
builder.append(" ").append(groupExpression.toString()).append("\n");
}
for (GroupExpression groupExpression : group.getPhysicalExpressions()) {
builder.append(" ").append(groupExpression.toString()).append("\n");
}
builder.append(" lowest Plan(cost, properties, plan)");
group.getAllProperties().forEach(
prop -> {
Optional<Pair<Cost, GroupExpression>> costAndGroupExpression = group.getLowestCostPlan(prop);
if (costAndGroupExpression.isPresent()) {
builder.append("\n " + costAndGroupExpression.get().first.getValue() + " " + prop)
.append("\n ").append(costAndGroupExpression.get().second);
}
}
);
builder.append("\n");
}
return builder.toString();
}

View File

@ -283,6 +283,8 @@ public class SessionVariable implements Serializable, Writable {
public static final String SHOW_USER_DEFAULT_ROLE = "show_user_default_role";
public static final String DUMP_NEREIDS_MEMO = "dump_nereids_memo";
// fix replica to query. If num = 1, query the smallest replica, if 2 is the second smallest replica.
public static final String USE_FIX_REPLICA = "use_fix_replica";
@ -760,6 +762,9 @@ public class SessionVariable implements Serializable, Writable {
@VariableMgr.VarAttr(name = USE_FIX_REPLICA)
public int useFixReplica = -1;
@VariableMgr.VarAttr(name = DUMP_NEREIDS_MEMO)
public boolean dumpNereidsMemo = false;
// If set to true, all query will be executed without returning result
@VariableMgr.VarAttr(name = DRY_RUN_QUERY, needForward = true)
public boolean dryRunQuery = false;
@ -1847,4 +1852,12 @@ public class SessionVariable implements Serializable, Writable {
}
return "";
}
public boolean isDumpNereidsMemo() {
return dumpNereidsMemo;
}
public void setDumpNereidsMemo(boolean dumpNereidsMemo) {
this.dumpNereidsMemo = dumpNereidsMemo;
}
}