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;
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
impl DFSchema
sourcepub fn as_arrow(&self) -> &Schema
pub fn as_arrow(&self) -> &Schema
Return a reference to the inner Arrow Schema
Note this does not have the qualifier information
sourcepub fn inner(&self) -> &SchemaRef
pub fn inner(&self) -> &SchemaRef
Return a reference to the inner Arrow SchemaRef
Note this does not have the qualifier information
sourcepub fn new_with_metadata(
qualified_fields: Vec<(Option<TableReference>, Arc<Field>)>,
metadata: HashMap<String, String>,
) -> Result<Self>
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
sourcepub fn from_unqualified_fields(
fields: Fields,
metadata: HashMap<String, String>,
) -> Result<Self>
pub fn from_unqualified_fields( fields: Fields, metadata: HashMap<String, String>, ) -> Result<Self>
Create a new DFSchema
from a list of Arrow Fields
sourcepub 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
pub fn from_unqualifed_fields( fields: Fields, metadata: HashMap<String, String>, ) -> Result<Self>
from_unqualified_fields
instead (this one’s name is a typo). This method is subject to be removed soonCreate a new DFSchema
from a list of Arrow Fields
sourcepub fn try_from_qualified_schema(
qualifier: impl Into<TableReference>,
schema: &Schema,
) -> Result<Self>
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
.
sourcepub fn from_field_specific_qualified_schema(
qualifiers: Vec<Option<TableReference>>,
schema: &SchemaRef,
) -> Result<Self>
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
sourcepub fn check_names(&self) -> Result<()>
pub fn check_names(&self) -> Result<()>
Check if the schema have some fields with the same name
sourcepub fn with_functional_dependencies(
self,
functional_dependencies: FunctionalDependencies,
) -> Result<Self>
pub fn with_functional_dependencies( self, functional_dependencies: FunctionalDependencies, ) -> Result<Self>
Assigns functional dependencies.
sourcepub fn join(&self, schema: &DFSchema) -> Result<Self>
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.
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) -> &Field
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
sourcepub fn qualified_field(&self, i: usize) -> (Option<&TableReference>, &Field)
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
pub fn index_of_column_by_name( &self, qualifier: Option<&TableReference>, name: &str, ) -> Option<usize>
sourcepub fn maybe_index_of_column(&self, col: &Column) -> Option<usize>
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
sourcepub fn index_of_column(&self, col: &Column) -> Result<usize>
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
sourcepub fn is_column_from_schema(&self, col: &Column) -> bool
pub fn is_column_from_schema(&self, col: &Column) -> bool
Check if the column is in the current schema
sourcepub fn field_with_name(
&self,
qualifier: Option<&TableReference>,
name: &str,
) -> Result<&Field>
pub fn field_with_name( &self, qualifier: Option<&TableReference>, name: &str, ) -> Result<&Field>
Find the field with the given name
sourcepub fn qualified_field_with_name(
&self,
qualifier: Option<&TableReference>,
name: &str,
) -> Result<(Option<&TableReference>, &Field)>
pub fn qualified_field_with_name( &self, qualifier: Option<&TableReference>, name: &str, ) -> Result<(Option<&TableReference>, &Field)>
Find the qualified field with the given name
sourcepub fn fields_with_qualified(&self, qualifier: &TableReference) -> Vec<&Field>
pub fn fields_with_qualified(&self, qualifier: &TableReference) -> Vec<&Field>
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<&Field>
pub fn fields_with_unqualified_name(&self, name: &str) -> Vec<&Field>
Find all fields that match the given name
sourcepub fn qualified_fields_with_unqualified_name(
&self,
name: &str,
) -> Vec<(Option<&TableReference>, &Field)>
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
sourcepub fn columns_with_unqualified_name(&self, name: &str) -> Vec<Column>
pub fn columns_with_unqualified_name(&self, name: &str) -> Vec<Column>
Find all fields that match the given name and convert to column
sourcepub fn qualified_field_with_unqualified_name(
&self,
name: &str,
) -> Result<(Option<&TableReference>, &Field)>
pub fn qualified_field_with_unqualified_name( &self, name: &str, ) -> Result<(Option<&TableReference>, &Field)>
Find the qualified field with the given unqualified name
sourcepub fn field_with_unqualified_name(&self, name: &str) -> Result<&Field>
pub fn field_with_unqualified_name(&self, name: &str) -> Result<&Field>
Find the field with the given name
sourcepub fn field_with_qualified_name(
&self,
qualifier: &TableReference,
name: &str,
) -> Result<&Field>
pub fn field_with_qualified_name( &self, qualifier: &TableReference, name: &str, ) -> Result<&Field>
Find the field with the given qualified name
sourcepub fn field_from_column(&self, column: &Column) -> Result<&Field>
pub fn field_from_column(&self, column: &Column) -> Result<&Field>
Find the field with the given qualified column
sourcepub fn qualified_field_from_column(
&self,
column: &Column,
) -> Result<(Option<&TableReference>, &Field)>
pub fn qualified_field_from_column( &self, column: &Column, ) -> Result<(Option<&TableReference>, &Field)>
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<()>
pub fn check_arrow_schema_type_compatible( &self, arrow_schema: &Schema, ) -> Result<()>
Check to see if fields in 2 Arrow schemas are compatible
sourcepub fn logically_equivalent_names_and_types(&self, other: &Self) -> bool
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.
sourcepub fn equivalent_names_and_types(&self, other: &Self) -> bool
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.
sourcepub fn datatype_is_logically_equal(dt1: &DataType, dt2: &DataType) -> bool
pub fn datatype_is_logically_equal(dt1: &DataType, dt2: &DataType) -> bool
Checks if two DataType
s 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>.
sourcepub fn strip_qualifiers(self) -> Self
pub fn strip_qualifiers(self) -> Self
Strip all field qualifier in schema
sourcepub fn replace_qualifier(self, qualifier: impl Into<TableReference>) -> Self
pub fn replace_qualifier(self, qualifier: impl Into<TableReference>) -> Self
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 AsRef<Arc<Schema>> for DFSchema
impl AsRef<Arc<Schema>> for DFSchema
Allow DFSchema to be converted into an Arrow &SchemaRef
(to clone, for
example)
source§impl ExprSchema for DFSchema
impl ExprSchema 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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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.