arrow-zerobus-sdk-wrapper 0.8.1

Cross-platform Rust SDK wrapper for Databricks Zerobus with Python bindings
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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
//! PyO3 bindings implementation
//!
//! This module implements Python bindings for the Zerobus SDK Wrapper,
//! providing a Pythonic API that matches the Rust API functionality.

// PyO3's #[pymethods] macro generates non-local impl blocks, which is necessary for bindings
// This lint must be disabled for PyO3 bindings to work correctly
#![allow(non_local_definitions)]

use crate::config::OtlpSdkConfig;
use crate::config::WrapperConfiguration;
use crate::error::ZerobusError;
use crate::wrapper::{TransmissionResult, ZerobusWrapper};
use arrow::datatypes::DataType;
use arrow::record_batch::RecordBatch;
use pyo3::exceptions::{PyException, PyNotImplementedError, PyTypeError};
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyModule};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::runtime::Runtime;

/// Register all Python classes and functions in the module
pub fn register_module(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_class::<PyZerobusWrapper>()?;
    m.add_class::<PyTransmissionResult>()?;
    m.add_class::<PyWrapperConfiguration>()?;

    // Register exception classes - base class must be registered first
    m.add_class::<PyZerobusError>()?;
    m.add_class::<PyConfigurationError>()?;
    m.add_class::<PyAuthenticationError>()?;
    m.add_class::<PyConnectionError>()?;
    m.add_class::<PyConversionError>()?;
    m.add_class::<PyTransmissionError>()?;
    m.add_class::<PyRetryExhausted>()?;
    m.add_class::<PyTokenRefreshError>()?;

    Ok(())
}

/// Convert Rust ZerobusError to Python exception
// Note: Made pub for re-export to tests (which are in a separate crate)
pub fn rust_error_to_python_error(error: ZerobusError) -> PyErr {
    match error {
        ZerobusError::ConfigurationError(msg) => PyErr::new::<PyConfigurationError, _>(msg),
        ZerobusError::AuthenticationError(msg) => PyErr::new::<PyAuthenticationError, _>(msg),
        ZerobusError::ConnectionError(msg) => PyErr::new::<PyConnectionError, _>(msg),
        ZerobusError::ConversionError(msg) => PyErr::new::<PyConversionError, _>(msg),
        ZerobusError::TransmissionError(msg) => PyErr::new::<PyTransmissionError, _>(msg),
        ZerobusError::RetryExhausted(msg) => PyErr::new::<PyRetryExhausted, _>(msg),
        ZerobusError::TokenRefreshError(msg) => PyErr::new::<PyTokenRefreshError, _>(msg),
    }
}

/// Parse error string to ZerobusError
///
/// Handles strings like "ConversionError: message" or plain "message"
fn parse_error_string(error_msg: String) -> ZerobusError {
    if error_msg.starts_with("ConversionError:") {
        ZerobusError::ConversionError(
            error_msg
                .strip_prefix("ConversionError:")
                .unwrap()
                .trim()
                .to_string(),
        )
    } else if error_msg.starts_with("TransmissionError:") {
        ZerobusError::TransmissionError(
            error_msg
                .strip_prefix("TransmissionError:")
                .unwrap()
                .trim()
                .to_string(),
        )
    } else if error_msg.starts_with("ConnectionError:") {
        ZerobusError::ConnectionError(
            error_msg
                .strip_prefix("ConnectionError:")
                .unwrap()
                .trim()
                .to_string(),
        )
    } else if error_msg.starts_with("AuthenticationError:") {
        ZerobusError::AuthenticationError(
            error_msg
                .strip_prefix("AuthenticationError:")
                .unwrap()
                .trim()
                .to_string(),
        )
    } else if error_msg.starts_with("ConfigurationError:") {
        ZerobusError::ConfigurationError(
            error_msg
                .strip_prefix("ConfigurationError:")
                .unwrap()
                .trim()
                .to_string(),
        )
    } else if error_msg.starts_with("RetryExhausted:") {
        ZerobusError::RetryExhausted(
            error_msg
                .strip_prefix("RetryExhausted:")
                .unwrap()
                .trim()
                .to_string(),
        )
    } else if error_msg.starts_with("TokenRefreshError:") {
        ZerobusError::TokenRefreshError(
            error_msg
                .strip_prefix("TokenRefreshError:")
                .unwrap()
                .trim()
                .to_string(),
        )
    } else {
        // Default to ConversionError if no prefix
        ZerobusError::ConversionError(error_msg)
    }
}

// Exception classes
// Note: In PyO3, all custom exceptions must extend PyException directly.
// We cannot use a custom base class (PyZerobusError) for other exceptions
// because PyO3 doesn't support that pattern. Instead, all exceptions extend
// PyException directly, but they're logically grouped as ZerobusError exceptions.
#[pyclass(name = "ZerobusError", extends=PyException)]
#[derive(Debug)]
pub struct PyZerobusError;

#[pymethods]
impl PyZerobusError {
    // Base exception class for Zerobus errors
}

// Exception classes with message storage for Python construction
#[pyclass(name = "ConfigurationError", extends=PyException)]
#[derive(Debug)]
pub struct PyConfigurationError {
    message: String,
}

