Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix on DFField to Field conversion: preserve metadata #3965

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions datafusion/common/src/dfschema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ impl DFSchema {
if !can_cast_types(r_field.data_type(), l_field.data_type()) {
Err(DataFusionError::Plan(
format!("Column {} (type: {}) is not compatible with column {} (type: {})",
r_field.name(),
r_field.data_type(),
l_field.name(),
l_field.data_type())))
r_field.name(),
r_field.data_type(),
l_field.name(),
l_field.data_type())))
} else {
Ok(())
}
Expand Down Expand Up @@ -367,21 +367,7 @@ impl From<DFSchema> for Schema {
/// Convert DFSchema into a Schema
fn from(df_schema: DFSchema) -> Self {
Schema::new_with_metadata(
df_schema
.fields
.into_iter()
.map(|f| {
if f.qualifier().is_some() {
Field::new(
f.name().as_str(),
f.data_type().to_owned(),
f.is_nullable(),
)
} else {
f.field
}
})
.collect(),
df_schema.fields.into_iter().map(|f| f.field).collect(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 for fixing a bug by deleting code

df_schema.metadata,
)
}
Expand Down Expand Up @@ -608,6 +594,7 @@ impl DFField {
mod tests {
use super::*;
use arrow::datatypes::DataType;
use std::collections::BTreeMap;

#[test]
fn from_unqualified_field() {
Expand Down Expand Up @@ -805,6 +792,25 @@ mod tests {
Field::new("c1", DataType::Boolean, true),
])
}
#[test]
fn test_dfschema_to_schema_convertion() {
let mut a: DFField = DFField::new(Some("table1"), "a", DataType::Int64, false);
let mut b: DFField = DFField::new(Some("table1"), "b", DataType::Int64, false);
let mut a_metadata = BTreeMap::new();
a_metadata.insert("key".to_string(), "value".to_string());
a.field.set_metadata(Some(a_metadata));
let mut b_metadata = BTreeMap::new();
b_metadata.insert("key".to_string(), "value".to_string());
b.field.set_metadata(Some(b_metadata));

let df_schema = Arc::new(
DFSchema::new_with_metadata([a, b].to_vec(), HashMap::new()).unwrap(),
);
let schema: Schema = df_schema.as_ref().clone().into();
let a_df = df_schema.fields.get(0).unwrap().field();
let a_arrow = schema.fields.get(0).unwrap();
assert_eq!(a_df.metadata(), a_arrow.metadata())
}

fn test_schema_2() -> Schema {
Schema::new(vec![
Expand Down