alamb opened a new pull request #8888: URL: https://github.com/apache/arrow/pull/8888
While trying to update to the latest arrow code in IOx (https://github.com/influxdata/influxdb_iox/pull/540) I found it quite cumbersome to convert between `SchemaRef` <--> `DFSchemaRef` so I wanted to try and make it easier. This PR is the best I could come up with -- namely Implement a trait to convert from Schema to DFSchema / DFSchemaRef with less typing Basically it avoids patterns such as this: ``` Arc::new(DFSchema::from(&parquet.schema())?), ``` I had grander visions of being able to do something even cleaner and more idomatic like ``` parquet.schema().try_into()? ``` But since an Arc is involved in both sides I couldn't figure out how to make it work with the standard traits. When I tried to do this using the standard Into and TryInto traits, this is what I ended up with (which while you have very fine control over when the schema is copied, I think that control comes at too much expense of readability) ``` #[test] fn into() { // Demonstrate how to convert back and forth between Schema, SchemaRef, DFSchema, and DFSchemaRef let arrow_schema = Schema::new(vec![ Field::new("c0", DataType::Int64, true), ]); let arrow_schema_ref = Arc::new(arrow_schema.clone()); let df_schema = DFSchema::new(vec![ DFField::new(None, "c0", DataType::Int64, true), ]).unwrap(); let df_schema_ref = Arc::new(df_schema.clone()); // Make a matrix of all combinations and conversions assert_eq!(arrow_schema, df_schema.clone().into()); assert_eq!(arrow_schema, df_schema_ref.as_ref().into()); assert_eq!(arrow_schema_ref, Arc::new(df_schema.clone().into())); assert_eq!(arrow_schema_ref, Arc::new(df_schema_ref.as_ref().into())); assert_eq!(df_schema, arrow_schema.clone().try_into().unwrap()); assert_eq!(df_schema, arrow_schema_ref.as_ref().try_into().unwrap()); assert_eq!(df_schema_ref, Arc::new(arrow_schema.clone().try_into().unwrap())); assert_eq!(df_schema_ref, Arc::new(arrow_schema_ref.as_ref().try_into().unwrap())); } ``` I would love any other opinions ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
