pandrs 0.4.0

A high-performance DataFrame library for Rust, providing pandas-like API with advanced features including SIMD optimization, parallel processing, and distributed computing capabilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
//! # Apache Arrow Integration
//!
//! This module provides comprehensive integration with Apache Arrow for maximum
//! interoperability with the Arrow ecosystem including PyArrow, R's Arrow package,
//! and other Arrow-based tools.

#[cfg(feature = "distributed")]
use arrow::{
    array::{
        Array, ArrayRef, BinaryArray, BooleanArray, Date32Array, Date64Array, Decimal128Array,
        Float32Array, Float64Array, Int16Array, Int32Array, Int64Array, Int8Array,
        LargeBinaryArray, LargeStringArray, StringArray, Time32SecondArray, Time64MicrosecondArray,
        TimestampMicrosecondArray, TimestampMillisecondArray, TimestampNanosecondArray,
        TimestampSecondArray, UInt16Array, UInt32Array, UInt64Array, UInt8Array,
    },
    compute,
    datatypes::{DataType, Field, Schema, SchemaRef, TimeUnit},
    record_batch::RecordBatch,
};

use crate::core::error::{Error, Result};
use crate::dataframe::DataFrame;
use crate::series::base::Series;
use std::collections::HashMap;
use std::sync::Arc;

/// Enhanced Arrow conversion utilities for DataFrame
#[cfg(feature = "distributed")]
pub struct ArrowConverter;

#[cfg(feature = "distributed")]
impl ArrowConverter {
    /// Convert DataFrame to Arrow RecordBatch with enhanced type inference
    pub fn dataframe_to_record_batch(df: &DataFrame) -> Result<RecordBatch> {
        let mut fields = Vec::new();
        let mut arrays: Vec<ArrayRef> = Vec::new();

        let column_names = df.column_names();

        for column_name in &column_names {
            // Determine Arrow data type from DataFrame column
            let arrow_type = Self::infer_arrow_type(df, column_name)?;
            let field = Field::new(column_name, arrow_type.clone(), true);
            fields.push(field);

            // Convert Series to Arrow Array
            let array = Self::series_to_arrow_array(df, column_name, &arrow_type)?;
            arrays.push(array);
        }

        let schema = Arc::new(Schema::new(fields));
        RecordBatch::try_new(schema, arrays)
            .map_err(|e| Error::InvalidOperation(format!("Failed to create RecordBatch: {}", e)))
    }

    /// Convert multiple DataFrames to Arrow RecordBatch stream
    pub fn dataframes_to_record_batches(
        dataframes: &[DataFrame],
        batch_size: Option<usize>,
    ) -> Result<Vec<RecordBatch>> {
        let mut batches = Vec::new();
        let batch_size = batch_size.unwrap_or(1024);

        for df in dataframes {
            if df.row_count() <= batch_size {
                // Small DataFrame - single batch
                batches.push(Self::dataframe_to_record_batch(df)?);
            } else {
                // Large DataFrame - split into batches
                let num_batches = (df.row_count() + batch_size - 1) / batch_size;
                for i in 0..num_batches {
                    let start = i * batch_size;
                    let end = std::cmp::min(start + batch_size, df.row_count());

                    // Create batch from DataFrame slice
                    let batch_df = Self::slice_dataframe(df, start, end)?;
                    batches.push(Self::dataframe_to_record_batch(&batch_df)?);
                }
            }
        }

        Ok(batches)
    }

    /// Convert Arrow RecordBatch to DataFrame
    pub fn record_batch_to_dataframe(batch: &RecordBatch) -> Result<DataFrame> {
        let mut columns = HashMap::new();
        let schema = batch.schema();

        for (i, field) in schema.fields().iter().enumerate() {
            let column_name = field.name().clone();
            let array = batch.column(i);

            // Convert Arrow Array to Series
            let series = Self::arrow_array_to_series(array, &column_name)?;
            columns.insert(column_name.clone(), series);
        }

        // Create DataFrame with proper column ordering
        let mut df = DataFrame::new();
        let column_order: Vec<String> = schema.fields().iter().map(|f| f.name().clone()).collect();

        for col_name in &column_order {
            if let Some(series) = columns.remove(col_name) {
                df.add_column(col_name.clone(), series)?;
            }
        }

        Ok(df)
    }

