lance-file 11.0.0

Utilities for the Lance file format
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

use std::{
    fmt::{Display, Formatter},
    str::FromStr,
};

use lance_core::deepsize::{Context, DeepSizeOf};
use lance_core::{Error, Result};

pub const LEGACY_FORMAT_VERSION: &str = "0.1";
pub const V2_FORMAT_2_0: &str = "2.0";
pub const V2_FORMAT_2_1: &str = "2.1";
pub const V2_FORMAT_2_2: &str = "2.2";
pub const V2_FORMAT_2_3: &str = "2.3";

/// Resolve the current stable release policy to an exact file version.
pub const fn stable_file_version() -> ConcreteFileVersion {
    ConcreteFileVersion::V2_1
}

/// Resolve the current next release policy to an exact file version.
pub const fn next_file_version() -> ConcreteFileVersion {
    ConcreteFileVersion::V2_3
}

/// A caller-facing Lance file-version request.
///
/// `Stable` and `Next` are release selectors. They resolve to an exact
/// [`ConcreteFileVersion`] before file or dataset dispatch and are never persisted.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum LanceFileVersion {
    /// The legacy v1 format.
    Legacy,
    /// Exact v2.0.
    V2_0,
    /// Exact v2.1 and the current default.
    #[default]
    V2_1,
    /// The latest stable release.
    Stable,
    /// Exact v2.2.
    V2_2,
    /// The latest unstable release.
    Next,
    /// Exact v2.3.
    V2_3,
}

impl DeepSizeOf for LanceFileVersion {
    fn deep_size_of_children(&self, _context: &mut Context) -> usize {
        0
    }
}

impl LanceFileVersion {
    /// Resolve this request through the current release policy.
    pub const fn resolve(self) -> ConcreteFileVersion {
        match self {
            Self::Legacy => ConcreteFileVersion::V1,
            Self::V2_0 => ConcreteFileVersion::V2_0,
            Self::V2_1 => ConcreteFileVersion::V2_1,
            Self::Stable => stable_file_version(),
            Self::V2_2 => ConcreteFileVersion::V2_2,
            Self::Next => next_file_version(),
            Self::V2_3 => ConcreteFileVersion::V2_3,
        }
    }

    /// Whether this request resolves to an unstable exact format.
    pub const fn is_unstable(self) -> bool {
        self.resolve().is_unstable()
    }
}

impl Display for LanceFileVersion {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::Legacy => LEGACY_FORMAT_VERSION,
            Self::V2_0 => V2_FORMAT_2_0,
            Self::V2_1 => V2_FORMAT_2_1,
            Self::V2_2 => V2_FORMAT_2_2,
            Self::V2_3 => V2_FORMAT_2_3,
            Self::Stable => "stable",
            Self::Next => "next",
        })
    }
}

impl FromStr for LanceFileVersion {
    type Err = Error;

    fn from_str(value: &str) -> Result<Self> {
        match value.to_lowercase().as_str() {
            LEGACY_FORMAT_VERSION | "legacy" => Ok(Self::Legacy),
            V2_FORMAT_2_0 | "0.3" => Ok(Self::V2_0),
            V2_FORMAT_2_1 => Ok(Self::V2_1),
            V2_FORMAT_2_2 => Ok(Self::V2_2),
            V2_FORMAT_2_3 => Ok(Self::V2_3),
            "stable" => Ok(Self::Stable),
            "next" => Ok(Self::Next),
            _ => Err(unknown_version(value)),
        }
    }
}

/// The exact persisted identity of a Lance file format.
///
/// Unlike [`LanceFileVersion`], this type cannot represent release selectors such as
/// `stable` or `next`. Exact versions deliberately have no ordering because format
/// capabilities are not implied by release order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ConcreteFileVersion {
    /// The legacy v1 file format.
    V1,
    /// The v2.0 file format.
    V2_0,
    /// The v2.1 file format.
    V2_1,
    /// The v2.2 file format.
    V2_2,
    /// The v2.3 file format.
    V2_3,
}

impl DeepSizeOf for ConcreteFileVersion {
    fn deep_size_of_children(&self, _context: &mut Context) -> usize {
        0
    }
}

impl ConcreteFileVersion {
    /// Convert this exact identity to the corresponding exact public selector.
    ///
    /// This never produces the release selectors `stable` or `next`.
    pub const fn to_selector(self) -> LanceFileVersion {
        match self {
            Self::V1 => LanceFileVersion::Legacy,
            Self::V2_0 => LanceFileVersion::V2_0,
            Self::V2_1 => LanceFileVersion::V2_1,
            Self::V2_2 => LanceFileVersion::V2_2,
            Self::V2_3 => LanceFileVersion::V2_3,
        }
    }

    /// Whether this exact format is covered only by the unstable release policy.
    pub const fn is_unstable(self) -> bool {
        matches!(self, Self::V2_3)
    }

    /// Decode the exact version string stored in a dataset manifest.
    ///
    /// Public selector aliases such as `legacy`, `0.3`, `stable`, and `next` are
    /// intentionally rejected because manifests only store canonical exact versions.
    pub fn from_manifest_string(value: &str) -> Result<Self> {
        match value {
            LEGACY_FORMAT_VERSION => Ok(Self::V1),
            V2_FORMAT_2_0 => Ok(Self::V2_0),
            V2_FORMAT_2_1 => Ok(Self::V2_1),
            V2_FORMAT_2_2 => Ok(Self::V2_2),
            V2_FORMAT_2_3 => Ok(Self::V2_3),
            _ => Err(unknown_version(value)),
        }
    }

    /// Encode this exact version as the canonical string stored in a dataset manifest.
    pub const fn to_manifest_string(self) -> &'static str {
        match self {
            Self::V1 => LEGACY_FORMAT_VERSION,
            Self::V2_0 => V2_FORMAT_2_0,
            Self::V2_1 => V2_FORMAT_2_1,
            Self::V2_2 => V2_FORMAT_2_2,
            Self::V2_3 => V2_FORMAT_2_3,
        }
    }

    /// Decode the major/minor version stored in `DataFile` metadata.
    ///
    /// Legacy manifests may omit these fields and decode to `(0, 0)`, so all legacy
    /// v1 number pairs accepted by the historical decoder remain valid inputs. The
    /// historical generic decoder also accepted the standard v2.0 footer pair `(0, 3)`;
    /// decoding retains that compatibility while encoding always emits `(2, 0)`.
    pub fn from_data_file_numbers(major: u32, minor: u32) -> Result<Self> {
        match (major, minor) {
            (0, 0..=2) => Ok(Self::V1),
            (0, 3) | (2, 0) => Ok(Self::V2_0),
            (2, 1) => Ok(Self::V2_1),
            (2, 2) => Ok(Self::V2_2),
            (2, 3) => Ok(Self::V2_3),
            _ => Err(unknown_version(format_args!("{}.{}", major, minor))),
        }
    }

    /// Encode the canonical major/minor pair stored in `DataFile` metadata.
    pub const fn to_data_file_numbers(self) -> (u32, u32) {
        match self {
            Self::V1 => (0, 2),
            Self::V2_0 => (2, 0),
            Self::V2_1 => (2, 1),
            Self::V2_2 => (2, 2),
            Self::V2_3 => (2, 3),
        }
    }

    /// Decode the major/minor version stored in a Lance file footer.
    ///
    /// V2.0 has two accepted representations: `(0, 3)` from the standard file writer
    /// and `(2, 0)` from self-described and mini-lance writers.
    pub fn from_footer_numbers(major: u16, minor: u16) -> Result<Self> {
        match (major, minor) {
            (0, 0..=2) => Ok(Self::V1),
            (0, 3) | (2, 0) => Ok(Self::V2_0),
            (2, 1) => Ok(Self::V2_1),
            (2, 2) => Ok(Self::V2_2),
            (2, 3) => Ok(Self::V2_3),
            _ => Err(unknown_version(format_args!("{}.{}", major, minor))),
        }
    }

    /// Encode the footer numbers emitted by the standard Lance file writer.
    pub const fn to_standard_footer_numbers(self) -> (u16, u16) {
        match self {
            Self::V1 => (0, 2),
            Self::V2_0 => (0, 3),
            Self::V2_1 => (2, 1),
            Self::V2_2 => (2, 2),
            Self::V2_3 => (2, 3),
        }
    }

    /// Encode the footer numbers emitted by self-described and mini-lance writers.
    pub const fn to_embedded_footer_numbers(self) -> (u16, u16) {
        match self {
            Self::V1 => (0, 2),
            Self::V2_0 => (2, 0),
            Self::V2_1 => (2, 1),
            Self::V2_2 => (2, 2),
            Self::V2_3 => (2, 3),
        }
    }
}

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

