@@ -130,36 +130,63 @@ impl FromArrowType<SchemaRef> for DType {
130130 }
131131}
132132
133+ impl TryFromArrowType < SchemaRef > for DType {
134+ fn try_from_arrow ( value : SchemaRef ) -> VortexResult < Self > {
135+ Self :: try_from_arrow ( value. as_ref ( ) )
136+ }
137+ }
138+
133139impl FromArrowType < & Schema > for DType {
134140 fn from_arrow ( value : & Schema ) -> Self {
135- Self :: Struct (
136- StructFields :: from_arrow ( value. fields ( ) ) ,
141+ Self :: try_from_arrow ( value) . vortex_expect ( "arrow schema to dtype" )
142+ }
143+ }
144+
145+ impl TryFromArrowType < & Schema > for DType {
146+ fn try_from_arrow ( value : & Schema ) -> VortexResult < Self > {
147+ Ok ( Self :: Struct (
148+ StructFields :: try_from_arrow ( value. fields ( ) ) ?,
137149 Nullability :: NonNullable , // Must match From<RecordBatch> for Array
138- )
150+ ) )
139151 }
140152}
141153
142154impl FromArrowType < & Fields > for StructFields {
143155 fn from_arrow ( value : & Fields ) -> Self {
144- StructFields :: from_iter ( value. into_iter ( ) . map ( |f| {
145- (
146- FieldName :: from ( f. name ( ) . as_str ( ) ) ,
147- DType :: from_arrow ( f. as_ref ( ) ) ,
148- )
149- } ) )
156+ Self :: try_from_arrow ( value) . vortex_expect ( "arrow fields to struct fields" )
157+ }
158+ }
159+
160+ impl TryFromArrowType < & Fields > for StructFields {
161+ fn try_from_arrow ( value : & Fields ) -> VortexResult < Self > {
162+ value
163+ . into_iter ( )
164+ . map ( |f| {
165+ Ok ( (
166+ FieldName :: from ( f. name ( ) . as_str ( ) ) ,
167+ DType :: try_from_arrow ( f. as_ref ( ) ) ?,
168+ ) )
169+ } )
170+ . collect :: < VortexResult < StructFields > > ( )
150171 }
151172}
152173
153174impl FromArrowType < ( & DataType , Nullability ) > for DType {
154- fn from_arrow ( ( data_type, nullability) : ( & DataType , Nullability ) ) -> Self {
175+ fn from_arrow ( value : ( & DataType , Nullability ) ) -> Self {
176+ Self :: try_from_arrow ( value) . vortex_expect ( "arrow data type to dtype" )
177+ }
178+ }
179+
180+ impl TryFromArrowType < ( & DataType , Nullability ) > for DType {
181+ fn try_from_arrow ( ( data_type, nullability) : ( & DataType , Nullability ) ) -> VortexResult < Self > {
155182 if data_type. is_integer ( ) || data_type. is_floating ( ) {
156- return DType :: Primitive (
157- PType :: try_from_arrow ( data_type) . vortex_expect ( "arrow float/integer to ptype" ) ,
183+ return Ok ( DType :: Primitive (
184+ PType :: try_from_arrow ( data_type) ? ,
158185 nullability,
159- ) ;
186+ ) ) ;
160187 }
161188
162- match data_type {
189+ Ok ( match data_type {
163190 DataType :: Null => DType :: Null ,
164191 DataType :: Decimal32 ( precision, scale)
165192 | DataType :: Decimal64 ( precision, scale)
@@ -189,36 +216,42 @@ impl FromArrowType<(&DataType, Nullability)> for DType {
189216 | DataType :: LargeList ( e)
190217 | DataType :: ListView ( e)
191218 | DataType :: LargeListView ( e) => {
192- DType :: List ( Arc :: new ( Self :: from_arrow ( e. as_ref ( ) ) ) , nullability)
219+ DType :: List ( Arc :: new ( Self :: try_from_arrow ( e. as_ref ( ) ) ? ) , nullability)
193220 }
194221 DataType :: FixedSizeList ( e, size) => DType :: FixedSizeList (
195- Arc :: new ( Self :: from_arrow ( e. as_ref ( ) ) ) ,
222+ Arc :: new ( Self :: try_from_arrow ( e. as_ref ( ) ) ? ) ,
196223 * size as u32 ,
197224 nullability,
198225 ) ,
199- DataType :: Struct ( f) => DType :: Struct ( StructFields :: from_arrow ( f) , nullability) ,
226+ DataType :: Struct ( f) => DType :: Struct ( StructFields :: try_from_arrow ( f) ? , nullability) ,
200227 DataType :: Dictionary ( _, value_type) => {
201- Self :: from_arrow ( ( value_type. as_ref ( ) , nullability) )
228+ Self :: try_from_arrow ( ( value_type. as_ref ( ) , nullability) ) ?
202229 }
203230 DataType :: RunEndEncoded ( _, value_type) => {
204- Self :: from_arrow ( ( value_type. data_type ( ) , nullability) )
231+ Self :: try_from_arrow ( ( value_type. data_type ( ) , nullability) ) ?
205232 }
206- _ => unimplemented ! ( "Arrow data type not yet supported: {:?}" , data_type ) ,
207- }
233+ _ => vortex_bail ! ( "Arrow data type not supported: {data_type :?}" ) ,
234+ } )
208235 }
209236}
210237
211238impl FromArrowType < & Field > for DType {
212239 fn from_arrow ( field : & Field ) -> Self {
240+ Self :: try_from_arrow ( field) . vortex_expect ( "arrow field to dtype" )
241+ }
242+ }
243+
244+ impl TryFromArrowType < & Field > for DType {
245+ fn try_from_arrow ( field : & Field ) -> VortexResult < Self > {
213246 if field
214247 . metadata ( )
215248 . get ( "ARROW:extension:name" )
216249 . map ( |s| s. as_str ( ) )
217250 == Some ( "arrow.parquet.variant" )
218251 {
219- return DType :: Variant ( field. is_nullable ( ) . into ( ) ) ;
252+ return Ok ( DType :: Variant ( field. is_nullable ( ) . into ( ) ) ) ;
220253 }
221- Self :: from_arrow ( ( field. data_type ( ) , field. is_nullable ( ) . into ( ) ) )
254+ Self :: try_from_arrow ( ( field. data_type ( ) , field. is_nullable ( ) . into ( ) ) )
222255 }
223256}
224257
@@ -594,4 +627,30 @@ mod test {
594627
595628 assert_eq ! ( original_dtype, roundtripped_dtype) ;
596629 }
630+
631+ // Regression test for https://github.com/vortex-data/vortex/issues/8346: unsupported Arrow
632+ // types must return an error instead of panicking with `unimplemented!`.
633+ #[ rstest]
634+ #[ case:: duration( DataType :: Duration ( ArrowTimeUnit :: Microsecond ) ) ]
635+ #[ case:: interval( DataType :: Interval ( arrow_schema:: IntervalUnit :: DayTime ) ) ]
636+ #[ case:: fixed_size_binary( DataType :: FixedSizeBinary ( 3 ) ) ]
637+ fn test_try_from_arrow_unsupported_type_errors ( #[ case] data_type : DataType ) {
638+ let err = DType :: try_from_arrow ( ( & data_type, Nullability :: NonNullable ) )
639+ . expect_err ( "unsupported Arrow type should not convert" )
640+ . to_string ( ) ;
641+ assert ! ( err. contains( "not supported" ) , "unexpected error: {err}" ) ;
642+
643+ // The same unsupported type nested in a field or schema must also error cleanly.
644+ let field = Field :: new ( "c0" , data_type, true ) ;
645+ assert ! ( DType :: try_from_arrow( & field) . is_err( ) ) ;
646+ let schema = Schema :: new ( vec ! [ field] ) ;
647+ assert ! ( DType :: try_from_arrow( & schema) . is_err( ) ) ;
648+ }
649+
650+ #[ test]
651+ fn test_try_from_arrow_supported_type_succeeds ( ) -> VortexResult < ( ) > {
652+ let dtype = DType :: try_from_arrow ( ( & DataType :: Int32 , Nullability :: Nullable ) ) ?;
653+ assert_eq ! ( dtype, DType :: Primitive ( PType :: I32 , Nullability :: Nullable ) ) ;
654+ Ok ( ( ) )
655+ }
597656}
0 commit comments