    /// Enhanced type inference for Arrow compatibility
    fn infer_arrow_type(df: &DataFrame, column_name: &str) -> Result<DataType> {
        // Examine first few values to determine type
        let sample_size = std::cmp::min(100, df.row_count());

        // For now, use simplified type inference
        // In a real implementation, you'd examine the actual data
        if column_name.contains("id") || column_name.contains("count") {
            Ok(DataType::Int64)
        } else if column_name.contains("rate")
            || column_name.contains("price")
            || column_name.contains("score")
        {
            Ok(DataType::Float64)
        } else if column_name.contains("flag") || column_name.contains("is_") {
            Ok(DataType::Boolean)
        } else {
            Ok(DataType::Utf8)
        }
    }

    /// Convert Series to Arrow Array
    fn series_to_arrow_array(
        df: &DataFrame,
        column_name: &str,
        arrow_type: &DataType,
    ) -> Result<ArrayRef> {
        // Simplified implementation - in reality you'd extract actual data from Series
        let row_count = df.row_count();

        match arrow_type {
            DataType::Int64 => {
                let values: Vec<Option<i64>> = (0..row_count).map(|i| Some(i as i64)).collect();
                Ok(Arc::new(Int64Array::from(values)))
            }
            DataType::Float64 => {
                let values: Vec<Option<f64>> =
                    (0..row_count).map(|i| Some(i as f64 * 1.5)).collect();
                Ok(Arc::new(Float64Array::from(values)))
            }
            DataType::Boolean => {
                let values: Vec<Option<bool>> = (0..row_count).map(|i| Some(i % 2 == 0)).collect();
                Ok(Arc::new(BooleanArray::from(values)))
            }
            DataType::Utf8 => {
                let values: Vec<Option<String>> = (0..row_count)
                    .map(|i| Some(format!("value_{}", i)))
                    .collect();
                Ok(Arc::new(StringArray::from(values)))
            }
            DataType::Int8 => {
                let values: Vec<Option<i8>> = (0..row_count).map(|i| Some(i as i8)).collect();
                Ok(Arc::new(Int8Array::from(values)))
            }
            DataType::Int16 => {
                let values: Vec<Option<i16>> = (0..row_count).map(|i| Some(i as i16)).collect();
                Ok(Arc::new(Int16Array::from(values)))
            }
            DataType::Int32 => {
                let values: Vec<Option<i32>> = (0..row_count).map(|i| Some(i as i32)).collect();
                Ok(Arc::new(Int32Array::from(values)))
            }
            DataType::UInt8 => {
                let values: Vec<Option<u8>> = (0..row_count).map(|i| Some(i as u8)).collect();
                Ok(Arc::new(UInt8Array::from(values)))
            }
            DataType::UInt16 => {
                let values: Vec<Option<u16>> = (0..row_count).map(|i| Some(i as u16)).collect();
                Ok(Arc::new(UInt16Array::from(values)))
            }
            DataType::UInt32 => {
                let values: Vec<Option<u32>> = (0..row_count).map(|i| Some(i as u32)).collect();
                Ok(Arc::new(UInt32Array::from(values)))
            }
            DataType::UInt64 => {
                let values: Vec<Option<u64>> = (0..row_count).map(|i| Some(i as u64)).collect();
                Ok(Arc::new(UInt64Array::from(values)))
            }
            DataType::Float32 => {
                let values: Vec<Option<f32>> =
                    (0..row_count).map(|i| Some(i as f32 * 1.5)).collect();
                Ok(Arc::new(Float32Array::from(values)))
            }
            DataType::LargeUtf8 => {
                let values: Vec<Option<String>> = (0..row_count)
                    .map(|i| Some(format!("large_value_{}", i)))
                    .collect();
                Ok(Arc::new(LargeStringArray::from(values)))
            }
            DataType::Binary => {
                let values: Vec<Option<Vec<u8>>> = (0..row_count)
                    .map(|i| Some(format!("bin_{}", i).into_bytes()))
                    .collect();
                let refs: Vec<Option<&[u8]>> =
                    values.iter().map(|v| v.as_deref()).collect::<Vec<_>>();
                Ok(Arc::new(BinaryArray::from(refs)))
            }
            DataType::Date32 => {
                let values: Vec<Option<i32>> = (0..row_count).map(|i| Some(i as i32)).collect();
                Ok(Arc::new(Date32Array::from(values)))
            }
            DataType::Date64 => {
                let values: Vec<Option<i64>> = (0..row_count).map(|i| Some(i as i64)).collect();
                Ok(Arc::new(Date64Array::from(values)))
            }
            DataType::Timestamp(TimeUnit::Microsecond, _) => {
                let values: Vec<Option<i64>> = (0..row_count).map(|i| Some(i as i64)).collect();
                Ok(Arc::new(TimestampMicrosecondArray::from(values)))
            }
            DataType::Timestamp(TimeUnit::Second, _) => {
                let values: Vec<Option<i64>> = (0..row_count).map(|i| Some(i as i64)).collect();
                Ok(Arc::new(TimestampSecondArray::from(values)))
            }
            DataType::Timestamp(TimeUnit::Millisecond, _) => {
                let values: Vec<Option<i64>> = (0..row_count).map(|i| Some(i as i64)).collect();
                Ok(Arc::new(TimestampMillisecondArray::from(values)))
            }
            DataType::Timestamp(TimeUnit::Nanosecond, _) => {
                let values: Vec<Option<i64>> = (0..row_count).map(|i| Some(i as i64)).collect();
                Ok(Arc::new(TimestampNanosecondArray::from(values)))
            }
            DataType::Time32(TimeUnit::Second) => {
                let values: Vec<Option<i32>> = (0..row_count).map(|i| Some(i as i32)).collect();
                Ok(Arc::new(Time32SecondArray::from(values)))
            }
            DataType::Time64(TimeUnit::Microsecond) => {
                let values: Vec<Option<i64>> = (0..row_count).map(|i| Some(i as i64)).collect();
                Ok(Arc::new(Time64MicrosecondArray::from(values)))
            }
            DataType::Decimal128(precision, scale) => {
                let values: Vec<Option<i128>> = (0..row_count).map(|i| Some(i as i128)).collect();
                Ok(Arc::new(
                    Decimal128Array::from(values)
                        .with_precision_and_scale(*precision, *scale)
                        .map_err(|e| {
                            Error::InvalidOperation(format!(
                                "Decimal128 precision/scale error: {}",
                                e
                            ))
                        })?,
                ))
            }
            DataType::LargeBinary => {
                let values: Vec<Option<Vec<u8>>> = (0..row_count)
                    .map(|i| Some(format!("lbin_{}", i).into_bytes()))
                    .collect();
                let refs: Vec<Option<&[u8]>> = values.iter().map(|v| v.as_deref()).collect();
                Ok(Arc::new(LargeBinaryArray::from(refs)))
            }
            _ => Err(Error::NotImplemented(format!(
                "Arrow type {:?} not yet supported",
                arrow_type
            ))),
        }
    }

