private String getSourceAlias(ASTNode source, String sourceName) throws SemanticException {
    String sourceAlias;
    if (source.getType() == HiveParser.TOK_SUBQUERY) {
      //this includes the mandatory alias
      sourceAlias = getMatchedText(source);
    } else {
      sourceAlias = getFullTableNameForSQL(source);
      if (isAliased(source)) {
        sourceAlias = String.format("%s %s", sourceAlias, sourceName);
      }
    }
    return sourceAlias;
  }
Here we take a Merge statement AST and generate a semantically equivalent multi-insert
statement to execute.  Each Insert leg represents a single WHEN clause.  As much as possible,
the new SQL statement is made to look like the input SQL statement so that it's easier to map
Query Compiler errors from generated SQL to original one this way.
The generated SQL is a complete representation of the original input for the same reason.
In many places SemanticAnalyzer throws exceptions that contain (line, position) coordinates.
If generated SQL doesn't have everything and is patched up later, these coordinates point to
the wrong place.

@throws SemanticException
ql/src/java/org/apache/hadoop/hive/ql/parse/MergeSemanticAnalyzer.java