#[pyclass(name = "AuthenticationError", extends=PyException)]
#[derive(Debug)]
pub struct PyAuthenticationError {
    message: String,
}

#[pyclass(name = "ConnectionError", extends=PyException)]
#[derive(Debug)]
pub struct PyConnectionError {
    message: String,
}

#[pyclass(name = "ConversionError", extends=PyException)]
#[derive(Debug)]
pub struct PyConversionError {
    message: String,
}

#[pyclass(name = "TransmissionError", extends=PyException)]
#[derive(Debug)]
pub struct PyTransmissionError {
    message: String,
}

#[pyclass(name = "RetryExhausted", extends=PyException)]
#[derive(Debug)]
pub struct PyRetryExhausted {
    message: String,
}

#[pyclass(name = "TokenRefreshError", extends=PyException)]
#[derive(Debug)]
pub struct PyTokenRefreshError {
    message: String,
}

// Internal helper methods for creating PyErr from Rust
// These are used by rust_error_to_python_error to convert Rust errors to Python exceptions
#[allow(dead_code)] // Used indirectly via rust_error_to_python_error
impl PyConfigurationError {
    fn new_err(msg: String) -> PyErr {
        PyErr::new::<PyConfigurationError, _>(msg)
    }
}

#[allow(dead_code)] // Used indirectly via rust_error_to_python_error
impl PyAuthenticationError {
    fn new_err(msg: String) -> PyErr {
        PyErr::new::<PyAuthenticationError, _>(msg)
    }
}

#[allow(dead_code)] // Used indirectly via rust_error_to_python_error
impl PyConnectionError {
    fn new_err(msg: String) -> PyErr {
        PyErr::new::<PyConnectionError, _>(msg)
    }
}

#[allow(dead_code)] // Used indirectly via rust_error_to_python_error
impl PyConversionError {
    fn new_err(msg: String) -> PyErr {
        PyErr::new::<PyConversionError, _>(msg)
    }
}

#[allow(dead_code)] // Used indirectly via rust_error_to_python_error
impl PyTransmissionError {
    fn new_err(msg: String) -> PyErr {
        PyErr::new::<PyTransmissionError, _>(msg)
    }
}

#[allow(dead_code)] // Used indirectly via rust_error_to_python_error
impl PyRetryExhausted {
    fn new_err(msg: String) -> PyErr {
        PyErr::new::<PyRetryExhausted, _>(msg)
    }
}

#[allow(dead_code)] // Used indirectly via rust_error_to_python_error
impl PyTokenRefreshError {
    fn new_err(msg: String) -> PyErr {
        PyErr::new::<PyTokenRefreshError, _>(msg)
    }
}

// Python constructors for error classes
#[pymethods]
impl PyConfigurationError {
    #[new]
    fn new(msg: String) -> Self {
        Self { message: msg }
    }

    fn __str__(&self) -> &str {
        &self.message
    }
}

#[pymethods]
impl PyAuthenticationError {
    #[new]
    fn new(msg: String) -> Self {
        Self { message: msg }
    }

    fn __str__(&self) -> &str {
        &self.message
    }
}

#[pymethods]
impl PyConnectionError {
    #[new]
    fn new(msg: String) -> Self {
        Self { message: msg }
    }

    fn __str__(&self) -> &str {
        &self.message
    }
}

#[pymethods]
impl PyConversionError {
    #[new]
    fn new(msg: String) -> Self {
        Self { message: msg }
    }

    fn __str__(&self) -> &str {
        &self.message
    }
}

#[pymethods]
impl PyTransmissionError {
    #[new]
    fn new(msg: String) -> Self {
        Self { message: msg }
    }

    fn __str__(&self) -> &str {
        &self.message
    }
}

#[pymethods]
impl PyRetryExhausted {
    #[new]
    fn new(msg: String) -> Self {
        Self { message: msg }
    }

    fn __str__(&self) -> &str {
        &self.message
    }
}

#[pymethods]
impl PyTokenRefreshError {
    #[new]
    fn new(msg: String) -> Self {
        Self { message: msg }
    }

    fn __str__(&self) -> &str {
        &self.message
    }
}

/// Python wrapper for WrapperConfiguration
#[pyclass(name = "WrapperConfiguration")]
#[derive(Clone)]
#[allow(non_local_definitions)]
pub struct PyWrapperConfiguration {
    inner: WrapperConfiguration,
}

