hawdb 0.17.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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
// 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 crate::analytics::ProjectedGraph;
use crate::cypher;
use crate::error::{HawDBError, Result};
use crate::executor::{self, Row, RowRef};
use crate::optimizer::{
    CascadesOptimizer, OptimizerConfig, OptimizerContext, OptimizerSearchDirective, OptimizerTrace,
    PhysicalPlan, ResourceHints,
};
use crate::qos::{
    BackgroundWorkHint, BackgroundWorkPlan, LocalQosPolicy, LocalQosScheduler, LocalQosState,
    QosAdmission, WorkClass, WorkRequest,
};
#[cfg(test)]
use crate::schema::GraphStatistics;
use crate::schema::{Catalog, SchemaObjectState};
#[cfg(test)]
use crate::schema::{
    CompositeIndexDescriptor, ConstraintDescriptor, IndexDescriptor, PropertyDescriptor,
    TableDescriptor,
};
use crate::search::{
    projection_row_from_node_with_graph_metadata, search_metadata_predicate_pushdown,
    AdaptiveVectorSearchOptions, CompressedVectorSearchMode, MetadataRepairOptions,
    MetadataRepairSummary, SearchCandidateSetReport, SearchDerivedArtifactReport,
    SearchEmptyReasonCode, SearchFusionWeights, SearchIndex, SearchMatchedSpan,
    SearchPredicatePushdownReport, SearchProjectionDelta, SearchProjectionDeltaReport,
    SearchProjectionFreshness, SearchQueryOptions, SearchRebuildOptions, SearchRebuildSummary,
    SearchResultSet, SearchRetrieverCandidateSetReport,
};
use crate::store::{
    restore_storage_backup, AdjacencyConsistencyReport, AdjacencyConsolidationPlan,
    AdjacencyConsolidationReport, AdjacencyDirection, AdjacencyLayout, AppendSegmentReadOutput,
    AppendTableSchema, AppendTransaction, BasicStatisticsConsistencyReport,
    DegreeStatisticsConsistencyReport, DistinctValueStatisticsConsistencyReport, DurabilityPolicy,
    GraphMutationLockFootprint, GraphMutationSavepoint, GraphMutationTransaction,
    GraphSnapshotNodeImport, GraphSnapshotRelationshipImport, GraphStore, HawDBSnapshotRowsImport,
    KernelWriteBatch, MutationSummary, NodeId, NodeRecord, OptimizerStatisticsRefreshWork,
    PreparedCheckpoint, PropertyIndexConsistencyReport, PropertyIndexProjectionRebuildAction,
    PublishedReadView, RecoveryMode, RelId, RelRecord, SchemaMaintenanceAction,
    SegmentCacheSnapshot, StorageBackupReport, StoragePressureSnapshot,
    StorageReclamationWatermark, StorageRecoveryReport, StorageRestoreReport, StorageScrubReport,
    StoreStableIdMapping, WalReplayConfig,
};
use crate::telemetry::{
    operations_telemetry_readiness, qos_telemetry_sink, runtime_telemetry_sink, KernelTelemetry,
    KernelTelemetryOperation, OperationsTelemetryReadiness, TelemetrySink,
};
use crate::value::Value;
use canonical_snapshot::export_canonical_graph_snapshot_for;
use explain::{empty_read_execution_profile, explain_analyze_output_row, explain_output_row};
use hawdb_optimizer::{
    search_field_is_enum_like, SearchPredicate, SearchPredicateOp, SearchPredicateSet,
};
use plan_cache::{
    optimized_query_plan_for, statement_uses_plan_cache, OptimizedQueryPlan,
    OptimizerEnvironmentKey, OptimizerPlanningCache, PlanCache, PlanCacheContext, PlanCacheMode,
    PlanTraceMode, DEFAULT_PLAN_CACHE_MAX_ENTRIES,
};
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard};

use system_variables::{
    apply_set_system_variable, query_statement_variables_for_statement,
    query_work_request_for_statement, reject_system_variable_parameters,
};

mod access_control;
mod database_lifecycle;
mod database_maintenance;
mod database_projection;
mod database_query;
mod database_transactions;
mod retrieval;
mod transactions;
use retrieval::*;
use transactions::*;
pub(crate) use transactions::{
    commit_database_transaction_state, execute_concurrent_graph_transaction_request,
    execute_database_transaction_sql_request,
};
mod graph_adapter;
mod read;

