Schema

Struct Schema 

pub struct Schema {
    pub fields: Fields,
    pub metadata: HashMap<String, String>,
}
Expand description

Describes the meta-data of an ordered sequence of relative types.

Note that this information is only part of the meta-data and not part of the physical memory layout.

Fields§

§fields: Fields

A sequence of fields that describe the schema.

§metadata: HashMap<String, String>

A map of key-value pairs containing additional metadata.

Implementations§

§

impl Schema

pub fn empty() -> Schema

Creates an empty Schema

pub fn new(fields: impl Into<Fields>) -> Schema

Creates a new Schema from a sequence of Field values.

§Example
let field_a = Field::new("a", DataType::Int64, false);
let field_b = Field::new("b", DataType::Boolean, false);

let schema = Schema::new(vec![field_a, field_b]);

pub fn new_with_metadata( fields: impl Into<Fields>, metadata: HashMap<String, String>, ) -> Schema

Creates a new Schema from a sequence of Field values and adds additional metadata in form of key value pairs.

§Example

let field_a = Field::new("a", DataType::Int64, false);
let field_b = Field::new("b", DataType::Boolean, false);

let mut metadata: HashMap<String, String> = HashMap::new();
metadata.insert("row_count".to_string(), "100".to_string());

let schema = Schema::new_with_metadata(vec![field_a, field_b], metadata);

pub fn with_metadata(self, metadata: HashMap<String, String>) -> Schema

Sets the metadata of this Schema to be metadata and returns self

pub fn project(&self, indices: &[usize]) -> Result<Schema, ArrowError>

Returns a new schema with only the specified columns in the new schema This carries metadata from the parent schema over as well

pub fn try_merge( schemas: impl IntoIterator<Item = Schema>, ) -> Result<Schema, ArrowError>

Merge schema into self if it is compatible. Struct fields will be merged recursively.

Example:


let merged = Schema::try_merge(vec![
    Schema::new(vec![
        Field::new("c1", DataType::Int64, false),
        Field::new("c2", DataType::Utf8, false),
    ]),
    Schema::new(vec![
        Field::new("c1", DataType::Int64, true),
        Field::new("c2", DataType::Utf8, false),
        Field::new("c3", DataType::Utf8, false),
    ]),
]).unwrap();

assert_eq!(
    merged,
    Schema::new(vec![
        Field::new("c1", DataType::Int64, true),
        Field::new("c2", DataType::Utf8, false),
        Field::new("c3", DataType::Utf8, false),
    ]),
);

pub const fn fields(&self) -> &Fields

Returns an immutable reference of the vector of Field instances.

pub fn flattened_fields(&self) -> Vec<&Field>

Returns a vector with references to all fields (including nested fields)

§Example
use std::sync::Arc;
use arrow_schema::{DataType, Field, Fields, Schema};

let f1 = Arc::new(Field::new("a", DataType::Boolean, false));

let f2_inner = Arc::new(Field::new("b_inner", DataType::Int8, false));
let f2 = Arc::new(Field::new("b", DataType::List(f2_inner.clone()), false));

let f3_inner1 = Arc::new(Field::new("c_inner1", DataType::Int8, false));
let f3_inner2 = Arc::new(Field::new("c_inner2", DataType::Int8, false));
let f3 = Arc::new(Field::new(
    "c",
    DataType::Struct(vec![f3_inner1.clone(), f3_inner2.clone()].into()),
    false
));

let mut schema = Schema::new(vec![
  f1.clone(), f2.clone(), f3.clone()
]);
assert_eq!(
    schema.flattened_fields(),
    vec![
        f1.as_ref(),
        f2.as_ref(),
        f2_inner.as_ref(),
        f3.as_ref(),
        f3_inner1.as_ref(),
        f3_inner2.as_ref()
   ]
);

pub fn field(&self, i: usize) -> &Field

Returns an immutable reference of a specific Field instance selected using an offset within the internal fields vector.

§Panics

Panics if index out of bounds

pub fn field_with_name(&self, name: &str) -> Result<&Field, ArrowError>

Returns an immutable reference of a specific Field instance selected by name.

pub fn fields_with_dict_id(&self, dict_id: i64) -> Vec<&Field>

👎Deprecated since 54.0.0: The ability to preserve dictionary IDs will be removed. With it, all functions related to it.

Returns a vector of immutable references to all Field instances selected by the dictionary ID they use.

pub fn index_of(&self, name: &str) -> Result<usize, ArrowError>

Find the index of the column with the given name.

pub const fn metadata(&self) -> &HashMap<String, String>

Returns an immutable reference to the Map of custom metadata key-value pairs.

pub fn normalize( &self, separator: &str, max_level: Option<usize>, ) -> Result<Schema, ArrowError>

Normalize a Schema into a flat table.

Nested Fields will generate names separated by separator, up to a depth of max_level (unlimited if None).

e.g. given a Schema:

    "foo": StructArray<"bar": Utf8>

A separator of "." would generate a batch with the schema:

    "foo.bar": Utf8

Note that giving a depth of Some(0) to max_level is the same as passing in None; it will be treated as unlimited.

§Example
let schema = Schema::new(vec![
    Field::new(
        "a",
        DataType::Struct(Fields::from(vec![
            Arc::new(Field::new("animals", DataType::Utf8, true)),
            Arc::new(Field::new("n_legs", DataType::Int64, true)),
        ])),
        false,
    ),
])
.normalize(".", None)
.expect("valid normalization");
let expected = Schema::new(vec![
    Field::new("a.animals", DataType::Utf8, true),
    Field::new("a.n_legs", DataType::Int64, true),
]);
assert_eq!(schema, expected);

pub fn column_with_name(&self, name: &str) -> Option<(usize, &Field)>

Look up a column by name and return a immutable reference to the column along with its index.

pub fn contains(&self, other: &Schema) -> bool

Check to see if self is a superset of other schema.

In particular returns true if self.metadata is a superset of other.metadata and Fields::contains for self.fields and other.fields

In other words, any record that conforms to other should also conform to self.

Trait Implementations§

Source§

impl AsRef<Schema> for DFSchema

Allow DFSchema to be converted into an Arrow &Schema

Source§

fn as_ref(&self) -> &Schema

Converts this type into a shared reference of the (usually inferred) input type.
§

impl AsRef<Schema> for Schema

§

fn as_ref(&self) -> &Schema

Converts this type into a shared reference of the (usually inferred) input type.
§

impl Clone for Schema

§

fn clone(&self) -> Schema

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for Schema

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Display for Schema

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl From<&Schema> for SchemaBuilder

§

fn from(value: &Schema) -> SchemaBuilder

Converts to this type from the input type.
§

impl From<Schema> for SchemaBuilder

§

fn from(value: Schema) -> SchemaBuilder

Converts to this type from the input type.
§

impl Hash for Schema

§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
§

impl PartialEq for Schema

§

fn eq(&self, other: &Schema) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl SchemaExt for Schema

Source§

fn equivalent_names_and_types(&self, other: &Schema) -> bool

This is a specialized version of Eq that ignores differences in nullability and metadata. Read more
Source§

fn logically_equivalent_names_and_types( &self, other: &Schema, ) -> Result<(), DataFusionError>

Returns nothing if the two schemas have the same qualified named fields with logically equivalent data types. Returns internal error otherwise. Read more
Source§

impl ToDFSchema for Schema

Source§

fn to_dfschema(self) -> Result<DFSchema, DataFusionError>

Attempt to create a DSSchema
Source§

fn to_dfschema_ref(self) -> Result<Arc<DFSchema>, DataFusionError>

Attempt to create a DSSchemaRef
§

impl TryFrom<&FFI_ArrowSchema> for Schema

§

type Error = ArrowError

The type returned in the event of a conversion error.
§

fn try_from(c_schema: &FFI_ArrowSchema) -> Result<Schema, ArrowError>

Performs the conversion.
§

impl TryFrom<&Schema> for FFI_ArrowSchema

§

type Error = ArrowError

The type returned in the event of a conversion error.
§

fn try_from(schema: &Schema) -> Result<FFI_ArrowSchema, ArrowError>

Performs the conversion.
Source§

impl TryFrom<Schema> for DFSchema

Create a DFSchema from an Arrow schema

Source§

type Error = DataFusionError

The type returned in the event of a conversion error.
Source§

fn try_from( schema: Schema, ) -> Result<DFSchema, <DFSchema as TryFrom<Schema>>::Error>

Performs the conversion.
§

impl TryFrom<Schema> for FFI_ArrowSchema

§

type Error = ArrowError

The type returned in the event of a conversion error.
§

fn try_from(schema: Schema) -> Result<FFI_ArrowSchema, ArrowError>

Performs the conversion.
§

impl Eq for Schema

§

impl StructuralPartialEq for Schema

Auto Trait Implementations§

§

impl Freeze for Schema

§

impl RefUnwindSafe for Schema

§

impl Send for Schema

§

impl Sync for Schema

§

impl Unpin for Schema

§

impl UnwindSafe for Schema

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynEq for T
where T: Eq + Any,

Source§

fn dyn_eq(&self, other: &(dyn Any + 'static)) -> bool

Source§

impl<T> DynHash for T
where T: Hash + Any,

Source§

fn dyn_hash(&self, state: &mut dyn Hasher)

§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

impl<T> ToStringFallible for T
where T: Display,

§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

§

impl<T> ErasedDestructor for T
where T: 'static,