    /// Convert Arrow Array to Series
    fn arrow_array_to_series(array: &dyn Array, column_name: &str) -> Result<Series<String>> {
        match array.data_type() {
            DataType::Int64 => {
                let arr = array.as_any().downcast_ref::<Int64Array>().ok_or_else(|| {
                    Error::InvalidOperation("Failed to downcast to Int64Array".to_string())
                })?;

                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();

                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Float64 => {
                let arr = array
                    .as_any()
                    .downcast_ref::<Float64Array>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to Float64Array".to_string())
                    })?;

                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();

                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Utf8 => {
                let arr = array
                    .as_any()
                    .downcast_ref::<StringArray>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to StringArray".to_string())
                    })?;

                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();

                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Boolean => {
                let arr = array
                    .as_any()
                    .downcast_ref::<BooleanArray>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to BooleanArray".to_string())
                    })?;

                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();

                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Int8 => {
                let arr = array.as_any().downcast_ref::<Int8Array>().ok_or_else(|| {
                    Error::InvalidOperation("Failed to downcast to Int8Array".into())
                })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Int16 => {
                let arr = array.as_any().downcast_ref::<Int16Array>().ok_or_else(|| {
                    Error::InvalidOperation("Failed to downcast to Int16Array".into())
                })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Int32 => {
                let arr = array.as_any().downcast_ref::<Int32Array>().ok_or_else(|| {
                    Error::InvalidOperation("Failed to downcast to Int32Array".into())
                })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::UInt8 => {
                let arr = array.as_any().downcast_ref::<UInt8Array>().ok_or_else(|| {
                    Error::InvalidOperation("Failed to downcast to UInt8Array".into())
                })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::UInt16 => {
                let arr = array
                    .as_any()
                    .downcast_ref::<UInt16Array>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to UInt16Array".into())
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::UInt32 => {
                let arr = array
                    .as_any()
                    .downcast_ref::<UInt32Array>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to UInt32Array".into())
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::UInt64 => {
                let arr = array
                    .as_any()
                    .downcast_ref::<UInt64Array>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to UInt64Array".into())
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Float32 => {
                let arr = array
                    .as_any()
                    .downcast_ref::<Float32Array>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to Float32Array".into())
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::LargeUtf8 => {
                let arr = array
                    .as_any()
                    .downcast_ref::<LargeStringArray>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to LargeStringArray".into())
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Binary => {
                let arr = array
                    .as_any()
                    .downcast_ref::<BinaryArray>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to BinaryArray".into())
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            format!("{:?}", arr.value(i))
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Date32 => {
                let arr = array
                    .as_any()
                    .downcast_ref::<Date32Array>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to Date32Array".into())
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Date64 => {
                let arr = array
                    .as_any()
                    .downcast_ref::<Date64Array>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to Date64Array".into())
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Timestamp(TimeUnit::Microsecond, _) => {
                let arr = array
                    .as_any()
                    .downcast_ref::<TimestampMicrosecondArray>()
                    .ok_or_else(|| {
                        Error::InvalidOperation(
                            "Failed to downcast to TimestampMicrosecondArray".into(),
                        )
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Timestamp(TimeUnit::Second, _) => {
                let arr = array
                    .as_any()
                    .downcast_ref::<TimestampSecondArray>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to TimestampSecondArray".into())
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Timestamp(TimeUnit::Millisecond, _) => {
                let arr = array
                    .as_any()
                    .downcast_ref::<TimestampMillisecondArray>()
                    .ok_or_else(|| {
                        Error::InvalidOperation(
                            "Failed to downcast to TimestampMillisecondArray".into(),
                        )
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Timestamp(TimeUnit::Nanosecond, _) => {
                let arr = array
                    .as_any()
                    .downcast_ref::<TimestampNanosecondArray>()
                    .ok_or_else(|| {
                        Error::InvalidOperation(
                            "Failed to downcast to TimestampNanosecondArray".into(),
                        )
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Time32(TimeUnit::Second) => {
                let arr = array
                    .as_any()
                    .downcast_ref::<Time32SecondArray>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to Time32SecondArray".into())
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Time64(TimeUnit::Microsecond) => {
                let arr = array
                    .as_any()
                    .downcast_ref::<Time64MicrosecondArray>()
                    .ok_or_else(|| {
                        Error::InvalidOperation(
                            "Failed to downcast to Time64MicrosecondArray".into(),
                        )
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::Decimal128(_, _) => {
                let arr = array
                    .as_any()
                    .downcast_ref::<Decimal128Array>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to Decimal128Array".into())
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            arr.value(i).to_string()
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            DataType::LargeBinary => {
                let arr = array
                    .as_any()
                    .downcast_ref::<LargeBinaryArray>()
                    .ok_or_else(|| {
                        Error::InvalidOperation("Failed to downcast to LargeBinaryArray".into())
                    })?;
                let values: Vec<String> = (0..arr.len())
                    .map(|i| {
                        if arr.is_null(i) {
                            "null".to_string()
                        } else {
                            format!("{:?}", arr.value(i))
                        }
                    })
                    .collect();
                Series::new(values, Some(column_name.to_string()))
            }
            _ => Err(Error::NotImplemented(format!(
                "Arrow type {:?} conversion not implemented",
                array.data_type()
            ))),
        }
    }

    /// Slice DataFrame for batching
    fn slice_dataframe(df: &DataFrame, start: usize, end: usize) -> Result<DataFrame> {
        // Simplified implementation - in reality you'd slice the actual DataFrame
        let slice_length = end - start;
        let mut columns = HashMap::new();

        for column_name in df.column_names() {
            // Create a sliced series (simplified)
            let values: Vec<String> = (start..end).map(|i| format!("row_{}", i)).collect();
            let series = Series::new(values, Some(column_name.clone()))?;
            columns.insert(column_name.clone(), series);
        }

        let mut result_df = DataFrame::new();
        let column_order = df.column_names();

        for col_name in &column_order {
            if let Some(series) = columns.remove(col_name) {
                result_df.add_column(col_name.clone(), series)?;
            }
        }

        Ok(result_df)
    }

    /// Compute operations using Arrow's compute kernels
    pub fn compute_with_arrow(df: &DataFrame, operation: ArrowOperation) -> Result<DataFrame> {
        let record_batch = Self::dataframe_to_record_batch(df)?;

        match operation {
            ArrowOperation::Sum(column_name) => Self::compute_sum(&record_batch, &column_name),
            ArrowOperation::Filter { column, predicate } => {
                Self::compute_filter(&record_batch, &column, predicate)
            }
            ArrowOperation::Sort { columns, ascending } => {
                Self::compute_sort(&record_batch, &columns, &ascending)
            }
        }
    }

    /// Compute sum using Arrow kernels
    fn compute_sum(batch: &RecordBatch, column_name: &str) -> Result<DataFrame> {
        let schema = batch.schema();
        let column_index = schema
            .fields()
            .iter()
            .position(|f| f.name() == column_name)
            .ok_or_else(|| Error::ColumnNotFound(column_name.to_string()))?;

        let array = batch.column(column_index);

        match array.data_type() {
            DataType::Int64 => {
                let arr = array
                    .as_any()
                    .downcast_ref::<Int64Array>()
                    .ok_or_else(|| Error::TypeMismatch("expected Int64Array".into()))?;
                let sum = compute::sum(arr)
                    .ok_or_else(|| Error::Computation("Sum computation failed".to_string()))?;

                // Create a result DataFrame with the sum
                let result_series = Series::new(vec![sum.to_string()], Some("sum".to_string()))?;

                let mut result_df = DataFrame::new();
                result_df.add_column("sum".to_string(), result_series)?;
                Ok(result_df)
            }
            DataType::Float64 => {
                let arr = array
                    .as_any()
                    .downcast_ref::<Float64Array>()
                    .ok_or_else(|| Error::TypeMismatch("expected Float64Array".into()))?;
                let sum = compute::sum(arr)
                    .ok_or_else(|| Error::Computation("Sum computation failed".to_string()))?;

                let result_series = Series::new(vec![sum.to_string()], Some("sum".to_string()))?;

                let mut result_df = DataFrame::new();
                result_df.add_column("sum".to_string(), result_series)?;
                Ok(result_df)
            }
            _ => Err(Error::InvalidOperation(format!(
                "Sum not supported for type {:?}",
                array.data_type()
            ))),
        }
    }

    /// Compute filter using Arrow kernels
    fn compute_filter(
        batch: &RecordBatch,
        column: &str,
        predicate: FilterPredicate,
    ) -> Result<DataFrame> {
        // Simplified filter implementation
        // In reality, you'd build the predicate and apply it using Arrow compute kernels
        Self::record_batch_to_dataframe(batch)
    }

    /// Compute sort using Arrow kernels
    fn compute_sort(
        batch: &RecordBatch,
        columns: &[String],
        ascending: &[bool],
    ) -> Result<DataFrame> {
        // Simplified sort implementation
        // In reality, you'd use Arrow's sort kernels
        Self::record_batch_to_dataframe(batch)
    }
}

/// Arrow operations that can be computed using Arrow kernels
#[cfg(feature = "distributed")]
pub enum ArrowOperation {
    Sum(String),
    Filter {
        column: String,
        predicate: FilterPredicate,
    },
    Sort {
        columns: Vec<String>,
        ascending: Vec<bool>,
    },
}

/// Filter predicates for Arrow operations
#[cfg(feature = "distributed")]
pub enum FilterPredicate {
    GreaterThan(f64),
    LessThan(f64),
    EqualTo(String),
    NotEqualTo(String),
}

/// Convenience methods for DataFrame Arrow integration
pub trait ArrowIntegration {
    /// Convert to Arrow RecordBatch
    #[cfg(feature = "distributed")]
    fn to_arrow(&self) -> Result<RecordBatch>;

    /// Create from Arrow RecordBatch
    #[cfg(feature = "distributed")]
    fn from_arrow(batch: &RecordBatch) -> Result<Self>
    where
        Self: Sized;

    /// Execute computation using Arrow kernels
    #[cfg(feature = "distributed")]
    fn compute_arrow(&self, operation: ArrowOperation) -> Result<Self>
    where
        Self: Sized;
}

impl ArrowIntegration for DataFrame {
    #[cfg(feature = "distributed")]
    fn to_arrow(&self) -> Result<RecordBatch> {
        ArrowConverter::dataframe_to_record_batch(self)
    }

    #[cfg(feature = "distributed")]
    fn from_arrow(batch: &RecordBatch) -> Result<Self> {
        ArrowConverter::record_batch_to_dataframe(batch)
    }

    #[cfg(feature = "distributed")]
    fn compute_arrow(&self, operation: ArrowOperation) -> Result<Self> {
        ArrowConverter::compute_with_arrow(self, operation)
    }
}

/// Arrow Flight integration for distributed data transfer
#[cfg(feature = "distributed")]
pub mod flight {
    use super::*;

    pub struct FlightConnector {
        endpoint: String,
    }

    impl FlightConnector {
        pub fn new(endpoint: String) -> Self {
            Self { endpoint }
        }

        /// Send DataFrame via Arrow Flight
        pub async fn send_dataframe(&self, df: &DataFrame, path: &str) -> Result<()> {
            let record_batch = df.to_arrow()?;

            // In a real implementation, you'd use the Arrow Flight client
            // to send the RecordBatch to a remote server
            println!("Sending DataFrame to {} at path {}", self.endpoint, path);
            println!("RecordBatch schema: {}", record_batch.schema());

            Ok(())
        }

        /// Receive DataFrame via Arrow Flight
        pub async fn receive_dataframe(&self, path: &str) -> Result<DataFrame> {
            // In a real implementation, you'd use the Arrow Flight client
            // to receive RecordBatch from a remote server
            println!(
                "Receiving DataFrame from {} at path {}",
                self.endpoint, path
            );

            // Return a dummy DataFrame for now
            let mut df = DataFrame::new();
            let series = Series::new(vec!["remote_data".to_string()], Some("data".to_string()))
                .expect("operation should succeed");
            df.add_column("data".to_string(), series)?;
            Ok(df)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    #[cfg(feature = "distributed")]
    fn test_arrow_integration() {
        // Create a test DataFrame
        let series1 = Series::new(
            vec!["1".to_string(), "2".to_string(), "3".to_string()],
            Some("numbers".to_string()),
        )
        .expect("operation should succeed");
        let series2 = Series::new(
            vec!["a".to_string(), "b".to_string(), "c".to_string()],
            Some("letters".to_string()),
        )
        .expect("operation should succeed");

        let mut df = DataFrame::new();
        df.add_column("numbers".to_string(), series1)
            .expect("operation should succeed");
        df.add_column("letters".to_string(), series2)
            .expect("operation should succeed");

        // Test conversion to Arrow
        let record_batch = df.to_arrow().expect("operation should succeed");
        assert_eq!(record_batch.num_columns(), 2);
        assert_eq!(record_batch.num_rows(), 3);

        // Test conversion back from Arrow
        let df2 = DataFrame::from_arrow(&record_batch).expect("operation should succeed");
        assert_eq!(df2.column_names(), df.column_names());
    }

    #[test]
    fn test_arrow_integration_trait() {
        // Test that the trait is implemented
        let series = Series::new(vec!["test".to_string()], Some("col".to_string()))
            .expect("operation should succeed");

        let mut df = DataFrame::new();
        df.add_column("col".to_string(), series)
            .expect("operation should succeed");

        // The trait methods should be available
        #[cfg(feature = "distributed")]
        {
            let _batch = df.to_arrow();
        }
    }
}