pub struct UnionFields(/* private fields */);Expand description
A cheaply cloneable, owned collection of FieldRef and their corresponding type ids
Implementations§
Source§impl UnionFields
impl UnionFields
Sourcepub fn empty() -> Self
pub fn empty() -> Self
Create a new UnionFields with no fields
Sourcepub fn try_new<F, T>(type_ids: T, fields: F) -> Result<Self, ArrowError>
pub fn try_new<F, T>(type_ids: T, fields: F) -> Result<Self, ArrowError>
Create a new UnionFields from a Fields and array of type_ids
See https://arrow.apache.org/docs/format/Columnar.html#union-layout
§Errors
This function returns an error if:
- Any type_id appears more than once (duplicate type ids)
- The type_ids are duplicated
§Examples
use arrow_schema::{DataType, Field, UnionFields};
// Create a new UnionFields with type id mapping
// 1 -> DataType::UInt8
// 3 -> DataType::Utf8
let result = UnionFields::try_new(
vec![1, 3],
vec![
Field::new("field1", DataType::UInt8, false),
Field::new("field3", DataType::Utf8, false),
],
);
assert!(result.is_ok());
// This will fail due to duplicate type ids
let result = UnionFields::try_new(
vec![1, 1],
vec![
Field::new("field1", DataType::UInt8, false),
Field::new("field2", DataType::Utf8, false),
],
);
assert!(result.is_err());Sourcepub fn from_fields<F>(fields: F) -> Self
pub fn from_fields<F>(fields: F) -> Self
Create a new UnionFields from a collection of fields with automatically
assigned type IDs starting from 0.
The type IDs are assigned in increasing order: 0, 1, 2, 3, etc.
See https://arrow.apache.org/docs/format/Columnar.html#union-layout
§Panics
Panics if the number of fields exceeds 127 (the maximum value for i8 type IDs).
If you want to avoid panics, use UnionFields::try_from_fields instead, which
returns a Result.
§Examples
use arrow_schema::{DataType, Field, UnionFields};
// Create a new UnionFields with automatic type id assignment
// 0 -> DataType::UInt8
// 1 -> DataType::Utf8
let union_fields = UnionFields::from_fields(vec![
Field::new("field1", DataType::UInt8, false),
Field::new("field2", DataType::Utf8, false),
]);
assert_eq!(union_fields.len(), 2);Sourcepub fn try_from_fields<F>(fields: F) -> Result<Self, ArrowError>
pub fn try_from_fields<F>(fields: F) -> Result<Self, ArrowError>
Create a new UnionFields from a collection of fields with automatically
assigned type IDs starting from 0.
The type IDs are assigned in increasing order: 0, 1, 2, 3, etc.
This is the non-panicking version of UnionFields::from_fields.
See https://arrow.apache.org/docs/format/Columnar.html#union-layout
§Errors
Returns an error if the number of fields exceeds 127 (the maximum value for i8 type IDs).
§Examples
use arrow_schema::{DataType, Field, UnionFields};
// Create a new UnionFields with automatic type id assignment
// 0 -> DataType::UInt8
// 1 -> DataType::Utf8
let result = UnionFields::try_from_fields(vec![
Field::new("field1", DataType::UInt8, false),
Field::new("field2", DataType::Utf8, false),
]);
assert!(result.is_ok());
assert_eq!(result.unwrap().len(), 2);
// This will fail with too many fields
let many_fields: Vec<_> = (0..200)
.map(|i| Field::new(format!("field{}", i), DataType::Int32, false))
.collect();
let result = UnionFields::try_from_fields(many_fields);
assert!(result.is_err());Sourcepub fn new<F, T>(type_ids: T, fields: F) -> Self
👎Deprecated since 57.0.0: Use try_new instead
pub fn new<F, T>(type_ids: T, fields: F) -> Self
try_new insteadCreate a new UnionFields from a Fields and array of type_ids
See https://arrow.apache.org/docs/format/Columnar.html#union-layout
§Deprecated
Use UnionFields::try_new instead. This method panics on invalid input,
while try_new returns a Result.
§Panics
Panics if any type_id appears more than once (duplicate type ids).
use arrow_schema::{DataType, Field, UnionFields};
// Create a new UnionFields with type id mapping
// 1 -> DataType::UInt8
// 3 -> DataType::Utf8
UnionFields::try_new(
vec![1, 3],
vec![
Field::new("field1", DataType::UInt8, false),
Field::new("field3", DataType::Utf8, false),
],
);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of fields in this UnionFields
Sourcepub fn iter(&self) -> impl Iterator<Item = (i8, &FieldRef)> + '_
pub fn iter(&self) -> impl Iterator<Item = (i8, &FieldRef)> + '_
Returns an iterator over the fields and type ids in this UnionFields
Sourcepub fn get(&self, index: usize) -> Option<&(i8, FieldRef)>
pub fn get(&self, index: usize) -> Option<&(i8, FieldRef)>
Returns a reference to the field at the given index, or None if out of bounds.
This is a safe alternative to direct indexing via [].
§Example
use arrow_schema::{DataType, Field, UnionFields};
let fields = UnionFields::new(
vec![1, 3],
vec![
Field::new("field1", DataType::UInt8, false),
Field::new("field3", DataType::Utf8, false),
],
);
assert!(fields.get(0).is_some());
assert!(fields.get(1).is_some());
assert!(fields.get(2).is_none());Sourcepub fn find_by_type_id(&self, type_id: i8) -> Option<(i8, &FieldRef)>
pub fn find_by_type_id(&self, type_id: i8) -> Option<(i8, &FieldRef)>
Searches for a field by its type id, returning the type id and field reference if found.
Returns None if no field with the given type id exists.
Sourcepub fn find_by_field(&self, field: &Field) -> Option<(i8, &FieldRef)>
pub fn find_by_field(&self, field: &Field) -> Option<(i8, &FieldRef)>
Searches for a field by value equality, returning its type id and reference if found.
Returns None if no matching field exists in this UnionFields.
Trait Implementations§
Source§impl Clone for UnionFields
impl Clone for UnionFields
Source§fn clone(&self) -> UnionFields
fn clone(&self) -> UnionFields
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for UnionFields
impl Debug for UnionFields
Source§impl<'de> Deserialize<'de> for UnionFields
impl<'de> Deserialize<'de> for UnionFields
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl FromIterator<(i8, Arc<Field>)> for UnionFields
impl FromIterator<(i8, Arc<Field>)> for UnionFields
Source§impl Hash for UnionFields
impl Hash for UnionFields
Source§impl Index<usize> for UnionFields
Allows direct indexing into UnionFields to access fields by position.
impl Index<usize> for UnionFields
Allows direct indexing into UnionFields to access fields by position.
§Panics
Panics if the index is out of bounds. Note that UnionFields supports
a maximum of 128 fields, as type IDs are represented as i8 values.
For a non-panicking alternative, use UnionFields::get.