mod artifact_jobs;
mod automatic_checkpoint;
pub use automatic_checkpoint::AutomaticCheckpointReport;
mod branch_lifecycle;
mod canonical_snapshot;
mod concurrent;
#[cfg(test)]
pub(crate) use concurrent::AutocommitMutationPhase;
pub(crate) use concurrent::{
    ExclusiveDatabaseLease, PublishedConcurrentRead, SharedDatabaseGuard, SharedDatabaseOwner,
};
mod vector_index;
pub use vector_index::{
    VectorIndexBuildError, VectorIndexBuildOptions, VectorIndexField, VectorIndexLanguageBudget,
    VectorIndexLanguageReport, VectorIndexReadError, VectorIndexReadTransaction,
    VectorIndexSqlReport, VectorIndexSqlRequest,
};
mod transaction_read_dependencies;
pub use transaction_read_dependencies::{
    TransactionIsolation, TransactionReadDependencyLimits, TransactionReadDependencyUsage,
};
mod dependency_read;
pub use dependency_read::{
    PreparedProjectionFeed, PreparedProjectionFullSource, PreparedRead, PreparedReadDependencies,
    PreparedReadLimits, ProjectionBaselineComparisonConfig, ProjectionBaselineComparisonSession,
    ProjectionBaselineComparisonStep, ProjectionBaselineMappingContext,
    ProjectionBaselineMappingFunction, ProjectionBaselineMappingLimits, ProjectionCoverageConfig,
    ProjectionCoverageSession, ProjectionCoverageStep, ProjectionCoveredUpdate,
    ProjectionFeedError, ProjectionFeedItem, ProjectionFeedLimits, ProjectionFeedRead,
    ProjectionFeedResource, ProjectionFeedStep, ProjectionFeedUsage, ProjectionFullRebuildConfig,
    ProjectionFullRebuildSession, ProjectionFullRebuildStep, ProjectionFullSourceItem,
    ProjectionFullSourceLimits, ProjectionMappingContext, ProjectionMappingFunction,
    ProjectionMappingInput, ProjectionMappingLimits, ProjectionMappingStatus,
    ProjectionMappingUsage, ProjectionMatchedBaseline, ProjectionOutputVerificationContext,
    ProjectionOutputVerificationFunction, ProjectionOutputVerificationSession,
    ProjectionPreparedFullRebuild, ProjectionPublicationError, ProjectionSourceDurability,
    ProjectionSourceScope, ProjectionStagedInputs, ProjectionStagingSession,
    ProjectionStagingSessionConfig, ProjectionStagingSessionStep, ReadDependencyValidationFailures,
};
mod explain;
mod graph_analytics;
mod knowledge_graph_filters;
mod knowledge_graph_read;
pub use graph_analytics::{
    GraphAnalyticsAlgorithm, GraphAnalyticsFreshness, GraphAnalyticsPublicationStatus,
    GraphAnalyticsRequest, PreparedGraphAnalytics,
};
#[cfg(test)]
mod explain_format_tests;
mod observability;
mod plan_cache;
pub(crate) mod publication;
pub use publication::{DatabasePublicationOwner, DatabasePublicationState};
#[cfg(feature = "qualification")]
pub use publication::{PublicationLockDurationSnapshot, PublicationLockMetricsSnapshot};
#[cfg(feature = "qualification")]
mod query_preparation_metrics;
mod query_request;
mod query_runtime;
mod read_execution;
#[cfg(feature = "qualification")]
pub use query_preparation_metrics::{query_preparation_metrics, QueryPreparationMetricsSnapshot};
mod sql_runtime;
#[cfg(feature = "tokio-runtime")]
pub(crate) use sql_runtime::PreparedRuntimeSql;
mod retained_query;
pub use retained_query::{
    ArrowArray, ArrowArrayStream, ArrowSchema, RetainedArrowCodeOwner, RetainedArrowExport,
    RetainedArrowSchema, RetainedArrowStream, RetainedColumnRole, RetainedColumnSchema,
    RetainedColumnType, RetainedColumnValues, RetainedNodeCursor, RetainedNodeField,
    RetainedNodeLabels, RetainedNodeSnapshotCursor, RetainedNodeTaskCursor, RetainedNodeView,
    RetainedQueryBatch, RetainedQueryCursor, RetainedQueryError, RetainedQueryOptions,
    RetainedQueryProfile, RetainedQueryStatus,
};
mod resource_profile;
mod runtime_cell;
mod schema_guidance;
mod search_projection_catch_up;
mod search_projection_consumer;
mod statement_shape;
pub use search_projection_consumer::*;
mod source_candidates;
mod system_schema;
mod system_variables;
mod transaction_locks;
mod types;

pub(crate) use hawdb_system_sql as system_sql;

pub use branch_lifecycle::{
    BranchAbandonmentAcknowledgement, BranchAbandonmentLimits, BranchAbandonmentPlan,
    BranchAbandonmentReport, BranchCreateRequest, BranchInfo, BranchLifecycleError,
    BranchLifecycleState, BranchReclamationLimits, BranchReclamationReport, BranchSelector,
};
pub use hawdb_core::graph_rag::{
    MissingScoringFeature, ScoringCombination, ScoringProgram, ScoringProgramError,
    ScoringProgramShape,
};
pub(crate) use hawdb_executor::runtime_admission::runtime_planning_request;
#[cfg(feature = "tokio-runtime")]
pub(crate) use hawdb_executor::runtime_admission::RuntimeAdmissionPlan;
pub use hawdb_storage::branch_create_recovery::{
    BranchCreateRecoveryEntry, BranchCreateRecoveryLimits, BranchCreateRecoveryReport,
    BranchCreateRecoveryStatus,
};
use query_request::BoundScoringRequest;
pub use query_request::{
    BudgetedReadSnapshot, HostScoringRequest, OwnedHostScoringRequest, OwnedQueryRequest,
    OwnedSqlQueryRequest, QueryRequest, ReadSnapshotBudget, ReadSnapshotUsage, ScoringRequest,
    SqlQueryRequest,
};
#[cfg(feature = "tokio-runtime")]
pub(crate) use query_runtime::RuntimePlanningSnapshot;
pub(crate) use query_runtime::{runtime_result_memory_budget, PreparedRuntimeQuery};
pub use types::*;

const DEFAULT_SEARCH_PROJECTION_CHANGE_LOG_MAX_ENTRIES: usize = 4096;
const DEFAULT_SEARCH_PROJECTION_CHANGE_LOG_MAX_BYTES: usize = 64 * 1024 * 1024;