#[pymethods]
#[allow(clippy::too_many_arguments)]
impl PyWrapperConfiguration {
    /// Initialize WrapperConfiguration with parameters.
    ///
    /// Args:
    ///     endpoint: Zerobus endpoint URL (required)
    ///     table_name: Target table name (required)
    ///     client_id: OAuth2 client ID (optional when zerobus_writer_disabled is True)
    ///     client_secret: OAuth2 client secret (optional when zerobus_writer_disabled is True)
    ///     unity_catalog_url: Unity Catalog URL (optional when zerobus_writer_disabled is True)
    ///     observability_enabled: Enable OpenTelemetry observability
    ///     observability_config: OpenTelemetry configuration dict
    ///     debug_enabled: Enable debug file output (legacy, use debug_arrow_enabled and debug_protobuf_enabled instead)
    ///     debug_arrow_enabled: Enable Arrow debug file output (optional, default: None - uses debug_enabled if not set)
    ///     debug_protobuf_enabled: Enable Protobuf debug file output (optional, default: None - uses debug_enabled if not set)
    ///     debug_output_dir: Output directory for debug files (required when any debug format is enabled)
    ///     debug_flush_interval_secs: Debug file flush interval in seconds
    ///     debug_max_file_size: Maximum debug file size before rotation
    ///     debug_max_files_retained: Maximum number of rotated debug files to retain per type (optional, default: 10, None = unlimited)
    ///     retry_max_attempts: Maximum retry attempts for transient failures
    ///     retry_base_delay_ms: Base delay in milliseconds for exponential backoff
    ///     retry_max_delay_ms: Maximum delay in milliseconds for exponential backoff
    ///     zerobus_writer_disabled: Disable Zerobus SDK transmission while maintaining debug output
    ///
    /// Raises:
    ///     ZerobusError: If configuration is invalid or initialization fails
    ///         - ConfigurationError if debug_enabled is True but debug_output_dir is None
    ///         - ConfigurationError if zerobus_writer_disabled is True but debug_enabled is False
    #[new]
    #[pyo3(signature = (endpoint, table_name, *, client_id=None, client_secret=None, unity_catalog_url=None, observability_enabled=false, observability_config=None, debug_enabled=false, debug_arrow_enabled=None, debug_protobuf_enabled=None, debug_output_dir=None, debug_flush_interval_secs=5, debug_max_file_size=None, debug_max_files_retained=10, retry_max_attempts=5, retry_base_delay_ms=100, retry_max_delay_ms=30000, zerobus_writer_disabled=false))]
    pub fn new(
        endpoint: String,
        table_name: String,
        client_id: Option<String>,
        client_secret: Option<String>,
        unity_catalog_url: Option<String>,
        observability_enabled: bool,
        observability_config: Option<PyObject>,
        debug_enabled: bool,
        debug_arrow_enabled: Option<bool>,
        debug_protobuf_enabled: Option<bool>,
        debug_output_dir: Option<String>,
        debug_flush_interval_secs: u64,
        debug_max_file_size: Option<u64>,
        debug_max_files_retained: Option<usize>,
        retry_max_attempts: u32,
        retry_base_delay_ms: u64,
        retry_max_delay_ms: u64,
        zerobus_writer_disabled: bool,
    ) -> PyResult<Self> {
        let mut config = WrapperConfiguration::new(endpoint, table_name);

        if let (Some(cid), Some(cs)) = (client_id, client_secret) {
            config = config.with_credentials(cid, cs);
        }

        if let Some(url) = unity_catalog_url {
            config = config.with_unity_catalog(url);
        }

        if observability_enabled {
            let otlp_config = if let Some(config_obj) = observability_config {
                Python::with_gil(|py| {
                    let dict = config_obj.extract::<&PyDict>(py)?;
                    let endpoint = dict
                        .get_item("endpoint")?
                        .and_then(|v| v.extract::<String>().ok());

                    let output_dir = dict
                        .get_item("output_dir")?
                        .and_then(|v| v.extract::<String>().ok())
                        .map(std::path::PathBuf::from);

                    let write_interval_secs = dict
                        .get_item("write_interval_secs")?
                        .and_then(|v| v.extract::<u64>().ok())
                        .unwrap_or(5);

                    let log_level = dict
                        .get_item("log_level")?
                        .and_then(|v| v.extract::<String>().ok())
                        .unwrap_or_else(|| "info".to_string());

                    let otlp_config = OtlpSdkConfig {
                        endpoint,
                        output_dir,
                        write_interval_secs,
                        log_level,
                    };
                    // Validate configuration before using it
                    otlp_config.validate().map_err(|e| {
                        PyException::new_err(format!("Invalid OTLP SDK configuration: {}", e))
                    })?;
                    Ok::<OtlpSdkConfig, PyErr>(otlp_config)
                })?
            } else {
                OtlpSdkConfig::default()
            };
            config = config.with_observability(otlp_config);
        }

        // Handle new separate flags
        if let Some(arrow_enabled) = debug_arrow_enabled {
            config.debug_arrow_enabled = arrow_enabled;
        }
        if let Some(protobuf_enabled) = debug_protobuf_enabled {
            config.debug_protobuf_enabled = protobuf_enabled;
        }

        // Handle legacy debug_enabled flag (backward compatibility)
        if debug_enabled {
            // If new flags not explicitly set, enable both formats
            if debug_arrow_enabled.is_none() && debug_protobuf_enabled.is_none() {
                config.debug_arrow_enabled = true;
                config.debug_protobuf_enabled = true;
            }
            config.debug_enabled = true;
        }

        // Set output directory and other settings if any format is enabled
        let any_debug_enabled =
            config.debug_arrow_enabled || config.debug_protobuf_enabled || config.debug_enabled;

        if any_debug_enabled {
            if let Some(output_dir) = debug_output_dir {
                config.debug_output_dir = Some(PathBuf::from(output_dir));
                config.debug_flush_interval_secs = debug_flush_interval_secs;
                config.debug_max_file_size = debug_max_file_size;
                config.debug_max_files_retained = debug_max_files_retained;
            } else {
                // If any debug format is enabled but debug_output_dir is None, raise an error
                // This prevents silent failure where debug flags are ignored
                return Err(PyConfigurationError::new_err(
                    "debug_output_dir is required when any debug format is enabled. \
                    Either provide debug_output_dir or disable all debug flags."
                        .to_string(),
                ));
            }
        }

        config =
            config.with_retry_config(retry_max_attempts, retry_base_delay_ms, retry_max_delay_ms);

        if zerobus_writer_disabled {
            config = config.with_zerobus_writer_disabled(true);
        }

        Ok(Self { inner: config })
    }

