Class QueryResultCache
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbeginSessionScope(String sessionId) BindsessionIdto the calling thread for the duration of one statement, so the query IDs it records land in that session's history.cacheResult(String sql, ResultSet result) Store a query result with a generated query IDvoidclear()Clear all cached resultsvoidDrop this thread's session binding.voidforgetSession(String sessionId) Forget a session's query-ID history, once that session is gone.Generate the query ID of a statement that FAILED.generateQueryId(String sql) Generate a query ID for a SQL statement without caching a result Used for DML statements that don't return result setsGet the last query IDgetQueryId(int index) Get a query ID by relative index.Retrieve a cached result by query IDGet the SQL for a cached querybooleanWhether a query ID names a statement that failed.booleanCheck if a query ID exists in the cacheintsize()Get the number of cached results
-
Constructor Details
-
QueryResultCache
public QueryResultCache() -
QueryResultCache
public QueryResultCache(int maxCachedResults)
-
-
Method Details
-
beginSessionScope
BindsessionIdto the calling thread for the duration of one statement, so the query IDs it records land in that session's history. Pair withclearSessionScope()in a finally block — pooled threads outlive the statements they serve. -
clearSessionScope
public void clearSessionScope()Drop this thread's session binding. Idempotent. -
forgetSession
Forget a session's query-ID history, once that session is gone. -
cacheResult
Store a query result with a generated query ID- Parameters:
sql- The SQL statement executedresult- The result set- Returns:
- The generated query ID
-
getResult
Retrieve a cached result by query ID- Parameters:
queryId- The query ID- Returns:
- The cached result, or null if not found
-
getSql
Get the SQL for a cached query- Parameters:
queryId- The query ID- Returns:
- The SQL statement, or null if not found
-
getLastQueryId
Get the last query ID- Returns:
- The most recent query ID, or null if no queries have been executed
-
getQueryId
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
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
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
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
Whether a query ID names a statement that failed.- Parameters:
queryId- the query ID- Returns:
- true when the statement it names failed
-