radixdb-core 1.1.0

Canonical value, identity, and shared contracts for RadixDB
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
// Copyright 2026 RadixDB Contributors
//
// 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.

//! Canonical error types for RadixDB
//!
//! This module defines all error types used throughout the storage engine.

use std::fmt;

use thiserror::Error;

mod code;
mod context;

pub use code::{ErrorCategory, ErrorCode};
pub use context::ErrorContext;

/// Stable category for navigable-reference binding and execution failures.
///
/// The textual code is part of the SQL error contract. Details may gain
/// context, but callers can classify the error without parsing prose.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NavigationErrorCode {
    UnknownRoot,
    AmbiguousRoot,
    NotAReference,
    UnsupportedReferenceShape,
    TargetColumnNotFound,
    ReadOnly,
    SchemaChanged,
    TargetMissing,
    TargetNotUnique,
}

impl NavigationErrorCode {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::UnknownRoot => "NAVIGATION_UNKNOWN_ROOT",
            Self::AmbiguousRoot => "NAVIGATION_AMBIGUOUS_ROOT",
            Self::NotAReference => "NAVIGATION_NOT_A_REFERENCE",
            Self::UnsupportedReferenceShape => "NAVIGATION_UNSUPPORTED_REFERENCE_SHAPE",
            Self::TargetColumnNotFound => "NAVIGATION_TARGET_COLUMN_NOT_FOUND",
            Self::ReadOnly => "NAVIGATION_READ_ONLY",
            Self::SchemaChanged => "NAVIGATION_SCHEMA_CHANGED",
            Self::TargetMissing => "REFERENCE_TARGET_MISSING",
            Self::TargetNotUnique => "REFERENCE_TARGET_NOT_UNIQUE",
        }
    }
}

impl fmt::Display for NavigationErrorCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Result type alias for RadixDB operations
pub type Result<T> = std::result::Result<T, Error>;

/// Main error type for RadixDB storage operations
///
/// This enum covers all error cases including both sentinel errors
/// and structured errors with context.
#[derive(Error, Debug, Clone, PartialEq, Eq)]
pub enum Error {
    // =========================================================================
    // Table errors
    // =========================================================================
    /// Table not found in the database
    #[error("table '{0}' not found")]
    TableNotFound(String),

    /// Table already exists when trying to create
    #[error("table '{0}' already exists")]
    TableAlreadyExists(String),

    /// Table has been closed and cannot be used
    #[error("table closed")]
    TableClosed,

    /// Table column count mismatch
    #[error("table columns don't match, expected {expected}, got {got}")]
    TableColumnsNotMatch { expected: usize, got: usize },

    /// Cannot truncate table because other transactions hold uncommitted writes
    #[error("cannot truncate table: active transactions have uncommitted changes")]
    TableHasActiveTransactions,

    // =========================================================================
    // Column errors
    // =========================================================================
    /// Column not found in table schema
    #[error("column '{0}' not found")]
    ColumnNotFound(String),

    /// A result label resolves to more than one projected column.
    #[error("column '{0}' is ambiguous")]
    AmbiguousColumn(String),

    /// Invalid column type for operation
    #[error("invalid column type")]
    InvalidColumnType,

    /// Vector dimension mismatch
    #[error("Vector dimension mismatch: expected {expected}, got {got}")]
    VectorDimensionMismatch { expected: u16, got: u16 },

    /// Duplicate column name in schema
    #[error("duplicate column")]
    DuplicateColumn,

    // =========================================================================
    // Value errors
    // =========================================================================
    /// Invalid value for operation
    #[error("invalid value")]
    InvalidValue,

    /// Invalid argument for function
    #[error("invalid argument: {0}")]
    InvalidArgument(String),

    /// Authenticated Principal lacks authority for the requested operation.
    #[error("authorization denied: {0}")]
    AuthorizationDenied(String),

    /// Value exceeds maximum length
    #[error("value for column {column} is too long, max {max}, got {got}")]
    ValueTooLong {
        column: String,
        max: usize,
        got: usize,
    },

    // =========================================================================
    // Constraint errors
    // =========================================================================
    /// NOT NULL constraint violation
    #[error("not null constraint failed for column {column}")]
    NotNullConstraint { column: String },

    /// Primary key constraint violation
    #[error("primary key constraint failed with {row_id} already exists in this table")]
    PrimaryKeyConstraint { row_id: i64 },

    /// Unique constraint violation
    #[error("unique constraint failed for index {index} on column {column} with value {value}")]
    UniqueConstraint {
        index: String,
        column: String,
        value: String,
        /// Row ID of the conflicting row (-1 if unknown)
        row_id: i64,
    },

    /// CHECK constraint violation
    #[error("CHECK constraint failed for column {column}: {expression}")]
    CheckConstraintViolation { column: String, expression: String },

    /// Foreign key constraint violation
    #[error("foreign key constraint violation: column '{column}' in table '{table}' references '{ref_table}({ref_column})' — {detail}")]
    ForeignKeyViolation {
        table: String,
        column: String,
        ref_table: String,
        ref_column: String,
        detail: String,
    },

    // =========================================================================
    // Transaction errors
    // =========================================================================
    /// Transaction has not been started
    #[error("transaction not started")]
    TransactionNotStarted,

    /// Transaction has already been started
    #[error("transaction already started")]
    TransactionAlreadyStarted,

    /// Transaction has already ended (committed or rolled back)
    #[error("transaction already ended")]
    TransactionEnded,

    /// Transaction was aborted
    #[error("transaction aborted")]
    TransactionAborted,

    /// Transaction has already been committed
    #[error("transaction already committed")]
    TransactionCommitted,

    /// Transaction has been closed
    #[error("transaction already closed")]
    TransactionClosed,

    /// A registry transition was requested for a missing transaction or from
    /// a state that cannot legally perform that transition.
    #[error("invalid transaction {txn_id} transition: expected {expected}, found {actual}")]
    InvalidTransactionTransition {
        txn_id: i64,
        expected: String,
        actual: String,
    },

    /// Adding a row wait would close a cycle in the transaction wait graph.
    #[error(
        "transaction serialization conflict while acquiring row {row_id}; retry the transaction"
    )]
    TransactionSerializationConflict { row_id: i64 },

    /// A row writer exceeded the bounded claim wait.
    #[error("transaction timed out while waiting to update row {row_id} after {timeout_ms} ms")]
    RowLockTimeout { row_id: i64, timeout_ms: u64 },

    /// A table accumulated more uncompacted L0 work than the configured hard
    /// admission limit. The transaction has not published and may be retried.
    #[error(
        "COMPACTION_BACKPRESSURE: table '{table}' has L0 debt {segments} segments/{physical_bytes} bytes; hard limit {hard_segments} segments/{hard_bytes} bytes; retry after compaction"
    )]
    CompactionBackpressure {
        table: String,
        segments: u64,
        physical_bytes: u64,
        hard_segments: u64,
        hard_bytes: u64,
    },

    // =========================================================================
    // Index errors
    // =========================================================================
    /// Index not found
    #[error("index '{0}' not found")]
    IndexNotFound(String),

    /// Index already exists
    #[error("index '{0}' already exists")]
    IndexAlreadyExists(String),

    /// Column for index not found
    #[error("index column not found")]
    IndexColumnNotFound,

    /// Index is closed
    #[error("index is closed")]
    IndexClosed,

    // =========================================================================
    // Engine errors
    // =========================================================================
    /// Engine is not open
    #[error("engine is not open")]
    EngineNotOpen,

    /// Engine is already open
    #[error("engine is already open")]
    EngineAlreadyOpen,

    // =========================================================================
    // View errors
    // =========================================================================
    /// View already exists
    #[error("view '{0}' already exists")]
    ViewAlreadyExists(String),

    /// View not found
    #[error("view '{0}' not found")]
    ViewNotFound(String),

    // =========================================================================
    // Lock errors
    // =========================================================================
    /// Failed to acquire lock
    #[error("failed to acquire lock: {0}")]
    LockAcquisitionFailed(String),

    // =========================================================================
    // Query result errors
    // =========================================================================
    /// Query returned no rows
    #[error("query returned no rows")]
    NoRowsReturned,

    /// No statements to execute
    #[error("no statements to execute")]
    NoStatementsToExecute,

    /// Column index out of bounds
    #[error("column index {index} out of bounds")]
    ColumnIndexOutOfBounds { index: usize },

    /// A streaming cursor has no current row before its first successful
    /// advance or after end-of-stream.
    #[error("cursor is not positioned on a row")]
    CursorNotPositioned,

    // =========================================================================
    // WAL errors
    // =========================================================================
    /// WAL manager is not running
    #[error("WAL manager is not running")]
    WalNotRunning,

    /// WAL file is closed
    #[error("WAL file is closed")]
    WalFileClosed,

    /// WAL bytes containing an outcome were written, but their durability
    /// could not be established. The transaction must be treated as terminal
    /// and callers must not assume rollback or safely retry the logical action.
    #[error("WAL durability outcome is uncertain: {detail}")]
    WalDurabilityUncertain { detail: String },

    /// A bounded multi-transaction operation failed after publishing a
    /// durable prefix. Callers must not blindly retry the whole operation.
    #[error("{operation} failed after committing {committed_rows} rows: {cause}")]
    PartialCommit {
        operation: String,
        committed_rows: i64,
        cause: String,
    },

    /// One atomic COPY would exceed its configured resident-memory envelope.
    #[error(
        "COPY transaction memory budget exceeded at row {row}: limit {limit_bytes} bytes, attempted {attempted_bytes} bytes"
    )]
    CopyTransactionMemoryLimit {
        row: i64,
        limit_bytes: usize,
        attempted_bytes: usize,
    },

    /// WAL not initialized
    #[error("WAL not initialized")]
    WalNotInitialized,

    // =========================================================================
    // Database errors
    // =========================================================================
    /// Database is locked by another process
    #[error("database is locked by another process")]
    DatabaseLocked,

    /// Cannot drop primary key column
    #[error("cannot drop primary key column")]
    CannotDropPrimaryKey,

    // =========================================================================
    // Comparison errors
    // =========================================================================
    /// Cannot compare NULL with non-NULL value
    #[error("cannot compare NULL with non-NULL value")]
    NullComparison,

    /// Cannot compare incompatible types
    #[error("cannot compare incompatible types")]
    IncomparableTypes,

    // =========================================================================
    // Other errors
    // =========================================================================
    /// Operation not supported
    #[error("not supported: {0}")]
    NotSupported(String),

    /// A native extension function failed. Arguments and plugin-provided
    /// detail are deliberately excluded from this public diagnostic.
    #[error("native function '{function}' failed with plugin status {status}")]
    NativeFunction { function: String, status: u32 },

    /// Navigable-reference contract failure with a stable machine category.
    #[error("{code}: {detail}")]
    Navigation {
        code: NavigationErrorCode,
        detail: String,
    },

    /// Segment not found (internal storage error)
    #[error("segment not found")]
    SegmentNotFound,

    /// Expression evaluation failed
    #[error("expression evaluation failed")]
    ExpressionEvaluation,

    /// Expression evaluation failed with message
    #[error("expression evaluation failed: {message}")]
    ExpressionEvaluationWithMessage { message: String },

    /// Type conversion error
    #[error("type conversion error: cannot convert {from} to {to}")]
    TypeConversion { from: String, to: String },

    /// Parse error
    #[error("parse error: {0}")]
    Parse(String),

    /// IO error (wrapped)
    #[error("IO error: {message}")]
    Io { message: String },

    /// Internal error for unexpected conditions
    #[error("{message}")]
    Internal { message: String },

    // =========================================================================
    // Executor errors
    // =========================================================================
    /// Table or view not found (with name)
    #[error("table or view '{0}' not found")]
    TableOrViewNotFound(String),

    /// Type error
    #[error("type error: {0}")]
    Type(String),

    /// Division by zero
    #[error("division by zero")]
    DivisionByZero,

    /// Query cancelled
    #[error("query cancelled")]
    QueryCancelled,
}

impl Error {
    /// Return a stable machine-readable code without parsing presentation text.
    pub fn code(&self) -> ErrorCode {
        let code = match self {
            Self::TableNotFound(_) => "TABLE_NOT_FOUND",
            Self::TableAlreadyExists(_) => "TABLE_ALREADY_EXISTS",
            Self::TableClosed => "TABLE_CLOSED",
            Self::TableColumnsNotMatch { .. } => "TABLE_COLUMNS_NOT_MATCH",
            Self::TableHasActiveTransactions => "TABLE_HAS_ACTIVE_TRANSACTIONS",
            Self::ColumnNotFound(_) => "COLUMN_NOT_FOUND",
            Self::AmbiguousColumn(_) => "AMBIGUOUS_COLUMN",
            Self::InvalidColumnType => "INVALID_COLUMN_TYPE",
            Self::VectorDimensionMismatch { .. } => "VECTOR_DIMENSION_MISMATCH",
            Self::DuplicateColumn => "DUPLICATE_COLUMN",
            Self::InvalidValue => "INVALID_VALUE",
            Self::InvalidArgument(_) => "INVALID_ARGUMENT",
            Self::AuthorizationDenied(_) => "AUTHORIZATION_DENIED",
            Self::ValueTooLong { .. } => "VALUE_TOO_LONG",
            Self::NotNullConstraint { .. } => "NOT_NULL_CONSTRAINT",
            Self::PrimaryKeyConstraint { .. } => "PRIMARY_KEY_CONSTRAINT",
            Self::UniqueConstraint { .. } => "UNIQUE_CONSTRAINT",
            Self::CheckConstraintViolation { .. } => "CHECK_CONSTRAINT",
            Self::ForeignKeyViolation { .. } => "FOREIGN_KEY_CONSTRAINT",
            Self::TransactionNotStarted => "TRANSACTION_NOT_STARTED",
            Self::TransactionAlreadyStarted => "TRANSACTION_ALREADY_STARTED",
            Self::TransactionEnded => "TRANSACTION_ENDED",
            Self::TransactionAborted => "TRANSACTION_ABORTED",
            Self::TransactionCommitted => "TRANSACTION_COMMITTED",
            Self::TransactionClosed => "TRANSACTION_CLOSED",
            Self::InvalidTransactionTransition { .. } => "INVALID_TRANSACTION_TRANSITION",
            Self::TransactionSerializationConflict { .. } => "TRANSACTION_SERIALIZATION_CONFLICT",
            Self::RowLockTimeout { .. } => "ROW_LOCK_TIMEOUT",
            Self::CompactionBackpressure { .. } => "COMPACTION_BACKPRESSURE",
            Self::IndexNotFound(_) => "INDEX_NOT_FOUND",
            Self::IndexAlreadyExists(_) => "INDEX_ALREADY_EXISTS",
            Self::IndexColumnNotFound => "INDEX_COLUMN_NOT_FOUND",
            Self::IndexClosed => "INDEX_CLOSED",
            Self::EngineNotOpen => "ENGINE_NOT_OPEN",
            Self::EngineAlreadyOpen => "ENGINE_ALREADY_OPEN",
            Self::ViewAlreadyExists(_) => "VIEW_ALREADY_EXISTS",
            Self::ViewNotFound(_) => "VIEW_NOT_FOUND",
            Self::LockAcquisitionFailed(_) => "LOCK_ACQUISITION_FAILED",
            Self::NoRowsReturned => "NO_ROWS_RETURNED",
            Self::NoStatementsToExecute => "NO_STATEMENTS_TO_EXECUTE",
            Self::ColumnIndexOutOfBounds { .. } => "COLUMN_INDEX_OUT_OF_BOUNDS",
            Self::CursorNotPositioned => "CURSOR_NOT_POSITIONED",
            Self::WalNotRunning => "WAL_NOT_RUNNING",
            Self::WalFileClosed => "WAL_FILE_CLOSED",
            Self::WalDurabilityUncertain { .. } => "WAL_DURABILITY_UNCERTAIN",
            Self::PartialCommit { .. } => "PARTIAL_COMMIT",
            Self::CopyTransactionMemoryLimit { .. } => "COPY_TRANSACTION_MEMORY_LIMIT",
            Self::WalNotInitialized => "WAL_NOT_INITIALIZED",
            Self::DatabaseLocked => "DATABASE_LOCKED",
            Self::CannotDropPrimaryKey => "CANNOT_DROP_PRIMARY_KEY",
            Self::NullComparison => "NULL_COMPARISON",
            Self::IncomparableTypes => "INCOMPARABLE_TYPES",
            Self::NotSupported(_) => "NOT_SUPPORTED",
            Self::NativeFunction { .. } => "NATIVE_FUNCTION_ERROR",
            Self::Navigation { code, .. } => code.as_str(),
            Self::SegmentNotFound => "SEGMENT_NOT_FOUND",
            Self::ExpressionEvaluation => "EXPRESSION_EVALUATION",
            Self::ExpressionEvaluationWithMessage { .. } => "EXPRESSION_EVALUATION",
            Self::TypeConversion { .. } => "TYPE_CONVERSION",
            Self::Parse(_) => "PARSE_ERROR",
            Self::Io { .. } => "IO_ERROR",
            Self::Internal { .. } => "INTERNAL_ERROR",
            Self::TableOrViewNotFound(_) => "TABLE_OR_VIEW_NOT_FOUND",
            Self::Type(_) => "TYPE_ERROR",
            Self::DivisionByZero => "DIVISION_BY_ZERO",
            Self::QueryCancelled => "QUERY_CANCELLED",
        };
        ErrorCode::new(code)
    }

    /// Return neutral classification for protocol, API, and diagnostic mapping.
    pub fn context(&self) -> ErrorContext {
        use ErrorCategory as Category;

        let category = match self {
            Self::TableNotFound(_)
            | Self::TableAlreadyExists(_)
            | Self::TableClosed
            | Self::TableColumnsNotMatch { .. }
            | Self::TableHasActiveTransactions
            | Self::ColumnNotFound(_)
            | Self::AmbiguousColumn(_)
            | Self::DuplicateColumn
            | Self::ViewAlreadyExists(_)
            | Self::ViewNotFound(_)
            | Self::CannotDropPrimaryKey
            | Self::Navigation { .. }
            | Self::TableOrViewNotFound(_) => Category::Catalog,
            Self::InvalidColumnType
            | Self::VectorDimensionMismatch { .. }
            | Self::InvalidValue
            | Self::InvalidArgument(_)
            | Self::ValueTooLong { .. }
            | Self::NullComparison
            | Self::IncomparableTypes
            | Self::TypeConversion { .. }
            | Self::Type(_) => Category::Value,
            Self::AuthorizationDenied(_) => Category::Security,
            Self::NotNullConstraint { .. }
            | Self::PrimaryKeyConstraint { .. }
            | Self::UniqueConstraint { .. }
            | Self::CheckConstraintViolation { .. }
            | Self::ForeignKeyViolation { .. } => Category::Constraint,
            Self::TransactionNotStarted
            | Self::TransactionAlreadyStarted
            | Self::TransactionEnded
            | Self::TransactionAborted
            | Self::TransactionCommitted
            | Self::TransactionClosed
            | Self::InvalidTransactionTransition { .. }
            | Self::TransactionSerializationConflict { .. }
            | Self::RowLockTimeout { .. }
            | Self::CompactionBackpressure { .. }
            | Self::PartialCommit { .. }
            | Self::CopyTransactionMemoryLimit { .. } => Category::Transaction,
            Self::IndexNotFound(_)
            | Self::IndexAlreadyExists(_)
            | Self::IndexColumnNotFound
            | Self::IndexClosed => Category::Index,
            Self::EngineNotOpen | Self::EngineAlreadyOpen | Self::LockAcquisitionFailed(_) => {
                Category::Engine
            }
            Self::NoRowsReturned
            | Self::NoStatementsToExecute
            | Self::ColumnIndexOutOfBounds { .. }
            | Self::CursorNotPositioned
            | Self::NotSupported(_)
            | Self::NativeFunction { .. }
            | Self::QueryCancelled => Category::Query,
            Self::WalNotRunning
            | Self::WalFileClosed
            | Self::WalDurabilityUncertain { .. }
            | Self::WalNotInitialized
            | Self::SegmentNotFound => Category::Durability,
            Self::DatabaseLocked => Category::Database,
            Self::ExpressionEvaluation
            | Self::ExpressionEvaluationWithMessage { .. }
            | Self::DivisionByZero => Category::Evaluation,
            Self::Parse(_) => Category::Syntax,
            Self::Io { .. } => Category::Io,
            Self::Internal { .. } => Category::Internal,
        };
        ErrorContext::new(
            self.code(),
            category,
            self.is_retryable(),
            self.is_not_found(),
            self.is_constraint_violation(),
        )
    }

    /// Create a new TableColumnsNotMatch error
    pub fn table_columns_not_match(expected: usize, got: usize) -> Self {
        Error::TableColumnsNotMatch { expected, got }
    }

    /// Create a new ValueTooLong error
    pub fn value_too_long(column: impl Into<String>, max: usize, got: usize) -> Self {
        Error::ValueTooLong {
            column: column.into(),
            max,
            got,
        }
    }

    /// Create a new NotNullConstraint error
    pub fn not_null_constraint(column: impl Into<String>) -> Self {
        Error::NotNullConstraint {
            column: column.into(),
        }
    }

    /// Create a new PrimaryKeyConstraint error
    pub fn primary_key_constraint(row_id: i64) -> Self {
        Error::PrimaryKeyConstraint { row_id }
    }

    /// Create a new UniqueConstraint error
    pub fn unique_constraint(
        index: impl Into<String>,
        column: impl Into<String>,
        value: impl Into<String>,
    ) -> Self {
        Error::UniqueConstraint {
            index: index.into(),
            column: column.into(),
            value: value.into(),
            row_id: -1,
        }
    }

    /// Create a new ForeignKeyViolation error
    pub fn foreign_key_violation(
        table: impl Into<String>,
        column: impl Into<String>,
        ref_table: impl Into<String>,
        ref_column: impl Into<String>,
        detail: impl Into<String>,
    ) -> Self {
        Error::ForeignKeyViolation {
            table: table.into(),
            column: column.into(),
            ref_table: ref_table.into(),
            ref_column: ref_column.into(),
            detail: detail.into(),
        }
    }

    /// Create a new TypeConversion error
    pub fn type_conversion(from: impl Into<String>, to: impl Into<String>) -> Self {
        Error::TypeConversion {
            from: from.into(),
            to: to.into(),
        }
    }

    /// Create a new Parse error
    pub fn parse(message: impl Into<String>) -> Self {
        Error::Parse(message.into())
    }

    /// Create a new IO error
    pub fn io(message: impl Into<String>) -> Self {
        Error::Io {
            message: message.into(),
        }
    }

    /// Create a new Internal error
    pub fn internal(message: impl Into<String>) -> Self {
        Error::Internal {
            message: message.into(),
        }
    }

    /// Create a new ExpressionEvaluationWithMessage error
    pub fn expression_evaluation(message: impl Into<String>) -> Self {
        Error::ExpressionEvaluationWithMessage {
            message: message.into(),
        }
    }

    /// Create a new InvalidArgument error
    pub fn invalid_argument(message: impl Into<String>) -> Self {
        Error::InvalidArgument(message.into())
    }

    /// Create a stable authorization failure without exposing credentials or
    /// parameter values.
    pub fn authorization_denied(message: impl Into<String>) -> Self {
        Error::AuthorizationDenied(message.into())
    }

    /// Create a stable navigable-reference error.
    pub fn navigation(code: NavigationErrorCode, detail: impl Into<String>) -> Self {
        Error::Navigation {
            code,
            detail: detail.into(),
        }
    }

    /// Return the stable navigable-reference category, if this is one.
    pub fn navigation_code(&self) -> Option<NavigationErrorCode> {
        match self {
            Error::Navigation { code, .. } => Some(*code),
            _ => None,
        }
    }

    /// Check if this is a "not found" type error
    pub fn is_not_found(&self) -> bool {
        matches!(
            self,
            Error::TableNotFound(_)
                | Error::ColumnNotFound(_)
                | Error::IndexNotFound(_)
                | Error::IndexColumnNotFound
                | Error::SegmentNotFound
                | Error::ViewNotFound(_)
                | Error::TableOrViewNotFound(_)
        )
    }

    /// Check if this is a constraint violation error
    pub fn is_constraint_violation(&self) -> bool {
        matches!(
            self,
            Error::NotNullConstraint { .. }
                | Error::PrimaryKeyConstraint { .. }
                | Error::UniqueConstraint { .. }
                | Error::CheckConstraintViolation { .. }
                | Error::ForeignKeyViolation { .. }
        )
    }

    /// PK or UNIQUE violation only (excludes NOT NULL / FK).
    pub fn is_pk_or_unique_violation(&self) -> bool {
        matches!(
            self,
            Error::PrimaryKeyConstraint { .. } | Error::UniqueConstraint { .. }
        )
    }

    /// Check if this is a transaction-related error
    pub fn is_transaction_error(&self) -> bool {
        matches!(
            self,
            Error::TransactionNotStarted
                | Error::TransactionAlreadyStarted
                | Error::TransactionEnded
                | Error::TransactionAborted
                | Error::TransactionCommitted
                | Error::TransactionClosed
                | Error::TransactionSerializationConflict { .. }
                | Error::RowLockTimeout { .. }
                | Error::CompactionBackpressure { .. }
        )
    }

    /// The failed logical action did not publish and may be retried after the
    /// reported transient condition changes.
    pub fn is_retryable(&self) -> bool {
        matches!(
            self,
            Error::TransactionSerializationConflict { .. }
                | Error::RowLockTimeout { .. }
                | Error::CompactionBackpressure { .. }
        )
    }
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Self {
        Error::Io {
            message: err.to_string(),
        }
    }
}

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

    #[test]
    fn test_error_display() {
        assert_eq!(
            Error::TableNotFound("users".to_string()).to_string(),
            "table 'users' not found"
        );
        assert_eq!(
            Error::TableAlreadyExists("users".to_string()).to_string(),
            "table 'users' already exists"
        );
        assert_eq!(
            Error::ColumnNotFound("email".to_string()).to_string(),
            "column 'email' not found"
        );
        assert_eq!(Error::InvalidValue.to_string(), "invalid value");
        assert_eq!(
            Error::TransactionNotStarted.to_string(),
            "transaction not started"
        );
        assert_eq!(
            Error::IndexNotFound("idx_email".to_string()).to_string(),
            "index 'idx_email' not found"
        );
        assert_eq!(
            Error::NullComparison.to_string(),
            "cannot compare NULL with non-NULL value"
        );
    }

    #[test]
    fn test_structured_error_display() {
        let err = Error::table_columns_not_match(5, 3);
        assert_eq!(
            err.to_string(),
            "table columns don't match, expected 5, got 3"
        );

        let err = Error::value_too_long("name", 100, 150);
        assert_eq!(
            err.to_string(),
            "value for column name is too long, max 100, got 150"
        );

        let err = Error::not_null_constraint("email");
        assert_eq!(
            err.to_string(),
            "not null constraint failed for column email"
        );

        let err = Error::primary_key_constraint(42);
        assert_eq!(
            err.to_string(),
            "primary key constraint failed with 42 already exists in this table"
        );

        let err = Error::unique_constraint("idx_email", "email", "test@example.com");
        assert_eq!(
            err.to_string(),
            "unique constraint failed for index idx_email on column email with value test@example.com"
        );
    }

    #[test]
    fn test_error_classification() {
        assert!(Error::TableNotFound("t".to_string()).is_not_found());
        assert!(Error::ColumnNotFound("c".to_string()).is_not_found());
        assert!(Error::IndexNotFound("i".to_string()).is_not_found());
        assert!(!Error::InvalidValue.is_not_found());

        assert!(Error::not_null_constraint("col").is_constraint_violation());
        assert!(Error::primary_key_constraint(1).is_constraint_violation());
        assert!(Error::unique_constraint("idx", "col", "val").is_constraint_violation());
        assert!(Error::CheckConstraintViolation {
            column: "<table:t>".to_string(),
            expression: "a > b".to_string(),
        }
        .is_constraint_violation());
        assert!(!Error::TableNotFound("t".to_string()).is_constraint_violation());

        assert!(Error::TransactionNotStarted.is_transaction_error());
        assert!(Error::TransactionCommitted.is_transaction_error());
        assert!(!Error::TableNotFound("t".to_string()).is_transaction_error());
    }

    #[test]
    fn test_transaction_classifier_covers_retryable_transaction_variants() {
        let transaction_errors = [
            Error::TransactionNotStarted,
            Error::TransactionAlreadyStarted,
            Error::TransactionEnded,
            Error::TransactionAborted,
            Error::TransactionCommitted,
            Error::TransactionClosed,
            Error::TransactionSerializationConflict { row_id: 7 },
            Error::RowLockTimeout {
                row_id: 7,
                timeout_ms: 250,
            },
            Error::CompactionBackpressure {
                table: "items".to_string(),
                segments: 32,
                physical_bytes: 1024,
                hard_segments: 32,
                hard_bytes: 2048,
            },
        ];

        for error in transaction_errors {
            assert!(
                error.is_transaction_error(),
                "transaction classifier rejected {error:?}"
            );
        }

        assert!(Error::CompactionBackpressure {
            table: "items".to_string(),
            segments: 32,
            physical_bytes: 1024,
            hard_segments: 32,
            hard_bytes: 2048,
        }
        .is_retryable());
    }

    #[test]
    fn test_error_equality() {
        assert_eq!(
            Error::TableNotFound("t".to_string()),
            Error::TableNotFound("t".to_string())
        );
        assert_ne!(
            Error::TableNotFound("t".to_string()),
            Error::TableAlreadyExists("t".to_string())
        );

        let err1 = Error::table_columns_not_match(5, 3);
        let err2 = Error::table_columns_not_match(5, 3);
        let err3 = Error::table_columns_not_match(5, 4);
        assert_eq!(err1, err2);
        assert_ne!(err1, err3);
    }

    #[test]
    fn navigation_errors_have_stable_machine_categories() {
        let error = Error::navigation(
            NavigationErrorCode::TargetColumnNotFound,
            "target column 'profiles.missing' does not exist",
        );
        assert_eq!(
            error.navigation_code(),
            Some(NavigationErrorCode::TargetColumnNotFound)
        );
        assert_eq!(
            error.to_string(),
            "NAVIGATION_TARGET_COLUMN_NOT_FOUND: target column 'profiles.missing' does not exist"
        );
    }

    #[test]
    fn neutral_context_does_not_depend_on_presentation_text() {
        let error = Error::RowLockTimeout {
            row_id: 17,
            timeout_ms: 250,
        };
        let context = error.context();
        assert_eq!(context.code().as_str(), "ROW_LOCK_TIMEOUT");
        assert_eq!(context.category(), ErrorCategory::Transaction);
        assert!(context.retryable());
        assert!(!context.not_found());
        assert!(!context.constraint_violation());

        let error = Error::ColumnNotFound("missing".to_owned());
        let context = error.context();
        assert_eq!(context.code().as_str(), "COLUMN_NOT_FOUND");
        assert_eq!(context.category(), ErrorCategory::Catalog);
        assert!(context.not_found());
    }

    #[test]
    fn test_io_error_conversion() {
        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
        let err: Error = io_err.into();
        assert!(matches!(err, Error::Io { .. }));
        assert!(err.to_string().contains("file not found"));
    }
}