pub use artifact_jobs::{
    DerivedArtifactJob, DerivedArtifactJobReport, DerivedArtifactJobStatus,
    ExternalContentArtifactJobCompletion, ExternalContentArtifactJobSummary,
    ExternalContentArtifactRuntimeManifest,
};
pub use canonical_snapshot::{
    hawdb_lightning_initial_import_advance_checkpoint,
    hawdb_lightning_initial_import_advance_durable_state_streaming,
    hawdb_lightning_initial_import_advance_durable_state_with_search_projection_batch,
    hawdb_lightning_initial_import_checkpoint_readiness,
    hawdb_lightning_initial_import_cutover_catch_up_report,
    hawdb_lightning_initial_import_decode_durable_state,
    hawdb_lightning_initial_import_document_identity_coverage,
    hawdb_lightning_initial_import_durable_state_report,
    hawdb_lightning_initial_import_encode_durable_state, hawdb_lightning_initial_import_plan,
    hawdb_lightning_initial_import_plan_with_document_identities,
    hawdb_lightning_initial_import_readiness, hawdb_lightning_initial_import_recovery_readiness,
    hawdb_lightning_initial_import_resume_action,
    hawdb_lightning_initial_import_search_projection_batch_report,
    hawdb_lightning_initial_import_search_projection_batch_report_with_document_identities,
    hawdb_lightning_initial_import_session_bundle_readiness,
    hawdb_lightning_initial_import_session_report,
    hawdb_lightning_initial_import_source_bundle_readiness,
    hawdb_lightning_initial_import_source_fingerprint,
    hawdb_lightning_initial_import_startup_readiness, parse_hawdb_lightning_graph_stream_export,
    validate_hawdb_lightning_graph_stream, validate_hawdb_lightning_relational_stream,
    CanonicalGraphSnapshotExport, CanonicalGraphSnapshotValidation,
    CanonicalSnapshotEndpointViolation, CanonicalSnapshotIdentityAudit, CanonicalSnapshotNode,
    CanonicalSnapshotRelationship, CanonicalStableIdMapping, HawDBLightningBootstrapExport,
    HawDBLightningBootstrapManifest, HawDBLightningGraphStream,
    HawDBLightningGraphStreamValidation, HawDBLightningInitialImportApplyReport,
    HawDBLightningInitialImportCheckpoint, HawDBLightningInitialImportCheckpointProgress,
    HawDBLightningInitialImportCheckpointProgressReport,
    HawDBLightningInitialImportCheckpointReadiness,
    HawDBLightningInitialImportCutoverCatchUpReport, HawDBLightningInitialImportDocumentIdentity,
    HawDBLightningInitialImportDocumentIdentityCoverage,
    HawDBLightningInitialImportDocumentIdentityKindReport,
    HawDBLightningInitialImportDurableBatchAdvanceReport, HawDBLightningInitialImportDurableState,
    HawDBLightningInitialImportDurableStateCodecReport,
    HawDBLightningInitialImportDurableStateReport, HawDBLightningInitialImportIdempotencyKey,
    HawDBLightningInitialImportPlan, HawDBLightningInitialImportReadiness,
    HawDBLightningInitialImportReadinessInputs, HawDBLightningInitialImportRecoveryReadinessReport,
    HawDBLightningInitialImportResumeAction, HawDBLightningInitialImportResumeActionKind,
    HawDBLightningInitialImportSearchProjectionBatchReport,
    HawDBLightningInitialImportSessionBundleReadiness, HawDBLightningInitialImportSessionReport,
    HawDBLightningInitialImportSourceBundleReadiness, HawDBLightningInitialImportSourceFingerprint,
    HawDBLightningInitialImportStartupReadinessReport,
    HawDBLightningInitialImportStreamingBatchAdvanceReport, HawDBLightningRelationalStream,
    HawDBLightningRelationalStreamValidation, HAWDB_LIGHTNING_BOOTSTRAP_PROTOCOL_VERSION,
    HAWDB_LIGHTNING_GRAPH_STREAM_FORMAT_VERSION,
    HAWDB_LIGHTNING_INITIAL_IMPORT_DURABLE_STATE_PROTOCOL,
    HAWDB_LIGHTNING_RELATIONAL_STREAM_FORMAT_VERSION,
};
pub use concurrent::{
    ConcurrentDatabase, ConcurrentDatabaseMetrics, ConcurrentDatabaseTransaction,
    ConcurrentPhaseMetrics, ConcurrentTransactionMode, ConcurrentTransactionOptions,
    WalGroupCommitActivation, WalGroupCommitAdaptiveColdStartEvidence,
    WalGroupCommitAdaptivePolicyEvidence, WalGroupCommitAdaptiveSteadyStateEvidence,
    WalGroupCommitConfig, WalGroupCommitDelayPolicy, WalGroupCommitEvidence,
    WalGroupCommitSnapshot, WalGroupCommitTailLatencyEvidence, WalGroupCommitWaitDecision,
    DEFAULT_PESSIMISTIC_LOCK_TIMEOUT, DEFAULT_WAL_GROUP_COMMIT_MAX_BYTES,
    DEFAULT_WAL_GROUP_COMMIT_MAX_DELAY, DEFAULT_WAL_GROUP_COMMIT_MAX_ENTRIES,
};
pub use hawdb_core::QueryAccessControlContext;
pub use hawdb_evidence::AccessControlPolicyReadiness;
pub use hawdb_executor::{BoundedReadQueryOutput, QueryStreamOptions, QueryStreamReport};
pub use hawdb_explain::{ExplainAnalyzeOutput, ExplainOutput, NowledgeGraphExplainOutput};
pub use hawdb_system_sql::{
    SlowQueryLogExportOptions, SlowQueryLogRecordSummary, SLOW_QUERY_LOG_EVENT_PROTOCOL,
};
pub use plan_cache::{PlanCacheBypassReason, PlanCacheLookup, PlanCacheStats};
pub use resource_profile::{
    StorageResourceProfileLimits, StorageResourceProfileReport, STORAGE_RESOURCE_PROFILE_PROTOCOL,
};
pub use search_projection_catch_up::{
    ScheduledSearchProjectionCatchUpReport, SearchProjectionCatchUpReport,
    SearchProjectionCatchUpStopReason,
};
pub use source_candidates::{
    KnowledgeSourceCandidateRow, KnowledgeSourceCandidateScanOrigin,
    KnowledgeSourceCandidateScanOutput, KnowledgeSourceCandidateScanRequest,
};
pub use system_schema::{SystemSchemaMigration, SystemSchemaRegistry, SystemSchemaUpgradeReport};
pub use system_variables::QuerySystemVariables;

fn hawdb_lightning_initial_import_source_fingerprint_key(
    manifest: &HawDBLightningBootstrapManifest,
) -> String {
    let fingerprint = hawdb_lightning_initial_import_source_fingerprint(manifest);
    format!(
        "v{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}:{}",
        fingerprint.protocol_version,
        fingerprint.database_commit_epoch,
        fingerprint.graph_commit_epoch,
        fingerprint.logical_checksum,
        fingerprint.graph_stream_checksum,
        fingerprint.graph_stream_byte_len,
        fingerprint.relational_stream_checksum,
        fingerprint.relational_stream_byte_len,
        fingerprint.schema_checksum,
        fingerprint.node_count,
        fingerprint.relationship_count,
        fingerprint.relational_table_count,
        fingerprint.relational_row_count,
    )
}

/// Equality-only identity of one admitted database runtime in this process.
///
/// Clones retain only a marker, not database resources. Reopening or switching
/// to another branch creates a different identity, even at equal contents/epochs.
/// This token is not a persistent identity or evidence of a retained fence.
#[derive(Debug, Clone)]
pub struct DatabaseInstanceIdentity(Arc<()>);

