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