* Create the output PlanContext that should be returned by createExecPlan().
*/
private PlanContext createReturnedContext(PlanContext planContext,
List<TypedField> outputFields) {
PlanContext outContext = planContext;
FlowSpecification flowSpec = planContext.getFlowSpec();
if (planContext.isRoot()) {
String selectTarget = planContext.getConf().get(CLIENT_SELECT_TARGET_KEY,
DEFAULT_CLIENT_SELECT_TARGET);
if (CONSOLE_SELECT_TARGET.equals(selectTarget)) {
// SELECT statements that are root queries go to the output node.
// This output node may emit Avro records to a Flume node. These records
// should use more user-friendly names for the fields than the anonymized
// field names we use internally. Create a final schema for the output
// plan node.
String outputName = getOutputName();
List<TypedField> outSchemaFields = new ArrayList<TypedField>();
List<TypedField> distinctOutFields = distinctFields(outputFields);
for (TypedField outField : distinctOutFields) {
String safeName = avroSafeName(outField.getDisplayName());
outSchemaFields.add(new TypedField(safeName, outField.getType()));
}
Schema finalSchema = createFieldSchema(outSchemaFields, outputName);
OutputNode outputNode = new OutputNode(outputFields, outSchemaFields, outputName);
outputNode.setAttr(PlanNode.OUTPUT_SCHEMA_ATTR, finalSchema);
flowSpec.attachToLastLayer(outputNode);
} else {
// Client has specified that outputs of this root query go to a named memory buffer.
flowSpec.attachToLastLayer(new MemoryOutputNode(selectTarget,
distinctFields(outputFields)));
}
} else {
// If the initial projection contained both explicitly selected fields as
// well as implicitly selected fields (e.g., for the WHERE clause), attach another
// projection layer that extracts only the explicitly selected fields.
// SELECT as a sub-query needs to create an output context with a
// symbol table that contains the fields we expose through projection.
// We also need to set the output field names and output schema in our
// returned context.
outContext = new PlanContext(planContext);
SymbolTable inTable = planContext.getSymbolTable();
SymbolTable outTable = new HashSymbolTable(inTable);
outputFields = distinctFields(outputFields);
outTable.addAll(mFieldSymbols);
Schema outputSchema = createFieldSchema(outputFields);
ProjectionNode cleanupProjection = new ProjectionNode(outputFields, outputFields);
cleanupProjection.setAttr(PlanNode.OUTPUT_SCHEMA_ATTR, outputSchema);
flowSpec.attachToLastLayer(cleanupProjection);
outContext.setSymbolTable(outTable);
outContext.setSchema(outputSchema);
outContext.setOutFields(outputFields);
}