Class QueryResultCache

java.lang.Object
dev.frostlake.executor.QueryResultCache

public class QueryResultCache extends Object
Query results held by query ID so RESULT_SCAN can read them back, plus the per-session history of query IDs that LAST_QUERY_ID walks.

The history is keyed by SESSION, not by thread. A session's statements are not guaranteed to run on one thread — the HTTP server hands consecutive requests to whichever pool thread is free — so a thread-keyed history loses the previous statement's ID between two requests of the same session, and the SHOW … then SELECT … FROM TABLE(RESULT_SCAN(LAST_QUERY_ID())) pattern fails with "No previous query results available" while working in-process. Callers that serve more than one session bind the session around each statement with beginSessionScope(String) / clearSessionScope(); anything else shares one implicit session, which is what an embedded engine wants.

Cached RESULTS stay shared across sessions: a query ID is a handle anyone holding it can scan, which is how Snowflake behaves. Only the history of "what did I run last" is per session.

A statement that FAILED gets an ID too, and LAST_QUERY_ID names it, but it has no result: those IDs are remembered, bounded like the results, so RESULT_SCAN can refuse one with the reason.

  • Constructor Details

    • QueryResultCache

      public QueryResultCache()
    • QueryResultCache

      public QueryResultCache(int maxCachedResults)
  • Method Details

    • beginSessionScope

      public void beginSessionScope(String sessionId)
      Bind sessionId to the calling thread for the duration of one statement, so the query IDs it records land in that session's history. Pair with clearSessionScope() in a finally block — pooled threads outlive the statements they serve.
    • clearSessionScope

      public void clearSessionScope()
      Drop this thread's session binding. Idempotent.
    • forgetSession

      public void forgetSession(String sessionId)
      Forget a session's query-ID history, once that session is gone.
    • cacheResult

      public String cacheResult(String sql, ResultSet result)
      Store a query result with a generated query ID
      Parameters:
      sql - The SQL statement executed
      result - The result set
      Returns:
      The generated query ID
    • getResult

      public ResultSet getResult(String queryId)
      Retrieve a cached result by query ID
      Parameters:
      queryId - The query ID
      Returns:
      The cached result, or null if not found
    • getSql

      public String getSql(String queryId)
      Get the SQL for a cached query
      Parameters:
      queryId - The query ID
      Returns:
      The SQL statement, or null if not found
    • getLastQueryId

      public String getLastQueryId()
      Get the last query ID
      Returns:
      The most recent query ID, or null if no queries have been executed
    • getQueryId

      public String getQueryId(int index)
      Get a query ID by relative index. Snowflake semantics: -1 = most recent (default), -2 = one before that, etc. 0 is treated as -1 (most recent) for compatibility.
    • hasResult

      public boolean hasResult(String queryId)
      Check if a query ID exists in the cache
      Parameters:
      queryId - The query ID
      Returns:
      true if the result is cached
    • clear

      public void clear()
      Clear all cached results
    • size

      public int size()
      Get the number of cached results
      Returns:
      The cache size
    • generateQueryId

      public String generateQueryId(String sql)
      Generate a query ID for a SQL statement without caching a result Used for DML statements that don't return result sets
      Parameters:
      sql - The SQL statement
      Returns:
      The generated query ID
    • generateFailedQueryId

      public String generateFailedQueryId(String sql)
      Generate the query ID of a statement that FAILED. It enters the session's history like any other, so LAST_QUERY_ID names it, and is remembered as failed, so RESULT_SCAN refuses it with the reason.
      Parameters:
      sql - the SQL statement
      Returns:
      the generated query ID
    • hasFailed

      public boolean hasFailed(String queryId)
      Whether a query ID names a statement that failed.
      Parameters:
      queryId - the query ID
      Returns:
      true when the statement it names failed