arrow_schema/datatype.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt;
19use std::str::FromStr;
20use std::sync::Arc;
21
22use crate::{ArrowError, Field, FieldRef, Fields, UnionFields};
23
24/// Datatypes supported by this implementation of Apache Arrow.
25///
26/// The variants of this enum include primitive fixed size types as well as
27/// parametric or nested types. See [`Schema.fbs`] for Arrow's specification.
28///
29/// # Examples
30///
31/// Primitive types
32/// ```
33/// # use arrow_schema::DataType;
34/// // create a new 32-bit signed integer
35/// let data_type = DataType::Int32;
36/// ```
37///
38/// Nested Types
39/// ```
40/// # use arrow_schema::{DataType, Field};
41/// # use std::sync::Arc;
42/// // create a new list of 32-bit signed integers directly
43/// let list_data_type = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
44/// // Create the same list type with constructor
45/// let list_data_type2 = DataType::new_list(DataType::Int32, true);
46/// assert_eq!(list_data_type, list_data_type2);
47/// ```
48///
49/// Dictionary Types
50/// ```
51/// # use arrow_schema::{DataType};
52/// // String Dictionary (key type Int32 and value type Utf8)
53/// let data_type = DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8));
54/// ```
55///
56/// Timestamp Types
57/// ```
58/// # use arrow_schema::{DataType, TimeUnit};
59/// // timestamp with millisecond precision without timezone specified
60/// let data_type = DataType::Timestamp(TimeUnit::Millisecond, None);
61/// // timestamp with nanosecond precision in UTC timezone
62/// let data_type = DataType::Timestamp(TimeUnit::Nanosecond, Some("UTC".into()));
63///```
64///
65/// # Display and FromStr
66///
67/// The `Display` and `FromStr` implementations for `DataType` are
68/// human-readable, parseable, and reversible.
69///
70/// ```
71/// # use arrow_schema::DataType;
72/// let data_type = DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8));
73/// let data_type_string = data_type.to_string();
74/// assert_eq!(data_type_string, "Dictionary(Int32, Utf8)");
75/// // display can be parsed back into the original type
76/// let parsed_data_type: DataType = data_type.to_string().parse().unwrap();
77/// assert_eq!(data_type, parsed_data_type);
78/// ```
79///
80/// # Nested Support
81/// Currently, the Rust implementation supports the following nested types:
82/// - `List<T>`
83/// - `LargeList<T>`
84/// - `FixedSizeList<T>`
85/// - `Struct<T, U, V, ...>`
86/// - `Union<T, U, V, ...>`
87/// - `Map<K, V>`
88///
89/// Nested types can themselves be nested within other arrays.
90/// For more information on these types please see
91/// [the physical memory layout of Apache Arrow]
92///
93/// [`Schema.fbs`]: https://github.com/apache/arrow/blob/main/format/Schema.fbs
94/// [the physical memory layout of Apache Arrow]: https://arrow.apache.org/docs/format/Columnar.html#physical-memory-layout
95#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
96#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
97pub enum DataType {
98 /// Null type
99 Null,
100 /// A boolean datatype representing the values `true` and `false`.
101 Boolean,
102 /// A signed 8-bit integer.
103 Int8,
104 /// A signed 16-bit integer.
105 Int16,
106 /// A signed 32-bit integer.
107 Int32,
108 /// A signed 64-bit integer.
109 Int64,
110 /// An unsigned 8-bit integer.
111 UInt8,
112 /// An unsigned 16-bit integer.
113 UInt16,
114 /// An unsigned 32-bit integer.
115 UInt32,
116 /// An unsigned 64-bit integer.
117 UInt64,
118 /// A 16-bit floating point number.
119 Float16,
120 /// A 32-bit floating point number.
121 Float32,
122 /// A 64-bit floating point number.
123 Float64,
124 /// A timestamp with an optional timezone.
125 ///
126 /// Time is measured as a Unix epoch, counting the seconds from
127 /// 00:00:00.000 on 1 January 1970, excluding leap seconds,
128 /// as a signed 64-bit integer.
129 ///
130 /// The time zone is a string indicating the name of a time zone, one of:
131 ///
132 /// * As used in the Olson time zone database (the "tz database" or
133 /// "tzdata"), such as "America/New_York"
134 /// * An absolute time zone offset of the form +XX:XX or -XX:XX, such as +07:30
135 ///
136 /// Timestamps with a non-empty timezone
137 /// ------------------------------------
138 ///
139 /// If a Timestamp column has a non-empty timezone value, its epoch is
140 /// 1970-01-01 00:00:00 (January 1st 1970, midnight) in the *UTC* timezone
141 /// (the Unix epoch), regardless of the Timestamp's own timezone.
142 ///
143 /// Therefore, timestamp values with a non-empty timezone correspond to
144 /// physical points in time together with some additional information about
145 /// how the data was obtained and/or how to display it (the timezone).
146 ///
147 /// For example, the timestamp value 0 with the timezone string "Europe/Paris"
148 /// corresponds to "January 1st 1970, 00h00" in the UTC timezone, but the
149 /// application may prefer to display it as "January 1st 1970, 01h00" in
150 /// the Europe/Paris timezone (which is the same physical point in time).
151 ///
152 /// One consequence is that timestamp values with a non-empty timezone
153 /// can be compared and ordered directly, since they all share the same
154 /// well-known point of reference (the Unix epoch).
155 ///
156 /// Timestamps with an unset / empty timezone
157 /// -----------------------------------------
158 ///
159 /// If a Timestamp column has no timezone value, its epoch is
160 /// 1970-01-01 00:00:00 (January 1st 1970, midnight) in an *unknown* timezone.
161 ///
162 /// Therefore, timestamp values without a timezone cannot be meaningfully
163 /// interpreted as physical points in time, but only as calendar / clock
164 /// indications ("wall clock time") in an unspecified timezone.
165 ///
166 /// For example, the timestamp value 0 with an empty timezone string
167 /// corresponds to "January 1st 1970, 00h00" in an unknown timezone: there
168 /// is not enough information to interpret it as a well-defined physical
169 /// point in time.
170 ///
171 /// One consequence is that timestamp values without a timezone cannot
172 /// be reliably compared or ordered, since they may have different points of
173 /// reference. In particular, it is *not* possible to interpret an unset
174 /// or empty timezone as the same as "UTC".
175 ///
176 /// Conversion between timezones
177 /// ----------------------------
178 ///
179 /// If a Timestamp column has a non-empty timezone, changing the timezone
180 /// to a different non-empty value is a metadata-only operation:
181 /// the timestamp values need not change as their point of reference remains
182 /// the same (the Unix epoch).
183 ///
184 /// However, if a Timestamp column has no timezone value, changing it to a
185 /// non-empty value requires to think about the desired semantics.
186 /// One possibility is to assume that the original timestamp values are
187 /// relative to the epoch of the timezone being set; timestamp values should
188 /// then adjusted to the Unix epoch (for example, changing the timezone from
189 /// empty to "Europe/Paris" would require converting the timestamp values
190 /// from "Europe/Paris" to "UTC", which seems counter-intuitive but is
191 /// nevertheless correct).
192 ///
193 /// ```
194 /// # use arrow_schema::{DataType, TimeUnit};
195 /// DataType::Timestamp(TimeUnit::Second, None);
196 /// DataType::Timestamp(TimeUnit::Second, Some("literal".into()));
197 /// DataType::Timestamp(TimeUnit::Second, Some("string".to_string().into()));
198 /// ```
199 ///
200 /// # Timezone representation
201 /// ----------------------------
202 /// It is possible to use either the timezone string representation, such as "UTC", or the absolute time zone offset "+00:00".
203 /// For timezones with fixed offsets, such as "UTC" or "JST", the offset representation is recommended, as it is more explicit and less ambiguous.
204 ///
205 /// Most arrow-rs functionalities use the absolute offset representation,
206 /// such as [`PrimitiveArray::with_timezone_utc`] that applies a
207 /// UTC timezone to timestamp arrays.
208 ///
209 /// [`PrimitiveArray::with_timezone_utc`]: https://docs.rs/arrow/latest/arrow/array/struct.PrimitiveArray.html#method.with_timezone_utc
210 ///
211 /// Timezone string parsing
212 /// -----------------------
213 /// When feature `chrono-tz` is not enabled, allowed timezone strings are fixed offsets of the form "+09:00", "-09" or "+0930".
214 ///
215 /// When feature `chrono-tz` is enabled, additional strings supported by [chrono_tz](https://docs.rs/chrono-tz/latest/chrono_tz/)
216 /// are also allowed, which include [IANA database](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
217 /// timezones.
218 Timestamp(TimeUnit, Option<Arc<str>>),
219 /// A signed 32-bit date representing the elapsed time since UNIX epoch (1970-01-01)
220 /// in days.
221 Date32,
222 /// A signed 64-bit date representing the elapsed time since UNIX epoch (1970-01-01)
223 /// in milliseconds.
224 ///
225 /// # Valid Ranges
226 ///
227 /// According to the Arrow specification ([Schema.fbs]), values of Date64
228 /// are treated as the number of *days*, in milliseconds, since the UNIX
229 /// epoch. Therefore, values of this type must be evenly divisible by
230 /// `86_400_000`, the number of milliseconds in a standard day.
231 ///
232 /// It is not valid to store milliseconds that do not represent an exact
233 /// day. The reason for this restriction is compatibility with other
234 /// language's native libraries (specifically Java), which historically
235 /// lacked a dedicated date type and only supported timestamps.
236 ///
237 /// # Validation
238 ///
239 /// This library does not validate or enforce that Date64 values are evenly
240 /// divisible by `86_400_000` for performance and usability reasons. Date64
241 /// values are treated similarly to `Timestamp(TimeUnit::Millisecond,
242 /// None)`: values will be displayed with a time of day if the value does
243 /// not represent an exact day, and arithmetic will be done at the
244 /// millisecond granularity.
245 ///
246 /// # Recommendation
247 ///
248 /// Users should prefer [`Date32`] to cleanly represent the number
249 /// of days, or one of the Timestamp variants to include time as part of the
250 /// representation, depending on their use case.
251 ///
252 /// # Further Reading
253 ///
254 /// For more details, see [#5288](https://github.com/apache/arrow-rs/issues/5288).
255 ///
256 /// [`Date32`]: Self::Date32
257 /// [Schema.fbs]: https://github.com/apache/arrow/blob/main/format/Schema.fbs
258 Date64,
259 /// A signed 32-bit time representing the elapsed time since midnight in the unit of `TimeUnit`.
260 /// Must be either seconds or milliseconds.
261 Time32(TimeUnit),
262 /// A signed 64-bit time representing the elapsed time since midnight in the unit of `TimeUnit`.
263 /// Must be either microseconds or nanoseconds.
264 Time64(TimeUnit),
265 /// Measure of elapsed time in either seconds, milliseconds, microseconds or nanoseconds.
266 Duration(TimeUnit),
267 /// A "calendar" interval which models types that don't necessarily
268 /// have a precise duration without the context of a base timestamp (e.g.
269 /// days can differ in length during day light savings time transitions).
270 Interval(IntervalUnit),
271 /// Opaque binary data of variable length.
272 ///
273 /// A single Binary array can store up to [`i32::MAX`] bytes
274 /// of binary data in total.
275 Binary,
276 /// Opaque binary data of fixed size.
277 /// Enum parameter specifies the number of bytes per value.
278 FixedSizeBinary(i32),
279 /// Opaque binary data of variable length and 64-bit offsets.
280 ///
281 /// A single LargeBinary array can store up to [`i64::MAX`] bytes
282 /// of binary data in total.
283 LargeBinary,
284 /// Opaque binary data of variable length.
285 ///
286 /// Logically the same as [`Binary`], but the internal representation uses a view
287 /// struct that contains the string length and either the string's entire data
288 /// inline (for small strings) or an inlined prefix, an index of another buffer,
289 /// and an offset pointing to a slice in that buffer (for non-small strings).
290 ///
291 /// [`Binary`]: Self::Binary
292 BinaryView,
293 /// A variable-length string in Unicode with UTF-8 encoding.
294 ///
295 /// A single Utf8 array can store up to [`i32::MAX`] bytes
296 /// of string data in total.
297 Utf8,
298 /// A variable-length string in Unicode with UFT-8 encoding and 64-bit offsets.
299 ///
300 /// A single LargeUtf8 array can store up to [`i64::MAX`] bytes
301 /// of string data in total.
302 LargeUtf8,
303 /// A variable-length string in Unicode with UTF-8 encoding
304 ///
305 /// Logically the same as [`Utf8`], but the internal representation uses a view
306 /// struct that contains the string length and either the string's entire data
307 /// inline (for small strings) or an inlined prefix, an index of another buffer,
308 /// and an offset pointing to a slice in that buffer (for non-small strings).
309 ///
310 /// [`Utf8`]: Self::Utf8
311 Utf8View,
312 /// A list of some logical data type with variable length.
313 ///
314 /// A single List array can store up to [`i32::MAX`] elements in total.
315 List(FieldRef),
316
317 /// (NOT YET FULLY SUPPORTED) A list of some logical data type with variable length.
318 ///
319 /// Logically the same as [`List`], but the internal representation differs in how child
320 /// data is referenced, allowing flexibility in how data is layed out.
321 ///
322 /// Note this data type is not yet fully supported. Using it with arrow APIs may result in `panic`s.
323 ///
324 /// [`List`]: Self::List
325 ListView(FieldRef),
326 /// A list of some logical data type with fixed length.
327 FixedSizeList(FieldRef, i32),
328 /// A list of some logical data type with variable length and 64-bit offsets.
329 ///
330 /// A single LargeList array can store up to [`i64::MAX`] elements in total.
331 LargeList(FieldRef),
332
333 /// (NOT YET FULLY SUPPORTED) A list of some logical data type with variable length and 64-bit offsets.
334 ///
335 /// Logically the same as [`LargeList`], but the internal representation differs in how child
336 /// data is referenced, allowing flexibility in how data is layed out.
337 ///
338 /// Note this data type is not yet fully supported. Using it with arrow APIs may result in `panic`s.
339 ///
340 /// [`LargeList`]: Self::LargeList
341 LargeListView(FieldRef),
342 /// A nested datatype that contains a number of sub-fields.
343 Struct(Fields),
344 /// A nested datatype that can represent slots of differing types. Components:
345 ///
346 /// 1. [`UnionFields`]
347 /// 2. The type of union (Sparse or Dense)
348 Union(UnionFields, UnionMode),
349 /// A dictionary encoded array (`key_type`, `value_type`), where
350 /// each array element is an index of `key_type` into an
351 /// associated dictionary of `value_type`.
352 ///
353 /// Dictionary arrays are used to store columns of `value_type`
354 /// that contain many repeated values using less memory, but with
355 /// a higher CPU overhead for some operations.
356 ///
357 /// This type mostly used to represent low cardinality string
358 /// arrays or a limited set of primitive types as integers.
359 Dictionary(Box<DataType>, Box<DataType>),
360 /// Exact 128-bit width decimal value with precision and scale
361 ///
362 /// * precision is the total number of digits
363 /// * scale is the number of digits past the decimal
364 ///
365 /// For example the number 123.45 has precision 5 and scale 2.
366 ///
367 /// In certain situations, scale could be negative number. For
368 /// negative scale, it is the number of padding 0 to the right
369 /// of the digits.
370 ///
371 /// For example the number 12300 could be treated as a decimal
372 /// has precision 3 and scale -2.
373 Decimal128(u8, i8),
374 /// Exact 256-bit width decimal value with precision and scale
375 ///
376 /// * precision is the total number of digits
377 /// * scale is the number of digits past the decimal
378 ///
379 /// For example the number 123.45 has precision 5 and scale 2.
380 ///
381 /// In certain situations, scale could be negative number. For
382 /// negative scale, it is the number of padding 0 to the right
383 /// of the digits.
384 ///
385 /// For example the number 12300 could be treated as a decimal
386 /// has precision 3 and scale -2.
387 Decimal256(u8, i8),
388 /// A Map is a logical nested type that is represented as
389 ///
390 /// `List<entries: Struct<key: K, value: V>>`
391 ///
392 /// The keys and values are each respectively contiguous.
393 /// The key and value types are not constrained, but keys should be
394 /// hashable and unique.
395 /// Whether the keys are sorted can be set in the `bool` after the `Field`.
396 ///
397 /// In a field with Map type, the field has a child Struct field, which then
398 /// has two children: key type and the second the value type. The names of the
399 /// child fields may be respectively "entries", "key", and "value", but this is
400 /// not enforced.
401 Map(FieldRef, bool),
402 /// A run-end encoding (REE) is a variation of run-length encoding (RLE). These
403 /// encodings are well-suited for representing data containing sequences of the
404 /// same value, called runs. Each run is represented as a value and an integer giving
405 /// the index in the array where the run ends.
406 ///
407 /// A run-end encoded array has no buffers by itself, but has two child arrays. The
408 /// first child array, called the run ends array, holds either 16, 32, or 64-bit
409 /// signed integers. The actual values of each run are held in the second child array.
410 ///
411 /// These child arrays are prescribed the standard names of "run_ends" and "values"
412 /// respectively.
413 RunEndEncoded(FieldRef, FieldRef),
414}
415
416/// An absolute length of time in seconds, milliseconds, microseconds or nanoseconds.
417#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
418#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
419pub enum TimeUnit {
420 /// Time in seconds.
421 Second,
422 /// Time in milliseconds.
423 Millisecond,
424 /// Time in microseconds.
425 Microsecond,
426 /// Time in nanoseconds.
427 Nanosecond,
428}
429
430/// YEAR_MONTH, DAY_TIME, MONTH_DAY_NANO interval in SQL style.
431#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
432#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
433pub enum IntervalUnit {
434 /// Indicates the number of elapsed whole months, stored as 4-byte integers.
435 YearMonth,
436 /// Indicates the number of elapsed days and milliseconds,
437 /// stored as 2 contiguous 32-bit integers (days, milliseconds) (8-bytes in total).
438 DayTime,
439 /// A triple of the number of elapsed months, days, and nanoseconds.
440 /// The values are stored contiguously in 16 byte blocks. Months and
441 /// days are encoded as 32 bit integers and nanoseconds is encoded as a
442 /// 64 bit integer. All integers are signed. Each field is independent
443 /// (e.g. there is no constraint that nanoseconds have the same sign
444 /// as days or that the quantity of nanoseconds represents less
445 /// than a day's worth of time).
446 MonthDayNano,
447}
448
449/// Sparse or Dense union layouts
450#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Copy)]
451#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
452pub enum UnionMode {
453 /// Sparse union layout
454 Sparse,
455 /// Dense union layout
456 Dense,
457}
458
459impl fmt::Display for DataType {
460 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
461 write!(f, "{self:?}")
462 }
463}
464
465/// Parses `str` into a `DataType`.
466///
467/// This is the reverse of [`DataType`]'s `Display`
468/// impl, and maintains the invariant that
469/// `DataType::try_from(&data_type.to_string()).unwrap() == data_type`
470///
471/// # Example
472/// ```
473/// use arrow_schema::DataType;
474///
475/// let data_type: DataType = "Int32".parse().unwrap();
476/// assert_eq!(data_type, DataType::Int32);
477/// ```
478impl FromStr for DataType {
479 type Err = ArrowError;
480
481 fn from_str(s: &str) -> Result<Self, Self::Err> {
482 crate::datatype_parse::parse_data_type(s)
483 }
484}
485
486impl TryFrom<&str> for DataType {
487 type Error = ArrowError;
488
489 fn try_from(value: &str) -> Result<Self, Self::Error> {
490 value.parse()
491 }
492}
493
494impl DataType {
495 /// Returns true if the type is primitive: (numeric, temporal).
496 #[inline]
497 pub fn is_primitive(&self) -> bool {
498 self.is_numeric() || self.is_temporal()
499 }
500
501 /// Returns true if this type is numeric: (UInt*, Int*, Float*, Decimal*).
502 #[inline]
503 pub fn is_numeric(&self) -> bool {
504 use DataType::*;
505 matches!(
506 self,
507 UInt8
508 | UInt16
509 | UInt32
510 | UInt64
511 | Int8
512 | Int16
513 | Int32
514 | Int64
515 | Float16
516 | Float32
517 | Float64
518 | Decimal128(_, _)
519 | Decimal256(_, _)
520 )
521 }
522
523 /// Returns true if this type is temporal: (Date*, Time*, Duration, or Interval).
524 #[inline]
525 pub fn is_temporal(&self) -> bool {
526 use DataType::*;
527 matches!(
528 self,
529 Date32 | Date64 | Timestamp(_, _) | Time32(_) | Time64(_) | Duration(_) | Interval(_)
530 )
531 }
532
533 /// Returns true if this type is floating: (Float*).
534 #[inline]
535 pub fn is_floating(&self) -> bool {
536 use DataType::*;
537 matches!(self, Float16 | Float32 | Float64)
538 }
539
540 /// Returns true if this type is integer: (Int*, UInt*).
541 #[inline]
542 pub fn is_integer(&self) -> bool {
543 self.is_signed_integer() || self.is_unsigned_integer()
544 }
545
546 /// Returns true if this type is signed integer: (Int*).
547 #[inline]
548 pub fn is_signed_integer(&self) -> bool {
549 use DataType::*;
550 matches!(self, Int8 | Int16 | Int32 | Int64)
551 }
552
553 /// Returns true if this type is unsigned integer: (UInt*).
554 #[inline]
555 pub fn is_unsigned_integer(&self) -> bool {
556 use DataType::*;
557 matches!(self, UInt8 | UInt16 | UInt32 | UInt64)
558 }
559
560 /// Returns true if this type is valid as a dictionary key
561 #[inline]
562 pub fn is_dictionary_key_type(&self) -> bool {
563 self.is_integer()
564 }
565
566 /// Returns true if this type is valid for run-ends array in RunArray
567 #[inline]
568 pub fn is_run_ends_type(&self) -> bool {
569 use DataType::*;
570 matches!(self, Int16 | Int32 | Int64)
571 }
572
573 /// Returns true if this type is nested (List, FixedSizeList, LargeList, ListView. LargeListView, Struct, Union,
574 /// or Map), or a dictionary of a nested type
575 #[inline]
576 pub fn is_nested(&self) -> bool {
577 use DataType::*;
578 match self {
579 Dictionary(_, v) => DataType::is_nested(v.as_ref()),
580 List(_)
581 | FixedSizeList(_, _)
582 | LargeList(_)
583 | ListView(_)
584 | LargeListView(_)
585 | Struct(_)
586 | Union(_, _)
587 | Map(_, _) => true,
588 _ => false,
589 }
590 }
591
592 /// Returns true if this type is DataType::Null.
593 #[inline]
594 pub fn is_null(&self) -> bool {
595 use DataType::*;
596 matches!(self, Null)
597 }
598
599 /// Compares the datatype with another, ignoring nested field names
600 /// and metadata.
601 pub fn equals_datatype(&self, other: &DataType) -> bool {
602 match (&self, other) {
603 (DataType::List(a), DataType::List(b))
604 | (DataType::LargeList(a), DataType::LargeList(b))
605 | (DataType::ListView(a), DataType::ListView(b))
606 | (DataType::LargeListView(a), DataType::LargeListView(b)) => {
607 a.is_nullable() == b.is_nullable() && a.data_type().equals_datatype(b.data_type())
608 }
609 (DataType::FixedSizeList(a, a_size), DataType::FixedSizeList(b, b_size)) => {
610 a_size == b_size
611 && a.is_nullable() == b.is_nullable()
612 && a.data_type().equals_datatype(b.data_type())
613 }
614 (DataType::Struct(a), DataType::Struct(b)) => {
615 a.len() == b.len()
616 && a.iter().zip(b).all(|(a, b)| {
617 a.is_nullable() == b.is_nullable()
618 && a.data_type().equals_datatype(b.data_type())
619 })
620 }
621 (DataType::Map(a_field, a_is_sorted), DataType::Map(b_field, b_is_sorted)) => {
622 a_field.is_nullable() == b_field.is_nullable()
623 && a_field.data_type().equals_datatype(b_field.data_type())
624 && a_is_sorted == b_is_sorted
625 }
626 (DataType::Dictionary(a_key, a_value), DataType::Dictionary(b_key, b_value)) => {
627 a_key.equals_datatype(b_key) && a_value.equals_datatype(b_value)
628 }
629 (
630 DataType::RunEndEncoded(a_run_ends, a_values),
631 DataType::RunEndEncoded(b_run_ends, b_values),
632 ) => {
633 a_run_ends.is_nullable() == b_run_ends.is_nullable()
634 && a_run_ends
635 .data_type()
636 .equals_datatype(b_run_ends.data_type())
637 && a_values.is_nullable() == b_values.is_nullable()
638 && a_values.data_type().equals_datatype(b_values.data_type())
639 }
640 (
641 DataType::Union(a_union_fields, a_union_mode),
642 DataType::Union(b_union_fields, b_union_mode),
643 ) => {
644 a_union_mode == b_union_mode
645 && a_union_fields.len() == b_union_fields.len()
646 && a_union_fields.iter().all(|a| {
647 b_union_fields.iter().any(|b| {
648 a.0 == b.0
649 && a.1.is_nullable() == b.1.is_nullable()
650 && a.1.data_type().equals_datatype(b.1.data_type())
651 })
652 })
653 }
654 _ => self == other,
655 }
656 }
657
658 /// Returns the byte width of this type if it is a primitive type
659 ///
660 /// Returns `None` if not a primitive type
661 #[inline]
662 pub fn primitive_width(&self) -> Option<usize> {
663 match self {
664 DataType::Null => None,
665 DataType::Boolean => None,
666 DataType::Int8 | DataType::UInt8 => Some(1),
667 DataType::Int16 | DataType::UInt16 | DataType::Float16 => Some(2),
668 DataType::Int32 | DataType::UInt32 | DataType::Float32 => Some(4),
669 DataType::Int64 | DataType::UInt64 | DataType::Float64 => Some(8),
670 DataType::Timestamp(_, _) => Some(8),
671 DataType::Date32 | DataType::Time32(_) => Some(4),
672 DataType::Date64 | DataType::Time64(_) => Some(8),
673 DataType::Duration(_) => Some(8),
674 DataType::Interval(IntervalUnit::YearMonth) => Some(4),
675 DataType::Interval(IntervalUnit::DayTime) => Some(8),
676 DataType::Interval(IntervalUnit::MonthDayNano) => Some(16),
677 DataType::Decimal128(_, _) => Some(16),
678 DataType::Decimal256(_, _) => Some(32),
679 DataType::Utf8 | DataType::LargeUtf8 | DataType::Utf8View => None,
680 DataType::Binary | DataType::LargeBinary | DataType::BinaryView => None,
681 DataType::FixedSizeBinary(_) => None,
682 DataType::List(_)
683 | DataType::ListView(_)
684 | DataType::LargeList(_)
685 | DataType::LargeListView(_)
686 | DataType::Map(_, _) => None,
687 DataType::FixedSizeList(_, _) => None,
688 DataType::Struct(_) => None,
689 DataType::Union(_, _) => None,
690 DataType::Dictionary(_, _) => None,
691 DataType::RunEndEncoded(_, _) => None,
692 }
693 }
694
695 /// Return size of this instance in bytes.
696 ///
697 /// Includes the size of `Self`.
698 pub fn size(&self) -> usize {
699 std::mem::size_of_val(self)
700 + match self {
701 DataType::Null
702 | DataType::Boolean
703 | DataType::Int8
704 | DataType::Int16
705 | DataType::Int32
706 | DataType::Int64
707 | DataType::UInt8
708 | DataType::UInt16
709 | DataType::UInt32
710 | DataType::UInt64
711 | DataType::Float16
712 | DataType::Float32
713 | DataType::Float64
714 | DataType::Date32
715 | DataType::Date64
716 | DataType::Time32(_)
717 | DataType::Time64(_)
718 | DataType::Duration(_)
719 | DataType::Interval(_)
720 | DataType::Binary
721 | DataType::FixedSizeBinary(_)
722 | DataType::LargeBinary
723 | DataType::BinaryView
724 | DataType::Utf8
725 | DataType::LargeUtf8
726 | DataType::Utf8View
727 | DataType::Decimal128(_, _)
728 | DataType::Decimal256(_, _) => 0,
729 DataType::Timestamp(_, s) => s.as_ref().map(|s| s.len()).unwrap_or_default(),
730 DataType::List(field)
731 | DataType::ListView(field)
732 | DataType::FixedSizeList(field, _)
733 | DataType::LargeList(field)
734 | DataType::LargeListView(field)
735 | DataType::Map(field, _) => field.size(),
736 DataType::Struct(fields) => fields.size(),
737 DataType::Union(fields, _) => fields.size(),
738 DataType::Dictionary(dt1, dt2) => dt1.size() + dt2.size(),
739 DataType::RunEndEncoded(run_ends, values) => {
740 run_ends.size() - std::mem::size_of_val(run_ends) + values.size()
741 - std::mem::size_of_val(values)
742 }
743 }
744 }
745
746 /// Check to see if `self` is a superset of `other`
747 ///
748 /// If DataType is a nested type, then it will check to see if the nested type is a superset of the other nested type
749 /// else it will check to see if the DataType is equal to the other DataType
750 pub fn contains(&self, other: &DataType) -> bool {
751 match (self, other) {
752 (DataType::List(f1), DataType::List(f2))
753 | (DataType::LargeList(f1), DataType::LargeList(f2))
754 | (DataType::ListView(f1), DataType::ListView(f2))
755 | (DataType::LargeListView(f1), DataType::LargeListView(f2)) => f1.contains(f2),
756 (DataType::FixedSizeList(f1, s1), DataType::FixedSizeList(f2, s2)) => {
757 s1 == s2 && f1.contains(f2)
758 }
759 (DataType::Map(f1, s1), DataType::Map(f2, s2)) => s1 == s2 && f1.contains(f2),
760 (DataType::Struct(f1), DataType::Struct(f2)) => f1.contains(f2),
761 (DataType::Union(f1, s1), DataType::Union(f2, s2)) => {
762 s1 == s2
763 && f1
764 .iter()
765 .all(|f1| f2.iter().any(|f2| f1.0 == f2.0 && f1.1.contains(f2.1)))
766 }
767 (DataType::Dictionary(k1, v1), DataType::Dictionary(k2, v2)) => {
768 k1.contains(k2) && v1.contains(v2)
769 }
770 _ => self == other,
771 }
772 }
773
774 /// Create a [`DataType::List`] with elements of the specified type
775 /// and nullability, and conventionally named inner [`Field`] (`"item"`).
776 ///
777 /// To specify field level metadata, construct the inner [`Field`]
778 /// directly via [`Field::new`] or [`Field::new_list_field`].
779 pub fn new_list(data_type: DataType, nullable: bool) -> Self {
780 DataType::List(Arc::new(Field::new_list_field(data_type, nullable)))
781 }
782
783 /// Create a [`DataType::LargeList`] with elements of the specified type
784 /// and nullability, and conventionally named inner [`Field`] (`"item"`).
785 ///
786 /// To specify field level metadata, construct the inner [`Field`]
787 /// directly via [`Field::new`] or [`Field::new_list_field`].
788 pub fn new_large_list(data_type: DataType, nullable: bool) -> Self {
789 DataType::LargeList(Arc::new(Field::new_list_field(data_type, nullable)))
790 }
791
792 /// Create a [`DataType::FixedSizeList`] with elements of the specified type, size
793 /// and nullability, and conventionally named inner [`Field`] (`"item"`).
794 ///
795 /// To specify field level metadata, construct the inner [`Field`]
796 /// directly via [`Field::new`] or [`Field::new_list_field`].
797 pub fn new_fixed_size_list(data_type: DataType, size: i32, nullable: bool) -> Self {
798 DataType::FixedSizeList(Arc::new(Field::new_list_field(data_type, nullable)), size)
799 }
800}
801
802/// The maximum precision for [DataType::Decimal128] values
803pub const DECIMAL128_MAX_PRECISION: u8 = 38;
804
805/// The maximum scale for [DataType::Decimal128] values
806pub const DECIMAL128_MAX_SCALE: i8 = 38;
807
808/// The maximum precision for [DataType::Decimal256] values
809pub const DECIMAL256_MAX_PRECISION: u8 = 76;
810
811/// The maximum scale for [DataType::Decimal256] values
812pub const DECIMAL256_MAX_SCALE: i8 = 76;
813
814/// The default scale for [DataType::Decimal128] and [DataType::Decimal256]
815/// values
816pub const DECIMAL_DEFAULT_SCALE: i8 = 10;
817
818#[cfg(test)]
819mod tests {
820 use super::*;
821
822 #[test]
823 #[cfg(feature = "serde")]
824 fn serde_struct_type() {
825 use std::collections::HashMap;
826
827 let kv_array = [("k".to_string(), "v".to_string())];
828 let field_metadata: HashMap<String, String> = kv_array.iter().cloned().collect();
829
830 // Non-empty map: should be converted as JSON obj { ... }
831 let first_name =
832 Field::new("first_name", DataType::Utf8, false).with_metadata(field_metadata);
833
834 // Empty map: should be omitted.
835 let last_name =
836 Field::new("last_name", DataType::Utf8, false).with_metadata(HashMap::default());
837
838 let person = DataType::Struct(Fields::from(vec![
839 first_name,
840 last_name,
841 Field::new(
842 "address",
843 DataType::Struct(Fields::from(vec![
844 Field::new("street", DataType::Utf8, false),
845 Field::new("zip", DataType::UInt16, false),
846 ])),
847 false,
848 ),
849 ]));
850
851 let serialized = serde_json::to_string(&person).unwrap();
852
853 // NOTE that this is testing the default (derived) serialization format, not the
854 // JSON format specified in metadata.md
855
856 assert_eq!(
857 "{\"Struct\":[\
858 {\"name\":\"first_name\",\"data_type\":\"Utf8\",\"nullable\":false,\"dict_id\":0,\"dict_is_ordered\":false,\"metadata\":{\"k\":\"v\"}},\
859 {\"name\":\"last_name\",\"data_type\":\"Utf8\",\"nullable\":false,\"dict_id\":0,\"dict_is_ordered\":false,\"metadata\":{}},\
860 {\"name\":\"address\",\"data_type\":{\"Struct\":\
861 [{\"name\":\"street\",\"data_type\":\"Utf8\",\"nullable\":false,\"dict_id\":0,\"dict_is_ordered\":false,\"metadata\":{}},\
862 {\"name\":\"zip\",\"data_type\":\"UInt16\",\"nullable\":false,\"dict_id\":0,\"dict_is_ordered\":false,\"metadata\":{}}\
863 ]},\"nullable\":false,\"dict_id\":0,\"dict_is_ordered\":false,\"metadata\":{}}]}",
864 serialized
865 );
866
867 let deserialized = serde_json::from_str(&serialized).unwrap();
868
869 assert_eq!(person, deserialized);
870 }
871
872 #[test]
873 fn test_list_datatype_equality() {
874 // tests that list type equality is checked while ignoring list names
875 let list_a = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, true)));
876 let list_b = DataType::List(Arc::new(Field::new("array", DataType::Int32, true)));
877 let list_c = DataType::List(Arc::new(Field::new_list_field(DataType::Int32, false)));
878 let list_d = DataType::List(Arc::new(Field::new_list_field(DataType::UInt32, true)));
879 assert!(list_a.equals_datatype(&list_b));
880 assert!(!list_a.equals_datatype(&list_c));
881 assert!(!list_b.equals_datatype(&list_c));
882 assert!(!list_a.equals_datatype(&list_d));
883
884 let list_e =
885 DataType::FixedSizeList(Arc::new(Field::new_list_field(list_a.clone(), false)), 3);
886 let list_f =
887 DataType::FixedSizeList(Arc::new(Field::new("array", list_b.clone(), false)), 3);
888 let list_g = DataType::FixedSizeList(
889 Arc::new(Field::new_list_field(DataType::FixedSizeBinary(3), true)),
890 3,
891 );
892 assert!(list_e.equals_datatype(&list_f));
893 assert!(!list_e.equals_datatype(&list_g));
894 assert!(!list_f.equals_datatype(&list_g));
895
896 let list_h = DataType::Struct(Fields::from(vec![Field::new("f1", list_e, true)]));
897 let list_i = DataType::Struct(Fields::from(vec![Field::new("f1", list_f.clone(), true)]));
898 let list_j = DataType::Struct(Fields::from(vec![Field::new("f1", list_f.clone(), false)]));
899 let list_k = DataType::Struct(Fields::from(vec![
900 Field::new("f1", list_f.clone(), false),
901 Field::new("f2", list_g.clone(), false),
902 Field::new("f3", DataType::Utf8, true),
903 ]));
904 let list_l = DataType::Struct(Fields::from(vec![
905 Field::new("ff1", list_f.clone(), false),
906 Field::new("ff2", list_g.clone(), false),
907 Field::new("ff3", DataType::LargeUtf8, true),
908 ]));
909 let list_m = DataType::Struct(Fields::from(vec![
910 Field::new("ff1", list_f, false),
911 Field::new("ff2", list_g, false),
912 Field::new("ff3", DataType::Utf8, true),
913 ]));
914 assert!(list_h.equals_datatype(&list_i));
915 assert!(!list_h.equals_datatype(&list_j));
916 assert!(!list_k.equals_datatype(&list_l));
917 assert!(list_k.equals_datatype(&list_m));
918
919 let list_n = DataType::Map(Arc::new(Field::new("f1", list_a.clone(), true)), true);
920 let list_o = DataType::Map(Arc::new(Field::new("f2", list_b.clone(), true)), true);
921 let list_p = DataType::Map(Arc::new(Field::new("f2", list_b.clone(), true)), false);
922 let list_q = DataType::Map(Arc::new(Field::new("f2", list_c.clone(), true)), true);
923 let list_r = DataType::Map(Arc::new(Field::new("f1", list_a.clone(), false)), true);
924
925 assert!(list_n.equals_datatype(&list_o));
926 assert!(!list_n.equals_datatype(&list_p));
927 assert!(!list_n.equals_datatype(&list_q));
928 assert!(!list_n.equals_datatype(&list_r));
929
930 let list_s = DataType::Dictionary(Box::new(DataType::UInt8), Box::new(list_a));
931 let list_t = DataType::Dictionary(Box::new(DataType::UInt8), Box::new(list_b.clone()));
932 let list_u = DataType::Dictionary(Box::new(DataType::Int8), Box::new(list_b));
933 let list_v = DataType::Dictionary(Box::new(DataType::UInt8), Box::new(list_c));
934
935 assert!(list_s.equals_datatype(&list_t));
936 assert!(!list_s.equals_datatype(&list_u));
937 assert!(!list_s.equals_datatype(&list_v));
938
939 let union_a = DataType::Union(
940 UnionFields::new(
941 vec![1, 2],
942 vec![
943 Field::new("f1", DataType::Utf8, false),
944 Field::new("f2", DataType::UInt8, false),
945 ],
946 ),
947 UnionMode::Sparse,
948 );
949 let union_b = DataType::Union(
950 UnionFields::new(
951 vec![1, 2],
952 vec![
953 Field::new("ff1", DataType::Utf8, false),
954 Field::new("ff2", DataType::UInt8, false),
955 ],
956 ),
957 UnionMode::Sparse,
958 );
959 let union_c = DataType::Union(
960 UnionFields::new(
961 vec![2, 1],
962 vec![
963 Field::new("fff2", DataType::UInt8, false),
964 Field::new("fff1", DataType::Utf8, false),
965 ],
966 ),
967 UnionMode::Sparse,
968 );
969 let union_d = DataType::Union(
970 UnionFields::new(
971 vec![2, 1],
972 vec![
973 Field::new("fff1", DataType::Int8, false),
974 Field::new("fff2", DataType::UInt8, false),
975 ],
976 ),
977 UnionMode::Sparse,
978 );
979 let union_e = DataType::Union(
980 UnionFields::new(
981 vec![1, 2],
982 vec![
983 Field::new("f1", DataType::Utf8, true),
984 Field::new("f2", DataType::UInt8, false),
985 ],
986 ),
987 UnionMode::Sparse,
988 );
989
990 assert!(union_a.equals_datatype(&union_b));
991 assert!(union_a.equals_datatype(&union_c));
992 assert!(!union_a.equals_datatype(&union_d));
993 assert!(!union_a.equals_datatype(&union_e));
994
995 let list_w = DataType::RunEndEncoded(
996 Arc::new(Field::new("f1", DataType::Int64, true)),
997 Arc::new(Field::new("f2", DataType::Utf8, true)),
998 );
999 let list_x = DataType::RunEndEncoded(
1000 Arc::new(Field::new("ff1", DataType::Int64, true)),
1001 Arc::new(Field::new("ff2", DataType::Utf8, true)),
1002 );
1003 let list_y = DataType::RunEndEncoded(
1004 Arc::new(Field::new("ff1", DataType::UInt16, true)),
1005 Arc::new(Field::new("ff2", DataType::Utf8, true)),
1006 );
1007 let list_z = DataType::RunEndEncoded(
1008 Arc::new(Field::new("f1", DataType::Int64, false)),
1009 Arc::new(Field::new("f2", DataType::Utf8, true)),
1010 );
1011
1012 assert!(list_w.equals_datatype(&list_x));
1013 assert!(!list_w.equals_datatype(&list_y));
1014 assert!(!list_w.equals_datatype(&list_z));
1015 }
1016
1017 #[test]
1018 fn create_struct_type() {
1019 let _person = DataType::Struct(Fields::from(vec![
1020 Field::new("first_name", DataType::Utf8, false),
1021 Field::new("last_name", DataType::Utf8, false),
1022 Field::new(
1023 "address",
1024 DataType::Struct(Fields::from(vec![
1025 Field::new("street", DataType::Utf8, false),
1026 Field::new("zip", DataType::UInt16, false),
1027 ])),
1028 false,
1029 ),
1030 ]));
1031 }
1032
1033 #[test]
1034 fn test_nested() {
1035 let list = DataType::List(Arc::new(Field::new("foo", DataType::Utf8, true)));
1036 let list_view = DataType::ListView(Arc::new(Field::new("foo", DataType::Utf8, true)));
1037 let large_list_view =
1038 DataType::LargeListView(Arc::new(Field::new("foo", DataType::Utf8, true)));
1039
1040 assert!(!DataType::is_nested(&DataType::Boolean));
1041 assert!(!DataType::is_nested(&DataType::Int32));
1042 assert!(!DataType::is_nested(&DataType::Utf8));
1043 assert!(DataType::is_nested(&list));
1044 assert!(DataType::is_nested(&list_view));
1045 assert!(DataType::is_nested(&large_list_view));
1046
1047 assert!(!DataType::is_nested(&DataType::Dictionary(
1048 Box::new(DataType::Int32),
1049 Box::new(DataType::Boolean)
1050 )));
1051 assert!(!DataType::is_nested(&DataType::Dictionary(
1052 Box::new(DataType::Int32),
1053 Box::new(DataType::Int64)
1054 )));
1055 assert!(!DataType::is_nested(&DataType::Dictionary(
1056 Box::new(DataType::Int32),
1057 Box::new(DataType::LargeUtf8)
1058 )));
1059 assert!(DataType::is_nested(&DataType::Dictionary(
1060 Box::new(DataType::Int32),
1061 Box::new(list)
1062 )));
1063 }
1064
1065 #[test]
1066 fn test_integer() {
1067 // is_integer
1068 assert!(DataType::is_integer(&DataType::Int32));
1069 assert!(DataType::is_integer(&DataType::UInt64));
1070 assert!(!DataType::is_integer(&DataType::Float16));
1071
1072 // is_signed_integer
1073 assert!(DataType::is_signed_integer(&DataType::Int32));
1074 assert!(!DataType::is_signed_integer(&DataType::UInt64));
1075 assert!(!DataType::is_signed_integer(&DataType::Float16));
1076
1077 // is_unsigned_integer
1078 assert!(!DataType::is_unsigned_integer(&DataType::Int32));
1079 assert!(DataType::is_unsigned_integer(&DataType::UInt64));
1080 assert!(!DataType::is_unsigned_integer(&DataType::Float16));
1081
1082 // is_dictionary_key_type
1083 assert!(DataType::is_dictionary_key_type(&DataType::Int32));
1084 assert!(DataType::is_dictionary_key_type(&DataType::UInt64));
1085 assert!(!DataType::is_dictionary_key_type(&DataType::Float16));
1086 }
1087
1088 #[test]
1089 fn test_floating() {
1090 assert!(DataType::is_floating(&DataType::Float16));
1091 assert!(!DataType::is_floating(&DataType::Int32));
1092 }
1093
1094 #[test]
1095 fn test_datatype_is_null() {
1096 assert!(DataType::is_null(&DataType::Null));
1097 assert!(!DataType::is_null(&DataType::Int32));
1098 }
1099
1100 #[test]
1101 fn size_should_not_regress() {
1102 assert_eq!(std::mem::size_of::<DataType>(), 24);
1103 }
1104
1105 #[test]
1106 #[should_panic(expected = "duplicate type id: 1")]
1107 fn test_union_with_duplicated_type_id() {
1108 let type_ids = vec![1, 1];
1109 let _union = DataType::Union(
1110 UnionFields::new(
1111 type_ids,
1112 vec![
1113 Field::new("f1", DataType::Int32, false),
1114 Field::new("f2", DataType::Utf8, false),
1115 ],
1116 ),
1117 UnionMode::Dense,
1118 );
1119 }
1120
1121 #[test]
1122 fn test_try_from_str() {
1123 let data_type: DataType = "Int32".try_into().unwrap();
1124 assert_eq!(data_type, DataType::Int32);
1125 }
1126
1127 #[test]
1128 fn test_from_str() {
1129 let data_type: DataType = "UInt64".parse().unwrap();
1130 assert_eq!(data_type, DataType::UInt64);
1131 }
1132}