Skip to content

Apache DataFusion SQL Query Engine

ARROW-10829: [Rust] [DataFusion] Implement Into<Schema> for DFSchema

Andy Grovecb6b38d890a4
diff --git a/rust/datafusion/src/logical_plan/dfschema.rs b/rust/datafusion/src/logical_plan/dfschema.rs
index 0883425005..d781a0cadc 100644
--- a/rust/datafusion/src/logical_plan/dfschema.rs
+++ b/rust/datafusion/src/logical_plan/dfschema.rs
@@ -199,12 +199,32 @@ impl DFSchema {
))),
}
}
+}
+
+impl Into<Schema> for DFSchema {
+ fn into(self) -> Schema {
+ Schema::new(
+ self.fields
+ .iter()
+ .map(|f| {
+ if f.qualifier().is_some() {
+ Field::new(
+ f.qualified_name().as_str(),
+ f.data_type().to_owned(),
+ f.is_nullable(),
+ )
+ } else {
+ f.field.clone()
+ }
+ })
+ .collect(),
+ )
+ }
+}
- /// Convert to an Arrow schema
- pub fn to_arrow_schema(&self) -> SchemaRef {
- SchemaRef::new(Schema::new(
- self.fields.iter().map(|f| f.field.clone()).collect(),
- ))
+impl Into<SchemaRef> for DFSchema {
+ fn into(self) -> SchemaRef {
+ SchemaRef::new(self.into())
}
}
@@ -326,6 +346,14 @@ mod tests {
Ok(())
}
+ #[test]
+ fn from_qualified_schema_into_arrow_schema() -> Result<()> {
+ let schema = DFSchema::from_qualified("t1", &test_schema_1())?;
+ let arrow_schema: Schema = schema.into();
+ assert_eq!("t1.c0: Boolean, t1.c1: Boolean", arrow_schema.to_string());
+ Ok(())
+ }
+
#[test]
fn join_qualified() -> Result<()> {
let left = DFSchema::from_qualified("t1", &test_schema_1())?;