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
//! # `serde_arrow` - convert sequences Rust objects to / from arrow arrays
//!
//! The arrow in-memory format is a powerful way to work with data frame like structures. However,
//! the API of the underlying Rust crates can be at times cumbersome to use due to the statically
//! typed nature of Rust. `serde_arrow`, offers a simple way to convert Rust objects into Arrow
//! arrays and back. `serde_arrow` relies on [Serde](https://serde.rs) to interpret Rust objects.
//! Therefore, adding support for `serde_arrow` to custom types is as easy as using Serde's derive
//! macros.
//!
//! `serde_arrow` mainly targets the [`arrow`](https://github.com/apache/arrow-rs) crate, but also
//! supports the deprecated [`arrow2`](https://github.com/jorgecarleitao/arrow2) crate. The arrow
//! implementations can be selected via [features](#features).
//!
//! `serde_arrow` relies on a schema to translate between Rust and Arrow as their type systems do
//! not directly match. The schema is expressed as a collection of Arrow fields with additional
//! metadata describing the arrays. E.g., to convert a vector of Rust strings representing
//! timestamps to an arrow `Timestamp` array, the schema should contain a field with data type
//! `Timestamp`. `serde_arrow` supports to derive the schema from the data or the Rust types
//! themselves via schema tracing, but does not require it. It is always possible to specify the
//! schema manually. See the [`schema` module][schema] and [`SchemaLike`][schema::SchemaLike] for
//! further details.
//!
//!
//! See also:
//!
//! - the [quickstart guide][_impl::docs::quickstart] for more examples of how to use this package
//! - the [status summary][_impl::docs::status] for an overview over the supported Arrow and Rust
//! constructs
//!
//! ## Example
//!
//! ```rust
//! # use serde::{Deserialize, Serialize};
//! # #[cfg(has_arrow)]
//! # fn main() -> serde_arrow::Result<()> {
//! # use serde_arrow::_impl::arrow;
//! use arrow::datatypes::FieldRef;
//! use serde_arrow::schema::{SchemaLike, TracingOptions};
//!
//! ##[derive(Serialize, Deserialize)]
//! struct Record {
//! a: f32,
//! b: i32,
//! }
//!
//! let records = vec![
//! Record { a: 1.0, b: 1 },
//! Record { a: 2.0, b: 2 },
//! Record { a: 3.0, b: 3 },
//! ];
//!
//! // Determine Arrow schema
//! let fields = Vec::<FieldRef>::from_type::<Record>(TracingOptions::default())?;
//!
//! // Build the record batch
//! let batch = serde_arrow::to_record_batch(&fields, &records)?;
//! # Ok(())
//! # }
//! # #[cfg(not(has_arrow))]
//! # fn main() { }
//! ```
//!
//! The `RecordBatch` can then be written to disk, e.g., as parquet using the [`ArrowWriter`] from
//! the [`parquet`] crate.
//!
//! [`ArrowWriter`]:
//! https://docs.rs/parquet/latest/parquet/arrow/arrow_writer/struct.ArrowWriter.html
//! [`parquet`]: https://docs.rs/parquet/latest/parquet/
//!
//! # Features:
//!
//! The version of `arrow` or `arrow2` used can be selected via features. Per default no arrow
//! implementation is used. In that case only the base features of `serde_arrow` are available.
//!
//! The `arrow-*` and `arrow2-*` feature groups are compatible with each other. I.e., it is possible
//! to use `arrow` and `arrow2` together. Within each group the highest version is selected, if
//! multiple features are activated. E.g, when selecting `arrow2-0-16` and `arrow2-0-17`,
//! `arrow2=0.17` will be used.
//!
//! Note that because the highest version is selected, the features are not additive. In particular,
//! it is not possible to use `serde_arrow::to_arrow` for multiple different `arrow` versions at the
//! same time. See the next section for how to use `serde_arrow` in library code.
//!
//! Available features:
//!
//! | Arrow Feature | Arrow Version |
//! |---------------|---------------|
// arrow-version:insert: //! | `arrow-{version}` | `arrow={version}` |
//! | `arrow-58` | `arrow=58` |
//! | `arrow-57` | `arrow=57` |
//! | `arrow-56` | `arrow=56` |
//! | `arrow-55` | `arrow=55` |
//! | `arrow-54` | `arrow=54` |
//! | `arrow-53` | `arrow=53` |
//! | `arrow-52` | `arrow=52` |
//! | `arrow-51` | `arrow=51` |
//! | `arrow-50` | `arrow=50` |
//! | `arrow-49` | `arrow=49` |
//! | `arrow-48` | `arrow=48` |
//! | `arrow-47` | `arrow=47` |
//! | `arrow-46` | `arrow=46` |
//! | `arrow-45` | `arrow=45` |
//! | `arrow-44` | `arrow=44` |
//! | `arrow-43` | `arrow=43` |
//! | `arrow-42` | `arrow=42` |
//! | `arrow-41` | `arrow=41` |
//! | `arrow-40` | `arrow=40` |
//! | `arrow-39` | `arrow=39` |
//! | `arrow-38` | `arrow=38` |
//! | `arrow-37` | `arrow=37` |
//! | `arrow2-0-17` | `arrow2=0.17` |
//! | `arrow2-0-16` | `arrow2=0.16` |
//!
//! # Usage in libraries
//!
//! In libraries, it is not recommended to use the `arrow` and `arrow2` functions directly. Rather
//! it is recommended to rely on the [`marrow`] based functionality, as the features of [`marrow`]
//! are designed to be strictly additive.
//!
//! For example to build a record batch, first build the corresponding marrow types and then use
//! them to build the record batch:
//!
//! ```rust
//! # use serde::{Deserialize, Serialize};
//! # fn main() -> serde_arrow::Result<()> {
//! # #[cfg(has_arrow)] {
//! # use serde_arrow::_impl::arrow;
//! # use std::sync::Arc;
//! # use serde_arrow::schema::{SchemaLike, TracingOptions};
//! #
//! # #[derive(Serialize, Deserialize)]
//! # struct Record {
//! # a: f32,
//! # b: i32,
//! # }
//! #
//! # let records = vec![
//! # Record { a: 1.0, b: 1 },
//! # Record { a: 2.0, b: 2 },
//! # Record { a: 3.0, b: 3 },
//! # ];
//! #
//! // Determine Arrow schema
//! let fields = Vec::<marrow::datatypes::Field>::from_type::<Record>(TracingOptions::default())?;
//!
//! // Build the marrow arrays
//! let arrays = serde_arrow::to_marrow(&fields, &records)?;
//!
//! // Build the record batch
//! let arrow_fields = fields.iter()
//! .map(arrow::datatypes::Field::try_from)
//! .collect::<Result<Vec<_>, _>>()?;
//!
//! let arrow_arrays = arrays.into_iter()
//! .map(arrow::array::ArrayRef::try_from)
//! .collect::<Result<Vec<_>, _>>()?;
//!
//! let record_batch = arrow::array::RecordBatch::try_new(
//! Arc::new(arrow::datatypes::Schema::new(arrow_fields)),
//! arrow_arrays,
//! );
//! # }
//! # Ok(())
//! # }
//! ```
// be more forgiving without any active implementation
/// *Internal. Do not use*
///
/// This module is an internal implementation detail and not subject to any
/// compatibility promises. It re-exports the arrow impls selected via features
/// to allow usage in doc tests or benchmarks.
///
pub use crate;
pub use crateDeserializer;
pub use crateSerializer;
pub use crateArrayBuilder;
pub use ;
pub use ;
pub use ;
/// Helpers that may be useful when using `serde_arrow`
/// Deserialization of items
/// Type mapping between Rust, Serde, and Arrow
///
/// `serde_arrow` bridges three distinct type systems: Rust types, the actual
/// types in your Rust code (`Vec<T>`, structs, enums, etc.),
/// [Serde data model][serde-model], the abstract representation Serde uses
/// during serialization, and [Arrow types][arrow-model], the columnar data
/// types defined by Apache Arrow. To convert between thse type systems ,
/// `serde_arrow` requires schema information as a list of Arrow fields with
/// additional metadata. See [`SchemaLike`][crate::schema::SchemaLike] for
/// details on how to specify the schema.
///
///
/// In most cases, `serde_arrow` expects data as a sequence of records:
///
/// ```rust
/// # struct Record { f0: i32, f1: i32 }
/// # let (v0, v1, v2, v3) = (0_i32, 1_i32, 2_i32, 3_i32);
/// vec![
/// Record { f0: v0, f1: v1 },
/// Record { f0: v2, f1: v3 },
/// // ..
/// ]
/// # ;
/// ```
///
/// The outer container must be one of these [Serde data types][serde-model]:
///
/// | Serde data type | Example Rust types | Comment |
/// |---|---|---|
/// |`seq` | [`Vec<T>`][std::vec::Vec], `&[T]` | variable-sized sequences |
/// | `tuple`, `tuple_struct`, `tuple_variant` | `(T0, T1)`, `[T; N]`, `struct S(T0, T1)`) | fixed-sized sequences|
/// | `newtype_struct`, `newtype_variant` | `struct S(T)`, `enum E { V(T) }` | wrappers around the preceding types |
///
/// Each record must be one of these Serde data types:
///
/// | Serde data type | Example Rust types | Comment |
/// |---|---|---|
/// | `struct`, `struct_variant` | `struct S { f0: T0, f1: T1 }` | named fields |
/// | `map` | [`HashMap<K, V>`][std::collections::HashMap], [`BTreeMap<K, V>`][std::collections::BTreeMap] | key-value pairs |
/// | `seq`, `tuple`, `tuple_struct`, `tuple_variant` | `(T0, T1)`, `[T; N]` | ordered fields |
/// | `newtype_struct`, `newtype_variant` | `struct S(T)` | wrappers around the preceding types |
///
/// Schema fields and struct fields do not have to be specified in the same
/// order, but matching order improves lookup performance. Missing schema
/// fields are serialized as null. Extra struct fields are ignored. Maps follow
/// the same semantics.
///
/// The following table shows how [Serde data types][serde-model], Rust types,
/// and [Arrow types][arrow-model] map to each other:
///
///
/// | Serde data type | Example Rust types | Default Arrow type |
/// |------------------|-------------------|------------|
/// | `unit` | `()` | `Null` |
/// | `bool` | `bool` | `Boolean` |
/// | `i8`, `i16`, `i32`, `i64` | `i8`, `i16`, `i32`, `i64` | `Int8`, `Int16`, `Int32`, `Int64` |
/// | `u8`, `u16`, `u32`, `u64` | `u8`, `u16`, `u32`, `u64` | `UInt8`, `UInt16`, `UInt32`, `UInt64` |
/// | `char` | `char` | `UInt32` |
/// | `bytes` | | `LargeBinary` |
/// | `f32`, `f64` | `f32`, `f64` | `Float32`, `Float64` |
/// | `str` | `str`, `String`, `&str` | `LargeUtf8` |
/// | `seq` | `Vec<T>`, `&[T]` | `LargeList` |
/// | `struct`, `tuple`, `tuple_struct` | `struct S { .. }`, `(T0, T1)` | `Struct` |
/// | `map` | [`HashMap<K, V>`][std::collections::HashMap], [`BTreeMap<K, V>`][std::collections::BTreeMap] | `Map` |
/// | `unit_variant`, `struct_variant`, `tuple_variant`, `newtype_variant` | `enum E { .. }` | Dense `Union` |
///
///
/// Enums are mapped to dense Arrow `Union` types, with each variant becoming a separate field:
///
/// - Unit variants (`V`) map to the `Null` Arrow type, but can also be serialized as arrow string types
/// - Newtype variants (`V(T)`) map to the inner type `T`
/// - Tuple variants or struct variants (`V(T0, T1)`, `V { f0: T0 }`) map to the Arrow `Struct` type
///
/// [serde-model]: https://serde.rs/data-model.html
/// [arrow-model]: https://arrow.apache.org/docs/format/Columnar.html
/// Re-export of the used marrow version
pub use marrow;