hawdb 0.23.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
// 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.

//! Complete, pinned analytics staging and atomic Cypher/SQL publication.

use super::*;
use std::num::NonZeroUsize;
use std::sync::atomic::{AtomicU64, Ordering};

static NEXT_PUBLICATION: AtomicU64 = AtomicU64::new(0);

const PUBLICATIONS: &str = "__hawdb_analytics_publications";
const STAGED_ROW_BYTES: usize = 128;
const STAGED_HEADER_BYTES: usize = 1024;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GraphAnalyticsAlgorithm {
    PageRank(crate::PageRankOptions),
    Louvain(crate::LouvainOptions),
    PageRankProcedure(crate::PageRankProcedureOptions),
    LouvainProcedure(crate::LouvainProcedureOptions),
}

/// Row and payload limits cover the complete query output, including every
/// Louvain hierarchy level. The staging limit covers one final value per node.
#[derive(Debug, Clone, PartialEq)]
pub struct GraphAnalyticsRequest {
    pub projection: String,
    pub algorithm: GraphAnalyticsAlgorithm,
    pub max_rows: NonZeroUsize,
    pub max_payload_bytes: NonZeroUsize,
    pub max_staged_bytes: NonZeroUsize,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GraphAnalyticsFreshness {
    Unavailable,
    Fresh,
    Stale,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GraphAnalyticsPublicationStatus {
    pub projection: String,
    pub property: String,
    pub publication_id: Option<String>,
    pub computed_at_commit_epoch: Option<u64>,
    pub published_at_commit_epoch: Option<u64>,
    pub current_commit_epoch: u64,
    pub freshness: GraphAnalyticsFreshness,
}

/// A complete result bound to one database incarnation, branch, projection,
/// options and pinned source epoch. Keeping it permits a lost-response retry.
#[derive(Debug)]
pub struct PreparedGraphAnalytics {
    snapshot: DatabaseReadTransaction,
    request: GraphAnalyticsRequest,
    publication_id: String,
    rows: BTreeMap<i64, (i64, Value)>,
    _staging: hawdb_executor::QueryMemoryLease,
    _retained: Option<hawdb_qos::RuntimeRetainedMemory>,
    report: QueryStreamReport,
}

impl PreparedGraphAnalytics {
    pub fn computed_at_commit_epoch(&self) -> u64 {
        self.snapshot.commit_epoch()
    }

    pub fn publication_id(&self) -> &str {
        &self.publication_id
    }

    /// Number of node values to publish, after selecting the highest Louvain
    /// level per node. The execution report counts all hierarchy rows instead.
    pub fn row_count(&self) -> usize {
        self.rows.len()
    }

    pub fn execution_report(&self) -> &QueryStreamReport {
        &self.report
    }
}

impl Database {
    /// Computes a complete result under background admission. Ordinary algorithm
    /// CALLs remain read-only; this method stages without publishing properties.
    pub fn prepare_graph_analytics(
        &self,
        request: GraphAnalyticsRequest,
        task_context: Option<&hawdb_core::RuntimeTaskContext>,
    ) -> Result<PreparedGraphAnalytics> {
        self.ensure_runtime_capability(hawdb_core::RuntimeCapability::BackgroundMaintenance)?;
        validate_request(&request)?;
        hawdb_executor::pipeline::runtime_checkpoint(task_context)?;
        let budget = task_context
            .and_then(hawdb_core::RuntimeTaskContext::memory_reservation)
            .map_or(
                self.config.execution_memory.query_memory_bytes.get(),
                |reservation| {
                    self.config
                        .execution_memory
                        .query_memory_bytes
                        .get()
                        .min(usize::try_from(reservation.memory_bytes()).unwrap_or(usize::MAX))
                },
            );
        let query_bytes = budget
            .checked_sub(request.max_staged_bytes.get())
            .and_then(NonZeroUsize::new)
            .ok_or_else(|| {
                HawDBError::Execution(
                    "analytics staging leaves no query_memory_bytes within the caller/configured budget".into(),
                )
            })?;
        let mut query_context = task_context.cloned();
        if let Some(context) = &mut query_context
            && let Some(reservation) = context.memory_reservation()
        {
            *context =
                context
                    .clone()
                    .with_memory_reservation(hawdb_core::RuntimeMemoryReservation::new(
                        query_bytes.get() as u64,
                        reservation.result_bytes(),
                    ));
        }
        let result_bytes = task_context
            .and_then(hawdb_core::RuntimeTaskContext::memory_reservation)
            .map_or(request.max_payload_bytes.get() as u64, |reservation| {
                reservation
                    .result_bytes()
                    .min(request.max_payload_bytes.get() as u64)
            });
        let runtime_permit =
            self.analytics_runtime_permit(query_bytes.get() as u64, result_bytes)?;
        // Staging survives active work; its extra charge must survive without
        // keeping CPU, background-task or I/O slots occupied.
        let retained = runtime_permit
            .as_ref()
            .map(|permit| permit.reserve_retained_memory(request.max_staged_bytes.get() as u64))
            .transpose()
            .map_err(|admission| {
                HawDBError::Execution(format!(
                    "analytics staging memory not admitted: {admission}"
                ))
            })?;
        if let Some(permit) = &runtime_permit {
            query_context = Some(permit.bind_task_context(query_context.unwrap_or_default()));
        }
        let ledger = hawdb_executor::QueryMemoryLedger::new(
            NonZeroUsize::new(budget).expect("the staging subtraction proved a positive budget"),
        );
        let staging = ledger
            .account(
                hawdb_executor::QueryMemoryClass::ResultMaterialization,
                "complete analytics staging",
                request.max_staged_bytes,
            )
            .reserve(request.max_staged_bytes.get())?;
        let mut snapshot = match query_context.as_ref() {
            Some(context) => self.begin_read_transaction_with_context(context)?,
            None => self.begin_read_transaction()?,
        };
        snapshot.config.execution_memory.query_memory_bytes = query_bytes;
        let scheduler = self.local_qos_scheduler_for_work();
        let units = snapshot
            .store
            .node_count_for_label(None)
            .saturating_add(snapshot.store.relationship_count_for_type(None))
            .max(1);
        let permit = scheduler
            .try_start(WorkRequest::background(WorkClass::Projection, units))
            .map_err(|admission| {
                HawDBError::Execution(format!(
                    "background graph analytics not admitted: {admission:?}"
                ))
            })?;
        let (query, parameters, value_column) = algorithm_query(&request)?;
        let mut rows = BTreeMap::new();
        let mut streamed_rows = 0;
        let result = snapshot.query_with_params_streaming(
            &query,
            &parameters,
            QueryStreamOptions {
                max_rows: Some(request.max_rows.get()),
                max_payload_bytes: Some(request.max_payload_bytes.get()),
            },
            |row| {
                let Some(Value::Int(node)) = row.get("node") else {
                    return Err(HawDBError::StorageIntegrity(
                        "analytics returned an invalid node ID".into(),
                    ));
                };
                let value = row.get(value_column).ok_or_else(|| {
                    HawDBError::StorageIntegrity("analytics result is missing its value".into())
                })?;
                if !matches!(value, Value::Int(_) | Value::Float(_)) {
                    return Err(HawDBError::StorageIntegrity(
                        "analytics returned a nonscalar value".into(),
                    ));
                }
                let level = match request.algorithm {
                    GraphAnalyticsAlgorithm::PageRank(_)
                    | GraphAnalyticsAlgorithm::PageRankProcedure(_) => 0,
                    GraphAnalyticsAlgorithm::Louvain(_)
                    | GraphAnalyticsAlgorithm::LouvainProcedure(_) => match row.get("level") {
                        Some(Value::Int(level))
                            if usize::try_from(*level).is_ok_and(|level| {
                                level
                                    < match request.algorithm {
                                        GraphAnalyticsAlgorithm::Louvain(options) => {
                                            options.max_levels.max(1)
                                        }
                                        GraphAnalyticsAlgorithm::LouvainProcedure(options) => {
                                            options.max_levels.max(1)
                                        }
                                        _ => unreachable!(),
                                    }
                            }) =>
                        {
                            *level
                        }
                        _ => {
                            return Err(HawDBError::StorageIntegrity(
                                "analytics returned an invalid hierarchy level".into(),
                            ));
                        }
                    },
                };
                stage_analytics_row(
                    &mut rows,
                    *node,
                    level,
                    value,
                    request.max_staged_bytes.get(),
                )?;
                streamed_rows += 1;
                Ok(())
            },
        );
        permit.finish_with_outcome(result.is_ok());
        let report = result?;
        if !report.fully_streamed || report.output_rows != streamed_rows {
            return Err(HawDBError::Execution(
                "analytics staging did not cover the complete result".into(),
            ));
        }
        let nonce = NEXT_PUBLICATION
            .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |value| {
                value.checked_add(1)
            })
            .map_err(|_| {
                HawDBError::Execution("analytics publication identity exhausted".into())
            })?;
        let publication_id = format!("{}:{nonce}", snapshot.commit_epoch());
        Ok(PreparedGraphAnalytics {
            snapshot,
            request,
            publication_id,
            rows,
            _staging: staging,
            _retained: retained,
            report,
        })
    }

    /// Atomically applies the staged scalar results and their provenance. A
    /// changed source is rejected before staging mutations. Repeating the same
    /// publication ID returns its durable outcome, even after a lost response.
    pub fn publish_graph_analytics(
        &mut self,
        prepared: &PreparedGraphAnalytics,
        property: &str,
        task_context: Option<&hawdb_core::RuntimeTaskContext>,
    ) -> Result<GraphAnalyticsPublicationStatus> {
        validate_identifier(property)?;
        if !Arc::ptr_eq(
            &prepared.snapshot._pin.pins,
            &self.runtime.get()?.reader_pins,
        ) {
            return Err(HawDBError::Execution(
                "analytics result belongs to a different database incarnation or branch".into(),
            ));
        }
        let existing =
            self.graph_analytics_publication_status(&prepared.request.projection, property)?;
        if existing.publication_id.as_deref() == Some(prepared.publication_id()) {
            return Ok(existing);
        }
        hawdb_executor::pipeline::runtime_checkpoint(task_context)?;
        let source_epoch = prepared.computed_at_commit_epoch();
        if self.commit_epoch()? != source_epoch {
            return Err(HawDBError::Execution(
                "analytics source epoch changed before publication".into(),
            ));
        }
        let published_epoch = source_epoch
            .checked_add(1)
            .ok_or_else(|| HawDBError::Execution("analytics publication epoch overflow".into()))?;
        let source = i64::try_from(source_epoch).map_err(|_| {
            HawDBError::Execution("analytics source epoch exceeds SQL BIGINT".into())
        })?;
        let published = i64::try_from(published_epoch).map_err(|_| {
            HawDBError::Execution("analytics publication epoch exceeds SQL BIGINT".into())
        })?;
        let operations = prepared.rows.len().saturating_mul(4).saturating_add(3);
        if operations > self.config.mutation_limits.max_operations.get()
            || self
                .config
                .max_wal_batch_operations
                .is_some_and(|max| operations > max)
        {
            return Err(HawDBError::Execution(
                "complete analytics publication exceeds transaction operation budget".into(),
            ));
        }
        let staging_bytes = prepared.request.max_staged_bytes.get() as u64;
        let query_bytes = (self.config.execution_memory.query_memory_bytes.get() as u64)
            .saturating_sub(staging_bytes);
        let mutation_bytes = hawdb_executor::memory::estimated_mutation_memory_bytes(
            self.config.mutation_limits,
            self.config.max_wal_record_bytes,
        );
        let mut working_bytes = query_bytes.max(mutation_bytes);
        let mut result_bytes = self.config.mutation_limits.max_result_payload_bytes.get() as u64;
        let mut publication_context = task_context.cloned();
        if let Some(context) = &mut publication_context
            && let Some(reservation) = context.memory_reservation()
        {
            let available = reservation.memory_bytes().saturating_sub(staging_bytes);
            if available == 0 || mutation_bytes > available {
                return Err(HawDBError::Execution(
                    "complete analytics publication exceeds caller memory reservation".into(),
                ));
            }
            working_bytes = working_bytes.min(available);
            result_bytes = result_bytes.min(reservation.result_bytes());
            *context =
                context
                    .clone()
                    .with_memory_reservation(hawdb_core::RuntimeMemoryReservation::new(
                        working_bytes,
                        result_bytes,
                    ));
        }
        let runtime_permit = self.analytics_runtime_permit(working_bytes, result_bytes)?;
        if let Some(permit) = &runtime_permit {
            publication_context =
                Some(permit.bind_task_context(publication_context.unwrap_or_default()));
        }
        let task_context = publication_context.as_ref();
        let scheduler = self.local_qos_scheduler_for_work();
        let permit = scheduler
            .try_start(WorkRequest::background(WorkClass::Projection, operations))
            .map_err(|admission| {
                HawDBError::Execution(format!(
                    "background analytics publication not admitted: {admission:?}"
                ))
            })?;
        let result = (|| {
            let needs_schema = self
                .runtime
                .get()?
                .store
                .relational_state()
                .table_schema(PUBLICATIONS)
                .is_none();
            let mut tx = match task_context {
                Some(context) => self.begin_transaction_with_context(context)?,
                None => self.begin_transaction()?,
            };
            // The property name is validated structural input. Every data value,
            // internal ID, and epoch is bound. The host does no graph scan or join.
            let update = format!("MATCH (n) WHERE id(n) = $node SET n.{property} = $value, n.{property}_computed_at_commit_epoch = $source, n.{property}_published_at_commit_epoch = $published, n.{property}_publication_id = $publication");
            for (ordinal, (node, (_, value))) in prepared.rows.iter().enumerate() {
                if ordinal.is_multiple_of(1024) {
                    hawdb_executor::pipeline::runtime_checkpoint(task_context)?;
                }
                tx.query_with_params(
                    &update,
                    &BTreeMap::from([
                        ("node".into(), Value::Int(*node)),
                        ("value".into(), value.clone()),
                        ("source".into(), Value::Int(source)),
                        ("published".into(), Value::Int(published)),
                        (
                            "publication".into(),
                            Value::String(prepared.publication_id.clone()),
                        ),
                    ]),
                )?;
            }
            if needs_schema {
                tx.query_sql(&format!("CREATE TABLE {PUBLICATIONS} (projection TEXT NOT NULL, property TEXT NOT NULL, publication_id TEXT NOT NULL, computed_at_commit_epoch BIGINT NOT NULL, published_at_commit_epoch BIGINT NOT NULL, algorithm TEXT NOT NULL, definition TEXT NOT NULL, row_count BIGINT NOT NULL, PRIMARY KEY (projection, property))"))?;
            }
            let key = [
                Value::String(prepared.request.projection.clone()),
                Value::String(property.into()),
            ];
            tx.query_sql_with_params(
                &format!("DELETE FROM {PUBLICATIONS} WHERE projection = $1 AND property = $2"),
                &key,
            )?;
            let definition = prepared
                .snapshot
                .store
                .projected_graph_definition(&prepared.request.projection)
                .ok_or_else(|| {
                    HawDBError::StorageIntegrity("pinned analytics projection disappeared".into())
                })?;
            tx.query_sql_with_params(&format!("INSERT INTO {PUBLICATIONS} (projection, property, publication_id, computed_at_commit_epoch, published_at_commit_epoch, algorithm, definition, row_count) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"), &[
            key[0].clone(), key[1].clone(), Value::String(prepared.publication_id.clone()), Value::Int(source), Value::Int(published),
            Value::String(format!("{:?}", prepared.request.algorithm)), Value::String(format!("{definition:?}")),
            Value::Int(i64::try_from(prepared.rows.len()).map_err(|_| HawDBError::Execution("analytics row count overflow".into()))?),
        ])?;
            hawdb_executor::pipeline::runtime_checkpoint(task_context)?;
            tx.commit()?;
            self.graph_analytics_publication_status(&prepared.request.projection, property)
        })();
        permit.finish_with_outcome(result.is_ok());
        result
    }

    fn analytics_runtime_permit(
        &self,
        memory_bytes: u64,
        result_bytes: u64,
    ) -> Result<Option<hawdb_qos::RuntimePermit>> {
        self.runtime_governor
            .as_ref()
            .map(|governor| {
                governor.try_admit(
                    hawdb_qos::RuntimeWorkRequest::background_maintenance(memory_bytes)
                        .with_result_bytes(
                            result_bytes.min(governor.snapshot().limits.result_budget_bytes),
                        )
                        .with_blocking(true)
                        .with_io_slots(1),
                )
            })
            .transpose()
            .map_err(|admission| {
                HawDBError::Execution(format!(
                    "background graph analytics runtime not admitted: {admission}"
                ))
            })
    }

    /// Freshness is conservative: any commit after publication makes the result
    /// stale. Publishing the result itself does not make it immediately stale.
    /// Nodes outside a refreshed projection retain older property values;
    /// readers must match the property's `_publication_id` to this status.
    pub fn graph_analytics_publication_status(
        &self,
        projection: &str,
        property: &str,
    ) -> Result<GraphAnalyticsPublicationStatus> {
        let snapshot = self.begin_read_transaction()?;
        let current = snapshot.commit_epoch();
        let mut status = GraphAnalyticsPublicationStatus {
            projection: projection.into(),
            property: property.into(),
            publication_id: None,
            computed_at_commit_epoch: None,
            published_at_commit_epoch: None,
            current_commit_epoch: current,
            freshness: GraphAnalyticsFreshness::Unavailable,
        };
        let Some(schema) = snapshot.store.relational_state().table_schema(PUBLICATIONS) else {
            return Ok(status);
        };
        if schema != &publication_schema() {
            return Err(HawDBError::StorageIntegrity(
                "analytics publication schema does not match its contract".into(),
            ));
        }
        let output = snapshot.query_sql_with_params_bounded(&format!("SELECT publication_id, computed_at_commit_epoch, published_at_commit_epoch FROM {PUBLICATIONS} WHERE projection = $1 AND property = $2"), &[
            Value::String(projection.into()), Value::String(property.into()),
        ], Some(1))?;
        if let Some(row) = output.rows.first() {
            let (Some(Value::String(id)), Some(Value::Int(source)), Some(Value::Int(published))) = (
                row.get("publication_id"),
                row.get("computed_at_commit_epoch"),
                row.get("published_at_commit_epoch"),
            ) else {
                return Err(HawDBError::StorageIntegrity(
                    "analytics publication has invalid provenance".into(),
                ));
            };
            if *source < 0 || *published <= *source || *published as u64 > current {
                return Err(HawDBError::StorageIntegrity(
                    "analytics publication has inconsistent epochs".into(),
                ));
            }
            status.publication_id = Some(id.clone());
            status.computed_at_commit_epoch = Some(*source as u64);
            status.published_at_commit_epoch = Some(*published as u64);
            status.freshness = if *published as u64 == current {
                GraphAnalyticsFreshness::Fresh
            } else {
                GraphAnalyticsFreshness::Stale
            };
        }
        Ok(status)
    }
}

fn stage_analytics_row(
    rows: &mut BTreeMap<i64, (i64, Value)>,
    node: i64,
    level: i64,
    value: &Value,
    max_staged_bytes: usize,
) -> Result<()> {
    // The charge covers the scalar, hierarchy level and BTreeMap overhead.
    // Publication iterates this map directly, avoiding a second retained copy.
    let bytes = rows
        .len()
        .saturating_add(1)
        .saturating_mul(STAGED_ROW_BYTES)
        .saturating_add(STAGED_HEADER_BYTES);
    match rows.entry(node) {
        std::collections::btree_map::Entry::Vacant(entry) => {
            if bytes > max_staged_bytes {
                return Err(HawDBError::Execution(
                    "complete analytics result exceeds max_staged_bytes".into(),
                ));
            }
            entry.insert((level, value.clone()));
        }
        std::collections::btree_map::Entry::Occupied(mut entry) => {
            let (previous_level, previous_value) = entry.get();
            if level > *previous_level {
                entry.insert((level, value.clone()));
            } else if level == *previous_level && value != previous_value {
                return Err(HawDBError::StorageIntegrity(
                    "analytics returned conflicting values for one node and level".into(),
                ));
            }
        }
    }
    Ok(())
}

fn validate_request(request: &GraphAnalyticsRequest) -> Result<()> {
    if request.projection.is_empty()
        || request.projection.len() > 256
        || request.max_staged_bytes.get() < STAGED_HEADER_BYTES
    {
        return Err(HawDBError::Execution(
            "invalid analytics projection or staging budget".into(),
        ));
    }
    match request.algorithm {
        GraphAnalyticsAlgorithm::PageRank(options)
            if !options.damping.is_finite() || !(0.0..1.0).contains(&options.damping) =>
        {
            Err(HawDBError::Execution(
                "analytics damping must be finite and in [0, 1)".into(),
            ))
        }
        GraphAnalyticsAlgorithm::PageRankProcedure(options)
            if !options.damping.is_finite() || !(0.0..1.0).contains(&options.damping) =>
        {
            Err(HawDBError::Execution(
                "analytics damping must be finite and in [0, 1)".into(),
            ))
        }
        GraphAnalyticsAlgorithm::PageRankProcedure(options)
            if !options.tolerance.is_finite() || options.tolerance < 0.0 =>
        {
            Err(HawDBError::Execution(
                "analytics tolerance must be finite and non-negative".into(),
            ))
        }
        GraphAnalyticsAlgorithm::LouvainProcedure(options)
            if !options.resolution.is_finite() || options.resolution <= 0.0 =>
        {
            Err(HawDBError::Execution(
                "analytics resolution must be finite and greater than 0".into(),
            ))
        }
        _ => Ok(()),
    }
}

fn validate_identifier(identifier: &str) -> Result<()> {
    if identifier.is_empty()
        || identifier.len() > 128
        || !identifier.bytes().enumerate().all(|(index, byte)| {
            byte == b'_' || byte.is_ascii_alphabetic() || index > 0 && byte.is_ascii_digit()
        })
    {
        return Err(HawDBError::Semantic(
            "analytics property must be an ASCII identifier of at most 128 bytes".into(),
        ));
    }
    Ok(())
}

fn algorithm_query(
    request: &GraphAnalyticsRequest,
) -> Result<(String, BTreeMap<String, Value>, &'static str)> {
    let graph = request
        .projection
        .replace('\\', "\\\\")
        .replace('\'', "\\'")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t");
    let integer = |value: usize| {
        i64::try_from(value).map(Value::Int).map_err(|_| {
            HawDBError::Execution("analytics iteration count exceeds Cypher integer".into())
        })
    };
    let algorithm = match request.algorithm {
        GraphAnalyticsAlgorithm::PageRank(options) => {
            GraphAnalyticsAlgorithm::PageRankProcedure(crate::PageRankProcedureOptions {
                iterations: options.iterations,
                damping: options.damping,
                ..crate::PageRankProcedureOptions::default()
            })
        }
        GraphAnalyticsAlgorithm::Louvain(options) => {
            GraphAnalyticsAlgorithm::LouvainProcedure(options.into())
        }
        procedure => procedure,
    };
    match algorithm {
        GraphAnalyticsAlgorithm::PageRank(_) | GraphAnalyticsAlgorithm::Louvain(_) => unreachable!("legacy options converted above"),
        GraphAnalyticsAlgorithm::PageRankProcedure(options) => Ok((
            format!("CALL page_rank('{graph}', maxIterations := $iterations, dampingFactor := $damping, tolerance := $tolerance, normalizeInitial := $normalize) RETURN node, pagerank_score"),
            BTreeMap::from([("iterations".into(), integer(options.iterations)?), ("damping".into(), Value::Float(options.damping)), ("tolerance".into(), Value::Float(options.tolerance)), ("normalize".into(), Value::Bool(options.normalize_initial))]), "pagerank_score",
        )),
        GraphAnalyticsAlgorithm::LouvainProcedure(options) => Ok((
            format!("CALL louvain('{graph}', maxIterations := $iterations, {} := $levels, resolution := $resolution) RETURN node, level, louvain_id", if options.hierarchy { "maxLevels" } else { "maxPhases" }),
            BTreeMap::from([("iterations".into(), integer(options.max_iterations)?), ("levels".into(), integer(options.max_levels)?), ("resolution".into(), Value::Float(options.resolution))]), "louvain_id",
        )),
    }
}

fn publication_schema() -> hawdb_storage::relational::RelationalTableSchema {
    use hawdb_storage::relational::{
        RelationalColumnSchema, RelationalScalarType, RelationalTableSchema,
    };
    RelationalTableSchema {
        materialized_view: None,
        name: PUBLICATIONS.into(),
        columns: [
            ("projection", RelationalScalarType::Text),
            ("property", RelationalScalarType::Text),
            ("publication_id", RelationalScalarType::Text),
            ("computed_at_commit_epoch", RelationalScalarType::BigInt),
            ("published_at_commit_epoch", RelationalScalarType::BigInt),
            ("algorithm", RelationalScalarType::Text),
            ("definition", RelationalScalarType::Text),
            ("row_count", RelationalScalarType::BigInt),
        ]
        .into_iter()
        .map(|(name, scalar_type)| RelationalColumnSchema {
            name: name.into(),
            scalar_type,
            nullable: false,
            default: None,
        })
        .collect(),
        primary_key: vec!["projection".into(), "property".into()],
        unique_constraints: vec![],
        foreign_keys: vec![],
        indexes: vec![],
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn staging_selects_the_highest_level_independently_of_row_order() {
        let inputs = [(1, 0, 1), (2, 0, 2), (1, 2, 3), (2, 2, 3), (1, 1, 2)];
        let budget = STAGED_HEADER_BYTES + 2 * STAGED_ROW_BYTES;
        let mut forward = BTreeMap::new();
        let mut reverse = BTreeMap::new();
        for (node, level, value) in inputs {
            stage_analytics_row(&mut forward, node, level, &Value::Int(value), budget).unwrap();
        }
        for (node, level, value) in inputs.into_iter().rev() {
            stage_analytics_row(&mut reverse, node, level, &Value::Int(value), budget).unwrap();
        }
        let expected = BTreeMap::from([(1, (2, Value::Int(3))), (2, (2, Value::Int(3)))]);
        assert_eq!(forward, expected);
        assert_eq!(reverse, expected);
        assert!(
            stage_analytics_row(&mut forward, 3, 0, &Value::Int(3), budget)
                .unwrap_err()
                .to_string()
                .contains("max_staged_bytes")
        );
        assert!(
            stage_analytics_row(&mut forward, 1, 2, &Value::Int(4), budget)
                .unwrap_err()
                .to_string()
                .contains("conflicting values")
        );
        assert_eq!(forward, expected);
    }
}