datafusion_common

Struct DFSchema

Source
pub struct DFSchema { /* private fields */ }
Expand description

DFSchema wraps an Arrow schema and adds relation names.

The schema may hold the fields across multiple tables. Some fields may be qualified and some unqualified. A qualified field is a field that has a relation name associated with it.

Unqualified fields must be unique not only amongst themselves, but also must have a distinct name from any qualified field names. This allows finding a qualified field by name to be possible, so long as there aren’t multiple qualified fields with the same name.

There is an alias to Arc<DFSchema> named DFSchemaRef.

§Creating qualified schemas

Use DFSchema::try_from_qualified_schema to create a qualified schema from an Arrow schema.

use datafusion_common::{DFSchema, Column};
use arrow_schema::{DataType, Field, Schema};

let arrow_schema = Schema::new(vec![
   Field::new("c1", DataType::Int32, false),
]);

let df_schema = DFSchema::try_from_qualified_schema("t1", &arrow_schema).unwrap();
let column = Column::from_qualified_name("t1.c1");
assert!(df_schema.has_column(&column));

// Can also access qualified fields with unqualified name, if it's unambiguous
let column = Column::from_qualified_name("c1");
assert!(df_schema.has_column(&column));

§Creating unqualified schemas

Create an unqualified schema using TryFrom:

use datafusion_common::{DFSchema, Column};
use arrow_schema::{DataType, Field, Schema};

let arrow_schema = Schema::new(vec![
   Field::new("c1", DataType::Int32, false),
]);

let df_schema = DFSchema::try_from(arrow_schema).unwrap();
let column = Column::new_unqualified("c1");
assert!(df_schema.has_column(&column));

§Converting back to Arrow schema

Use the Into trait to convert DFSchema into an Arrow schema:

use datafusion_common::DFSchema;
use arrow_schema::Schema;
use arrow::datatypes::Field;
use std::collections::HashMap;

let df_schema = DFSchema::from_unqualified_fields(vec![
   Field::new("c1", arrow::datatypes::DataType::Int32, false),
].into(),HashMap::new()).unwrap();
let schema = Schema::from(df_schema);
assert_eq!(schema.fields().len(), 1);

Implementations§

Source§

impl DFSchema

Source

pub fn empty() -> Self

Creates an empty DFSchema

Source

pub fn as_arrow(&self) -> &Schema

Return a reference to the inner Arrow Schema

Note this does not have the qualifier information

Source

pub fn inner(&self) -> &SchemaRef

Return a reference to the inner Arrow SchemaRef

Note this does not have the qualifier information

Source

pub fn new_with_metadata( qualified_fields: Vec<(Option<TableReference>, Arc<Field>)>, metadata: HashMap<String, String>, ) -> Result<Self>

Create a DFSchema from an Arrow schema where all the fields have a given qualifier

Source

pub fn from_unqualified_fields( fields: Fields, metadata: HashMap<String, String>, ) -> Result<Self>

Create a new DFSchema from a list of Arrow Fields

Source

pub fn from_unqualifed_fields( fields: Fields, metadata: HashMap<String, String>, ) -> Result<Self>

👎Deprecated since 40.0.0: Please use from_unqualified_fields instead (this one’s name is a typo). This method is subject to be removed soon

Create a new DFSchema from a list of Arrow Fields

Source

pub fn try_from_qualified_schema( qualifier: impl Into<TableReference>, schema: &Schema, ) -> Result<Self>

Create a DFSchema from an Arrow schema and a given qualifier

To create a schema from an Arrow schema without a qualifier, use DFSchema::try_from.

Source

pub fn from_field_specific_qualified_schema( qualifiers: Vec<Option<TableReference>>, schema: &SchemaRef, ) -> Result<Self>

Create a DFSchema from an Arrow schema where all the fields have a given qualifier

Source

pub fn check_names(&self) -> Result<()>

Check if the schema have some fields with the same name

Source

pub fn with_functional_dependencies( self, functional_dependencies: FunctionalDependencies, ) -> Result<Self>

Assigns functional dependencies.

Source

pub fn join(&self, schema: &DFSchema) -> Result<Self>

Create a new schema that contains the fields from this schema followed by the fields from the supplied schema. An error will be returned if there are duplicate field names.

Source

pub fn merge(&mut self, other_schema: &DFSchema)

Modify this schema by appending the fields from the supplied schema, ignoring any duplicate fields.

Source

pub fn fields(&self) -> &Fields

Get a list of fields

Source

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

Source

pub fn qualified_field(&self, i: usize) -> (Option<&TableReference>, &Field)

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

Source

pub fn index_of_column_by_name( &self, qualifier: Option<&TableReference>, name: &str, ) -> Option<usize>

Source

pub fn maybe_index_of_column(&self, col: &Column) -> Option<usize>

Find the index of the column with the given qualifier and name, returning None if not found

See Self::index_of_column for a version that returns an error if the column is not found

Source

pub fn index_of_column(&self, col: &Column) -> Result<usize>

Find the index of the column with the given qualifier and name, returning Err if not found

See Self::maybe_index_of_column for a version that returns None if the column is not found

Source

pub fn is_column_from_schema(&self, col: &Column) -> bool

Check if the column is in the current schema

Source

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

Find the field with the given name

Source

pub fn qualified_field_with_name( &self, qualifier: Option<&TableReference>, name: &str, ) -> Result<(Option<&TableReference>, &Field)>

Find the qualified field with the given name

Source

pub fn fields_with_qualified(&self, qualifier: &TableReference) -> Vec<&Field>

Find all fields having the given qualifier

Source

pub fn fields_indices_with_qualified( &self, qualifier: &TableReference, ) -> Vec<usize>