impl DatabaseInstanceIdentity {
    fn new() -> Self {
        Self(Arc::new(()))
    }
}

impl PartialEq for DatabaseInstanceIdentity {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

impl Eq for DatabaseInstanceIdentity {}

#[derive(Debug)]
pub struct Database {
    runtime: runtime_cell::BranchRuntimeCell,
    project_root_path: Option<PathBuf>,
    branch_create_recovery: Option<BranchCreateRecoveryReport>,
    durability: DurabilityPolicy,
    slow_query_log: Arc<SharedState<system_sql::SlowQueryLog>>,
    statement_summary: Arc<SharedState<system_sql::StatementSummary>>,
    config: DatabaseConfig,
    local_qos_scheduler: LocalQosScheduler,
    system_variables: QuerySystemVariables,
    derived_artifact_jobs: hawdb_artifact::DerivedArtifactJobQueue,
    derived_artifact_root_pins:
        BTreeMap<u64, Option<Arc<hawdb_storage::branch_root_pins::RootPin>>>,
    telemetry: Option<Arc<dyn TelemetrySink>>,
    runtime_governor: Option<hawdb_qos::RuntimeGovernor>,
    retained_runtime: Arc<retained_query::RetainedRuntime>,
}

pub(crate) struct DatabaseCheckpointSource {
    catalog: Catalog,
    store: GraphStore,
    suspension: Option<automatic_checkpoint::Suspension>,
}

pub(crate) struct PreparedDatabaseCheckpoint {
    checkpoint: PreparedCheckpoint,
    _suspension: Option<automatic_checkpoint::Suspension>,
}

impl DatabaseCheckpointSource {
    pub(crate) fn prepare(self) -> Result<Option<PreparedDatabaseCheckpoint>> {
        Ok(self
            .store
            .prepare_checkpoint(&self.catalog)?
            .map(|checkpoint| PreparedDatabaseCheckpoint {
                checkpoint,
                _suspension: self.suspension,
            }))
    }
}

#[derive(Debug)]
pub(super) struct SharedState<T>(Mutex<T>);

impl<T> SharedState<T> {
    fn new(value: T) -> Self {
        Self(Mutex::new(value))
    }

    pub(super) fn borrow(&self) -> MutexGuard<'_, T> {
        self.0
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }

    pub(super) fn borrow_mut(&self) -> MutexGuard<'_, T> {
        self.borrow()
    }
}

#[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.
    /// 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.
    pub max_read_result_rows: Option<usize>,
    pub max_read_result_payload_bytes: Option<usize>,
    pub execution_memory: executor::ExecutionMemoryConfig,
    pub mutation_limits: hawdb_storage::mutation::MutationLimits,
    /// 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,
    pub max_wal_record_bytes: Option<usize>,
    pub max_wal_batch_operations: Option<usize>,
    pub max_checkpoint_encoded_bytes: Option<u64>,
    pub max_checkpoint_decoded_bytes: Option<u64>,
    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,
    pub storage_residency_mode: hawdb_storage::config::StorageResidencyMode,
    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,
    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 or relational SQL-template cache.
    pub max_plan_cache_entries: Option<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;
pub const DEFAULT_MAX_READ_RESULT_PAYLOAD_BYTES: usize = 64 * 1024 * 1024;

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,
    }
}

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);
    let max_intermediate_rows = config.read_result_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);
    let max_scan_pages = max_intermediate_rows
        .div_ceil(hawdb_storage::relational::DEFAULT_RELATIONAL_ROW_PAGE_ROWS)
        .max(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()
            },
            ..hawdb_storage::relational::RelationalRowPageSnapshotReadLimits::default()
        },
    }
}

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),
    }
}

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,
    )
}

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,
    ))
}

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)
        }
    }
}

/// 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 {
    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)
    }

    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_result_payload_bytes: Some(DEFAULT_MAX_READ_RESULT_PAYLOAD_BYTES),
            execution_memory: executor::ExecutionMemoryConfig::default(),
            mutation_limits: hawdb_storage::mutation::MutationLimits::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,
            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),
            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(crate) fn system_runtime_snapshot(
    config: &DatabaseConfig,
) -> system_sql::SystemRuntimeSnapshot {
    system_sql::SystemRuntimeSnapshot::new(
        config.read_only,
        config.read_result_row_limit(),
        config.max_read_result_payload_bytes,
        config.storage_residency_mode,
        config.relational_index_mode,
        config.runtime_capabilities,
    )
}

pub use hawdb_executor::QueryOutput;

/// Deterministic outcome for one relational INSERT, UPDATE, or DELETE statement.
///
/// A staged statement is provisional until its enclosing transaction commits.
/// Conflict no-ops increment `conflict_rows`, never `affected_rows`, and never
/// contribute a `RETURNING` row. In the v1 contract, `rows` contains values for
/// supported `INSERT ... RETURNING` statements and remains empty for count-only
/// UPDATE and DELETE outcomes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RelationalMutationResult {
    pub affected_rows: usize,
    pub conflict_rows: usize,
    pub rows: executor::QueryRows,
    pub provisional: bool,
}

/// Result of one SQL statement executed inside a database transaction.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SqlStatementResult {
    pub output: QueryOutput,
    pub mutation: Option<RelationalMutationResult>,
}

/// Confirmed results produced by the durable transaction commit.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransactionCommitResult {
    pub output: QueryOutput,
    pub mutations: Vec<RelationalMutationResult>,
    pub append_mutations: Vec<hawdb_storage::append_table::AppendMutationOutcome>,
}

/// Confirmed generated order-key assignments from one durable append commit.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AppendCommitResult {
    pub commit_epoch: u64,
    pub mutations: Vec<hawdb_storage::append_table::AppendMutationOutcome>,
}

/// Declares the relational tables exposed by one owner-scoped projection
/// generation inside a pinned read transaction.
///
/// The binding is generic rather than route-specific. The application owns the
/// PostgreSQL DDL for the named tables and the projection version. HawDB owns
/// active-generation resolution, pin lifetime, row decoding, and query
/// admission.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectionRelationalReadBinding {
    projection: String,
    owner_key: Vec<u8>,
    projection_version: u64,
    tables: BTreeSet<String>,
}

