timelake_format/lib.rs
1//! Timelake's on-disk conventions, with no I/O and no heavy dependencies, so the engine
2//! (`timelake`) and the WebAssembly reader (`timelake-wasm`) share one definition: the table
3//! contract stored in `timelake.*` properties, time partition math, and Iceberg bound decoding.
4//!
5//! ```
6//! use std::collections::HashMap;
7//! use timelake_format::{Chunk, Contract, property};
8//!
9//! let properties = HashMap::from([
10//! (property::TIME_COLUMN.to_string(), "time".to_string()),
11//! (property::CHUNK.to_string(), "day".to_string()),
12//! ]);
13//! let contract = Contract::from_properties(&properties)?;
14//! assert_eq!(contract.chunk, Some(Chunk::Day));
15//!
16//! let day = Chunk::Day.value(1_767_225_600_000_000)?; // 2026-01-01T00:00:00Z in µs
17//! assert_eq!(Chunk::Day.label(day), "2026-01-01");
18//! # Ok::<(), timelake_format::FormatError>(())
19//! ```
20
21#![warn(missing_docs)]
22
23use std::fmt;
24
25pub mod bounds;
26pub mod contract;
27#[cfg(feature = "head")]
28pub mod head;
29pub mod settings;
30pub mod version;
31
32pub use contract::{Chunk, Contract, TableOptions, format_duration, parse_duration};
33pub use version::FormatVersion;
34
35/// Table property keys holding the [`Contract`].
36pub mod property {
37 /// The timestamp column.
38 pub const TIME_COLUMN: &str = "timelake.time-column";
39 /// Comma-separated series key columns.
40 pub const SERIES_KEY: &str = "timelake.series-key";
41 /// Time partition size: `hour`, `day` or `month`.
42 pub const CHUNK: &str = "timelake.chunk-interval";
43 /// `true` to drop duplicate `(series, time)` rows during compaction.
44 pub const DEDUP: &str = "timelake.dedup";
45 /// Data retention, e.g. `90d`.
46 pub const RETENTION: &str = "timelake.retention";
47 /// Target size of compacted files, in bytes.
48 pub const TARGET_FILE_SIZE: &str = "timelake.target-file-size-bytes";
49 /// Rows per Parquet row group.
50 pub const ROW_GROUP_ROWS: &str = "timelake.row-group-rows";
51 /// `false` to skip inline maintenance on append.
52 pub const AUTO_MAINTAIN: &str = "timelake.auto-maintain";
53 /// How long to keep old snapshots, e.g. `1h`.
54 pub const SNAPSHOT_RETENTION: &str = "timelake.snapshot-retention";
55 /// Snapshots always kept.
56 pub const MIN_SNAPSHOTS: &str = "timelake.min-snapshots";
57 /// Snapshots kept at most.
58 pub const MAX_SNAPSHOTS: &str = "timelake.max-snapshots";
59 /// Files per size tier that trigger inline compaction.
60 pub const COMPACT_MIN_FILES: &str = "timelake.compact-min-files";
61 /// Inline maintenance rewrites a partition whole (sorted, duplicates removed) once the table
62 /// holds data newer than the partition's end by this much, or `off`.
63 pub const COMPACT_COLD_AFTER: &str = "timelake.compact-cold-after";
64 /// The column holding each row's insertion time (`_ingest_ts`), set by the writer; absent for
65 /// tables without one. Among rows with the same `(series…, time)` the latest insertion wins.
66 pub const INGEST_TIME_COLUMN: &str = "timelake.ingest-time-column";
67 /// The Timelake reader removes copies of a `(series…, time)` when reading (default: as `dedup`).
68 pub const READ_DEDUP: &str = "timelake.read-dedup";
69 /// How the Timelake reader removes copies: `merge` (default) or `keys-first` (SPEC §9.1).
70 pub const READ_DEDUP_STRATEGY: &str = "timelake.read-dedup-strategy";
71 /// Format of the head object (`metadata/timelake-head`): `arrow` (default), `json`, another
72 /// registered codec, or `none` to not write one.
73 pub const HEAD_FORMAT: &str = "timelake.head-format";
74 /// Bytes the head may spend on optional sections such as the last-value index.
75 pub const HEAD_INLINE_BYTES: &str = "timelake.head-inline-bytes";
76 /// No snapshot expires before this time (milliseconds since the epoch). Set by `timelake upgrade`
77 /// so the table can be rolled back to its state before the upgrade.
78 pub const KEEP_SNAPSHOTS_UNTIL: &str = "timelake.keep-snapshots-until";
79 /// The table format version (see [`crate::FORMAT_VERSION`]); absent means 0.1.0.
80 pub const FORMAT_VERSION: &str = "timelake.format-version";
81 /// On a raw table: its rollups, comma-separated table names (SPEC §11.1).
82 pub const ROLLUPS: &str = "timelake.rollups";
83 /// `false` to stop the Timelake reader answering queries on this table from its rollups.
84 pub const ROLLUP_ROUTING: &str = "timelake.rollup-routing";
85 /// On a rollup: the raw table it summarises.
86 pub const ROLLUP_SOURCE: &str = "timelake.rollup-source";
87 /// On a rollup: the bucket width, e.g. `1m`.
88 pub const ROLLUP_BUCKET: &str = "timelake.rollup-bucket";
89 /// On a rollup: its aggregates, as JSON (`[{"name": "avg_value", "function": "avg", "column": "value"}]`).
90 pub const ROLLUP_AGGREGATES: &str = "timelake.rollup-aggregates";
91 /// On a rollup: inline maintenance of the raw table refreshes the rollup once its last refresh
92 /// is this old, or `off`.
93 pub const ROLLUP_REFRESH_INTERVAL: &str = "timelake.rollup-refresh-interval";
94}
95
96/// Snapshot summary keys: Timelake's own, and the standard Iceberg counters Timelake maintains.
97pub mod summary {
98 /// Location of the snapshot's last-value index.
99 pub const LAST_VALUE_INDEX: &str = "timelake.last-value-index";
100 /// The writer-supplied idempotency key of an append.
101 pub const BATCH_ID: &str = "timelake.batch-id";
102 /// On a rollup: the id of the raw table snapshot the rollup reflects.
103 pub const ROLLUP_SOURCE_SNAPSHOT: &str = "timelake.rollup-source-snapshot";
104 /// On a rollup: the sequence number of that snapshot. Data files of the raw table with a higher
105 /// data sequence number hold rows the rollup doesn't reflect yet.
106 pub const ROLLUP_SOURCE_SEQUENCE: &str = "timelake.rollup-source-sequence";
107
108 /// Data files added by the snapshot.
109 pub const ADDED_DATA_FILES: &str = "added-data-files";
110 /// Rows added by the snapshot.
111 pub const ADDED_RECORDS: &str = "added-records";
112 /// Bytes added by the snapshot.
113 pub const ADDED_FILES_SIZE: &str = "added-files-size";
114 /// Data files removed by the snapshot.
115 pub const DELETED_DATA_FILES: &str = "deleted-data-files";
116 /// Rows removed by the snapshot.
117 pub const DELETED_RECORDS: &str = "deleted-records";
118 /// Bytes removed by the snapshot.
119 pub const REMOVED_FILES_SIZE: &str = "removed-files-size";
120 /// Data files in the table after the snapshot.
121 pub const TOTAL_DATA_FILES: &str = "total-data-files";
122 /// Rows in the table after the snapshot.
123 pub const TOTAL_RECORDS: &str = "total-records";
124 /// Bytes in the table after the snapshot.
125 pub const TOTAL_FILES_SIZE: &str = "total-files-size";
126 /// Delete files in the table (always 0: Timelake doesn't write them).
127 pub const TOTAL_DELETE_FILES: &str = "total-delete-files";
128 /// Position deletes in the table (always 0).
129 pub const TOTAL_POSITION_DELETES: &str = "total-position-deletes";
130 /// Equality deletes in the table (always 0).
131 pub const TOTAL_EQUALITY_DELETES: &str = "total-equality-deletes";
132}
133
134/// The table format this version of Timelake writes, recorded in each table as
135/// `timelake.format-version`. It reads and writes older formats, and newer ones as far as
136/// [`FormatVersion`] allows. `timelake upgrade` brings a table to this format.
137///
138/// - 0.1.0: tables written by Timelake 0.1 and 0.2 (no property).
139/// - 0.2.0: the head object (`metadata/timelake-head`), from Timelake 0.3 (which wrote it as `2`).
140/// - 0.3.0: an insertion-time column (`timelake.ingest-time-column`) that writers fill.
141pub const FORMAT_VERSION: FormatVersion = FormatVersion::new(0, 3, 0);
142
143/// The name of the insertion-time column of new time-series tables.
144pub const INGEST_TIME_COLUMN: &str = "_ingest_ts";
145
146/// Optional warehouse-level index of table names, for stores that cannot list (plain HTTPS).
147pub const TABLES_INDEX: &str = "timelake-tables.json";
148
149/// An invalid contract property, duration, or partition value.
150#[derive(Debug, Clone, PartialEq, Eq)]
151pub struct FormatError(pub String);
152
153impl FormatError {
154 fn new(msg: impl Into<String>) -> Self {
155 Self(msg.into())
156 }
157}
158
159impl fmt::Display for FormatError {
160 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161 f.write_str(&self.0)
162 }
163}
164
165impl std::error::Error for FormatError {}
166
167/// `Result` with [`FormatError`].
168pub type Result<T> = std::result::Result<T, FormatError>;