break;
}
// compute resource usage using the single stats collector
stats = new PlanStatistics();
AbstractPlanNode planGraph = plan.fragments.get(0).planGraph;
// compute statistics about a plan
boolean result = planGraph.computeEstimatesRecursively(stats, m_cluster, m_db, m_estimates, paramHints);
assert(result);
// GENERATE JSON DEBUGGING OUTPUT BEFORE WE CLEAN UP THE PlanColumns
// convert a tree into an execution list
PlanNodeList nodeList = new PlanNodeList(planGraph);
// get the json serialized version of the plan
String json = "";
// try {
// String crunchJson = nodeList.toJSONString();
//System.out.println(crunchJson);
//System.out.flush();
/* FIXME
JSONObject jobj = new JSONObject(crunchJson);
json = jobj.toString(4);
} catch (JSONException e2) {
// Any plan that can't be serialized to JSON to
// write to debugging output is also going to fail
// to get written to the catalog, to sysprocs, etc.
// Just bail.
m_recentErrorMsg = "Plan for sql: '" + sql +
"' can't be serialized to JSON";
return null;
}
*/
// compute the cost based on the resources using the current cost model
double cost = costModel.getPlanCost(stats);
// find the minimum cost plan
if (cost < minCost) {
minCost = cost;
// free the PlanColumns held by the previous best plan
if (bestPlan != null) {
bestPlan.freePlan(m_context, plan.getColumnGuids());
}
bestPlan = plan;
} else {
plan.freePlan(m_context, bestPlan.getColumnGuids());
}
// output a description of the parsed stmt
String filename = String.valueOf(i++);
if (bestPlan == plan) winnerName = filename;
json = "COST: " + String.valueOf(cost) + "\n" + json;
planOutputs.put(filename, json);
// create a graph friendly version
dotPlanOutputs.put(filename, nodeList.toDOTString("name"));
}
}
tpce_limit = null;
}
// make sure we got a winner
if (bestPlan == null) {
m_recentErrorMsg = "Unable to plan for statement. Error unknown.";
return null;
}
// Validate that everything is there
Set<Integer> bestPlan_columns = bestPlan.getColumnGuids();
for (Integer column_guid : bestPlan_columns) {
if (m_context.hasColumn(column_guid) == false) {
m_recentErrorMsg = "Missing column guid " + column_guid;
return (null);
}
} // FOR
if (debug.val) LOG.debug(String.format("All columns are there for %s.%s: %s", procName, stmtName, bestPlan_columns));
// reset all the plan node ids for a given plan
bestPlan.resetPlanNodeIds();
if (!m_quietPlanner)
{
// print all the plans to disk for debugging
for (Entry<String, String> output : planOutputs.entrySet()) {
String filename = output.getKey();
if (winnerName.equals(filename)) {
filename = "WINNER " + filename;
}
PrintStream candidatePlanOut =
BuildDirectoryUtils.getDebugOutputPrintStream("statement-all-plans/" + procName + "_" + stmtName,
filename + ".txt");
candidatePlanOut.println(output.getValue());
candidatePlanOut.close();
}
for (Entry<String, String> output : dotPlanOutputs.entrySet()) {
String filename = output.getKey();
if (winnerName.equals(filename)) {
filename = "WINNER " + filename;
}
PrintStream candidatePlanOut =
BuildDirectoryUtils.getDebugOutputPrintStream("statement-all-plans/" + procName + "_" + stmtName,
filename + ".dot");
candidatePlanOut.println(output.getValue());
candidatePlanOut.close();
}
// output the plan statistics to disk for debugging
PrintStream plansOut =
BuildDirectoryUtils.getDebugOutputPrintStream("statement-stats", procName + "_" + stmtName + ".txt");
plansOut.println(stats.toString());
plansOut.close();
}
// PAVLO: Get the full plan json
AbstractPlanNode root = bestPlan.fragments.get(0).planGraph;
// String orig_debug = PlanNodeUtil.debug(root);
assert(root != null);
String json = null;
try {
JSONObject jobj = new JSONObject(new PlanNodeList(root).toJSONString());