impl ProjectionRelationalReadBinding {
    pub fn new(
        projection: impl Into<String>,
        owner_key: impl Into<Vec<u8>>,
        projection_version: u64,
        tables: impl IntoIterator<Item = impl Into<String>>,
    ) -> Result<Self> {
        let projection = projection.into();
        let owner_key = owner_key.into();
        let tables = tables.into_iter().map(Into::into).collect::<BTreeSet<_>>();
        if projection.trim().is_empty() {
            return Err(HawDBError::Semantic(
                "projection relational binding requires a non-empty projection name".to_string(),
            ));
        }
        if owner_key.is_empty() {
            return Err(HawDBError::Semantic(
                "projection relational binding requires a non-empty owner key".to_string(),
            ));
        }
        if projection_version == 0 {
            return Err(HawDBError::Semantic(
                "projection relational binding requires a non-zero projection version".to_string(),
            ));
        }
        if tables.is_empty() || tables.iter().any(|table| table.trim().is_empty()) {
            return Err(HawDBError::Semantic(
                "projection relational binding requires non-empty table names".to_string(),
            ));
        }
        Ok(Self {
            projection,
            owner_key,
            projection_version,
            tables,
        })
    }

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

    pub fn owner_key(&self) -> &[u8] {
        &self.owner_key
    }

    pub const fn projection_version(&self) -> u64 {
        self.projection_version
    }

    pub fn tables(&self) -> &BTreeSet<String> {
        &self.tables
    }
}

#[derive(Debug, Clone, Copy, Default)]
pub(super) struct StatementExecutionContext<'a> {
    execution_profile: Option<&'a executor::ReadExecutionProfile>,
    retained_profile: Option<&'a RetainedQueryProfile>,
    access_control: Option<&'a QueryAccessControlContext>,
    parse_nanos: u64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NowledgeGraphStatement {
    pub cypher: String,
    pub parameters: BTreeMap<String, Value>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NowledgeGraphTransactionOutput {
    pub statement_outputs: Vec<QueryOutput>,
    pub commit_output: QueryOutput,
}

#[derive(Debug)]
pub struct DatabaseTransaction<'a> {
    db: &'a mut Database,
    runtime: DatabaseTransactionRuntime,
    state: DatabaseTransactionState,
    task_context: Option<hawdb_core::RuntimeTaskContext>,
}

#[derive(Debug)]
pub(super) struct DatabaseTransactionRuntime {
    instance_identity: DatabaseInstanceIdentity,
    session_config: SessionConfig,
    optimizer: CascadesOptimizer,
    plan_cache: SharedState<PlanCache>,
    relational_plan_template_cache: Arc<crate::relational_sql::RelationalPlanTemplateCache>,
    optimizer_planning_cache: SharedState<OptimizerPlanningCache>,
    config: DatabaseConfig,
    system_variables: QuerySystemVariables,
    branch_catalog_path: Option<PathBuf>,
    current_branch: Option<branch_lifecycle::CurrentBranch>,
}

#[derive(Debug)]
pub(super) struct DatabaseTransactionState {
    read_dependencies: Option<transaction_read_dependencies::TransactionReadDependencies>,
    graph_transaction: Option<GraphMutationTransaction>,
    relational_transaction: hawdb_storage::relational::RelationalTransaction,
    relational_state: hawdb_storage::relational::RelationalState,
    append_transaction: hawdb_storage::append_table::AppendTransaction,
    append_state: hawdb_storage::append_table::AppendState,
    pending_generated_append_tables: BTreeSet<String>,
    relational_returning: Vec<Option<crate::relational_sql::RelationalReturningProjection>>,
    relational_index:
        std::result::Result<Option<crate::store::RelationalTransactionIndexView>, String>,
    relational_rows:
        std::result::Result<Option<crate::store::RelationalTransactionRowView>, String>,
    // Moves with the private workspace into a queued commit. Checkpoint
    // reclamation must retain its physical generation until the state retires.
    snapshot_pin: Option<ReaderPin>,
}

struct SparseRelationalStatementStage {
    state: hawdb_storage::relational::RelationalState,
    mutation_outcomes: Vec<hawdb_storage::relational::RelationalMutationOutcome>,
    index_capture: hawdb_storage::relational::RelationalIndexChangeCapture,
    row_capture: hawdb_storage::relational::RelationalRowChangeCapture,
}

pub(crate) struct GraphTransactionStatementOutcome {
    pub(crate) output: QueryOutput,
    pub(crate) savepoint: Option<GraphMutationSavepoint>,
    pub(crate) lock_footprint: GraphMutationLockFootprint,
}

#[derive(Debug)]
pub struct DatabaseSession<'a> {
    config: SessionConfig,
    db: &'a mut Database,
    graph_transaction: Option<GraphMutationTransaction>,
    transaction_runtime: Option<DatabaseTransactionRuntime>,
    system_variables: QuerySystemVariables,
}

#[derive(Debug)]
enum ReadPlanCache {
    Private(SharedState<PlanCache>),
    Shared(Arc<SharedState<PlanCache>>),
}

impl std::ops::Deref for ReadPlanCache {
    type Target = SharedState<PlanCache>;

    fn deref(&self) -> &Self::Target {
        match self {
            Self::Private(cache) => cache,
            Self::Shared(cache) => cache,
        }
    }
}

#[derive(Debug)]
pub struct DatabaseReadTransaction<S: crate::executor::ExecutionStore = GraphStore> {
    session_config: SessionConfig,
    system_variables: QuerySystemVariables,
    catalog: Catalog,
    store: S,
    published_read_view: PublishedReadView,
    optimizer: CascadesOptimizer,
    plan_cache: ReadPlanCache,
    relational_plan_template_cache: Arc<crate::relational_sql::RelationalPlanTemplateCache>,
    optimizer_planning_cache: Arc<SharedState<OptimizerPlanningCache>>,
    slow_query_snapshot: Vec<system_sql::SlowQueryRecord>,
    statement_summary_snapshot: Vec<system_sql::StatementSummaryRecord>,
    config: DatabaseConfig,
    branch_catalog_path: Option<PathBuf>,
    current_branch: Option<branch_lifecycle::CurrentBranch>,
    projection_relational: Option<ProjectionRelationalReadSnapshot>,
    task_context: Option<hawdb_core::RuntimeTaskContext>,
    _pin: Arc<ReaderPin>,
    retained_runtime: Arc<retained_query::RetainedRuntime>,
}