    fn validate(&self) -> PyResult<()> {
        self.inner.validate().map_err(rust_error_to_python_error)?;
        Ok(())
    }

    // Getters for configuration fields
    #[getter]
    fn endpoint(&self) -> String {
        self.inner.zerobus_endpoint.clone()
    }

    #[getter]
    fn table_name(&self) -> String {
        self.inner.table_name.clone()
    }

    #[getter]
    fn client_id(&self) -> Option<String> {
        use secrecy::ExposeSecret;
        self.inner
            .client_id
            .as_ref()
            .map(|s| s.expose_secret().to_string())
    }

    #[getter]
    fn client_secret(&self) -> Option<String> {
        use secrecy::ExposeSecret;
        self.inner
            .client_secret
            .as_ref()
            .map(|s| s.expose_secret().to_string())
    }

    #[getter]
    fn unity_catalog_url(&self) -> Option<String> {
        self.inner.unity_catalog_url.clone()
    }

    #[getter]
    fn debug_enabled(&self) -> bool {
        self.inner.debug_enabled
    }

    #[getter]
    fn debug_output_dir(&self) -> Option<String> {
        self.inner
            .debug_output_dir
            .as_ref()
            .map(|p| p.to_string_lossy().to_string())
    }

    #[getter]
    fn debug_flush_interval_secs(&self) -> u64 {
        self.inner.debug_flush_interval_secs
    }

    #[getter]
    fn debug_max_file_size(&self) -> Option<u64> {
        self.inner.debug_max_file_size
    }

    #[getter]
    fn retry_max_attempts(&self) -> u32 {
        self.inner.retry_max_attempts
    }

    #[getter]
    fn retry_base_delay_ms(&self) -> u64 {
        self.inner.retry_base_delay_ms
    }

    #[getter]
    fn retry_max_delay_ms(&self) -> u64 {
        self.inner.retry_max_delay_ms
    }

    #[getter]
    fn observability_enabled(&self) -> bool {
        self.inner.observability_enabled
    }

    #[getter]
    fn zerobus_writer_disabled(&self) -> bool {
        self.inner.zerobus_writer_disabled
    }
}

/// Python wrapper for TransmissionResult
#[pyclass(name = "TransmissionResult")]
#[derive(Clone)]
pub struct PyTransmissionResult {
    // Made pub for tests (which are in a separate crate)
    #[allow(dead_code)] // Used in tests
    pub inner: TransmissionResult,
}

#[pymethods]
impl PyTransmissionResult {
    /// Create a new TransmissionResult instance
    ///
    /// Args:
    ///     success: Whether transmission succeeded
    ///     error: Optional batch-level error message (string)
    ///     attempts: Number of retry attempts made
    ///     latency_ms: Optional transmission latency in milliseconds
    ///     batch_size_bytes: Size of transmitted batch in bytes
    ///     failed_rows: Optional list of (row_index, error_message) tuples for failed rows
    ///     successful_rows: Optional list of row indices that succeeded
    ///     total_rows: Total number of rows in the batch
    ///     successful_count: Number of rows that succeeded
    ///     failed_count: Number of rows that failed
    ///     message: Optional message (ignored, kept for backward compatibility)
    #[new]
    #[allow(clippy::too_many_arguments)]
    #[pyo3(signature = (success, *, error=None, attempts=1, latency_ms=None, batch_size_bytes=0, failed_rows=None, successful_rows=None, total_rows=0, successful_count=0, failed_count=0, message=None))]
    pub fn new(
        success: bool,
        error: Option<String>,
        attempts: u32,
        latency_ms: Option<u64>,
        batch_size_bytes: usize,
        failed_rows: Option<Vec<(usize, String)>>,
        successful_rows: Option<Vec<usize>>,
        total_rows: usize,
        successful_count: usize,
        failed_count: usize,
        #[allow(unused_variables)] message: Option<String>,
    ) -> Self {
        // Convert string error messages to ZerobusError
        let rust_failed_rows = failed_rows.map(|rows| {
            rows.into_iter()
                .map(|(idx, error_msg)| (idx, parse_error_string(error_msg)))
                .collect()
        });

        // Convert string error to ZerobusError
        let rust_error = error.map(parse_error_string);

        Self {
            inner: TransmissionResult {
                success,
                error: rust_error,
                attempts,
                latency_ms,
                batch_size_bytes,
                failed_rows: rust_failed_rows,
                successful_rows,
                total_rows,
                successful_count,
                failed_count,
            },
        }
    }