Find all fields indices having the given qualifier

Source

pub fn fields_with_unqualified_name(&self, name: &str) -> Vec<&Field>

Find all fields that match the given name

Source

pub fn qualified_fields_with_unqualified_name( &self, name: &str, ) -> Vec<(Option<&TableReference>, &Field)>

Find all fields that match the given name and return them with their qualifier

Source

pub fn columns_with_unqualified_name(&self, name: &str) -> Vec<Column>

Find all fields that match the given name and convert to column

Source

pub fn columns(&self) -> Vec<Column>

Return all Columns for the schema

Source

pub fn qualified_field_with_unqualified_name( &self, name: &str, ) -> Result<(Option<&TableReference>, &Field)>

Find the qualified field with the given unqualified name

Source

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

Find the field with the given name

Source

pub fn field_with_qualified_name( &self, qualifier: &TableReference, name: &str, ) -> Result<&Field>

Find the field with the given qualified name

Source

pub fn field_from_column(&self, column: &Column) -> Result<&Field>

Find the field with the given qualified column

Source

pub fn qualified_field_from_column( &self, column: &Column, ) -> Result<(Option<&TableReference>, &Field)>

Find the field with the given qualified column

Source

pub fn has_column_with_unqualified_name(&self, name: &str) -> bool

Find if the field exists with the given name

Source

pub fn has_column_with_qualified_name( &self, qualifier: &TableReference, name: &str, ) -> bool

Find if the field exists with the given qualified name

Source

pub fn has_column(&self, column: &Column) -> bool

Find if the field exists with the given qualified column

Source

pub fn matches_arrow_schema(&self, arrow_schema: &Schema) -> bool

Check to see if unqualified field names matches field names in Arrow schema

Source

pub fn check_arrow_schema_type_compatible( &self, arrow_schema: &Schema, ) -> Result<()>

Check to see if fields in 2 Arrow schemas are compatible

Source

pub fn logically_equivalent_names_and_types(&self, other: &Self) -> bool

Returns true if the two schemas have the same qualified named fields with logically equivalent data types. Returns false otherwise.

Use DFSchema::equivalent_names_and_types for stricter semantic type equivalence checking.

Source

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

Returns true if the two schemas have the same qualified named fields with the same data types. Returns false otherwise.

This is a specialized version of Eq that ignores differences in nullability and metadata.

Use DFSchema::logically_equivalent_names_and_types for a weaker logical type checking, which for example would consider a dictionary encoded UTF8 array to be equivalent to a plain UTF8 array.

Source

pub fn datatype_is_logically_equal(dt1: &DataType, dt2: &DataType) -> bool

Checks if two DataTypes are logically equal. This is a notably weaker constraint than datatype_is_semantically_equal in that a Dictionary<K,V> type is logically equal to a plain V type, but not semantically equal. Dictionary<K1, V1> is also logically equal to Dictionary<K2, V1>.

Source

pub fn strip_qualifiers(self) -> Self

Strip all field qualifier in schema

Source

pub fn replace_qualifier(self, qualifier: impl Into<TableReference>) -> Self

Replace all field qualifier with new value in schema

Source

pub fn field_names(&self) -> Vec<String>

Get list of fully-qualified field names in this schema

Source

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

Get metadata of this schema

Source

pub fn functional_dependencies(&self) -> &FunctionalDependencies

Get functional dependencies

Source

pub fn iter(&self) -> impl Iterator<Item = (Option<&TableReference>, &FieldRef)>

Iterate over the qualifiers and fields in the DFSchema

Trait Implementations§

Source§

impl AsRef<Arc<Schema>> for DFSchema

Allow DFSchema to be converted into an Arrow &SchemaRef (to clone, for example)

Source§

fn as_ref(&self) -> &SchemaRef

Converts this type into a shared reference of the (usually inferred) input type.
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.
Source§

impl Clone for DFSchema

Source§

fn clone(&self) -> DFSchema

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

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

Performs copy-assignment from source. Read more
Source§

impl Debug for DFSchema

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for DFSchema

Source§

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

Formats the value using the given formatter. Read more
Source§

impl ExprSchema for DFSchema

Source§

fn nullable(&self, col: &Column) -> Result<bool>

Is this column reference nullable?
Source§

fn data_type(&self, col: &Column) -> Result<&DataType>

What is the datatype of this column?
Source§

fn metadata(&self, col: &Column) -> Result<&HashMap<String, String>>

Returns the column’s optional metadata.
Source§

fn data_type_and_nullable(&self, col: &Column) -> Result<(&DataType, bool)>

Return the coulmn’s datatype and nullability
Source§

impl From<&DFSchema> for Schema

Source§

fn from(df_schema: &DFSchema) -> Self

Convert DFSchema reference into a Schema

Source§

impl From<DFSchema> for SchemaRef

Source§

fn from(df_schema: DFSchema) -> Self

Converts to this type from the input type.
Source§

impl From<DFSchema> for Schema

Source§

fn from(df_schema: DFSchema) -> Self

Convert DFSchema into a Schema

Source§

impl Hash for DFSchema

Source§

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

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
Source§

impl PartialEq for DFSchema

Source§

fn eq(&self, other: &DFSchema) -> 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 TryFrom<Arc<Schema>> for DFSchema

Source§

type Error = DataFusionError

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

fn try_from(schema: SchemaRef) -> Result<Self, Self::Error>

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<Self, Self::Error>

Performs the conversion.
Source§

impl Eq for DFSchema

Source§

impl StructuralPartialEq for DFSchema

Auto Trait Implementations§

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, dst: *mut T)

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

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

Source§

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

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

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

Source§

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

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

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

Source§

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

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
Source§

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