/// An immutable canonical view that can create independent query contexts.
/// The shared pin survives publication replacement until the last query drops.
#[derive(Debug)]
pub(crate) struct DatabaseReadSnapshot(DatabaseReadTransaction);

impl DatabaseReadSnapshot {
    pub(crate) fn config(&self) -> &DatabaseConfig {
        &self.0.config
    }

    pub(crate) fn ensure_usable(&self) -> Result<()> {
        self.0.store.ensure_usable()
    }

    pub(crate) fn begin_read_transaction(
        &self,
        task_context: &hawdb_core::RuntimeTaskContext,
    ) -> Result<DatabaseReadTransaction> {
        self.fork(Some(task_context.clone()))
    }

    fn fork(
        &self,
        task_context: Option<hawdb_core::RuntimeTaskContext>,
    ) -> Result<DatabaseReadTransaction> {
        self.ensure_usable()?;
        let source = &self.0;
        Ok(DatabaseReadTransaction {
            session_config: source.session_config,
            system_variables: source.system_variables.clone(),
            catalog: source.catalog.clone(),
            store: source.store.snapshot_for_read(),
            published_read_view: source.published_read_view,
            optimizer: source.optimizer.clone(),
            plan_cache: ReadPlanCache::Private(SharedState::new(PlanCache::new(
                source.config.max_plan_cache_entries,
            ))),
            relational_plan_template_cache: Arc::clone(&source.relational_plan_template_cache),
            optimizer_planning_cache: Arc::clone(&source.optimizer_planning_cache),
            slow_query_snapshot: source.slow_query_snapshot.clone(),
            statement_summary_snapshot: source.statement_summary_snapshot.clone(),
            config: source.config.clone(),
            branch_catalog_path: source.branch_catalog_path.clone(),
            current_branch: source.current_branch.clone(),
            projection_relational: None,
            task_context,
            _pin: Arc::clone(&source._pin),
            retained_runtime: Arc::clone(&source.retained_runtime),
        })
    }
}

#[derive(Debug)]
struct ProjectionRelationalReadSnapshot {
    binding: ProjectionRelationalReadBinding,
    reader: hawdb_storage::projection_generation::ProjectionGenerationReader,
}

struct ReadStreamingExecutionContext<'a> {
    task_context: Option<&'a hawdb_core::RuntimeTaskContext>,
    external: Option<&'a mut dyn executor::ExternalReadOperator>,
    delivery: executor::StreamDelivery,
    scoring: Option<BoundScoringRequest<'a>>,
    access_control: Option<&'a QueryAccessControlContext>,
}

#[derive(Debug)]
pub struct NowledgeGraphAdapter<'a> {
    db: &'a mut Database,
}

#[derive(Debug, Default)]
struct ReaderPins {
    next_reader_id: u64,
    active_views: BTreeMap<u64, PublishedReadView>,
}

#[derive(Debug)]
struct ReaderPin {
    id: u64,
    pins: Arc<Mutex<ReaderPins>>,
}

fn configure_search_projection_changefeed(store: &mut GraphStore, config: &DatabaseConfig) {
    store.set_search_projection_primary_key_capture_limits(
        config.search_projection_relational_change_limits,
    );
    store.set_max_search_projection_change_log_entries(
        config.max_search_projection_change_log_entries,
    );
    store.set_max_search_projection_change_log_bytes(config.max_search_projection_change_log_bytes);
}

fn configure_relational_fast_paths(store: &mut GraphStore, config: &DatabaseConfig) {
    store.set_relational_monotonic_append_fast_path_enabled(
        config.relational_monotonic_append_fast_path,
    );
}

impl Default for Database {
    fn default() -> Self {
        let config = effective_database_config(DatabaseConfig::default());
        let local_qos_scheduler = LocalQosScheduler::new(config.local_qos_policy);
        let mut store = GraphStore::default();
        configure_search_projection_changefeed(&mut store, &config);
        configure_relational_fast_paths(&mut store, &config);
        Self {
            runtime: runtime_cell::BranchRuntimeCell::admitted(
                runtime_cell::AdmittedBranchRuntime {
                    catalog: Catalog::default(),
                    branch_selection: None,
                    projection_consumers: search_projection_consumer::ConsumerRegistry::load(
                        store.search_projection_registry_root(),
                        store.search_projection_database_identity(),
                    ),
                    store,
                    optimizer: optimizer_from_database_config(&config),
                    plan_cache: Arc::new(SharedState::new(PlanCache::new(
                        config.max_plan_cache_entries,
                    ))),
                    relational_plan_template_cache:
                        relational_plan_template_cache_from_database_config(&config),
                    optimizer_planning_cache: Arc::new(SharedState::new(
                        OptimizerPlanningCache::default(),
                    )),
                    reader_pins: Arc::new(Mutex::new(ReaderPins::default())),
                },
            ),
            project_root_path: None,

            durability: DurabilityPolicy::default(),

            slow_query_log: Arc::new(SharedState::new(system_sql::SlowQueryLog::new(
                config.slow_query_log_capacity,
            ))),
            statement_summary: Arc::new(SharedState::new(system_sql::StatementSummary::new(
                config.statement_summary_capacity,
            ))),
            config,
            local_qos_scheduler,
            system_variables: QuerySystemVariables::default(),

            derived_artifact_jobs: hawdb_artifact::DerivedArtifactJobQueue::default(),
            derived_artifact_root_pins: BTreeMap::new(),
            telemetry: None,
            runtime_governor: None,
            retained_runtime: Arc::new(retained_query::RetainedRuntime::default()),
            branch_create_recovery: None,
        }
    }
}

impl Database {
    fn ensure_writable(&self) -> Result<()> {
        self.runtime.get()?.store.ensure_usable()?;
        if self.config.read_only {
            return Err(HawDBError::Execution(
                "database is opened in read-only mode".to_string(),
            ));
        }
        Ok(())
    }

