CatalogMap<Column> cols = db.getTables().getExact(m_targetTableName).getColumns();
// you don't strictly need to sort this, but it makes diff-ing easier
for (Column col : CatalogUtil.getSortedCatalogItems(cols, "index"))
{
// must produce a tuple value expression for this column.
TupleValueExpression tve = new TupleValueExpression(
m_targetTableName, m_targetTableAlias, col.getTypeName(), col.getTypeName(),
col.getIndex());
tve.setTypeSizeBytes(col.getType(), col.getSize(), col.getInbytes());
m_tableSchema.addColumn(new SchemaColumn(m_targetTableName,
m_targetTableAlias,
col.getTypeName(),
col.getTypeName(),
tve));
}
}
}
// Until the scan has an implicit projection rather than an explicitly
// inlined one, the output schema generation is going to be a bit odd.
// It will depend on two bits of state: whether any scan columns were
// specified for this table and whether or not there is an inlined
// projection.
//
// If there is an inlined projection, then we'll just steal that
// output schema as our own.
// If there is no inlined projection, then, if there are no scan columns
// specified, use the entire table's schema as the output schema.
// Otherwise add an inline projection that projects the scan columns
// and then take that output schema as our own.
// These have the effect of repeatably generating the correct output
// schema if called again and again, but also allowing the planner
// to overwrite the inline projection and still have the right thing
// happen
ProjectionPlanNode proj =
(ProjectionPlanNode)getInlinePlanNode(PlanNodeType.PROJECTION);
if (proj != null)
{
// Does this operation needs to change complex expressions
// into tuple value expressions with an column alias?
// Is this always true for clone? Or do we need a new method?
m_outputSchema = proj.getOutputSchema().copyAndReplaceWithTVE();
m_hasSignificantOutputSchema = false; // It's just a cheap knock-off of the projection's
}
else
{
if (m_tableScanSchema.size() != 0)
{
// Order the scan columns according to the table schema
// before we stick them in the projection output
List<TupleValueExpression> scan_tves =
new ArrayList<TupleValueExpression>();
for (SchemaColumn col : m_tableScanSchema.getColumns())
{
assert(col.getExpression() instanceof TupleValueExpression);
scan_tves.addAll(ExpressionUtil.getTupleValueExpressions(col.getExpression()));
}
// and update their indexes against the table schema
for (TupleValueExpression tve : scan_tves)
{
int index = tve.resolveColumnIndexesUsingSchema(m_tableSchema);
tve.setColumnIndex(index);
}
m_tableScanSchema.sortByTveIndex();
// Create inline projection to map table outputs to scan outputs
ProjectionPlanNode projectionNode = new ProjectionPlanNode();
projectionNode.setOutputSchema(m_tableScanSchema);