    #[getter]
    pub fn success(&self) -> bool {
        self.inner.success
    }

    #[getter]
    pub fn error(&self) -> Option<String> {
        self.inner.error.as_ref().map(|e| e.to_string())
    }

    #[getter]
    pub fn attempts(&self) -> u32 {
        self.inner.attempts
    }

    #[getter]
    pub fn latency_ms(&self) -> Option<u64> {
        self.inner.latency_ms
    }

    #[getter]
    pub fn batch_size_bytes(&self) -> usize {
        self.inner.batch_size_bytes
    }

    /// Get failed rows with their errors
    ///
    /// Returns a list of tuples (row_index, error_message) for rows that failed.
    /// Returns None if all rows succeeded.
    #[getter]
    pub fn failed_rows(&self) -> Option<Vec<(usize, String)>> {
        self.inner.failed_rows.as_ref().map(|rows| {
            rows.iter()
                .map(|(idx, error)| (*idx, error.to_string()))
                .collect()
        })
    }

    /// Get indices of successfully written rows
    ///
    /// Returns a list of row indices that were successfully written.
    /// Returns None if all rows failed.
    #[getter]
    pub fn successful_rows(&self) -> Option<Vec<usize>> {
        self.inner.successful_rows.clone()
    }

    /// Get total number of rows in the batch
    #[getter]
    pub fn total_rows(&self) -> usize {
        self.inner.total_rows
    }

    /// Get count of successfully written rows
    #[getter]
    pub fn successful_count(&self) -> usize {
        self.inner.successful_count
    }

    /// Get count of failed rows
    #[getter]
    pub fn failed_count(&self) -> usize {
        self.inner.failed_count
    }

    /// Get indices of failed rows
    ///
    /// Returns a list of row indices that failed, or empty list if none failed.
    pub fn get_failed_row_indices(&self) -> Vec<usize> {
        self.inner.get_failed_row_indices()
    }

    /// Get indices of successful rows
    ///
    /// Returns a list of row indices that succeeded, or empty list if none succeeded.
    pub fn get_successful_row_indices(&self) -> Vec<usize> {
        self.inner.get_successful_row_indices()
    }

    /// Extract a RecordBatch containing only the failed rows from the original batch
    ///
    /// Args:
    ///     original_batch: The original PyArrow RecordBatch that was sent
    ///
    /// Returns:
    ///     PyArrow RecordBatch containing only the failed rows, or None if there are no failed rows.
    ///     Rows are extracted in the order they appear in failed_rows.
    pub fn extract_failed_batch(
        &self,
        py: Python,
        original_batch: PyObject,
    ) -> PyResult<Option<PyObject>> {
        let rust_batch = pyarrow_to_rust_batch(py, original_batch)?;

        match self.inner.extract_failed_batch(&rust_batch) {
            Some(batch) => {
                // Convert Rust RecordBatch back to PyArrow RecordBatch
                let py_batch = rust_batch_to_pyarrow(py, &batch)?;
                Ok(Some(py_batch))
            }
            None => Ok(None),
        }
    }

    /// Extract a RecordBatch containing only the successful rows from the original batch
    ///
    /// Args:
    ///     original_batch: The original PyArrow RecordBatch that was sent
    ///
    /// Returns:
    ///     PyArrow RecordBatch containing only the successful rows, or None if there are no successful rows.
    ///     Rows are extracted in the order they appear in successful_rows.
    pub fn extract_successful_batch(
        &self,
        py: Python,
        original_batch: PyObject,
    ) -> PyResult<Option<PyObject>> {
        let rust_batch = pyarrow_to_rust_batch(py, original_batch)?;

        match self.inner.extract_successful_batch(&rust_batch) {
            Some(batch) => {
                // Convert Rust RecordBatch back to PyArrow RecordBatch
                let py_batch = rust_batch_to_pyarrow(py, &batch)?;
                Ok(Some(py_batch))
            }
            None => Ok(None),
        }
    }

    /// Get indices of failed rows filtered by error type
    ///
    /// Args:
    ///     error_type: String representing the error type to filter by
    ///                 (e.g., "ConversionError", "TransmissionError", "ConnectionError")
    ///
    /// Returns:
    ///     List of row indices for failed rows that match the error type.
    pub fn get_failed_row_indices_by_error_type(&self, error_type: &str) -> Vec<usize> {
        self.inner
            .get_failed_row_indices_by_error_type(|error| match error_type {
                "ConversionError" => matches!(error, ZerobusError::ConversionError(_)),
                "TransmissionError" => matches!(error, ZerobusError::TransmissionError(_)),
                "ConnectionError" => matches!(error, ZerobusError::ConnectionError(_)),
                "AuthenticationError" => matches!(error, ZerobusError::AuthenticationError(_)),
                "ConfigurationError" => matches!(error, ZerobusError::ConfigurationError(_)),
                "RetryExhausted" => matches!(error, ZerobusError::RetryExhausted(_)),
                "TokenRefreshError" => matches!(error, ZerobusError::TokenRefreshError(_)),
                _ => false,
            })
    }

    /// Check if this result represents a partial success (some rows succeeded, some failed)
    ///
    /// Returns:
    ///     True if there are both successful and failed rows.
    pub fn is_partial_success(&self) -> bool {
        self.inner.is_partial_success()
    }

