private Optional parseStmt()

in linkis-engineconn-plugins/flink/flink-core/src/main/java/org/apache/linkis/engineconnplugin/flink/client/sql/parser/SqlCommandParserImpl.java [76:213]


  private Optional<SqlCommandCall> parseStmt(String stmt, boolean isBlinkPlanner)
      throws SqlParseException {
    SqlParser.Config config = createSqlParserConfig(isBlinkPlanner);
    SqlParser sqlParser = SqlParser.create(stmt, config);
    SqlNodeList sqlNodes;
    try {
      sqlNodes = sqlParser.parseStmtList();
      // no need check the statement is valid here
    } catch (org.apache.calcite.sql.parser.SqlParseException e) {
      throw new SqlParseException(FAILED_PARSE_STATEMENT.getErrorDesc(), e);
    }
    if (sqlNodes.size() != 1) {
      throw new SqlParseException(ONLY_SINGLE_STATEMENT.getErrorDesc());
    }

    final String[] operands;
    final SqlCommand cmd;
    SqlNode node = sqlNodes.get(0);
    if (node.getKind().belongsTo(SqlKind.QUERY)) {
      cmd = SqlCommand.SELECT;
      operands = new String[] {stmt};
    } else if (node instanceof RichSqlInsert) {
      RichSqlInsert insertNode = (RichSqlInsert) node;
      cmd = insertNode.isOverwrite() ? SqlCommand.INSERT_OVERWRITE : SqlCommand.INSERT_INTO;
      operands = new String[] {stmt, insertNode.getTargetTable().toString()};
    } else if (node instanceof SqlShowTables) {
      cmd = SqlCommand.SHOW_TABLES;
      operands = new String[0];
    } else if (node instanceof SqlCreateTable) {
      cmd = SqlCommand.CREATE_TABLE;
      operands = new String[] {stmt};
    } else if (node instanceof SqlDropTable) {
      cmd = SqlCommand.DROP_TABLE;
      operands = new String[] {stmt};
    } else if (node instanceof SqlCreateCatalog) {
      cmd = SqlCommand.CREATE_CATALOG;
      operands = new String[] {stmt};
    } else if (node instanceof SqlDropCatalog) {
      cmd = SqlCommand.DROP_CATALOG;
      operands = new String[] {stmt};
    } else if (node instanceof SqlAlterTable) {
      cmd = SqlCommand.ALTER_TABLE;
      operands = new String[] {stmt};
    } else if (node instanceof SqlCreateView) {
      // TableEnvironment currently does not support creating view
      // so we have to perform the modification here
      SqlCreateView createViewNode = (SqlCreateView) node;
      cmd = SqlCommand.CREATE_VIEW;
      operands =
          new String[] {
            createViewNode.getViewName().toString(), createViewNode.getQuery().toString()
          };
    } else if (node instanceof SqlDropView) {
      // TableEnvironment currently does not support dropping view
      // so we have to perform the modification here
      SqlDropView dropViewNode = (SqlDropView) node;

      Field ifExistsField;
      try {
        ifExistsField = SqlDrop.class.getDeclaredField("ifExists");
      } catch (NoSuchFieldException e) {
        throw new SqlParseException(FAILED_DROP_STATEMENT.getErrorDesc(), e);
      }
      ifExistsField.setAccessible(true);
      boolean ifExists;
      try {
        ifExists = ifExistsField.getBoolean(dropViewNode);
      } catch (IllegalAccessException e) {
        throw new SqlParseException(FAILED_DROP_STATEMENT.getErrorDesc(), e);
      }

      cmd = SqlCommand.DROP_VIEW;
      operands = new String[] {dropViewNode.getViewName().toString(), String.valueOf(ifExists)};
    } else if (node instanceof SqlShowDatabases) {
      cmd = SqlCommand.SHOW_DATABASES;
      operands = new String[0];
    } else if (node instanceof SqlCreateDatabase) {
      cmd = SqlCommand.CREATE_DATABASE;
      operands = new String[] {stmt};
    } else if (node instanceof SqlCreateFunction) {
      cmd = SqlCommand.CREATE_FUNCTION;
      operands = new String[] {stmt};
    } else if (node instanceof SqlDropDatabase) {
      cmd = SqlCommand.DROP_DATABASE;
      operands = new String[] {stmt};
    } else if (node instanceof SqlAlterDatabase) {
      cmd = SqlCommand.ALTER_DATABASE;
      operands = new String[] {stmt};
    } else if (node instanceof SqlShowCatalogs) {
      cmd = SqlCommand.SHOW_CATALOGS;
      operands = new String[0];
    } else if (node instanceof SqlShowFunctions) {
      cmd = SqlCommand.SHOW_FUNCTIONS;
      operands = new String[0];
    } else if (node instanceof SqlUseCatalog) {
      cmd = SqlCommand.USE_CATALOG;
      operands = new String[] {((SqlUseCatalog) node).getCatalogName().getSimple()};
    } else if (node instanceof SqlUseDatabase) {
      cmd = SqlCommand.USE;
      operands = new String[] {((SqlUseDatabase) node).getDatabaseName().toString()};
    } else if (node instanceof SqlRichDescribeTable) {
      cmd = SqlCommand.DESCRIBE_TABLE;
      // TODO support describe extended
      String[] fullTableName = ((SqlRichDescribeTable) node).fullTableName();
      String escapedName =
          Stream.of(fullTableName).map(s -> "`" + s + "`").collect(Collectors.joining("."));
      operands = new String[] {escapedName};
    } else if (node instanceof SqlExplain) {
      cmd = SqlCommand.EXPLAIN;
      // TODO support explain details
      operands = new String[] {((SqlExplain) node).getExplicandum().toString()};
    } else if (node instanceof SqlSetOption) {
      SqlSetOption setNode = (SqlSetOption) node;
      // refer to SqlSetOption#unparseAlterOperation
      if (setNode.getValue() != null) {
        cmd = SqlCommand.SET;
        operands = new String[] {setNode.getName().toString(), setNode.getValue().toString()};
      } else {
        cmd = SqlCommand.RESET;
        if ("ALL".equals(setNode.getName().toString().toUpperCase())) {
          operands = new String[0];
        } else {
          operands = new String[] {setNode.getName().toString()};
        }
      }
    } else {
      cmd = null;
      operands = new String[0];
    }

    if (cmd == null) {
      return Optional.empty();
    } else {
      // use the origin given statement to make sure
      // users can find the correct line number when parsing failed
      return Optional.of(new SqlCommandCall(cmd, operands));
    }
  }