fn unknown_version(value: impl Display) -> Error {
    Error::invalid_input_source(format!("Unknown Lance storage version: {}", value).into())
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use lance_io::object_store::ObjectStore;
    use object_store::path::Path;

    use super::*;

    const EXACT_VERSIONS: [ConcreteFileVersion; 5] = [
        ConcreteFileVersion::V1,
        ConcreteFileVersion::V2_0,
        ConcreteFileVersion::V2_1,
        ConcreteFileVersion::V2_2,
        ConcreteFileVersion::V2_3,
    ];

    #[test]
    fn selector_resolution_is_exact() {
        let cases = [
            (LanceFileVersion::Legacy, ConcreteFileVersion::V1),
            (LanceFileVersion::V2_0, ConcreteFileVersion::V2_0),
            (LanceFileVersion::V2_1, ConcreteFileVersion::V2_1),
            (LanceFileVersion::Stable, ConcreteFileVersion::V2_1),
            (LanceFileVersion::V2_2, ConcreteFileVersion::V2_2),
            (LanceFileVersion::Next, ConcreteFileVersion::V2_3),
            (LanceFileVersion::V2_3, ConcreteFileVersion::V2_3),
        ];

        for (selector, expected) in cases {
            assert_eq!(selector.resolve(), expected);
        }
    }

    #[test]
    fn public_selector_aliases_remain_unchanged() {
        let cases = [
            ("0.1", LanceFileVersion::Legacy),
            ("legacy", LanceFileVersion::Legacy),
            ("2.0", LanceFileVersion::V2_0),
            ("0.3", LanceFileVersion::V2_0),
            ("2.1", LanceFileVersion::V2_1),
            ("stable", LanceFileVersion::Stable),
            ("2.2", LanceFileVersion::V2_2),
            ("next", LanceFileVersion::Next),
            ("2.3", LanceFileVersion::V2_3),
        ];

        for (value, expected) in cases {
            assert_eq!(LanceFileVersion::from_str(value).unwrap(), expected);
        }
    }

    #[test]
    fn manifest_codec_only_accepts_canonical_exact_versions() {
        for version in EXACT_VERSIONS {
            let encoded = version.to_manifest_string();
            assert_eq!(
                ConcreteFileVersion::from_manifest_string(encoded).unwrap(),
                version
            );
        }

        for selector_or_alias in ["legacy", "0.3", "stable", "next"] {
            assert!(ConcreteFileVersion::from_manifest_string(selector_or_alias).is_err());
        }
    }

    #[test]
    fn data_file_codec_preserves_wire_numbers() {
        let cases = [
            (ConcreteFileVersion::V1, (0, 2)),
            (ConcreteFileVersion::V2_0, (2, 0)),
            (ConcreteFileVersion::V2_1, (2, 1)),
            (ConcreteFileVersion::V2_2, (2, 2)),
            (ConcreteFileVersion::V2_3, (2, 3)),
        ];

        for (version, encoded) in cases {
            assert_eq!(version.to_data_file_numbers(), encoded);
            assert_eq!(
                ConcreteFileVersion::from_data_file_numbers(encoded.0, encoded.1).unwrap(),
                version
            );
        }
        for minor in 0..=2 {
            assert_eq!(
                ConcreteFileVersion::from_data_file_numbers(0, minor).unwrap(),
                ConcreteFileVersion::V1
            );
        }
        assert_eq!(
            ConcreteFileVersion::from_data_file_numbers(0, 3).unwrap(),
            ConcreteFileVersion::V2_0
        );
    }

    #[test]
    fn footer_codec_preserves_both_v2_0_writer_representations() {
        let standard_cases = [
            (ConcreteFileVersion::V1, (0, 2)),
            (ConcreteFileVersion::V2_0, (0, 3)),
            (ConcreteFileVersion::V2_1, (2, 1)),
            (ConcreteFileVersion::V2_2, (2, 2)),
            (ConcreteFileVersion::V2_3, (2, 3)),
        ];
        let embedded_cases = [
            (ConcreteFileVersion::V1, (0, 2)),
            (ConcreteFileVersion::V2_0, (2, 0)),
            (ConcreteFileVersion::V2_1, (2, 1)),
            (ConcreteFileVersion::V2_2, (2, 2)),
            (ConcreteFileVersion::V2_3, (2, 3)),
        ];

        for (version, encoded) in standard_cases {
            assert_eq!(version.to_standard_footer_numbers(), encoded);
            assert_eq!(
                ConcreteFileVersion::from_footer_numbers(encoded.0, encoded.1).unwrap(),
                version
            );
        }
        for (version, encoded) in embedded_cases {
            assert_eq!(version.to_embedded_footer_numbers(), encoded);
            assert_eq!(
                ConcreteFileVersion::from_footer_numbers(encoded.0, encoded.1).unwrap(),
                version
            );
        }
        for minor in 0..=2 {
            assert_eq!(
                ConcreteFileVersion::from_footer_numbers(0, minor).unwrap(),
                ConcreteFileVersion::V1
            );
        }
    }

    #[tokio::test]
    async fn file_version_detection_accepts_all_legacy_footer_aliases() {
        let object_store = ObjectStore::memory();
        for minor in 0u16..=2 {
            let path = Path::from(format!("legacy-{minor}.lance"));
            let mut footer = Vec::with_capacity(8);
            footer.extend_from_slice(&0u16.to_le_bytes());
            footer.extend_from_slice(&minor.to_le_bytes());
            footer.extend_from_slice(crate::format::MAGIC);
            object_store.put(&path, &footer).await.unwrap();

            assert_eq!(
                crate::determine_file_version(&object_store, &path, Some(footer.len()))
                    .await
                    .unwrap(),
                ConcreteFileVersion::V1
            );
        }
    }
}