Skip to content

[DISCUSSION] Introducing a History Trait as Part of UserSession / UserQuery #939

@osipovartem

Description

@osipovartem

Motivation

The proposal is to integrate a history: Arc (WorksheetStore) field into the UserSession structure to enable first-class support for per-session query history. This will mirror behavior found in platforms like Snowflake, where functions like LAST_QUERY_ID() return query metadata for recently executed statements.

pub struct UserSession {
    pub metastore: Arc<dyn Metastore>,
    pub ctx: SessionContext,
    pub ident_normalizer: IdentNormalizer,
    pub executor: DedicatedExecutor,
    pub history: Arc<dyn HistoryStore>, // ← new field
}

The HistoryStore will be a trait responsible for recording and accessing query execution history within a session.


🔍 Problem with Current Architecture

Currently, history recording is handled in core-history via a custom implementation of the ExecutionService:

impl ExecutionService for RecordingExecutionService {
    async fn query(...) { ... }
}

This approach leads to duplication and architectural coupling that can be avoided. It also limits query history visibility strictly to the external recording service layer, rather than integrating it into the session-level context where it naturally belongs.


✅ Proposed Solution

Remove the existing RecordingExecutionService impl with a cleaner and unified CoreExecutionService implementation that records query history via the new History trait:

pub struct CoreExecutionService {
    metastore: Arc<dyn Metastore>,
    df_sessions: Arc<RwLock<HashMap<String, Arc<UserSession>>>>,
}

#[async_trait::async_trait]
impl ExecutionService for CoreExecutionService {
    async fn query(...) {
        // execute query
        // record query in session.history
    }
}

By making history part of the UserSession, we gain several benefits:
• History recording becomes mandatory and consistent across all queries.
• Query history (including last_query_id() support) is available at runtime inside SQL query processing.
• We avoid unnecessary indirection and implementation overhead in core-history.


🧩 Benefits
• ✅ Cleaner architecture with centralized session state.
• ✅ Full support for SQL functions like LAST_QUERY_ID().
• ✅ Removes boilerplate from RecordingExecutionService.
• ✅ Enables more extensible and testable history handling.


🔄 Next Steps
• Remove history recording logic from RecordingExecutionService to a part of CoreExecutor query.
• Add tests to ensure history works correctly across SQL query executions and is session-isolated.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions