hawdb 0.38.0

An embedded Rust graph database for local data-plane workloads
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// 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.

//! Database and session configuration; runtime ownership stays in the facade.

use super::*;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DatabaseConfig {
    pub read_only: bool,
    /// An existing, already durable ancestor whose own name and ancestry are
    /// guaranteed by the host. Writable open synchronizes every directory from
    /// the project through this anchor, inclusive. The host must keep that
    /// namespace stable while the project is open; existence is not proof.
    /// `None` retains the default walk through the existing filesystem mount.
    /// On Windows this includes the volume root and requires writable directory
    /// handles at every level. Ordinary user-profile storage should select an
    /// explicit host-guaranteed anchor; access denial never supplies one.
    /// Concurrent opens of one project must use the same canonical anchor.
    pub namespace_durability_anchor: Option<PathBuf>,
    /// Maximum monotonic age of uncheckpointed data in an admitted persistent
    /// writer. The background-maintenance capability controls worker creation.
    pub automatic_checkpoint_max_age: std::time::Duration,
    /// Bounded pending-child recovery during a writable project open.
    pub branch_create_recovery_limits: BranchCreateRecoveryLimits,
    /// Optional host-wide read row ceiling. `Some(0)` and `None` (the default) mean unlimited.
    /// Session settings may lower a positive ceiling, but cannot raise it.
    /// It also bounds relational intermediate rows unless
    /// `max_read_intermediate_rows` is set.
    pub max_read_result_rows: Option<usize>,
    /// Optional host-wide ceiling on the intermediate rows one relational SQL
    /// statement may scan, join or aggregate, independent of the rows it
    /// returns. The scan-page, row-read and index-read budgets derive from it.
    /// `None` (the default) inherits the effective `max_read_result_rows`
    /// ceiling, so a host that sets only the result ceiling keeps the coupled
    /// bound. `Some(0)` removes the intermediate row ceiling; byte, page-size
    /// and query-memory budgets remain enforced. `max_read_result_rows` and
    /// session settings continue to bound returned rows.
    pub max_read_intermediate_rows: Option<usize>,
    /// Ceiling on the payload bytes one read returns; 64 MiB by default.
    /// `None` removes the ceiling for `Database` calls. Through an admitted
    /// [`crate::HawDBEmbedded`] entry point the result is also bounded by the
    /// runtime governor's per-statement result budget (16 MiB on the
    /// shared-host profile, 2 MiB on mobile, unless the host configures it),
    /// so `None` there
    /// means that budget, not unlimited. The Python binding aligns its
    /// governor's budget with a `Some` ceiling.
    pub max_read_result_payload_bytes: Option<usize>,
    pub execution_memory: executor::ExecutionMemoryConfig,
    pub mutation_limits: hawdb_storage::mutation::MutationLimits,
    /// Directory Cypher `LOAD CSV` may read from. `None` (the default)
    /// refuses every `LOAD CSV` statement. When set, each source must resolve,
    /// after `..` components and symbolic links, to a regular file inside
    /// this directory; relative paths are resolved against it. Only local
    /// files are supported.
    pub csv_import_root: Option<PathBuf>,
    /// Per-file byte, record-size and field-count limits for `LOAD CSV`.
    pub csv_import_limits: executor::CsvImportLimits,
    /// Maximum memo groups available to graph and relational optimizer search.
    pub max_optimizer_groups: Option<usize>,
    /// Maximum memo expressions available to relational join enumeration.
    pub max_relational_join_expressions: Option<usize>,
    pub recovery_mode: RecoveryMode,
    pub max_wal_replay_entries: Option<usize>,
    pub max_wal_replay_bytes: Option<u64>,
    /// Maximum sealed WAL generations recovered for one branch admission.
    /// Checkpoint compaction resets the chain; default is 256.
    pub max_branch_sealed_wal_intervals: usize,
    /// Shared capacity target for all engine-owned project file descriptors.
    /// Defaults to 1024 without pre-opening files. Native Unix acquisition limits
    /// effective admission to the OS soft limit minus 64 host-owned handles.
    /// Process limits remain under host control; idle immutable files use LRU.
    /// Independent contexts for one canonical project must request the same value.
    pub max_open_files: usize,
    /// Reject foreground descriptor admission when `max_open_files` cannot be
    /// met by evicting idle handles. Defaults to false. Internal control I/O,
    /// recovery and maintenance admission always use a non-strict policy.
    pub strict_capacity_limit: bool,
    pub max_wal_quarantine_bytes: u64,
    /// Largest encoded WAL record one commit may write (persistent
    /// databases). A larger commit is refused before anything is written with
    /// the typed, non-poisoning `HawDBError::WalRecordLimitExceeded`. `None`
    /// disables the check.
    pub max_wal_record_bytes: Option<usize>,
    /// Most operations one commit's WAL record may hold; refused the same way.
    pub max_wal_batch_operations: Option<usize>,
    pub max_checkpoint_encoded_bytes: Option<u64>,
    pub max_checkpoint_decoded_bytes: Option<u64>,
    /// Resident byte limit for cached storage segments and pages. Defaults to
    /// 256 MiB. Smaller limits evict more aggressively and may increase reads.
    /// The limit covers the cache; query scratch and materialized rows have
    /// separate memory ownership.
    pub segment_cache_capacity_bytes: u64,
    /// Aggregate encoded graph-manifest bytes allowed during database open.
    /// Payload pages remain governed separately by the segment cache.
    pub max_graph_manifest_open_bytes: u64,
    /// Maximum physical relational-index file bytes fetched by one query.
    /// Cache hits consume the separately derived logical traversal budget only.
    pub max_relational_index_read_bytes: NonZeroUsize,
    pub max_relational_hydration_bytes: NonZeroUsize,
    /// Controls whether the canonical graph is materialized or read on demand.
    /// Defaults to `Auto`.
    pub storage_residency_mode: hawdb_storage::config::StorageResidencyMode,
    /// Size threshold for graph materialization under `Auto`. Defaults to
    /// 64 MiB. Explicit residency modes do not consult this threshold.
    pub auto_materialize_checkpoint_bytes: u64,
    pub max_out_of_core_delta_bytes: Option<u64>,
    /// Derived columnar shadow double-write: every checkpoint also
    /// publishes a column-group catalog under `column-groups/`, and recovery
    /// validates it. Off by default; with the flag off checkpoints are
    /// byte-for-byte unchanged and no shadow directory exists. Reads are
    /// never served from the shadow.
    pub graph_columnar_shadow_checkpoint: bool,
    /// Persistent relational-index publication and read activation mode.
    pub relational_index_mode: hawdb_storage::config::RelationalIndexMode,
    /// Enables the metadata-only monotonic INSERT fast path for RowPage tables.
    /// Disabled by default until workload qualification explicitly activates it.
    pub relational_monotonic_append_fast_path: bool,
    /// Also write the legacy full-row `relational.N.hawdb` file at every
    /// checkpoint. Off by default: the canonical row-page and overflow roots
    /// carry the complete relational state, so a checkpoint writes only the
    /// row pages and overflow extents changed since the previous one (#1737).
    /// Enable it only while a host must keep downgrading to a persistent
    /// format 5 release (HawDB 0.37), which needs the full-row file unless it
    /// opens with OutOfCore residency and Authoritative relational indexes. Each
    /// checkpoint then rewrites the whole relational state.
    pub legacy_relational_checkpoint_file: bool,
    pub max_search_projection_change_log_entries: Option<usize>,
    pub max_search_projection_change_log_bytes: Option<usize>,
    pub search_projection_relational_change_limits:
        hawdb_storage::relational::RelationalPrimaryKeyChangeCaptureLimits,
    /// Maximum entries per graph physical-plan, parameter-binding-template,
    /// or relational SQL-template cache.
    pub max_plan_cache_entries: Option<usize>,
    /// Requested-heap budget for the shared logical parameter-binding cache,
    /// including its keys and table capacity. Defaults to 16 MiB; zero disables
    /// admission. In-flight plans, physical-plan caches, allocator rounding and
    /// fixed cache metadata are outside this retention bound.
    pub max_parameter_binding_cache_bytes: usize,
    /// Requested-heap retention limit for each graph physical-plan cache.
    /// Includes keys, IR, traces, shared payloads and spare table capacity.
    /// Defaults to 16 MiB; zero disables admission. Allocator rounding,
    /// in-flight caller-owned plans and relational SQL templates are separate.
    /// Applied independently to live cache instances, including private read
    /// and transaction caches: total database/process retention grows with
    /// their number and is not charged to `query_memory_bytes`.
    pub max_physical_plan_cache_bytes: usize,
    /// Maximum requested bytes for one retained physical-plan entry, including
    /// its key/value layout. Defaults to 8 MiB; zero disables admission.
    pub max_physical_plan_cache_entry_bytes: usize,
    /// Commit-epoch lag tolerated before the inline query path recomputes
    /// optimizer statistics. `None` or `Some(0)` refreshes on every committed
    /// data change (freshest plans). Larger values amortize statistics
    /// recomputation over write-heavy workloads; stale statistics only affect
    /// plan choice, never correctness.
    pub optimizer_statistics_inline_commit_lag: Option<u64>,
    pub slow_query_log_capacity: usize,
    pub slow_query_log_threshold_micros: u128,
    pub statement_summary_capacity: usize,
    /// Background-work policy used for this database's full runtime lifetime
    /// by its shared local QoS scheduler.
    pub local_qos_policy: LocalQosPolicy,
    pub runtime_capabilities: hawdb_core::RuntimeCapabilities,
    pub compressed_vector_search_mode: CompressedVectorSearchMode,
    pub adaptive_vector_backend_policy: hawdb_optimizer::AdaptiveVectorBackendPolicy,
}

/// Default host row-count ceiling: no ceiling. Byte budgets remain active.
pub const DEFAULT_MAX_READ_RESULT_ROWS: Option<usize> = None;
/// Default relational intermediate-row ceiling: inherit the result-row ceiling.
pub const DEFAULT_MAX_READ_INTERMEDIATE_ROWS: Option<usize> = None;
pub const DEFAULT_MAX_READ_RESULT_PAYLOAD_BYTES: usize = 64 * 1024 * 1024;

/// Per-session result policy. Independent sessions start with no row-count limit.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct SessionConfig {
    /// Maximum result rows; zero means no session row-count limit.
    /// Payload, execution-memory and explicit host ceilings remain enforced.
    /// A positive ceiling at or above usize::MAX imposes no additional bound:
    /// no in-process result can exceed platform row capacity. On wasm32 this
    /// includes values such as 5,000,000,000; positive host ceilings still apply.
    pub max_result_rows: u64,
}

impl SessionConfig {
    pub(super) fn row_limit(self) -> Option<usize> {
        // No representable result can exceed usize::MAX rows. Avoid asking
        // the execution detector to add one to that effective infinity.
        (self.max_result_rows != 0)
            .then(|| usize::try_from(self.max_result_rows).unwrap_or(usize::MAX))
            .filter(|rows| *rows != usize::MAX)
    }
}

impl DatabaseConfig {
    /// Effective host row-count ceiling. Both zero and `None` disable it.
    pub fn read_result_row_limit(&self) -> Option<usize> {
        self.max_read_result_rows.filter(|rows| *rows != 0)
    }

    /// Effective relational intermediate-row ceiling. `None` inherits
    /// [`Self::read_result_row_limit`]; `Some(0)` disables the ceiling.
    pub fn read_intermediate_row_limit(&self) -> Option<usize> {
        match self.max_read_intermediate_rows {
            None => self.read_result_row_limit(),
            Some(0) => None,
            Some(rows) => Some(rows),
        }
    }

    /// The `LOAD CSV` file policy, or `None` when CSV import is disabled.
    pub(super) fn csv_import_policy(&self) -> Option<executor::CsvImportPolicy> {
        self.csv_import_root
            .as_ref()
            .map(|root| executor::CsvImportPolicy::new(root.clone(), self.csv_import_limits))
    }

    pub(super) fn wal_replay_config(&self) -> WalReplayConfig {
        WalReplayConfig {
            max_open_files: self.max_open_files,
            strict_capacity_limit: self.strict_capacity_limit,
            recovery_mode: self.recovery_mode,
            max_entries: self.max_wal_replay_entries,
            max_bytes: self.max_wal_replay_bytes,
            max_branch_sealed_wal_intervals: self.max_branch_sealed_wal_intervals,
            max_quarantine_bytes: self.max_wal_quarantine_bytes,
            max_record_bytes: self.max_wal_record_bytes,
            max_batch_operations: self.max_wal_batch_operations,
            max_checkpoint_encoded_bytes: self.max_checkpoint_encoded_bytes,
            max_checkpoint_decoded_bytes: self.max_checkpoint_decoded_bytes,
            segment_cache_capacity_bytes: self.segment_cache_capacity_bytes,
            max_graph_manifest_open_bytes: self.max_graph_manifest_open_bytes,
            residency_mode: self.storage_residency_mode,
            auto_materialize_checkpoint_bytes: self.auto_materialize_checkpoint_bytes,
            max_out_of_core_delta_bytes: self.max_out_of_core_delta_bytes,
            graph_columnar_shadow_checkpoint: self.graph_columnar_shadow_checkpoint,
            relational_index_mode: self.relational_index_mode,
        }
    }
}

impl Default for DatabaseConfig {
    fn default() -> Self {
        Self {
            read_only: false,
            namespace_durability_anchor: None,
            automatic_checkpoint_max_age: std::time::Duration::from_secs(60),
            branch_create_recovery_limits: BranchCreateRecoveryLimits::default(),
            max_read_result_rows: DEFAULT_MAX_READ_RESULT_ROWS,
            max_read_intermediate_rows: DEFAULT_MAX_READ_INTERMEDIATE_ROWS,
            max_read_result_payload_bytes: Some(DEFAULT_MAX_READ_RESULT_PAYLOAD_BYTES),
            execution_memory: executor::ExecutionMemoryConfig::default(),
            mutation_limits: hawdb_storage::mutation::MutationLimits::default(),
            csv_import_root: None,
            csv_import_limits: executor::CsvImportLimits::default(),
            max_optimizer_groups: None,
            max_relational_join_expressions: None,
            recovery_mode: RecoveryMode::default(),
            max_wal_replay_entries: Some(hawdb_storage::config::DEFAULT_MAX_WAL_REPLAY_ENTRIES),
            max_wal_replay_bytes: Some(hawdb_storage::config::DEFAULT_MAX_WAL_REPLAY_BYTES),
            max_branch_sealed_wal_intervals:
                hawdb_storage::config::DEFAULT_MAX_BRANCH_SEALED_WAL_INTERVALS,
            max_open_files: hawdb_storage::file_descriptors::DEFAULT_MAX_OPEN_FILES,
            strict_capacity_limit: false,
            max_wal_quarantine_bytes: hawdb_storage::config::DEFAULT_MAX_WAL_QUARANTINE_BYTES,
            max_wal_record_bytes: Some(hawdb_storage::config::DEFAULT_MAX_WAL_RECORD_BYTES),
            max_wal_batch_operations: Some(hawdb_storage::config::DEFAULT_MAX_WAL_BATCH_OPERATIONS),
            max_checkpoint_encoded_bytes: Some(
                hawdb_storage::config::DEFAULT_MAX_CHECKPOINT_ENCODED_BYTES,
            ),
            max_checkpoint_decoded_bytes: Some(
                hawdb_storage::config::DEFAULT_MAX_CHECKPOINT_DECODED_BYTES,
            ),
            segment_cache_capacity_bytes:
                hawdb_storage::config::DEFAULT_SEGMENT_CACHE_CAPACITY_BYTES,
            max_graph_manifest_open_bytes:
                hawdb_storage::config::DEFAULT_MAX_GRAPH_MANIFEST_OPEN_BYTES,
            max_relational_index_read_bytes: NonZeroUsize::new(
                hawdb_storage::relational::DEFAULT_RELATIONAL_INDEX_READ_BYTES,
            )
            .expect("default relational index read byte budget is non-zero"),
            max_relational_hydration_bytes: NonZeroUsize::new(
                hawdb_storage::relational::DEFAULT_MAX_RELATIONAL_HYDRATION_BYTES,
            )
            .expect("default relational hydration byte budget is non-zero"),
            storage_residency_mode: hawdb_storage::config::StorageResidencyMode::Auto,
            auto_materialize_checkpoint_bytes:
                hawdb_storage::config::DEFAULT_AUTO_MATERIALIZE_CHECKPOINT_BYTES,
            max_out_of_core_delta_bytes: Some(
                hawdb_storage::config::DEFAULT_MAX_OUT_OF_CORE_DELTA_BYTES,
            ),
            graph_columnar_shadow_checkpoint: false,
            relational_index_mode: hawdb_storage::config::RelationalIndexMode::default(),
            relational_monotonic_append_fast_path: false,
            legacy_relational_checkpoint_file: false,
            max_search_projection_change_log_entries: Some(
                DEFAULT_SEARCH_PROJECTION_CHANGE_LOG_MAX_ENTRIES,
            ),
            max_search_projection_change_log_bytes: Some(
                DEFAULT_SEARCH_PROJECTION_CHANGE_LOG_MAX_BYTES,
            ),
            search_projection_relational_change_limits: Default::default(),
            max_plan_cache_entries: Some(DEFAULT_PLAN_CACHE_MAX_ENTRIES),
            max_parameter_binding_cache_bytes: 16 * 1024 * 1024,
            max_physical_plan_cache_bytes: 16 * 1024 * 1024,
            max_physical_plan_cache_entry_bytes: 8 * 1024 * 1024,
            optimizer_statistics_inline_commit_lag: None,
            slow_query_log_capacity: system_sql::DEFAULT_SLOW_QUERY_LOG_CAPACITY,
            slow_query_log_threshold_micros: system_sql::DEFAULT_SLOW_QUERY_LOG_THRESHOLD_MICROS,
            statement_summary_capacity: system_sql::DEFAULT_STATEMENT_SUMMARY_CAPACITY,
            local_qos_policy: LocalQosPolicy::default(),
            runtime_capabilities: crate::compiled_runtime_capabilities(),
            compressed_vector_search_mode: CompressedVectorSearchMode::Disabled,
            adaptive_vector_backend_policy: hawdb_optimizer::AdaptiveVectorBackendPolicy::default(),
        }
    }
}

pub(super) fn restrictive_query_limit(
    configured: Option<usize>,
    requested: Option<usize>,
) -> Option<usize> {
    match (configured, requested) {
        (Some(configured), Some(requested)) => Some(configured.min(requested)),
        (Some(configured), None) => Some(configured),
        (None, Some(requested)) => Some(requested),
        (None, None) => None,
    }
}

pub(super) fn relational_query_limits_with_payload(
    config: &DatabaseConfig,
    max_rows: Option<usize>,
    max_payload_bytes: Option<usize>,
) -> crate::relational_sql::RelationalQueryLimits {
    let max_output_rows =
        restrictive_query_limit(config.read_result_row_limit(), max_rows).unwrap_or(usize::MAX);
    let max_output_payload_bytes =
        restrictive_query_limit(config.max_read_result_payload_bytes, max_payload_bytes)
            .unwrap_or(DEFAULT_MAX_READ_RESULT_PAYLOAD_BYTES);
    // Intermediate work has its own host ceiling (#1679). Unset, it inherits
    // the result-row ceiling; a request's output limit never lowers it.
    let max_intermediate_rows = config.read_intermediate_row_limit().unwrap_or(usize::MAX);
    // A blocking relational query first reads the bounded scan projection and
    // then re-reads at most `max_output_rows` selected locators with the final
    // output projection. Budget both phases explicitly so a scan that exactly
    // reaches its intermediate-row limit can still produce an admitted result.
    let max_row_read_rows = max_intermediate_rows.saturating_add(max_output_rows).max(1);
    // Row-page format v2 closes pages at a byte target, so rows per page
    // depend on row width and the row cap is not a density (#1521). A scan
    // decodes at least one row from every page it reads after its first, so
    // its pages are bounded by its intermediate rows plus one; each point
    // read touches one page. The target size is only writer packing policy:
    // every single-row page may be wider, up to the hard maximum. Budget the
    // maximum for every admitted page so consecutive wide pages are not
    // falsely refused by a limit derived from the row contract.
    let max_scan_pages = max_intermediate_rows.saturating_add(1);
    let max_row_read_pages = max_scan_pages.saturating_add(max_output_rows).max(1);
    let max_row_read_bytes = max_row_read_pages
        .saturating_mul(hawdb_storage::relational::DEFAULT_RELATIONAL_ROW_PAGE_BYTES)
        .max(1);
    // Logical index traversal includes cache hits and must scale with the
    // statement's admitted intermediate rows. Physical index reads retain a
    // separate fixed I/O ceiling so a warm cache cannot disable query bounds.
    let max_index_read_pages = max_intermediate_rows
        .saturating_mul(
            hawdb_storage::relational::DEFAULT_RELATIONAL_INDEX_READ_TREE_HEIGHT as usize,
        )
        .max(1);
    let max_index_read_bytes = max_index_read_pages
        .saturating_mul(hawdb_storage::index_page::DEFAULT_IMMUTABLE_INDEX_PAGE_BYTES)
        .max(1);
    crate::relational_sql::RelationalQueryLimits {
        max_output_rows,
        max_output_payload_bytes,
        max_intermediate_rows,
        max_candidate_work: max_intermediate_rows,
        hydration: hawdb_storage::relational::RelationalHydrationBudget {
            max_rows: max_row_read_rows,
            max_compressed_bytes: config.max_relational_hydration_bytes.get(),
            max_decompressed_bytes: config.max_relational_hydration_bytes.get(),
            max_memory_bytes: config.max_relational_hydration_bytes.get(),
            ..hawdb_storage::relational::RelationalHydrationBudget::default()
        },
        index_read: hawdb_storage::relational::RelationalIndexReadLimits {
            max_pages: NonZeroUsize::new(max_index_read_pages)
                .expect("relational index query page budget is non-zero"),
            max_rows: NonZeroUsize::new(max_intermediate_rows.max(1))
                .expect("relational index query row budget is non-zero"),
            max_bytes: NonZeroUsize::new(max_index_read_bytes)
                .expect("relational index query byte budget is non-zero"),
            max_file_bytes: config.max_relational_index_read_bytes.get(),
            ..hawdb_storage::relational::RelationalIndexReadLimits::default()
        },
        row_read: hawdb_storage::relational::RelationalRowPageSnapshotReadLimits {
            demand: hawdb_storage::relational::RelationalRowPageDemandReadLimits {
                max_pages: NonZeroUsize::new(max_row_read_pages)
                    .expect("relational row query page budget is non-zero"),
                max_rows: NonZeroUsize::new(max_row_read_rows)
                    .expect("relational row query row budget is non-zero"),
                max_bytes: NonZeroUsize::new(max_row_read_bytes)
                    .expect("relational row query byte budget is non-zero"),
                ..hawdb_storage::relational::RelationalRowPageDemandReadLimits::default()
            },
            // Each merged uncheckpointed entry is a row the statement reads,
            // so it gets the same limit as checkpointed rows instead of a
            // fixed entry count (#1480). The two are counted separately, so a
            // statement may read up to that many of each. The merge streams;
            // `max_overlay_bytes` bounds what it holds at once.
            max_overlay_entries: NonZeroUsize::new(max_row_read_rows)
                .expect("relational overlay entry budget is non-zero"),
            ..hawdb_storage::relational::RelationalRowPageSnapshotReadLimits::default()
        },
    }
}

