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

field_to_json() in arrow_integration_test/ field.rs does not serialize fields metadata #6700

Open
pshampanier opened this issue Nov 7, 2024 · 3 comments
Labels

Comments

@pshampanier
Copy link

Describe the bug
When calling arrow_integration_test::schema_to_json(schema) metadata at the field level are not serialized.

To Reproduce

#[cfg(test)]
mod tests {
    use arrow_integration_test::schema_to_json;
    use arrow_schema::{DataType, Field, Schema};
    use std::collections::HashMap;

    #[test]
    fn test_schema_to_json() {
        let metadata = [("key1".to_string(), "value1".to_string())].iter().cloned().collect::<HashMap<_, _>>();
        let fields = vec![Field::new("a", DataType::Int32, true).with_metadata(metadata.clone())];
        let schema = Schema::new(fields).with_metadata(metadata.clone());
        let json = schema_to_json(&schema);
        assert_eq!(
            serde_json::to_string_pretty(&json).unwrap(),
            serde_json::to_string_pretty(&serde_json::json!({
                "fields": [
                    {
                        "name": "a",
                        "nullable": true,
                        "type": {
                            "bitWidth": 32,
                            "isSigned": true,
                            "name": "int"
                        },
                        "children": [],
                        "metadata": {
                            "key1": "value1"
                        },
                    },
                ],
                "metadata": {
                    "key1": "value1"
                },
            }))
            .unwrap()
        );
    }
}

Expected behavior
Expected:

{
  "fields": [
    {
      "children": [],
      "metadata": {
        "key1": "value1"
      },
      "name": "a",
      "nullable": true,
      "type": {
        "bitWidth": 32,
        "isSigned": true,
        "name": "int"
      }
    }
  ],
  "metadata": {
    "key1": "value1"
  }
}

Found:

{
  "fields": [
    {
      "children": [],
      "name": "a",
      "nullable": true,
      "type": {
        "bitWidth": 32,
        "isSigned": true,
        "name": "int"
      }
    }
  ],
  "metadata": {
    "key1": "value1"
  }
}

The metadata key is available at the schema level but missing for fields.
Additional context
Tested with arrow version = "53.1.0"

@pshampanier pshampanier added the bug label Nov 7, 2024
@alamb
Copy link
Contributor

alamb commented Nov 7, 2024

Thanks for the report -- so that sounds like field metadata is being lost somewhere?

@pshampanier
Copy link
Author

Not lost, just omitted at serialization:

pub fn field_to_json(field: &Field) -> serde_json::Value {
let children: Vec<serde_json::Value> = match field.data_type() {
DataType::Struct(fields) => fields.iter().map(|x| field_to_json(x.as_ref())).collect(),
DataType::List(field)
| DataType::LargeList(field)
| DataType::FixedSizeList(field, _)
| DataType::Map(field, _) => vec![field_to_json(field)],
_ => vec![],
};
match field.data_type() {
DataType::Dictionary(ref index_type, ref value_type) => serde_json::json!({
"name": field.name(),
"nullable": field.is_nullable(),
"type": data_type_to_json(value_type),
"children": children,
"dictionary": {
"id": field.dict_id().unwrap(),
"indexType": data_type_to_json(index_type),
"isOrdered": field.dict_is_ordered().unwrap(),
}
}),
_ => serde_json::json!({
"name": field.name(),
"nullable": field.is_nullable(),
"type": data_type_to_json(field.data_type()),
"children": children
}),
}
}

Line 292 should be:

 "children": children,
 "metadata": serde_json::to_value(field.metadata()).unwrap()

Just like in:

pub fn schema_to_json(schema: &Schema) -> serde_json::Value {
serde_json::json!({
"fields": schema.fields().iter().map(|f| field_to_json(f.as_ref())).collect::<Vec<_>>(),
"metadata": serde_json::to_value(schema.metadata()).unwrap()
})
}

I tested it, and it's working fine.

@alamb
Copy link
Contributor

alamb commented Nov 13, 2024

Would you be willing to create a PR to fix this issue @pshampanier ?

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants