lance 4.0.0

A columnar data format that is 100x faster than Parquet for random access.
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

//! Serialization and deserialization of Arrow Schema to JSON.
//!
//! This serialization is a convenience utility.  It is not intended to be a standard
//! serialization format for Arrow.  No guarantees are made about the stability of the
//! serialization format.  Use at your own risk.

use std::collections::HashMap;
use std::sync::Arc;

use arrow_schema::{DataType, Field, Schema};
use serde::{Deserialize, Serialize};

use crate::datatypes::LogicalType;
use lance_core::error::{Error, Result};

/// JSON representation of an Apache Arrow [DataType].
#[derive(Serialize, Deserialize, Debug)]
pub struct JsonDataType {
    #[serde(rename = "type")]
    type_: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    fields: Option<Vec<JsonField>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    length: Option<usize>,
}

impl JsonDataType {
    fn try_new(dt: &DataType) -> Result<Self> {
        dt.try_into()
    }
}

impl TryFrom<&DataType> for JsonDataType {
    type Error = Error;

    fn try_from(dt: &DataType) -> Result<Self> {
        let (type_name, fields) = match dt {
            DataType::Null
            | DataType::Boolean
            | DataType::Int8
            | DataType::Int16
            | DataType::Int32
            | DataType::Int64
            | DataType::UInt8
            | DataType::UInt16
            | DataType::UInt32
            | DataType::UInt64
            | DataType::Float16
            | DataType::Float32
            | DataType::Float64
            | DataType::Decimal128(_, _)
            | DataType::Decimal256(_, _)
            | DataType::Utf8
            | DataType::Binary
            | DataType::LargeUtf8
            | DataType::LargeBinary
            | DataType::Date32
            | DataType::Date64
            | DataType::Time32(_)
            | DataType::Time64(_)
            | DataType::Timestamp(_, _)
            | DataType::Duration(_)
            | DataType::Interval(_)
            | DataType::Dictionary(_, _) => {
                let logical_type: LogicalType = dt.try_into()?;
                (logical_type.to_string(), None)
            }
            DataType::List(f) => {
                let fields = vec![JsonField::try_from(f.as_ref())?];
                ("list".to_string(), Some(fields))
            }
            DataType::LargeList(f) => {
                let fields = vec![JsonField::try_from(f.as_ref())?];
                ("large_list".to_string(), Some(fields))
            }
            DataType::FixedSizeList(f, len) => {
                let fields = vec![JsonField::try_from(f.as_ref())?];
                return Ok(Self {
                    type_: "fixed_size_list".to_string(),
                    fields: Some(fields),
                    length: Some(*len as usize),
                });
            }
            DataType::FixedSizeBinary(len) => {
                return Ok(Self {
                    type_: "fixed_size_binary".to_string(),
                    fields: None,
                    length: Some(*len as usize),
                });
            }
            DataType::Struct(fields) => {
                let fields = fields
                    .iter()
                    .map(|f| JsonField::try_from(f.as_ref()))
                    .collect::<Result<Vec<_>>>()?;
                ("struct".to_string(), Some(fields))
            }
            _ => {
                return Err(Error::arrow(format!(
                    "Json conversion: Unsupported type: {dt}"
                )));
            }
        };

        Ok(Self {
            type_: type_name,
            fields,
            length: None,
        })
    }
}

impl TryFrom<&JsonDataType> for DataType {
    type Error = Error;

    fn try_from(value: &JsonDataType) -> Result<Self> {
        let type_name = value.type_.as_str();
        match type_name {
            "null" | "bool" | "int8" | "int16" | "int32" | "int64" | "uint8" | "uint16"
            | "uint32" | "uint64" | "halffloat" | "float" | "double" | "string" | "binary"
            | "large_string" | "large_binary" | "date32:day" | "date64:ms" => {
                let logical_type: LogicalType = type_name.into();
                (&logical_type).try_into()
            }
            dt if dt.starts_with("time32:")
                || dt.starts_with("time64:")
                || dt.starts_with("timestamp:")
                || dt.starts_with("duration:")
                || dt.starts_with("dict:")
                || dt.starts_with("decimal:") =>
            {
                let logical_type: LogicalType = dt.into();
                (&logical_type).try_into()
            }
            "list" | "large_list" | "fixed_size_list" | "struct" => {
                let fields = value
                    .fields
                    .as_ref()
                    .ok_or_else(|| {
                        Error::arrow("Json conversion: List type requires a field".to_string())
                    })?
                    .iter()
                    .map(Field::try_from)
                    .collect::<Result<Vec<_>>>()?;

                match type_name {
                    "list" => Ok(Self::List(Arc::new(fields[0].clone()))),
                    "large_list" => Ok(Self::LargeList(Arc::new(fields[0].clone()))),
                    "fixed_size_list" => {
                        let length = value.length.ok_or_else(|| {
                            Error::arrow(
                                "Json conversion: FixedSizeList type requires a length".to_string(),
                            )
                        })?;
                        Ok(Self::FixedSizeList(
                            Arc::new(fields[0].clone()),
                            length as i32,
                        ))
                    }
                    "struct" => Ok(Self::Struct(fields.into())),
                    _ => unreachable!(),
                }
            }
            "fixed_size_binary" => {
                let length = value.length.ok_or_else(|| {
                    Error::arrow(
                        "Json conversion: FixedSizeBinary type requires a length".to_string(),
                    )
                })?;
                Ok(Self::FixedSizeBinary(length as i32))
            }
            _ => Err(Error::arrow(format!(
                "Json conversion: Unsupported type: {value:?}"
            ))),
        }
    }
}

/// JSON representation of an Apache Arrow [Field].
#[derive(Serialize, Deserialize, Debug)]
pub struct JsonField {
    name: String,
    #[serde(rename = "type")]
    type_: JsonDataType,
    nullable: bool,

    #[serde(skip_serializing_if = "Option::is_none")]
    metadata: Option<HashMap<String, String>>,
}

impl TryFrom<&Field> for JsonField {
    type Error = Error;

    fn try_from(field: &Field) -> Result<Self> {
        let data_type = JsonDataType::try_new(field.data_type())?;

        let metadata = if field.metadata().is_empty() {
            None
        } else {
            Some(field.metadata().clone())
        };

        Ok(Self {
            name: field.name().clone(),
            nullable: field.is_nullable(),
            type_: data_type,
            metadata,
        })
    }
}

impl TryFrom<&JsonField> for Field {
    type Error = Error;

    fn try_from(value: &JsonField) -> Result<Self> {
        let data_type = DataType::try_from(&value.type_)?;
        let mut field = Self::new(&value.name, data_type, value.nullable);
        if let Some(metadata) = value.metadata.clone() {
            field.set_metadata(metadata);
        }
        Ok(field)
    }
}

/// JSON representation of a Apache Arrow [Schema].
#[derive(Serialize, Deserialize, Debug)]
pub struct JsonSchema {
    fields: Vec<JsonField>,

    #[serde(skip_serializing_if = "Option::is_none")]
    metadata: Option<HashMap<String, String>>,
}

/// Convert Schema to JSON representation.
impl TryFrom<&Schema> for JsonSchema {
    type Error = Error;

    fn try_from(schema: &Schema) -> Result<Self> {
        let fields = schema
            .fields()
            .iter()
            .map(|f| JsonField::try_from(f.as_ref()))
            .collect::<Result<Vec<_>>>()?;

        let metadata = if schema.metadata.is_empty() {
            None
        } else {
            Some(schema.metadata.clone())
        };
        Ok(Self { fields, metadata })
    }
}

impl TryFrom<JsonSchema> for Schema {
    type Error = Error;

    fn try_from(json_schema: JsonSchema) -> Result<Self> {
        Self::try_from(&json_schema)
    }
}

impl TryFrom<&JsonSchema> for Schema {
    type Error = Error;

    fn try_from(json_schema: &JsonSchema) -> Result<Self> {
        let fields = json_schema
            .fields
            .iter()
            .map(Field::try_from)
            .collect::<Result<Vec<_>>>()?;

        let metadata = if let Some(metadata) = &json_schema.metadata {
            metadata.clone()
        } else {
            HashMap::new()
        };

        Ok(Self::new_with_metadata(fields, metadata))
    }
}

/// Conversion between Arrow [Schema] and JSON representation (string).
pub trait ArrowJsonExt: Sized {
    fn to_json(&self) -> Result<String>;

    fn from_json(json: &str) -> Result<Self>;
}

impl ArrowJsonExt for Schema {
    fn to_json(&self) -> Result<String> {
        let json_schema = JsonSchema::try_from(self)?;
        Ok(serde_json::to_string(&json_schema)?)
    }