pub(super) fn relational_join_enumeration_config_from_database_config(
    config: &DatabaseConfig,
) -> hawdb_optimizer::RelationalJoinEnumerationConfig {
    let defaults = hawdb_optimizer::RelationalJoinEnumerationConfig::default();
    hawdb_optimizer::RelationalJoinEnumerationConfig {
        max_groups: optimizer_config_from_database_config(config).max_groups,
        max_expressions: config
            .max_relational_join_expressions
            .unwrap_or(defaults.max_expressions),
    }
}

pub(super) fn relational_query_resource_context<'a>(
    config: &'a DatabaseConfig,
    max_rows: Option<usize>,
    max_payload_bytes: Option<usize>,
    task_context: Option<&'a hawdb_core::RuntimeTaskContext>,
) -> crate::relational_sql::RelationalQueryResourceContext<'a> {
    crate::relational_sql::RelationalQueryResourceContext::new(
        relational_join_enumeration_config_from_database_config(config),
        relational_query_limits_with_payload(config, max_rows, max_payload_bytes),
        &config.execution_memory,
        task_context,
    )
}

pub(super) fn relational_plan_template_cache_from_database_config(
    config: &DatabaseConfig,
) -> Arc<crate::relational_sql::RelationalPlanTemplateCache> {
    Arc::new(crate::relational_sql::RelationalPlanTemplateCache::new(
        config.max_plan_cache_entries,
    ))
}

pub(super) fn relational_index_read_mode<
    'a,
    R: hawdb_relational::index_runtime::RelationalIndexStoreReader,
>(
    config: &DatabaseConfig,
    store: &'a R,
) -> hawdb_relational::index_runtime::RelationalIndexReadMode<'a, R> {
    match config.relational_index_mode {
        hawdb_storage::config::RelationalIndexMode::Materialized => {
            hawdb_relational::index_runtime::RelationalIndexReadMode::Materialized
        }
        hawdb_storage::config::RelationalIndexMode::Shadow => {
            hawdb_relational::index_runtime::RelationalIndexReadMode::Shadow(store)
        }
        hawdb_storage::config::RelationalIndexMode::DemandPaged => {
            hawdb_relational::index_runtime::RelationalIndexReadMode::DemandPaged(store)
        }
        hawdb_storage::config::RelationalIndexMode::Authoritative => {
            hawdb_relational::index_runtime::RelationalIndexReadMode::Authoritative(store)
        }
    }
}

pub(super) fn optimizer_config_from_database_config(config: &DatabaseConfig) -> OptimizerConfig {
    let mut optimizer = OptimizerConfig::default();
    if let Some(max_groups) = config.max_optimizer_groups {
        optimizer.max_groups = max_groups;
    }
    optimizer
}

pub(super) fn optimizer_from_database_config(config: &DatabaseConfig) -> CascadesOptimizer {
    CascadesOptimizer::with_context(
        OptimizerContext::from_config(optimizer_config_from_database_config(config))
            .with_resource_hints(ResourceHints {
                priority: ResourceHints::default().priority,
                max_memory_bytes: Some(
                    u64::try_from(config.execution_memory.blocking_operator_bytes.get())
                        .unwrap_or(u64::MAX),
                ),
                max_parallelism: executor::MAX_MORSEL_PARALLELISM,
            }),
    )
}