Skip to content

Commit

Permalink
[Enhancement] Show plan cost in explain verbose/costs (#49106)
Browse files Browse the repository at this point in the history
Signed-off-by: zihe.liu <[email protected]>
  • Loading branch information
ZiheLiu authored Jul 30, 2024
1 parent b041c85 commit df5a254
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
15 changes: 14 additions & 1 deletion fe/fe-core/src/main/java/com/starrocks/sql/plan/ExecPlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import com.google.common.collect.Maps;
import com.starrocks.analysis.DescriptorTable;
import com.starrocks.analysis.Expr;
import com.starrocks.common.FeConstants;
import com.starrocks.common.IdGenerator;
import com.starrocks.common.util.ProfilingExecPlan;
import com.starrocks.planner.ExecGroup;
import com.starrocks.planner.PlanFragment;
import com.starrocks.planner.PlanFragmentId;
import com.starrocks.planner.PlanNodeId;
import com.starrocks.planner.ScanNode;
import com.starrocks.plugin.AuditEvent;
import com.starrocks.qe.ConnectContext;
import com.starrocks.sql.Explain;
import com.starrocks.sql.ast.StatementBase;
Expand Down Expand Up @@ -63,7 +65,6 @@ public class ExecPlan {
private LogicalPlan logicalPlan;
private ColumnRefFactory columnRefFactory;


@VisibleForTesting
public ExecPlan() {
connectContext = new ConnectContext();
Expand Down Expand Up @@ -153,6 +154,7 @@ public List<ColumnRefOperator> getOutputColumns() {
public void setExecGroups(List<ExecGroup> execGroups) {
this.execGroups = execGroups;
}

public List<ExecGroup> getExecGroups() {
return this.execGroups;
}
Expand Down Expand Up @@ -188,6 +190,17 @@ public ProfilingExecPlan getProfilingPlan() {

public String getExplainString(TExplainLevel level) {
StringBuilder str = new StringBuilder();

if (level == TExplainLevel.VERBOSE || level == TExplainLevel.COSTS) {
if (FeConstants.showFragmentCost) {
final String prefix = " ";
AuditEvent auditEvent = connectContext.getAuditEventBuilder().build();
str.append("PLAN COST").append("\n")
.append(prefix).append("CPU: ").append(auditEvent.planCpuCosts).append("\n")
.append(prefix).append("Memory: ").append(auditEvent.planMemCosts).append("\n\n");
}
}

if (level == null) {
str.append(Explain.toString(physicalPlan, outputColumns));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2296,31 +2296,46 @@ public void testRankWindowOptimization() throws Exception {

@Test
public void testPlanCost() throws Exception {
String plan = getVerboseExplain("select t1a, v1 " +
"from t0 join [broadcast] test_all_type " +
"join [shuffle] (select 1 as v1_c1 where abs(1) = 2) v1 on t1a=v1 and t1a=v1_c1");
assertContains(plan, " 11:HASH JOIN\n" +
" | join op: INNER JOIN (PARTITIONED)\n" +
" | equal join conjunct: [4: t1a, VARCHAR, true] = [16: cast, VARCHAR, false]\n" +
" | build runtime filters:\n" +
" | - filter_id = 1, build_expr = (16: cast), remote = true\n" +
" | output columns: 1, 4\n" +
" | cardinality: 9000\n" +
" | \n" +
" |----10:EXCHANGE\n" +
" | distribution type: SHUFFLE\n" +
" | partition exprs: [16: cast, VARCHAR, false]\n" +
" | cardinality: 1\n" +
" | \n" +
" 6:EXCHANGE\n" +
" distribution type: SHUFFLE\n" +
" partition exprs: [4: t1a, VARCHAR, true]\n" +
" cardinality: 9000");

AuditEvent event = connectContext.getAuditEventBuilder().build();
Assert.assertTrue("planMemCosts should be > 1, but: " + event.planMemCosts, event.planMemCosts > 1);
Assert.assertTrue("planCpuCosts should be > 1, but: " + event.planCpuCosts, event.planCpuCosts > 1);

final boolean prevShowFragmentCost = FeConstants.showFragmentCost;
try {
FeConstants.showFragmentCost = true;

String sql = "select t1a, v1 " +
"from t0 join [broadcast] test_all_type " +
"join [shuffle] (select 1 as v1_c1 where abs(1) = 2) v1 on t1a=v1 and t1a=v1_c1";
String plan = getVerboseExplain(sql);
assertContains(plan, " 11:HASH JOIN\n" +
" | join op: INNER JOIN (PARTITIONED)\n" +
" | equal join conjunct: [4: t1a, VARCHAR, true] = [16: cast, VARCHAR, false]\n" +
" | build runtime filters:\n" +
" | - filter_id = 1, build_expr = (16: cast), remote = true\n" +
" | output columns: 1, 4\n" +
" | cardinality: 9000\n" +
" | \n" +
" |----10:EXCHANGE\n" +
" | distribution type: SHUFFLE\n" +
" | partition exprs: [16: cast, VARCHAR, false]\n" +
" | cardinality: 1\n" +
" | \n" +
" 6:EXCHANGE\n" +
" distribution type: SHUFFLE\n" +
" partition exprs: [4: t1a, VARCHAR, true]\n" +
" cardinality: 9000");
System.out.println(plan);
assertContains(plan, "PLAN COST\n" +
" CPU: 1881002.0\n" +
" Memory: 288001.0");

assertContains(getCostExplain(sql), "PLAN COST\n" +
" CPU: 1881002.0\n" +
" Memory: 288001.0");

AuditEvent event = connectContext.getAuditEventBuilder().build();
Assert.assertTrue("planMemCosts should be > 1, but: " + event.planMemCosts, event.planMemCosts > 1);
Assert.assertTrue("planCpuCosts should be > 1, but: " + event.planCpuCosts, event.planCpuCosts > 1);
} finally {
FeConstants.showFragmentCost = prevShowFragmentCost;
}
}

@Test
Expand Down

0 comments on commit df5a254

Please sign in to comment.