    fn from_json(json: &str) -> Result<Self> {
        let json_schema: JsonSchema = serde_json::from_str(json)?;
        json_schema.try_into()
    }
}

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

    use arrow_schema::TimeUnit;
    use serde_json::{Value, json};

    fn assert_type_json_str(dt: DataType, val: Value) {
        assert_eq!(
            serde_json::from_str::<Value>(
                &serde_json::to_string(&JsonDataType::try_new(&dt).unwrap()).unwrap()
            )
            .unwrap(),
            val
        );
    }

    fn assert_primitive_types(dt: DataType, type_name: &str) {
        assert_type_json_str(dt, json!({ "type": type_name }));
    }

    #[test]
    fn test_data_type_to_json() {
        assert_primitive_types(DataType::Null, "null");
        assert_primitive_types(DataType::Boolean, "bool");
        assert_primitive_types(DataType::Int8, "int8");
        assert_primitive_types(DataType::Int16, "int16");
        assert_primitive_types(DataType::Int32, "int32");
        assert_primitive_types(DataType::Int64, "int64");
        assert_primitive_types(DataType::UInt8, "uint8");
        assert_primitive_types(DataType::UInt16, "uint16");
        assert_primitive_types(DataType::UInt32, "uint32");
        assert_primitive_types(DataType::UInt64, "uint64");
        assert_primitive_types(DataType::Float16, "halffloat");
        assert_primitive_types(DataType::Float32, "float");
        assert_primitive_types(DataType::Float64, "double");
        assert_primitive_types(DataType::Utf8, "string");
        assert_primitive_types(DataType::LargeUtf8, "large_string");
        assert_primitive_types(DataType::Binary, "binary");
        assert_primitive_types(DataType::LargeBinary, "large_binary");
        assert_primitive_types(DataType::Date32, "date32:day");
        assert_primitive_types(DataType::Date64, "date64:ms");
        assert_primitive_types(DataType::Time32(TimeUnit::Second), "time32:s");
        assert_primitive_types(DataType::Decimal128(38, 10), "decimal:128:38:10");
        assert_primitive_types(DataType::Decimal256(76, 20), "decimal:256:76:20");
        assert_primitive_types(DataType::Decimal128(18, 6), "decimal:128:18:6");
        assert_primitive_types(DataType::Decimal256(50, 15), "decimal:256:50:15");
    }

    #[test]
    fn test_complex_types_to_json() {
        assert_type_json_str(
            DataType::List(Arc::new(Field::new("item", DataType::Float32, false))),
            json!(
                {
                    "type": "list",
                    "fields": [
                        {
                            "name": "item",
                            "type": {
                                "type": "float"
                            },
                            "nullable": false
                        }
                    ]
                }
            ),
        );

        assert_type_json_str(
            DataType::FixedSizeList(Arc::new(Field::new("item", DataType::Float32, false)), 32),
            json!(
                {
                    "type": "fixed_size_list",
                    "fields": [
                        {
                            "name": "item",
                            "type": {
                                "type": "float"
                            },
                            "nullable": false
                        }
                    ],
                    "length": 32
                }
            ),
        );

        assert_type_json_str(
            DataType::FixedSizeBinary(32),
            json!({
                "type": "fixed_size_binary",
                "length": 32
            }),
        );

        assert_type_json_str(
            DataType::Struct(
                vec![
                    Field::new("a", DataType::Date32, false),
                    Field::new("b", DataType::Int32, true),
                ]
                .into(),
            ),
            json!({
                "type": "struct",
                "fields": [
                    {
                        "name": "a",
                        "type": {
                            "type": "date32:day"
                        },
                        "nullable": false
                    },
                    {
                        "name": "b",
                        "type": {
                            "type": "int32"
                        },
                        "nullable": true
                    }
                ]
            }),
        );
    }

    #[test]
    fn test_schema_to_json() {
        let schema = Schema::new(vec![
            Field::new("a", DataType::Date32, false),
            Field::new("b", DataType::Int32, true),
            Field::new(
                "s",
                DataType::Struct(vec![Field::new("str", DataType::Utf8, false)].into()),
                false,
            ),
        ]);

        let json_str = schema.to_json().unwrap();
        assert_eq!(
            serde_json::from_str::<Value>(&json_str).unwrap(),
            json!({
                "fields": [
                    {
                        "name": "a",
                        "type": {
                            "type": "date32:day"
                        },
                        "nullable": false
                    },
                    {
                        "name": "b",
                        "type": {
                            "type": "int32"
                        },
                        "nullable": true
                    },
                    {
                        "name": "s",
                        "type": {
                            "type": "struct",
                            "fields": [
                                {
                                    "name": "str",
                                    "type": {
                                        "type": "string"
                                    },
                                    "nullable": false
                                }
                            ]
                        },
                        "nullable": false
                    },
                ]
            })
        );

        let actual = Schema::from_json(&json_str).unwrap();
        assert_eq!(schema, actual);
    }

    #[test]
    fn test_metadata_roundtrip() {
        let mut schema_metadata = HashMap::new();
        schema_metadata.insert("sk_1".to_string(), "sv_1".to_string());

        let mut field1_metadata = HashMap::new();
        field1_metadata.insert("fk_1".to_string(), "fv_1".to_string());

        let field1 = Field::new("a", DataType::UInt8, false).with_metadata(field1_metadata.clone());
        let field2 = Field::new("b", DataType::Int32, true);

        let schema = Schema::new_with_metadata(vec![field1, field2], schema_metadata.clone());

        let json_str = schema.to_json().unwrap();
        assert_eq!(
            serde_json::from_str::<Value>(&json_str).unwrap(),
            json!({
                "fields": [
                    {
                        "name": "a",
                        "type": {
                            "type": "uint8"
                        },
                        "nullable": false,
                        "metadata": {
                            "fk_1": "fv_1"
                        }
                    },
                    {
                        "name": "b",
                        "type": {
                            "type": "int32"
                        },
                        "nullable": true
                    }
                ],
                "metadata": {
                    "sk_1": "sv_1"
                }
            })
        );

        let actual = Schema::from_json(&json_str).unwrap();
        assert_eq!(schema, actual);

        assert_eq!(actual.metadata, schema_metadata);

        assert_eq!(actual.field(0).metadata(), &field1_metadata);
        assert_eq!(actual.field(1).metadata(), &HashMap::new());
    }
}