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