    /// Check if there are any failed rows
    ///
    /// Returns:
    ///     True if failed_rows contains any entries.
    pub fn has_failed_rows(&self) -> bool {
        self.inner.has_failed_rows()
    }

    /// Check if there are any successful rows
    ///
    /// Returns:
    ///     True if successful_rows contains any entries.
    pub fn has_successful_rows(&self) -> bool {
        self.inner.has_successful_rows()
    }

    /// Group failed rows by error type
    ///
    /// Returns:
    ///     Dictionary mapping error type names to lists of row indices.
    pub fn group_errors_by_type(&self) -> HashMap<String, Vec<usize>> {
        self.inner.group_errors_by_type()
    }

    /// Get error statistics for this transmission result
    ///
    /// Returns:
    ///     Dictionary containing error statistics including:
    ///     - total_rows: Total number of rows
    ///     - successful_count: Number of successful rows
    ///     - failed_count: Number of failed rows
    ///     - success_rate: Success rate (0.0 to 1.0)
    ///     - failure_rate: Failure rate (0.0 to 1.0)
    ///     - error_type_counts: Dictionary mapping error types to counts
    pub fn get_error_statistics(&self, py: Python) -> PyResult<PyObject> {
        let stats = self.inner.get_error_statistics();
        let dict = PyDict::new(py);
        dict.set_item("total_rows", stats.total_rows)?;
        dict.set_item("successful_count", stats.successful_count)?;
        dict.set_item("failed_count", stats.failed_count)?;
        dict.set_item("success_rate", stats.success_rate)?;
        dict.set_item("failure_rate", stats.failure_rate)?;

        let error_type_counts = PyDict::new(py);
        for (error_type, count) in stats.error_type_counts {
            error_type_counts.set_item(error_type, count)?;
        }
        dict.set_item("error_type_counts", error_type_counts)?;

        Ok(dict.to_object(py))
    }

    /// Get all error messages from failed rows
    ///
    /// Returns:
    ///     List of error message strings for all failed rows.
    pub fn get_error_messages(&self) -> Vec<String> {
        self.inner.get_error_messages()
    }
}

/// Python wrapper for ZerobusWrapper
///
/// Thread-safe wrapper that handles Arrow RecordBatch to Protobuf conversion,
/// authentication, retry logic, and transmission to Zerobus.
#[pyclass(name = "ZerobusWrapper")]
#[allow(non_local_definitions)]
pub struct PyZerobusWrapper {
    inner: Arc<ZerobusWrapper>,
    runtime: Arc<Runtime>,
}

#[pymethods]
impl PyZerobusWrapper {
    #[new]
    fn new(config: PyWrapperConfiguration) -> PyResult<Self> {
        // Validate configuration
        config.validate()?;

        // Create Tokio runtime for async operations
        let runtime = Runtime::new()
            .map_err(|e| PyException::new_err(format!("Failed to create Tokio runtime: {}", e)))?;

        // Initialize wrapper
        let wrapper = runtime.block_on(async {
            ZerobusWrapper::new(config.inner.clone())
                .await
                .map_err(rust_error_to_python_error)
        })?;

        Ok(Self {
            inner: Arc::new(wrapper),
            runtime: Arc::new(runtime),
        })
    }

    /// Send an Arrow RecordBatch to Zerobus.
    ///
    /// Converts PyArrow RecordBatch to Rust RecordBatch and transmits to Zerobus
    /// with automatic retry on transient failures.
    ///
    /// Args:
    ///     batch: PyArrow RecordBatch to send
    ///
    /// Returns:
    ///     TransmissionResult indicating success or failure
    ///
    /// Raises:
    ///     ZerobusError: If transmission fails after all retry attempts
    fn send_batch(&self, py: Python, batch: PyObject) -> PyResult<PyTransmissionResult> {
        // Convert PyArrow RecordBatch to Rust RecordBatch
        // This uses zero-copy conversion via PyArrow's C data interface
        let rust_batch = pyarrow_to_rust_batch(py, batch)?;

        // Execute async operation on Tokio runtime
        let result = self
            .runtime
            .block_on(async { self.inner.send_batch(rust_batch).await });

        match result {
            Ok(transmission_result) => Ok(PyTransmissionResult {
                inner: transmission_result,
            }),
            Err(e) => Err(rust_error_to_python_error(e)),
        }
    }

    /// Flush any pending operations and ensure data is transmitted.
    ///
    /// Raises:
    ///     ZerobusError: If flush operation fails
    fn flush(&self, _py: Python) -> PyResult<()> {
        self.runtime
            .block_on(async { self.inner.flush().await })
            .map_err(rust_error_to_python_error)?;
        Ok(())
    }

    /// Shutdown the wrapper gracefully, closing connections and cleaning up resources.
    ///
    /// Raises:
    ///     ZerobusError: If shutdown fails
    fn shutdown(&self, _py: Python) -> PyResult<()> {
        self.runtime
            .block_on(async { self.inner.shutdown().await })
            .map_err(rust_error_to_python_error)?;
        Ok(())
    }

    /// Async context manager entry
    fn __aenter__(&self) -> PyResult<Self> {
        Ok(self.clone())
    }

