int n = derivedSchema.size();
for (int i = 0; i < n; ++i) {
if (i > 0) {
sb.append(", ");
}
FieldSchema fieldSchema = derivedSchema.get(i);
// Modify a copy, not the original
fieldSchema = new FieldSchema(fieldSchema);
derivedSchema.set(i, fieldSchema);
sb.append(HiveUtils.unparseIdentifier(fieldSchema.getName()));
sb.append(" AS ");
String imposedName = imposedSchema.get(i).getName();
sb.append(HiveUtils.unparseIdentifier(imposedName));
fieldSchema.setName(imposedName);
// We don't currently allow imposition of a type
fieldSchema.setComment(imposedSchema.get(i).getComment());
}
sb.append(" FROM (");
sb.append(expandedText);
sb.append(") ");
sb.append(HiveUtils.unparseIdentifier(createVwDesc.getViewName()));
expandedText = sb.toString();
}
if (createVwDesc.getPartColNames() != null) {
// Make sure all partitioning columns referenced actually
// exist and are in the correct order at the end
// of the list of columns produced by the view. Also move the field
// schema descriptors from derivedSchema to the partitioning key
// descriptor.
List<String> partColNames = createVwDesc.getPartColNames();
if (partColNames.size() > derivedSchema.size()) {
throw new SemanticException(
ErrorMsg.VIEW_PARTITION_MISMATCH.getMsg());
}
// Get the partition columns from the end of derivedSchema.
List<FieldSchema> partitionColumns = derivedSchema.subList(
derivedSchema.size() - partColNames.size(),
derivedSchema.size());
// Verify that the names match the PARTITIONED ON clause.
Iterator<String> colNameIter = partColNames.iterator();
Iterator<FieldSchema> schemaIter = partitionColumns.iterator();
while (colNameIter.hasNext()) {
String colName = colNameIter.next();
FieldSchema fieldSchema = schemaIter.next();
if (!fieldSchema.getName().equals(colName)) {
throw new SemanticException(
ErrorMsg.VIEW_PARTITION_MISMATCH.getMsg());
}
}