Skip to main content

arrow_sql_server/arrow/
field.rs

1//! Arrow field metadata model.
2
3use arrow_schema::DataType;
4
5/// Reference to an Arrow source field used by a schema mapping.
6///
7/// This is this crate's mapped source-field metadata. It is not a replacement
8/// for `arrow_schema::Field`.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct ArrowFieldRef {
11    index: usize,
12    name: String,
13    nullable: bool,
14    data_type: DataType,
15}
16
17impl ArrowFieldRef {
18    /// Creates an Arrow source field reference.
19    pub const fn new(index: usize, name: String, nullable: bool, data_type: DataType) -> Self {
20        Self {
21            index,
22            name,
23            nullable,
24            data_type,
25        }
26    }
27
28    /// Returns the Arrow field index.
29    pub const fn index(&self) -> usize {
30        self.index
31    }
32
33    /// Returns the Arrow field name.
34    pub fn name(&self) -> &str {
35        &self.name
36    }
37
38    /// Returns true when the Arrow field is nullable.
39    pub const fn nullable(&self) -> bool {
40        self.nullable
41    }
42
43    /// Returns the Arrow field data type.
44    pub const fn data_type(&self) -> &DataType {
45        &self.data_type
46    }
47}
48
49/// Returns true for Arrow string representations that carry UTF-8 text values.
50pub(crate) fn is_arrow_string_family(data_type: &DataType) -> bool {
51    matches!(
52        data_type,
53        DataType::Utf8 | DataType::LargeUtf8 | DataType::Utf8View
54    )
55}
56
57/// Returns true for Arrow binary representations that carry variable-width bytes.
58pub(crate) fn is_arrow_binary_family(data_type: &DataType) -> bool {
59    matches!(
60        data_type,
61        DataType::Binary | DataType::LargeBinary | DataType::BinaryView
62    )
63}
64
65#[cfg(test)]
66mod tests {
67    use arrow_schema::DataType;
68
69    use super::{is_arrow_binary_family, is_arrow_string_family};
70
71    #[test]
72    fn identifies_arrow_variable_width_representation_families() {
73        for data_type in [DataType::Utf8, DataType::LargeUtf8, DataType::Utf8View] {
74            assert!(is_arrow_string_family(&data_type));
75            assert!(!is_arrow_binary_family(&data_type));
76        }
77
78        for data_type in [
79            DataType::Binary,
80            DataType::LargeBinary,
81            DataType::BinaryView,
82        ] {
83            assert!(is_arrow_binary_family(&data_type));
84            assert!(!is_arrow_string_family(&data_type));
85        }
86
87        for data_type in [DataType::Int32, DataType::FixedSizeBinary(4)] {
88            assert!(!is_arrow_string_family(&data_type));
89            assert!(!is_arrow_binary_family(&data_type));
90        }
91    }
92}