private String getUserName(TOpenSessionReq req) throws HiveSQLException, IOException {
    String userName = null;

    if (hiveAuthFactory != null && hiveAuthFactory.isSASLWithKerberizedHadoop()) {
      userName = hiveAuthFactory.getRemoteUser();
    }
    // NOSASL
    if (userName == null) {
      userName = TSetIpAddressProcessor.getUserName();
    }
    // Http transport mode.
    // We set the thread local username, in ThriftHttpServlet.
    if (getTransportMode() == HiveServer2TransportMode.http) {
      userName = SessionManager.getUserName();
    }
    if (userName == null) {
      userName = req.getUsername();
    }

    if (cliService.getHiveConf().getBoolVar(ConfVars.HIVE_AUTHORIZATION_KERBEROS_USE_SHORTNAME))
    {
      userName = getShortName(userName);
    }

    String effectiveClientUser = getProxyUser(userName, req.getConfiguration(), getIpAddress());
    LOG.debug("Client's username: {}", effectiveClientUser);
    return effectiveClientUser;
  }
Returns the effective username.
1. If hive.server2.allow.user.substitution = false: the username of the connecting user
2. If hive.server2.allow.user.substitution = true: the username of the end user,
that the connecting user is trying to proxy for.
This includes a check whether the connecting user is allowed to proxy for the end user.
@param req
@return
@throws HiveSQLException
service/src/java/org/apache/hive/service/cli/thrift/ThriftCLIService.java