Struct datafusion::common::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, DFField};
use arrow_schema::Schema;
use std::collections::HashMap;
let df_schema = DFSchema::new_with_metadata(vec![
DFField::new_unqualified("c1", arrow::datatypes::DataType::Int32, false),
], HashMap::new()).unwrap();
let schema = Schema::from(df_schema);
assert_eq!(schema.fields().len(), 1);
Implementations§
source§impl DFSchema
impl DFSchema
sourcepub fn new_with_metadata(
fields: Vec<DFField>,
metadata: HashMap<String, String>
) -> Result<DFSchema, DataFusionError>
pub fn new_with_metadata( fields: Vec<DFField>, metadata: HashMap<String, String> ) -> Result<DFSchema, DataFusionError>
Create a new DFSchema
sourcepub fn try_from_qualified_schema<'a>(
qualifier: impl Into<TableReference<'a>>,
schema: &Schema
) -> Result<DFSchema, DataFusionError>
pub fn try_from_qualified_schema<'a>( qualifier: impl Into<TableReference<'a>>, schema: &Schema ) -> Result<DFSchema, DataFusionError>
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
.
sourcepub fn with_functional_dependencies(
self,
functional_dependencies: FunctionalDependencies
) -> Result<DFSchema, DataFusionError>
pub fn with_functional_dependencies( self, functional_dependencies: FunctionalDependencies ) -> Result<DFSchema, DataFusionError>
Assigns functional dependencies.
sourcepub fn join(&self, schema: &DFSchema) -> Result<DFSchema, DataFusionError>
pub fn join(&self, schema: &DFSchema) -> Result<DFSchema, DataFusionError>
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.
sourcepub fn merge(&mut self, other_schema: &DFSchema)
pub fn merge(&mut self, other_schema: &DFSchema)
Modify this schema by appending the fields from the supplied schema, ignoring any duplicate fields.
sourcepub fn field(&self, i: usize) -> &DFField
pub fn field(&self, i: usize) -> &DFField
Returns an immutable reference of a specific Field
instance selected using an
offset within the internal fields
vector
pub fn index_of_column_by_name( &self, qualifier: Option<&TableReference<'_>>, name: &str ) -> Result<Option<usize>, DataFusionError>
sourcepub fn index_of_column(&self, col: &Column) -> Result<usize, DataFusionError>
pub fn index_of_column(&self, col: &Column) -> Result<usize, DataFusionError>
Find the index of the column with the given qualifier and name
sourcepub fn is_column_from_schema(
&self,
col: &Column
) -> Result<bool, DataFusionError>
pub fn is_column_from_schema( &self, col: &Column ) -> Result<bool, DataFusionError>
Check if the column is in the current schema
sourcepub fn field_with_name(
&self,
qualifier: Option<&TableReference<'_>>,
name: &str
) -> Result<&DFField, DataFusionError>
pub fn field_with_name( &self, qualifier: Option<&TableReference<'_>>, name: &str ) -> Result<&DFField, DataFusionError>
Find the field with the given name
sourcepub fn fields_with_qualified(
&self,
qualifier: &TableReference<'_>
) -> Vec<&DFField>
pub fn fields_with_qualified( &self, qualifier: &TableReference<'_> ) -> Vec<&DFField>
Find all fields having the given qualifier
sourcepub fn fields_indices_with_qualified(
&self,
qualifier: &TableReference<'_>
) -> Vec<usize>
pub fn fields_indices_with_qualified( &self, qualifier: &TableReference<'_> ) -> Vec<usize>
Find all fields indices having the given qualifier
sourcepub fn fields_with_unqualified_name(&self, name: &str) -> Vec<&DFField>
pub fn fields_with_unqualified_name(&self, name: &str) -> Vec<&DFField>
Find all fields match the given name
sourcepub fn field_with_unqualified_name(
&self,
name: &str
) -> Result<&DFField, DataFusionError>
pub fn field_with_unqualified_name( &self, name: &str ) -> Result<&DFField, DataFusionError>
Find the field with the given name
sourcepub fn field_with_qualified_name(
&self,
qualifier: &TableReference<'_>,
name: &str
) -> Result<&DFField, DataFusionError>
pub fn field_with_qualified_name( &self, qualifier: &TableReference<'_>, name: &str ) -> Result<&DFField, DataFusionError>
Find the field with the given qualified name
sourcepub fn field_from_column(
&self,
column: &Column
) -> Result<&DFField, DataFusionError>
pub fn field_from_column( &self, column: &Column ) -> Result<&DFField, DataFusionError>
Find the field with the given qualified column
sourcepub fn has_column_with_unqualified_name(&self, name: &str) -> bool
pub fn has_column_with_unqualified_name(&self, name: &str) -> bool
Find if the field exists with the given name
sourcepub fn has_column_with_qualified_name(
&self,
qualifier: &TableReference<'_>,
name: &str
) -> bool
pub fn has_column_with_qualified_name( &self, qualifier: &TableReference<'_>, name: &str ) -> bool
Find if the field exists with the given qualified name
sourcepub fn has_column(&self, column: &Column) -> bool
pub fn has_column(&self, column: &Column) -> bool
Find if the field exists with the given qualified column
sourcepub fn matches_arrow_schema(&self, arrow_schema: &Schema) -> bool
pub fn matches_arrow_schema(&self, arrow_schema: &Schema) -> bool
Check to see if unqualified field names matches field names in Arrow schema
sourcepub fn check_arrow_schema_type_compatible(
&self,
arrow_schema: &Schema
) -> Result<(), DataFusionError>
pub fn check_arrow_schema_type_compatible( &self, arrow_schema: &Schema ) -> Result<(), DataFusionError>
Check to see if fields in 2 Arrow schemas are compatible
sourcepub fn logically_equivalent_names_and_types(&self, other: &DFSchema) -> bool
pub fn logically_equivalent_names_and_types(&self, other: &DFSchema) -> 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.
sourcepub fn equivalent_names_and_types(&self, other: &DFSchema) -> bool
pub fn equivalent_names_and_types(&self, other: &DFSchema) -> 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.
sourcepub fn strip_qualifiers(self) -> DFSchema
pub fn strip_qualifiers(self) -> DFSchema
Strip all field qualifier in schema
sourcepub fn replace_qualifier(
self,
qualifier: impl Into<TableReference<'static>>
) -> DFSchema
pub fn replace_qualifier( self, qualifier: impl Into<TableReference<'static>> ) -> DFSchema
Replace all field qualifier with new value in schema
sourcepub fn field_names(&self) -> Vec<String>
pub fn field_names(&self) -> Vec<String>
Get list of fully-qualified field names in this schema
sourcepub fn functional_dependencies(&self) -> &FunctionalDependencies
pub fn functional_dependencies(&self) -> &FunctionalDependencies
Get functional dependencies
Trait Implementations§
source§impl ExprSchema for DFSchema
impl ExprSchema for DFSchema
source§fn nullable(&self, col: &Column) -> Result<bool, DataFusionError>
fn nullable(&self, col: &Column) -> Result<bool, DataFusionError>
source§fn data_type(&self, col: &Column) -> Result<&DataType, DataFusionError>
fn data_type(&self, col: &Column) -> Result<&DataType, DataFusionError>
source§fn metadata(
&self,
col: &Column
) -> Result<&HashMap<String, String>, DataFusionError>
fn metadata( &self, col: &Column ) -> Result<&HashMap<String, String>, DataFusionError>
source§fn data_type_and_nullable(
&self,
col: &Column
) -> Result<(&DataType, bool), DataFusionError>
fn data_type_and_nullable( &self, col: &Column ) -> Result<(&DataType, bool), DataFusionError>
source§impl PartialEq for DFSchema
impl PartialEq for DFSchema
impl Eq for DFSchema
impl StructuralPartialEq for DFSchema
Auto Trait Implementations§
impl Freeze for DFSchema
impl RefUnwindSafe for DFSchema
impl Send for DFSchema
impl Sync for DFSchema
impl Unpin for DFSchema
impl UnwindSafe for DFSchema
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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