    pub fn project_graph(&self, rel_type: Option<&str>) -> Result<ProjectedGraph> {
        Ok({
            let runtime = self.runtime.get()?;
            match rel_type {
                Some(name) => runtime
                    .catalog
                    .rel_type_id(name)
                    .map(|rel_type_id| {
                        ProjectedGraph::from_store(&runtime.store, Some(rel_type_id))
                    })
                    .unwrap_or_else(|| ProjectedGraph::from_store_without_edges(&runtime.store)),
                None => ProjectedGraph::from_store(&runtime.store, None),
            }
        })
    }
}

fn effective_database_config(mut config: DatabaseConfig) -> DatabaseConfig {
    config.runtime_capabilities =
        crate::compiled_capabilities::effective_runtime_capabilities(config.runtime_capabilities);
    config
}

fn require_search_projection_source_epoch(expected: Option<u64>, actual: u64) -> Result<()> {
    if let Some(expected) = expected
        && expected != actual
    {
        if expected > actual {
            return Err(HawDBError::Storage(format!(
                "search projection graph delta complete-through epoch {expected} is ahead of graph commit epoch {actual}"
            )));
        }
        return Err(HawDBError::Execution(format!(
            "search projection complete-through epoch {expected} differs from source snapshot epoch {actual}; prepare the complete source interval before publishing its watermark"
        )));
    }
    Ok(())
}

fn search_projection_graph_delta_for(
    catalog: &Catalog,
    store: &GraphStore,
    request: &SearchProjectionGraphDeltaRequest,
) -> Result<SearchProjectionDelta> {
    let operation_count = request.operation_count();
    if let Some(limit) = request.max_operations
        && operation_count > limit
    {
        return Err(HawDBError::Storage(format!(
                "search projection graph delta operation count {operation_count} exceeded configured limit {limit}"
            )));
    }
    // A changefeed prefix describes IDs, not a historical read view.
    // Hydrating it from a newer head would publish future data under an old watermark.
    require_search_projection_source_epoch(
        request.complete_through_graph_commit_epoch,
        store.commit_epoch(),
    )?;

    let mut upserts = Vec::new();
    for node_id in &request.upsert_node_ids {
        let Some(node) = store.node_owned(NodeId(*node_id))? else {
            continue;
        };
        if let Some(row) = projection_row_from_node_with_graph_metadata(catalog, store, &node)? {
            upserts.push(row);
        }
    }

    Ok(SearchProjectionDelta {
        upserts,
        deletes: request.delete_document_ids.clone(),
        max_operations: request.max_operations,
        source_graph_commit_epoch: request.complete_through_graph_commit_epoch,
    })
}

fn search_projection_commit_lag(
    search_index: &SearchIndex,
    required_projection_commit_epoch: u64,
) -> u64 {
    search_projection_freshness_commit_lag(
        &search_index.projection_freshness(),
        required_projection_commit_epoch,
    )
}

fn knowledge_entity_from_node(catalog: &Catalog, node: &NodeRecord) -> KnowledgeEntity {
    KnowledgeEntity {
        node_id: node.id.0,
        labels: node_label_names(catalog, node),
        external_id: Some(projected_node_external_id(node)),
        properties: node.properties.clone(),
    }
}

impl ReaderPins {
    fn oldest_epoch(&self) -> Option<u64> {
        self.oldest_epoch_excluding(None)
    }

    fn oldest_epoch_excluding(&self, excluded: Option<u64>) -> Option<u64> {
        self.active_views
            .iter()
            .filter_map(|(&id, view)| (Some(id) != excluded).then_some(view.visible_commit_epoch()))
            .min()
    }

    fn pinned_physical_generations(&self) -> BTreeSet<u64> {
        self.active_views
            .values()
            .filter_map(|view| view.physical_generation())
            .map(|generation| generation.0)
            .collect()
    }
}

fn elapsed_micros(started: hawdb_core::time::Instant) -> u64 {
    started.elapsed().as_micros().min(u64::MAX as u128) as u64
}

impl ReaderPin {
    fn new(id: u64, pins: Arc<Mutex<ReaderPins>>) -> Self {
        Self { id, pins }
    }
}

impl Drop for ReaderPin {
    fn drop(&mut self) {
        self.pins
            .lock()
            .expect("database reader pins lock should not be poisoned")
            .active_views
            .remove(&self.id);
    }
}

fn search_kind_to_label(kind: &str) -> Option<&'static str> {
    match kind {
        "Memory" | "memory" => Some("Memory"),
        "Message" | "message" => Some("Message"),
        "Entity" | "entity" => Some("Entity"),
        "Source" | "source" => Some("Source"),
        "SourceChunk" | "source_chunk" | "sourcechunk" | "chunk" => Some("SourceChunk"),
        "Community" | "community" => Some("Community"),
        _ => None,
    }
}

fn search_label_to_kind(label: &str) -> Option<&'static str> {
    match label {
        "Memory" | "memory" => Some("memory"),
        "Message" | "message" => Some("message"),
        "Entity" | "entity" => Some("entity"),
        "Source" | "source" => Some("source"),
        "SourceChunk" | "source_chunk" | "sourcechunk" | "chunk" => Some("source_chunk"),
        "Community" | "community" => Some("community"),
        _ => None,
    }
}

fn node_label_names(catalog: &Catalog, node: &NodeRecord) -> Vec<String> {
    node.labels
        .iter()
        .filter_map(|label_id| catalog.label_name(*label_id))
        .map(str::to_string)
        .collect()
}

fn node_external_id(node: &NodeRecord) -> Option<String> {
    node.properties
        .get("id")
        .map(value_to_external_id)
        .filter(|external_id| !external_id.is_empty())
}

fn projected_node_external_id(node: &NodeRecord) -> String {
    node_external_id(node).unwrap_or_else(|| node.id.0.to_string())
}

fn value_to_external_id(value: &Value) -> String {
    match value {
        Value::Null => String::new(),
        Value::Bool(value) => value.to_string(),
        Value::Int(value) => value.to_string(),
        Value::Float(value) => value.to_string(),
        Value::String(value) => value.clone(),
        Value::Binary(value) => format!(
            "\\x{}",
            value
                .iter()
                .map(|byte| format!("{byte:02x}"))
                .collect::<String>()
        ),
        Value::Uuid(value) => value.to_string(),
        Value::Vector(_) => value.to_string(),
        Value::List(values) => values
            .iter()
            .map(value_to_external_id)
            .collect::<Vec<_>>()
            .join(","),
        Value::Map(values) => values
            .iter()
            .map(|(key, value)| format!("{key}:{}", value_to_external_id(value)))
            .collect::<Vec<_>>()
            .join(","),
    }
}

fn schema_state_value(state: SchemaObjectState) -> Value {
    let value = match state {
        SchemaObjectState::DeleteOnly => "delete_only",
        SchemaObjectState::WriteOnly => "write_only",
        SchemaObjectState::Backfill => "backfill",
        SchemaObjectState::Validating => "validating",
        SchemaObjectState::Public => "public",
        SchemaObjectState::Gc => "gc",
    };
    Value::String(value.to_string())
}

fn schema_maintenance_actions_output(actions: Vec<SchemaMaintenanceAction>) -> QueryOutput {
    let rows = actions
        .into_iter()
        .map(|action| {
            BTreeMap::from([
                ("object_type".to_string(), Value::String(action.object_type)),
                ("object".to_string(), Value::String(action.object)),
                (
                    "from_state".to_string(),
                    schema_state_value(action.from_state),
                ),
                (
                    "to_state".to_string(),
                    action
                        .to_state
                        .map(schema_state_value)
                        .unwrap_or(Value::Null),
                ),
                ("action".to_string(), Value::String(action.action)),
            ])
        })
        .collect();
    QueryOutput { rows }
}

fn property_index_projection_rebuild_output(
    actions: Vec<PropertyIndexProjectionRebuildAction>,
) -> QueryOutput {
    let rows = actions
        .into_iter()
        .map(|action| {
            BTreeMap::from([
                ("index_kind".to_string(), Value::String(action.index_kind)),
                ("label".to_string(), Value::String(action.label)),
                (
                    "properties".to_string(),
                    Value::List(action.properties.into_iter().map(Value::String).collect()),
                ),
                (
                    "estimated_operations".to_string(),
                    Value::Int(i64::try_from(action.estimated_operations).unwrap_or(i64::MAX)),
                ),
                (
                    "indexed_entries".to_string(),
                    Value::Int(i64::try_from(action.indexed_entries).unwrap_or(i64::MAX)),
                ),
            ])
        })
        .collect();
    QueryOutput { rows }
}

fn optional_u64_value(value: Option<u64>) -> Value {
    value
        .map(|value| Value::Int(value as i64))
        .unwrap_or(Value::Null)
}

fn optional_usize_value(value: Option<usize>) -> Value {
    value
        .map(|value| Value::Int(value as i64))
        .unwrap_or(Value::Null)
}

fn statement_body(statement: &cypher::Statement) -> &cypher::Statement {
    match statement {
        cypher::Statement::CypherQuery(query) => &query.statement,
        _ => statement,
    }
}

pub(crate) fn statement_kind(statement: &cypher::Statement) -> &'static str {
    match statement {
        cypher::Statement::AlterPropertyState(_) => "alter_property_state",
        cypher::Statement::AlterTableState(_) => "alter_table_state",
        cypher::Statement::BeginTransaction => "begin_transaction",
        cypher::Statement::Checkpoint => "checkpoint",
        cypher::Statement::Commit => "commit",
        cypher::Statement::CreateCompositeIndex(_) => "create_composite_index",
        cypher::Statement::CreateFullTextIndex(_) => "create_full_text_index",
        cypher::Statement::CreateIndex(_) => "create_index",
        cypher::Statement::CreateNode(_) => "create_node",
        cypher::Statement::CreateNodeLabel(_) => "create_node_label",
        cypher::Statement::CreateNodePropertyExistsConstraint(_) => {
            "create_node_property_exists_constraint"
        }
        cypher::Statement::CreateNodeTable(_) => "create_node_table",
        cypher::Statement::CreateProperty(_) => "create_property",
        cypher::Statement::CreateRangeIndex(_) => "create_range_index",
        cypher::Statement::CreateRelationship(_) => "create_relationship",
        cypher::Statement::CreateRelationshipPropertyExistsConstraint(_) => {
            "create_relationship_property_exists_constraint"
        }
        cypher::Statement::CreateRelationshipTable(_) => "create_relationship_table",
        cypher::Statement::CreateRelationshipType(_) => "create_relationship_type",
        cypher::Statement::CreateRelationshipUniqueConstraint(_) => {
            "create_relationship_unique_constraint"
        }
        cypher::Statement::CreateUniqueConstraint(_) => "create_unique_constraint",
        cypher::Statement::CypherQuery(query) => statement_kind(&query.statement),
        cypher::Statement::Explain(explain) => {
            if explain.analyze {
                "explain_analyze"
            } else {
                "explain"
            }
        }
        cypher::Statement::GraphAlgorithm(_) => "graph_algorithm",
        cypher::Statement::VectorSearch(_) => "vector_search",
        cypher::Statement::Pipeline(query) => statement_shape::pipeline_statement_kind(query),
        cypher::Statement::MergeNode(_) => "merge_node",
        cypher::Statement::MergeRelationship(_) => "merge_relationship",
        cypher::Statement::UnwindMutation(_) => "unwind_mutation",
        cypher::Statement::ProjectGraph(_) => "project_graph",
        cypher::Statement::Rollback => "rollback",
        cypher::Statement::SetSystemVariable(_) => "set_system_variable",
    }
}

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
}

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,
            }),
    )
}

fn single_import_label(node: &CanonicalSnapshotNode) -> Result<String> {
    match node.labels.as_slice() {
        [label] => Ok(label.clone()),
        [] => Err(HawDBError::Storage(
            "HawDB Lightning initial import node has no label".to_string(),
        )),
        _ => Err(HawDBError::Storage(
            "HawDB Lightning initial import multi-label node is unsupported".to_string(),
        )),
    }
}

fn relational_state_counts(state: &hawdb_storage::relational::RelationalState) -> (usize, usize) {
    let table_count = state.table_schemas().count();
    let row_count = state
        .table_schemas()
        .map(|schema| state.row_count(&schema.name))
        .sum();
    (table_count, row_count)
}

#[cfg(test)]
mod tests;

#[cfg(test)]
mod legacy_graph_seed_budget_tests;