取消语句

取消语句的推荐方法是,使用运行查询的应用程序的界面(例如 Snowflake Web 界面中的 Worksheet),或者使用由 Snowflake ODBC 或 JDBC 驱动程序提供的取消 API。但是,在某些情况下,必须使用 SQL 取消查询。

Snowflake 提供以下函数,以支持使用 SQL 取消正在运行的/活跃的语句:

示例

The following Java sample code uses SYSTEM$CANCEL_ALL_QUERIES and other Snowflake functions to cancel a running statement in the current session after 5 seconds:

  1. The sample code first issues a SQL command for CURRENT_SESSION to obtain the session identifier.
  2. It then creates a task to be executed 5 seconds later. This task uses the session identifier as a parameter to SYSTEM$CANCEL_ALL_QUERIES.
  3. Then a long running statement is executed using the GENERATOR table function to generate rows for 120 seconds.
public void testCancelQuery() throws IOException, SQLException
{
  Statement         statement         = null;
  ResultSet         resultSet         = null;
  ResultSetMetaData resultSetMetaData = null;
  final Connection  connection        = getConnection(true);
  try
  {
    // Get the current session identifier
    Statement getSessionIdStmt = connection.createStatement();
    resultSet                  = getSessionIdStmt.executeQuery("SELECT current_session()");
    resultSetMetaData          = resultSet.getMetaData();
    assertTrue(resultSet.next());
    final int sessionId = resultSet.getInt(1);

    // Use Timer to cancel all queries of session in 5 seconds
    Timer timer = new Timer();
    timer.schedule( new TimerTask()
    {
      @Override
      public void run()
      {
        try
        {
          // Cancel all queries on session
          PreparedStatement cancelAll;
          cancelAll = connection.prepareStatement(
                                    "call system$cancel_all_queries(?)");

          // bind the session identifier as first argument
          cancelAll.setInt(1, sessionId);
          cancelAll.executeQuery();
        }
        catch (SQLException ex)
        {
          logger.log(Level.SEVERE, "Cancel failed with exception {}", ex);
        }
      }
    }, 5000);

    // Use the internal row generator to execute a query for 120 seconds
    statement = connection.createStatement();
    resultSet = statement.executeQuery(
                   "SELECT count(*) FROM TABLE(generator(timeLimit => 120))");
    resultSetMetaData = resultSet.getMetaData();
    statement.close();
  }
  catch (SQLException ex)
  {
    // assert the sqlstate is what we expect (QUERY CANCELLED)
    assertEquals("sqlstate mismatch",
                 SqlState.QUERY_CANCELED, ex.getSQLState());
  }
  catch (Throwable ex)
  {
    logger.log(Level.SEVERE, "Test failed with exception: ", ex);
  }
  finally
  {
    if (resultSet != null)
      resultSet.close();
    if (statement != null)
      statement.close();
    // close connection
    if (connection != null)
      connection.close();
  }
}