public String executeScript(String terminalId, String catalog, String script) {
    CatalogMeta catalogMeta = catalogManager.getCatalogMeta(catalog);
    TableMetaStore metaStore = getCatalogTableMetaStore(catalogMeta);
    String sessionId = getSessionId(terminalId, metaStore, catalog);
    String connectorType = catalogConnectorType(catalogMeta);
    applyClientProperties(catalogMeta);
    Configurations configuration = new Configurations();
    configuration.set(
        AmoroManagementConf.TERMINAL_SENSITIVE_CONF_KEYS,
        serviceConfig.get(AmoroManagementConf.TERMINAL_SENSITIVE_CONF_KEYS));
    configuration.setInteger(TerminalSessionFactory.SessionConfigOptions.FETCH_SIZE, resultLimits);
    configuration.set(
        TerminalSessionFactory.SessionConfigOptions.CATALOGS, Lists.newArrayList(catalog));
    configuration.set(
        TerminalSessionFactory.SessionConfigOptions.catalogConnector(catalog), connectorType);
    configuration.set(
        TerminalSessionFactory.SessionConfigOptions.CATALOG_URL_BASE,
        AmsUtil.getAMSThriftAddress(serviceConfig, Constants.THRIFT_TABLE_SERVICE_NAME));
    for (String key : catalogMeta.getCatalogProperties().keySet()) {
      String value = catalogMeta.getCatalogProperties().get(key);
      configuration.set(
          TerminalSessionFactory.SessionConfigOptions.catalogProperty(catalog, key), value);
    }

    synchronized (sessionMapLock) {
      sessionMap.compute(
          sessionId,
          (id, ctx) -> {
            if (ctx == null) {
              return new TerminalSessionContext(
                  id, metaStore, executionPool, sessionFactory, configuration);
            } else {
              // need to re-create session context if configuration is changed
              return ctx.sessionConfiguration().equals(configuration)
                  ? ctx
                  : new TerminalSessionContext(
                      id, metaStore, executionPool, sessionFactory, configuration);
            }
          });
    }

    TerminalSessionContext context = sessionMap.get(sessionId);
    if (!context.isReadyToExecute()) {
      throw new IllegalStateException(
          "current session is not ready to execute script. status:" + context.getStatus());
    }
    context.submit(catalog, script, resultLimits, stopOnError);
    return sessionId;
  }
execute script, return terminal sessionId

@param terminalId - id to mark different terminal windows
@param catalog - current catalog to execute script
@param script - sql script to be executed
@return - sessionId, session refer to a sql execution context
amoro-ams/src/main/java/org/apache/amoro/server/terminal/TerminalManager.java