@@ -195,6 +195,19 @@ impl Drop for CudfColumnView {
195195
196196#[ cfg( test) ]
197197mod tests {
198+ use arrow_array:: ffi:: FFI_ArrowSchema ;
199+ use arrow_schema:: DataType ;
200+ use futures:: executor:: block_on;
201+ use vortex_array:: Canonical ;
202+ use vortex_array:: IntoArray ;
203+ use vortex_array:: arrays:: PrimitiveArray ;
204+ use vortex_array:: validity:: Validity ;
205+ use vortex_buffer:: Buffer ;
206+ use vortex_cuda:: CudaSession ;
207+ use vortex_cuda:: arrow:: CudaDeviceArrayExecute ;
208+ use vortex_cuda:: executor:: CudaArrayExt ;
209+ use vortex_session:: VortexSession ;
210+
198211 use super :: * ;
199212
200213 #[ test]
@@ -210,4 +223,63 @@ mod tests {
210223 }
211224 }
212225 }
226+
227+ #[ test]
228+ fn test_primitive_array_to_cudf_tableview ( ) -> Result < ( ) > {
229+ // Create a PrimitiveArray with 100 i64 values
230+ let data: Vec < i64 > = ( 0 ..100 ) . collect ( ) ;
231+ let expected_len = data. len ( ) ;
232+ let primitive_array =
233+ PrimitiveArray :: new ( Buffer :: from ( data) , Validity :: NonNullable ) . into_array ( ) ;
234+
235+ // Create CUDA execution context
236+ let mut cuda_ctx = match CudaSession :: create_execution_ctx ( & VortexSession :: empty ( ) ) . unwrap ( ) ;
237+
238+ // Export as ArrowDeviceArray using CudaDeviceArrayExecute
239+ let device_array = block_on ( Canonical :: execute (
240+ & primitive_array,
241+ primitive_array. clone ( ) ,
242+ & mut cuda_ctx,
243+ ) )
244+ . unwrap ( ) ;
245+
246+ // Synchronize the CUDA stream to ensure the data is ready
247+ cuda_ctx. synchronize_stream ( ) . map_err ( |e| CudfError {
248+ message : e. to_string ( ) ,
249+ } ) ?;
250+
251+ // Create FFI_ArrowSchema from the data type
252+ let mut ffi_schema =
253+ FFI_ArrowSchema :: try_from ( & DataType :: Int64 ) . map_err ( |e| CudfError {
254+ message : format ! ( "Failed to create FFI schema: {}" , e) ,
255+ } ) ?;
256+
257+ // Create cudf context
258+ let cudf_ctx = CudfContext :: new ( ) ?;
259+
260+ // Import into cudf tableview
261+ let tableview = unsafe {
262+ cudf_ctx. tableview_from_device (
263+ ( & raw mut ffi_schema) . cast :: < ArrowSchema > ( ) ,
264+ ( & raw const device_array) . cast :: < ArrowDeviceArray > ( ) ,
265+ ) ?
266+ } ;
267+
268+ // Verify row count
269+ let num_rows = tableview. num_rows ( ) ?;
270+ assert_eq ! ( num_rows, expected_len as i64 , "Row count mismatch" ) ;
271+ println ! (
272+ "Successfully imported PrimitiveArray into cudf tableview with {} rows" ,
273+ num_rows
274+ ) ;
275+
276+ // Verify column count (should be 1 for a primitive array)
277+ let num_columns = tableview. num_columns ( ) ?;
278+ assert_eq ! ( num_columns, 1 , "Column count mismatch" ) ;
279+ println ! ( "Tableview has {} column(s)" , num_columns) ;
280+
281+ // Tableview and cudf_ctx will be deallocated automatically via Drop
282+
283+ Ok ( ( ) )
284+ }
213285}
0 commit comments