protected void saveViewDefinition() throws SemanticException {
    // Make a copy of the statement's result schema, since we may
    // modify it below as part of imposing view column names.
    List<FieldSchema> derivedSchema =
        new ArrayList<FieldSchema>(resultSchema);
    ParseUtils.validateColumnNameUniqueness(derivedSchema);

    List<FieldSchema> imposedSchema = createVwDesc.getCols();
    if (imposedSchema != null) {
      int explicitColCount = imposedSchema.size();
      int derivedColCount = derivedSchema.size();
      if (explicitColCount != derivedColCount) {
        throw new SemanticException(generateErrorMessage(
            viewSelect,
            ErrorMsg.VIEW_COL_MISMATCH.getMsg()));
      }
    }

    // Preserve the original view definition as specified by the user.
    if (createVwDesc.getViewOriginalText() == null) {
      String originalText = ctx.getTokenRewriteStream().toString(
          viewSelect.getTokenStartIndex(), viewSelect.getTokenStopIndex());
      createVwDesc.setViewOriginalText(originalText);
    }

    // Now expand the view definition with extras such as explicit column
    // references; this expanded form is what we'll re-parse when the view is
    // referenced later.
    unparseTranslator.applyTranslations(ctx.getTokenRewriteStream());
    String expandedText = ctx.getTokenRewriteStream().toString(
        viewSelect.getTokenStartIndex(), viewSelect.getTokenStopIndex());

    if (createVwDesc.getPartColNames() != null) {
      // If we are creating a materialized view and it has partition columns,
      // we may need to reorder column projection in expanded query. The reason
      // is that Hive assumes that in the partition columns are at the end of
      // the MV schema, and if we do not do this, we will have a mismatch between
      // the SQL query for the MV and the MV itself.
      boolean first = true;
      StringBuilder sb = new StringBuilder();
      sb.append("SELECT ");
      for (FieldSchema fieldSchema : derivedSchema) {
        if (!createVwDesc.getPartColNames().contains(fieldSchema.getName())) {
          if (first) {
            first = false;
          } else {
            sb.append(", ");
          }
          sb.append(HiveUtils.unparseIdentifier(fieldSchema.getName(), conf));
        }
      }
      for (String partColName : createVwDesc.getPartColNames()) {
        sb.append(", ");
        sb.append(HiveUtils.unparseIdentifier(partColName, conf));
      }
      sb.append(" FROM (");
      sb.append(expandedText);
      sb.append(") ");
      sb.append(HiveUtils.unparseIdentifier(Utilities.getDbTableName(createVwDesc.getViewName())[1], conf));
      expandedText = sb.toString();
    }

    // Set schema and expanded text for the view
    createVwDesc.setCols(derivedSchema);
    createVwDesc.setViewExpandedText(expandedText);
  }
Planner specific stuff goes in here.
ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java