    /// Async context manager exit
    fn __aexit__(
        &self,
        _py: Python,
        _exc_type: PyObject,
        _exc_val: PyObject,
        _exc_tb: PyObject,
    ) -> PyResult<()> {
        self.shutdown(_py)?;
        Ok(())
    }
}

impl Clone for PyZerobusWrapper {
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
            runtime: Arc::clone(&self.runtime),
        }
    }
}

/// Convert PyArrow RecordBatch to Rust RecordBatch
///
/// Uses PyArrow's C data interface for efficient conversion when possible.
/// Falls back to Python API extraction if C data interface is not available.
fn pyarrow_to_rust_batch(py: Python, batch: PyObject) -> PyResult<RecordBatch> {
    // Import PyArrow module
    let pyarrow = PyModule::import(py, "pyarrow")?;

    // Get RecordBatch class
    let record_batch_class = pyarrow.getattr("RecordBatch")?;

    // Check if the object is a RecordBatch
    let batch_ref = batch.as_ref(py);
    if !batch_ref.is_instance(record_batch_class)? {
        return Err(PyTypeError::new_err(
            "Expected pyarrow.RecordBatch, got different type",
        ));
    }

    // Try to use PyArrow's C data interface for zero-copy conversion
    // This is the most efficient method
    if let Ok(c_batch) = pyarrow_to_rust_batch_c_interface(py, batch_ref) {
        return Ok(c_batch);
    }

    // Fallback: Use PyArrow's Python API to extract data
    // This is less efficient but works for all PyArrow versions
    pyarrow_to_rust_batch_python_api(py, batch_ref)
}

/// Convert PyArrow RecordBatch using C data interface (zero-copy when possible)
///
/// Uses PyArrow's IPC serialization as an efficient intermediate format.
/// PyArrow's `to_pybytes()` serializes to Arrow IPC format, which can be
/// efficiently deserialized in Rust without copying individual array elements.
fn pyarrow_to_rust_batch_c_interface(_py: Python, batch_ref: &PyAny) -> PyResult<RecordBatch> {
    use arrow::ipc::reader::StreamReader;
    use std::io::Cursor;

    // Use PyArrow's IPC serialization for efficient conversion
    // This avoids copying individual array elements by using Arrow's
    // binary format as an intermediate representation

    // Serialize RecordBatch to IPC format using PyArrow
    let serialized = batch_ref.call_method0("to_pybytes")?;
    let bytes: Vec<u8> = serialized.extract()?;

    // Deserialize in Rust using Arrow IPC reader
    // This is efficient because Arrow IPC format matches Rust Arrow format
    let cursor = Cursor::new(bytes);
    let mut reader = StreamReader::try_new(cursor, None)
        .map_err(|e| PyException::new_err(format!("Failed to create IPC reader: {}", e)))?;

    // Read the RecordBatch from the IPC stream
    let batch = reader
        .next()
        .ok_or_else(|| PyException::new_err("No RecordBatch in IPC stream"))?
        .map_err(|e| PyException::new_err(format!("Failed to read RecordBatch: {}", e)))?;

    Ok(batch)
}

/// Convert PyArrow RecordBatch using Python API (fallback method)
fn pyarrow_to_rust_batch_python_api(py: Python, batch_ref: &PyAny) -> PyResult<RecordBatch> {
    use arrow::array::*;
    use arrow::datatypes::{Field, Schema};
    use std::sync::Arc;

    // Get schema from PyArrow RecordBatch
    // PyArrow Schema objects are sequences - they support len() and indexing
    let schema_obj = batch_ref.getattr("schema")?;

    // Get number of fields using len() method (Schema objects support len())
    let num_fields = schema_obj.call_method0("__len__")?.extract::<usize>()?;

    let mut rust_fields = Vec::new();
    let mut rust_arrays = Vec::new();

    // Convert each field and array
    // PyArrow Schema objects support indexing: schema[i] returns the field
    for i in 0..num_fields {
        let field_obj = schema_obj.get_item(i)?;
        let field_name = field_obj.getattr("name")?.extract::<String>()?;
        let field_type_obj = field_obj.getattr("type")?;
        let field_type_str = format!("{}", field_type_obj);

        // Map PyArrow type to Rust Arrow type
        let rust_type = pyarrow_type_to_rust_type(&field_type_str)?;
        rust_fields.push(Field::new(field_name.clone(), rust_type.clone(), true));

        // Get array from batch
        let array_obj = batch_ref.call_method1("column", (i,))?;

        // Convert PyArrow array to Rust array
        let rust_array = pyarrow_array_to_rust_array(py, array_obj, &rust_type)?;
        rust_arrays.push(rust_array);
    }

    // Create Rust RecordBatch
    let schema = Schema::new(rust_fields);
    RecordBatch::try_new(Arc::new(schema), rust_arrays)
        .map_err(|e| PyException::new_err(format!("Failed to create RecordBatch: {}", e)))
}

