hawdb 0.12.0

An embedded Rust graph database for local data-plane workloads
Documentation
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::plan_cache::OptimizedQueryPlan;
use super::query_runtime::{query_runtime_checkpoint, QueryExecutionOptions};
use super::{restrictive_query_limit, DatabaseConfig, QueryOutput};
use crate::executor::{self, Row};
use crate::qos::WorkRequest;
use hawdb_core::Result;
use hawdb_executor::binding::Binding;
use hawdb_executor::memory::enforced_result_memory_budget_with_option;
use hawdb_executor::result_delivery::{ConsumerMemoryMode, OutputLimits, QueryOutputAccumulator};
use hawdb_executor::QueryMemoryLedger;
use hawdb_explain::json::{
    empty_read_execution_profile as owner_empty_read_execution_profile,
    explain_analyze_output_row as owner_explain_analyze_output_row,
    explain_output_row as owner_explain_output_row, ExplainJsonInput,
};

fn explain_json_input<'a>(
    optimized: &'a OptimizedQueryPlan,
    work_request: &'a WorkRequest,
    statement_kind: &'static str,
) -> ExplainJsonInput<'a> {
    ExplainJsonInput {
        physical_plan: &optimized.physical_plan,
        trace: &optimized.trace,
        plan_cache_lookup: optimized.plan_cache_lookup,
        configured_max_optimizer_groups: optimized.configured_max_optimizer_groups,
        effective_max_optimizer_groups: optimized.effective_max_optimizer_groups,
        work_request,
        statement_kind,
    }
}

pub(super) fn explain_output_row(
    optimized: &OptimizedQueryPlan,
    work_request: WorkRequest,
    statement_kind: &'static str,
) -> Row {
    owner_explain_output_row(&explain_json_input(
        optimized,
        &work_request,
        statement_kind,
    ))
}

pub(super) fn explain_analyze_output_row(
    optimized: &OptimizedQueryPlan,
    work_request: WorkRequest,
    statement_kind: &'static str,
    row_count: usize,
    profile: &executor::ReadExecutionProfile,
) -> Row {
    owner_explain_analyze_output_row(
        explain_json_input(optimized, &work_request, statement_kind),
        row_count,
        profile,
    )
}

pub(super) fn empty_read_execution_profile() -> executor::ReadExecutionProfile {
    owner_empty_read_execution_profile()
}

/// The diagnostic row is read output too, independently of ANALYZE's data rows.
pub(super) fn admit_explain_output(
    row: Row,
    config: &DatabaseConfig,
    options: QueryExecutionOptions<'_>,
) -> Result<QueryOutput> {
    query_runtime_checkpoint(options.task_context)?;
    let ledger = QueryMemoryLedger::for_query(&config.execution_memory, options.task_context)?;
    let mut rows = Vec::new();
    let mut consumer = |row| {
        rows.push(row);
        Ok(())
    };
    let (result_budget, budget_option) =
        enforced_result_memory_budget_with_option(&config.execution_memory, options.task_context)?;
    let mut accumulator = QueryOutputAccumulator::new_with_budget_option(
        OutputLimits {
            max_rows: restrictive_query_limit(
                config.read_result_row_limit(),
                options.output_limits.max_rows,
            ),
            max_payload_bytes: restrictive_query_limit(
                config.max_read_result_payload_bytes,
                options.output_limits.max_payload_bytes,
            ),
        },
        result_budget,
        &ledger,
        budget_option,
        ConsumerMemoryMode::Retained,
        &mut consumer,
    )?;
    accumulator.emit(Binding::values(row))?;
    query_runtime_checkpoint(options.task_context)?;
    drop(accumulator);
    Ok(QueryOutput { rows: rows.into() })
}