code: public SessionHandle openSession(TProtocolVersion protocol, String username, String password, String ipAddress,
        Map<String, String> sessionConf, boolean withImpersonation, String delegationToken)
            throws HiveSQLException {
      HiveSession session;
      // If doAs is set to true for HiveServer2, we will create a proxy object for the session impl.
      // Within the proxy object, we wrap the method call in a UserGroupInformation#doAs
      if (withImpersonation) {
        HiveSessionImplwithUGI sessionWithUGI = new HiveSessionImplwithUGI(protocol, username, password,
            hiveConf, ipAddress, delegationToken);
        session = HiveSessionProxy.getProxy(sessionWithUGI, sessionWithUGI.getSessionUgi());
        sessionWithUGI.setProxySession(session);
      } else {
        session = new HiveSessionImpl(protocol, username, password, hiveConf, ipAddress);
      }
      session.setSessionManager(this);
      session.setOperationManager(operationManager);
      try {
        session.open(sessionConf);
      } catch (Exception e) {
        try {
          session.close();
        } catch (Throwable t) {
          LOG.warn("Error closing session", t);
        }
        session = null;
        throw new HiveSQLException("Failed to open new session: " + e, e);
      }
      if (isOperationLogEnabled) {
        session.setOperationLogSessionDir(operationLogRootDir);
      }
      handleToSession.put(session.getSessionHandle(), session);
      return session.getSessionHandle();
    }
docs: Opens a new session and creates a session handle.
  The username passed to this method is the effective username.
  If withImpersonation is true (==doAs true) we wrap all the calls in HiveSession
  within a UGI.doAs, where UGI corresponds to the effective user.
  
  Please see {@code org.apache.hive.service.cli.thrift.ThriftCLIService.getUserName()} for
  more details.
  
  @param protocol
  @param username
  @param password
  @param ipAddress
  @param sessionConf
  @param withImpersonation
  @param delegationToken
  @return
  @throws HiveSQLException