/// Convert PyArrow type string to Rust Arrow DataType
fn pyarrow_type_to_rust_type(type_str: &str) -> PyResult<DataType> {
    // Map PyArrow type strings to Rust Arrow types
    // This is a simplified mapping - full implementation should handle all types
    if type_str.contains("int64") {
        Ok(DataType::Int64)
    } else if type_str.contains("int32") {
        Ok(DataType::Int32)
    } else if type_str.contains("string") || type_str.contains("utf8") {
        Ok(DataType::Utf8)
    } else if type_str.contains("float64") || type_str.contains("double") {
        Ok(DataType::Float64)
    } else if type_str.contains("float32") || type_str.contains("float") {
        Ok(DataType::Float32)
    } else if type_str.contains("bool") {
        Ok(DataType::Boolean)
    } else if type_str.contains("binary") {
        Ok(DataType::Binary)
    } else {
        Err(PyNotImplementedError::new_err(format!(
            "Unsupported PyArrow type: {}",
            type_str
        )))
    }
}

/// Convert PyArrow array to Rust Arrow array
fn pyarrow_array_to_rust_array(
    _py: Python,
    array_obj: &PyAny,
    data_type: &DataType,
) -> PyResult<Arc<dyn arrow::array::Array>> {
    use arrow::array::*;
    use std::sync::Arc;

    // Get array length
    // PyArrow arrays support __len__() method, not a len attribute
    let len = array_obj.call_method0("__len__")?.extract::<usize>()?;

    match data_type {
        DataType::Int64 => {
            let values: Vec<Option<i64>> = (0..len)
                .map(|i| {
                    let val = array_obj.get_item(i)?;
                    if val.is_none() {
                        Ok(None)
                    } else {
                        Ok(Some(val.extract::<i64>()?))
                    }
                })
                .collect::<PyResult<Vec<_>>>()?;
            Ok(Arc::new(Int64Array::from(values)))
        }
        DataType::Utf8 => {
            let values: Vec<Option<String>> = (0..len)
                .map(|i| {
                    let val = array_obj.get_item(i)?;
                    if val.is_none() {
                        Ok(None)
                    } else {
                        // PyArrow returns StringScalar objects, not plain strings
                        // Use as_py() method to convert to Python string, then extract
                        let py_str = if val.hasattr("as_py")? {
                            val.call_method0("as_py")?
                        } else {
                            // Fallback: convert to string representation
                            val.call_method0("__str__")?
                        };
                        Ok(Some(py_str.extract::<String>()?))
                    }
                })
                .collect::<PyResult<Vec<_>>>()?;
            Ok(Arc::new(StringArray::from(values)))
        }
        DataType::Float64 => {
            let values: Vec<Option<f64>> = (0..len)
                .map(|i| {
                    let val = array_obj.get_item(i)?;
                    if val.is_none() {
                        Ok(None)
                    } else {
                        Ok(Some(val.extract::<f64>()?))
                    }
                })
                .collect::<PyResult<Vec<_>>>()?;
            Ok(Arc::new(Float64Array::from(values)))
        }
        DataType::Boolean => {
            let values: Vec<Option<bool>> = (0..len)
                .map(|i| {
                    let val = array_obj.get_item(i)?;
                    if val.is_none() {
                        Ok(None)
                    } else {
                        Ok(Some(val.extract::<bool>()?))
                    }
                })
                .collect::<PyResult<Vec<_>>>()?;
            Ok(Arc::new(BooleanArray::from(values)))
        }
        _ => Err(PyNotImplementedError::new_err(format!(
            "Array type conversion not yet implemented for: {:?}",
            data_type
        ))),
    }
}

/// Convert Rust RecordBatch to PyArrow RecordBatch
///
/// Uses Arrow IPC serialization as an efficient intermediate format.
/// Serializes the Rust RecordBatch to IPC format, then deserializes it in Python.
fn rust_batch_to_pyarrow(py: Python, batch: &RecordBatch) -> PyResult<PyObject> {
    use arrow::ipc::writer::StreamWriter;
    use pyo3::types::PyBytes;
    use std::io::Cursor;

    // Serialize Rust RecordBatch to IPC format
    let mut buffer = Vec::new();
    let cursor = Cursor::new(&mut buffer);
    let mut writer = StreamWriter::try_new(cursor, &batch.schema())
        .map_err(|e| PyException::new_err(format!("Failed to create IPC writer: {}", e)))?;

    writer
        .write(batch)
        .map_err(|e| PyException::new_err(format!("Failed to write RecordBatch: {}", e)))?;

    writer
        .finish()
        .map_err(|e| PyException::new_err(format!("Failed to finish IPC writer: {}", e)))?;

    // Import PyArrow IPC module
    let pyarrow = PyModule::import(py, "pyarrow")?;
    let ipc_module = pyarrow.getattr("ipc")?;

    // Create a BufferReader from the IPC bytes
    let buffer_reader_class = pyarrow.getattr("BufferReader")?;
    let ipc_bytes = PyBytes::new(py, &buffer);
    let buffer_reader = buffer_reader_class.call1((ipc_bytes,))?;

    // Use open_stream to read the RecordBatch
    let open_stream = ipc_module.getattr("open_stream")?;
    let stream_reader = open_stream.call1((buffer_reader,))?;

    // Read the first (and only) batch from the stream
    let read_next = stream_reader.call_method0("read_next_batch")?;

    Ok(read_next.to_object(py))
}