String disambiguateName(String name) throws ParsingException {

    String[] fields = name.split("[.]");
    if (fields.length == 2 && (!fields[0].equals("null")))
      return name;
    if (fields.length > 2)
      throw new ParsingException("Field " + name + " is not a valid field reference.");
    String newName = name;
    if (fields.length == 2)
      newName = fields[1];
    if (newName.equals("*"))
      return newName;
    // now look for occurrences of name in all of the tables
    Iterator<LogicalScanNode> tableIt = tables.iterator();
    String tableName = null;
    while (tableIt.hasNext()) {
      LogicalScanNode table = tableIt.next();
      try {
        TupleDesc td = Database.getCatalog().getDatabaseFile(table.t).getTupleDesc();
        // int id =
        td.fieldNameToIndex(newName);
        if (tableName == null) {
          tableName = table.alias;
        } else {
          throw new ParsingException("Field " + newName
              + " appears in multiple tables; disambiguate by referring to it as tablename."
              + newName);
        }
      } catch (NoSuchElementException e) {
        // ignore
      }
    }
    if (tableName != null)
      return tableName + "." + name;
    throw new ParsingException("Field " + name + " does not appear in any tables.");

  }