timeseries-table-format 0.5.0

Append-only time-series table format with gap/overlap tracking
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
//! Top-level error facade for table operations.
//!
//! [`TableError`] adds only operation context. Each variant wraps the complete
//! error owned by that operation and delegates its source and backtrace. Add
//! detailed failures to the owning operation error rather than copying
//! subsystem-specific variants into this facade.

use snafu::prelude::*;

use super::operations::{
    AppendError, CoverageQueryError, CreateTableError, OpenTableError, OptimizeError, ScanError,
    TableStateAccessError,
};

/// Errors from high-level time-series table operations.
///
/// Each variant carries enough context for callers to surface actionable
/// messages to users or implement retries where appropriate (for example,
/// conflicts on optimistic concurrency control).
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
#[non_exhaustive]
pub enum TableError {
    /// A table creation operation failed.
    #[snafu(display("Table creation failed: {source}"))]
    Create {
        /// Complete table creation failure.
        #[snafu(source, backtrace)]
        source: CreateTableError,
    },

    /// A table open operation failed.
    #[snafu(display("Table open failed: {source}"))]
    Open {
        /// Complete table open failure.
        #[snafu(source, backtrace)]
        source: OpenTableError,
    },

    /// A table state access or refresh operation failed.
    #[snafu(display("Table state access failed: {source}"))]
    StateAccess {
        /// Complete state-access failure.
        #[snafu(source, backtrace)]
        source: TableStateAccessError,
    },

    /// An append operation failed.
    #[snafu(display("Append failed: {source}"))]
    Append {
        /// Complete append-owned failure.
        #[snafu(source, backtrace)]
        source: AppendError,
    },

    /// A table scan failed during planning or lazy execution.
    #[snafu(display("Table scan failed: {source}"))]
    Scan {
        /// Complete scan operation error.
        #[snafu(source, backtrace)]
        source: ScanError,
    },

    /// A table coverage query failed.
    #[snafu(display("Table coverage query failed: {source}"))]
    CoverageQuery {
        /// Complete coverage query operation error.
        #[snafu(source, backtrace)]
        source: CoverageQueryError,
    },

    /// An entity-layout optimization operation failed.
    #[snafu(display("Entity-layout optimization failed: {source}"))]
    Optimize {
        /// Complete optimization-owned failure.
        #[snafu(source, backtrace)]
        source: OptimizeError,
    },
}

#[cfg(test)]
mod tests {
    use std::{error::Error as _, io};

    use arrow::error::ArrowError;
    use parquet::errors::ParquetError;
    use snafu::{Backtrace, ErrorCompat, IntoError};

    use crate::coverage::{CoverageCodecError, CoverageSidecarError};
    use crate::formats::parquet::EntityRewriteError;
    use crate::metadata::{
        index::IndexSpecError, logical_schema::LogicalToArrowSchemaError,
        schema_compat::SchemaCompatibilityError,
    };
    use crate::storage::StorageError;
    use crate::transaction_log::CommitError;

    use super::*;

    #[test]
    fn append_facade_preserves_arrow_source_and_backtrace() {
        let append_error = AppendError::ArrowInput {
            source: ArrowError::ComputeError("input failed".to_string()),
            backtrace: Backtrace::capture(),
        };
        let error = AppendSnafu.into_error(append_error);

        let append_source = error
            .source()
            .and_then(|source| source.downcast_ref::<AppendError>())
            .expect("append source");
        let arrow_source = append_source
            .source()
            .and_then(|source| source.downcast_ref::<ArrowError>())
            .expect("Arrow source");
        let append_backtrace = ErrorCompat::backtrace(append_source).expect("append backtrace");
        let table_backtrace = ErrorCompat::backtrace(&error).expect("table backtrace");

        assert!(
            matches!(arrow_source, ArrowError::ComputeError(message) if message == "input failed")
        );
        assert!(std::ptr::eq(table_backtrace, append_backtrace));
    }

    #[test]
    fn append_facade_preserves_parquet_source_and_backtrace() {
        let error = AppendSnafu.into_error(AppendError::ParquetWrite {
            source: ParquetError::General("write failed".to_string()),
            backtrace: Backtrace::capture(),
        });

        let append_source = error
            .source()
            .and_then(|source| source.downcast_ref::<AppendError>())
            .expect("append source");
        let parquet_source = append_source
            .source()
            .and_then(|source| source.downcast_ref::<ParquetError>())
            .expect("Parquet source");
        let append_backtrace = ErrorCompat::backtrace(append_source).expect("append backtrace");
        let table_backtrace = ErrorCompat::backtrace(&error).expect("table backtrace");

        assert!(
            matches!(parquet_source, ParquetError::General(message) if message == "write failed")
        );
        assert!(std::ptr::eq(table_backtrace, append_backtrace));
    }

    #[test]
    fn append_schema_validation_leaf_does_not_manufacture_a_backtrace() {
        let error = AppendError::from(SchemaCompatibilityError::MissingTableSchema);

        assert!(error.to_string().starts_with("Schema validation failed:"));
        assert!(ErrorCompat::backtrace(&error).is_none());
    }

    #[test]
    fn schema_wrappers_delegate_the_originating_backtrace() {
        let schema = SchemaCompatibilityError::RegisteredSchemaConversion {
            source: Box::new(LogicalToArrowSchemaError::Int96Unsupported {
                column: "time".to_string(),
                backtrace: Backtrace::capture(),
            }),
        };
        let error = AppendSnafu.into_error(AppendError::from(schema));

        let append = error
            .source()
            .and_then(|source| source.downcast_ref::<AppendError>())
            .expect("append source");
        let schema = append
            .source()
            .and_then(|source| source.downcast_ref::<Box<SchemaCompatibilityError>>())
            .map(Box::as_ref)
            .expect("schema compatibility source");
        let conversion = schema
            .source()
            .and_then(|source| source.downcast_ref::<Box<LogicalToArrowSchemaError>>())
            .map(Box::as_ref)
            .expect("logical-to-Arrow source");
        let originating_backtrace = ErrorCompat::backtrace(conversion).expect("source backtrace");

        assert!(std::ptr::eq(
            ErrorCompat::backtrace(schema).expect("schema backtrace"),
            originating_backtrace
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(append).expect("append backtrace"),
            originating_backtrace
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(&error).expect("table backtrace"),
            originating_backtrace
        ));
    }

    #[test]
    fn append_rollback_preserves_primary_chain_cleanup_errors_and_backtrace() {
        let commit_error = CommitError::Conflict {
            expected: 1,
            found: 2,
            backtrace: Backtrace::capture(),
        };
        let cleanup_error = StorageError::OtherIo {
            path: "data/segment.parquet".to_string(),
            source: io::Error::other("cleanup failed").into(),
            backtrace: Backtrace::capture(),
        };
        let error = AppendSnafu.into_error(AppendError::Rollback {
            source: Box::new(AppendError::Commit {
                source: commit_error,
            }),
            cleanup_errors: vec![cleanup_error],
        });

        let rollback = error
            .source()
            .and_then(|source| source.downcast_ref::<AppendError>())
            .expect("rollback source");
        let cleanup_errors = match rollback {
            AppendError::Rollback { cleanup_errors, .. } => cleanup_errors,
            other => panic!("unexpected append error: {other:?}"),
        };
        let primary = rollback
            .source()
            .and_then(|source| source.downcast_ref::<Box<AppendError>>())
            .map(Box::as_ref)
            .expect("primary append source");
        let commit = primary
            .source()
            .and_then(|source| source.downcast_ref::<CommitError>())
            .expect("commit source");
        let commit_backtrace = ErrorCompat::backtrace(commit).expect("commit backtrace");

        assert!(matches!(
            commit,
            CommitError::Conflict {
                expected: 1,
                found: 2,
                ..
            }
        ));
        assert!(matches!(
            cleanup_errors.as_slice(),
            [StorageError::OtherIo { path, .. }] if path == "data/segment.parquet"
        ));
        let message = error.to_string();
        assert!(message.contains("cleanup failed"));
        assert!(!message.contains("Backtrace"));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(&error).expect("table backtrace"),
            commit_backtrace
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(rollback).expect("rollback backtrace"),
            commit_backtrace
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(primary).expect("primary backtrace"),
            commit_backtrace
        ));
    }

    #[test]
    fn create_facade_preserves_index_source_and_operation_backtrace() {
        let error = CreateSnafu.into_error(CreateTableError::IndexSpecValidation {
            source: IndexSpecError::EmptyColumn,
            backtrace: Backtrace::capture(),
        });

        let create = error
            .source()
            .and_then(|source| source.downcast_ref::<CreateTableError>())
            .expect("create source");
        let index = create
            .source()
            .and_then(|source| source.downcast_ref::<IndexSpecError>())
            .expect("index specification source");

        assert!(matches!(index, IndexSpecError::EmptyColumn));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(&error).expect("table backtrace"),
            ErrorCompat::backtrace(create).expect("create backtrace")
        ));
    }

    #[test]
    fn open_facade_preserves_commit_source_and_backtrace() {
        let error = OpenSnafu.into_error(OpenTableError::Commit {
            source: CommitError::Conflict {
                expected: 8,
                found: 9,
                backtrace: Backtrace::capture(),
            },
        });

        let open = error
            .source()
            .and_then(|source| source.downcast_ref::<OpenTableError>())
            .expect("open source");
        let commit = open
            .source()
            .and_then(|source| source.downcast_ref::<CommitError>())
            .expect("commit source");
        let commit_backtrace = ErrorCompat::backtrace(commit).expect("commit backtrace");

        assert!(matches!(commit, CommitError::Conflict { .. }));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(open).expect("open backtrace"),
            commit_backtrace
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(&error).expect("table backtrace"),
            commit_backtrace
        ));
    }

    #[test]
    fn state_access_facade_preserves_commit_source_and_backtrace() {
        let error = StateAccessSnafu.into_error(TableStateAccessError::Commit {
            source: CommitError::MissingTableMetadata {
                current_version: 7,
                backtrace: Backtrace::capture(),
            },
        });

        let state_access = error
            .source()
            .and_then(|source| source.downcast_ref::<TableStateAccessError>())
            .expect("state access source");
        let commit = state_access
            .source()
            .and_then(|source| source.downcast_ref::<CommitError>())
            .expect("commit source");
        let commit_backtrace = ErrorCompat::backtrace(commit).expect("commit backtrace");

        assert!(matches!(commit, CommitError::MissingTableMetadata { .. }));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(state_access).expect("state access backtrace"),
            commit_backtrace
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(&error).expect("table backtrace"),
            commit_backtrace
        ));
    }

    #[test]
    fn scan_facade_preserves_parquet_source_and_backtrace() {
        let error = ScanSnafu.into_error(ScanError::Parquet {
            path: "data/segment.parquet".to_string(),
            operation: "reading metadata",
            source: Box::new(ParquetError::General("corrupt footer".to_string())),
            backtrace: Box::new(Backtrace::capture()),
        });

        let scan = error
            .source()
            .and_then(|source| source.downcast_ref::<ScanError>())
            .expect("scan source");
        let parquet = scan
            .source()
            .and_then(|source| source.downcast_ref::<Box<ParquetError>>())
            .map(Box::as_ref)
            .expect("Parquet source");
        let scan_backtrace = ErrorCompat::backtrace(scan).expect("scan backtrace");

        assert!(matches!(parquet, ParquetError::General(message) if message == "corrupt footer"));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(&error).expect("table backtrace"),
            scan_backtrace
        ));
    }

    #[test]
    fn coverage_facade_preserves_codec_source_and_backtrace() {
        let error = CoverageQuerySnafu.into_error(CoverageQueryError::CoverageSnapshotRead {
            coverage_path: "coverage/table.coverage".to_string(),
            source: Box::new(CoverageSidecarError::Codec {
                source: CoverageCodecError::InvalidEntityCoverageMagic {
                    backtrace: Backtrace::capture(),
                },
            }),
        });

        let coverage_query = error
            .source()
            .and_then(|source| source.downcast_ref::<CoverageQueryError>())
            .expect("coverage query source");
        let sidecar = coverage_query
            .source()
            .and_then(|source| source.downcast_ref::<Box<CoverageSidecarError>>())
            .map(Box::as_ref)
            .expect("coverage sidecar source");
        let codec = sidecar
            .source()
            .and_then(|source| source.downcast_ref::<CoverageCodecError>())
            .expect("coverage codec source");
        let codec_backtrace = ErrorCompat::backtrace(codec).expect("codec backtrace");

        assert!(matches!(
            codec,
            CoverageCodecError::InvalidEntityCoverageMagic { .. }
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(coverage_query).expect("coverage query backtrace"),
            codec_backtrace
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(&error).expect("table backtrace"),
            codec_backtrace
        ));
    }

    #[test]
    fn optimize_facade_preserves_commit_source_and_backtrace() {
        let error = OptimizeSnafu.into_error(OptimizeError::Commit {
            source: CommitError::Conflict {
                expected: 3,
                found: 4,
                backtrace: Backtrace::capture(),
            },
        });

        let optimize = error
            .source()
            .and_then(|source| source.downcast_ref::<OptimizeError>())
            .expect("optimize source");
        let commit = optimize
            .source()
            .and_then(|source| source.downcast_ref::<CommitError>())
            .expect("commit source");
        let commit_backtrace = ErrorCompat::backtrace(commit).expect("commit backtrace");

        assert!(matches!(
            commit,
            CommitError::Conflict {
                expected: 3,
                found: 4,
                ..
            }
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(optimize).expect("optimize backtrace"),
            commit_backtrace
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(&error).expect("table backtrace"),
            commit_backtrace
        ));
    }

    #[test]
    fn optimize_facade_preserves_rewrite_storage_source_and_backtrace() {
        let error = OptimizeSnafu.into_error(OptimizeError::from(EntityRewriteError::Storage {
            source: StorageError::OtherIo {
                path: "data/mixed.parquet".to_string(),
                source: io::Error::other("read failed").into(),
                backtrace: Backtrace::capture(),
            },
        }));

        let optimize = error
            .source()
            .and_then(|source| source.downcast_ref::<OptimizeError>())
            .expect("optimize source");
        let rewrite = optimize
            .source()
            .and_then(|source| source.downcast_ref::<Box<EntityRewriteError>>())
            .map(Box::as_ref)
            .expect("rewrite source");
        let storage = rewrite
            .source()
            .and_then(|source| source.downcast_ref::<StorageError>())
            .expect("storage source");
        let storage_backtrace = ErrorCompat::backtrace(storage).expect("storage backtrace");

        assert!(matches!(
            storage,
            StorageError::OtherIo { path, .. } if path == "data/mixed.parquet"
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(rewrite).expect("rewrite backtrace"),
            storage_backtrace
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(optimize).expect("optimize backtrace"),
            storage_backtrace
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(&error).expect("table backtrace"),
            storage_backtrace
        ));
    }

    #[test]
    fn optimize_rollback_preserves_primary_chain_and_typed_cleanup_errors() {
        let error = OptimizeSnafu.into_error(OptimizeError::Rollback {
            source: Box::new(OptimizeError::Commit {
                source: CommitError::Conflict {
                    expected: 5,
                    found: 6,
                    backtrace: Backtrace::capture(),
                },
            }),
            cleanup_errors: vec![StorageError::OtherIo {
                path: "data/_staged/segment.parquet".to_string(),
                source: io::Error::other("cleanup failed").into(),
                backtrace: Backtrace::capture(),
            }],
        });

        let rollback = error
            .source()
            .and_then(|source| source.downcast_ref::<OptimizeError>())
            .expect("optimize source");
        let cleanup_errors = match rollback {
            OptimizeError::Rollback { cleanup_errors, .. } => cleanup_errors,
            other => panic!("unexpected optimize error: {other:?}"),
        };
        let primary = rollback
            .source()
            .and_then(|source| source.downcast_ref::<Box<OptimizeError>>())
            .map(Box::as_ref)
            .expect("primary optimize source");
        let commit = primary
            .source()
            .and_then(|source| source.downcast_ref::<CommitError>())
            .expect("commit source");
        let commit_backtrace = ErrorCompat::backtrace(commit).expect("commit backtrace");

        assert!(matches!(
            cleanup_errors.as_slice(),
            [StorageError::OtherIo { path, .. }] if path == "data/_staged/segment.parquet"
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(rollback).expect("rollback backtrace"),
            commit_backtrace
        ));
        assert!(std::ptr::eq(
            ErrorCompat::backtrace(&error).expect("table backtrace"),
            commit_backtrace
        ));
    }
}