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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Extension types.
pub use *;
use crate::;
use HashMap;
/// The metadata key for the string name identifying an [`ExtensionType`].
pub const EXTENSION_TYPE_NAME_KEY: &str = "ARROW:extension:name";
/// The metadata key for a serialized representation of the [`ExtensionType`]
/// necessary to reconstruct the custom type.
pub const EXTENSION_TYPE_METADATA_KEY: &str = "ARROW:extension:metadata";
/// Extension types.
///
/// User-defined “extension” types can be defined setting certain key value
/// pairs in the [`Field`] metadata structure. These extension keys are:
/// - [`EXTENSION_TYPE_NAME_KEY`]
/// - [`EXTENSION_TYPE_METADATA_KEY`]
///
/// Canonical extension types support in this crate requires the
/// `canonical_extension_types` feature.
///
/// Extension types may or may not use the [`EXTENSION_TYPE_METADATA_KEY`]
/// field.
///
/// # Example
///
/// The example below demonstrates how to implement this trait for a `Uuid`
/// type. Note this is not the canonical extension type for `Uuid`, which does
/// not include information about the `Uuid` version.
///
/// ```
/// # use arrow_schema::ArrowError;
/// # fn main() -> Result<(), ArrowError> {
/// use arrow_schema::{DataType, extension::ExtensionType, Field};
/// use std::{fmt, str::FromStr};
///
/// /// The different Uuid versions.
/// #[derive(Clone, Copy, Debug, PartialEq)]
/// enum UuidVersion {
/// V1,
/// V2,
/// V3,
/// V4,
/// V5,
/// V6,
/// V7,
/// V8,
/// }
///
/// // We'll use `Display` to serialize.
/// impl fmt::Display for UuidVersion {
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// write!(
/// f,
/// "{}",
/// match self {
/// Self::V1 => "V1",
/// Self::V2 => "V2",
/// Self::V3 => "V3",
/// Self::V4 => "V4",
/// Self::V5 => "V5",
/// Self::V6 => "V6",
/// Self::V7 => "V7",
/// Self::V8 => "V8",
/// }
/// )
/// }
/// }
///
/// // And `FromStr` to deserialize.
/// impl FromStr for UuidVersion {
/// type Err = ArrowError;
///
/// fn from_str(s: &str) -> Result<Self, Self::Err> {
/// match s {
/// "V1" => Ok(Self::V1),
/// "V2" => Ok(Self::V2),
/// "V3" => Ok(Self::V3),
/// "V4" => Ok(Self::V4),
/// "V5" => Ok(Self::V5),
/// "V6" => Ok(Self::V6),
/// "V7" => Ok(Self::V7),
/// "V8" => Ok(Self::V8),
/// _ => Err(ArrowError::ParseError("Invalid UuidVersion".to_owned())),
/// }
/// }
/// }
///
/// /// This is the extension type, not the container for Uuid values. It
/// /// stores the Uuid version (this is the metadata of this extension type).
/// #[derive(Clone, Copy, Debug, PartialEq)]
/// struct Uuid(UuidVersion);
///
/// impl ExtensionType for Uuid {
/// // We use a namespace as suggested by the specification.
/// const NAME: &'static str = "myorg.example.uuid";
///
/// // The metadata type is the Uuid version.
/// type Metadata = UuidVersion;
///
/// // We just return a reference to the Uuid version.
/// fn metadata(&self) -> &Self::Metadata {
/// &self.0
/// }
///
/// // We use the `Display` implementation to serialize the Uuid
/// // version.
/// fn serialize_metadata(&self) -> Option<String> {
/// Some(self.0.to_string())
/// }
///
/// // We use the `FromStr` implementation to deserialize the Uuid
/// // version.
/// fn deserialize_metadata(metadata: Option<&str>) -> Result<Self::Metadata, ArrowError> {
/// metadata.map_or_else(
/// || {
/// Err(ArrowError::InvalidArgumentError(
/// "Uuid extension type metadata missing".to_owned(),
/// ))
/// },
/// str::parse,
/// )
/// }
///
/// // The only supported data type is `FixedSizeBinary(16)`.
/// fn supports_data_type(&self, data_type: &DataType) -> Result<(), ArrowError> {
/// match data_type {
/// DataType::FixedSizeBinary(16) => Ok(()),
/// data_type => Err(ArrowError::InvalidArgumentError(format!(
/// "Uuid data type mismatch, expected FixedSizeBinary(16), found {data_type}"
/// ))),
/// }
/// }
///
/// // We should always check if the data type is supported before
/// // constructing the extension type.
/// fn try_new(data_type: &DataType, metadata: Self::Metadata) -> Result<Self, ArrowError> {
/// let uuid = Self(metadata);
/// uuid.supports_data_type(data_type)?;
/// Ok(uuid)
/// }
/// }
///
/// // We can now construct the extension type.
/// let uuid_v1 = Uuid(UuidVersion::V1);
///
/// // And add it to a field.
/// let mut field =
/// Field::new("", DataType::FixedSizeBinary(16), false).with_extension_type(uuid_v1);
///
/// // And extract it from this field.
/// assert_eq!(field.try_extension_type::<Uuid>()?, uuid_v1);
///
/// // When we try to add this to a field with an unsupported data type we
/// // get an error.
/// let result = Field::new("", DataType::Null, false).try_with_extension_type(uuid_v1);
/// assert!(result.is_err());
/// # Ok(()) }
/// ```
///
/// <https://arrow.apache.org/docs/format/Columnar.html#extension-types>
///
/// [`Field`]: crate::Field