spark_connect_rs/
readwriter.rs

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
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
//! DataFrameReader & DataFrameWriter representations

use std::collections::HashMap;

use crate::column::Column;
use crate::errors::SparkError;
use crate::expressions::VecExpression;
use crate::plan::LogicalPlanBuilder;
use crate::session::SparkSession;
use crate::spark;
use crate::types::{SparkDataType, StructType};
use crate::DataFrame;

use spark::write_operation::SaveMode;
use spark::write_operation_v2::Mode;
use spark::Expression;

/// A trait used to a create a DDL string or JSON string
///
/// Primarily used for [StructType] and Strings
pub trait ToSchema {
    fn to_schema(&self) -> String;
}

impl ToSchema for StructType {
    fn to_schema(&self) -> String {
        self.json()
    }
}

impl ToSchema for String {
    fn to_schema(&self) -> String {
        self.to_string()
    }
}

impl ToSchema for &str {
    fn to_schema(&self) -> String {
        self.to_string()
    }
}

/// A trait used to convert to the expected Spark Options for readwriters
///
/// This sets multiple options at once using a HashMap
pub trait ConfigOpts {
    fn to_options(&self) -> HashMap<String, String>;
}

/// Common file options that are shared across multiple file formats
/// (e.g., CSV, JSON, ORC, Parquet, Text).
///
/// These options allow for file filtering and error handling during
/// file discovery and processing.
///
/// # Fields
///
/// - `path_glob_filter`: Optional glob pattern to filter files by path.
///   This can be used to select specific files within a directory.
///
/// - `recursive_file_lookup`: Whether to recursively search for files in
///   subdirectories. If set to `true`, the reader will search all subdirectories
///   for matching files.
///
/// - `modified_before`: An optional string specifying a cutoff date for filtering
///   files. Files modified after this date will not be included.
///
/// - `modified_after`: An optional string specifying a start date for filtering
///   files. Only files modified after this date will be included.
///
/// - `ignore_corrupt_files`: If set to `true`, the reader will skip corrupt files
///   instead of throwing an error. This is useful in scenarios where some files
///   may be malformed.
///
/// - `ignore_missing_files`: If set to `true`, missing files (e.g., those filtered out by
///   the glob pattern) will be ignored instead of causing an error. This is useful when
///   running in environments where files may be missing intermittently.
#[derive(Debug, Clone)]
pub struct CommonFileOptions {
    pub path_glob_filter: Option<String>,
    pub recursive_file_lookup: Option<bool>,
    pub modified_before: Option<String>,
    pub modified_after: Option<String>,
    pub ignore_corrupt_files: Option<bool>,
    pub ignore_missing_files: Option<bool>,
}

impl Default for CommonFileOptions {
    fn default() -> Self {
        Self {
            path_glob_filter: None,
            recursive_file_lookup: Some(false),
            modified_before: None,
            modified_after: None,
            ignore_corrupt_files: None,
            ignore_missing_files: None,
        }
    }
}

impl ConfigOpts for CommonFileOptions {
    fn to_options(&self) -> HashMap<String, String> {
        let mut options = HashMap::new();

        if let Some(path_glob_filter) = &self.path_glob_filter {
            options.insert("pathGlobFilter".to_string(), path_glob_filter.clone());
        }

        if let Some(recursive_file_lookup) = self.recursive_file_lookup {
            options.insert(
                "recursiveFileLookup".to_string(),
                recursive_file_lookup.to_string(),
            );
        }

        if let Some(modified_before) = &self.modified_before {
            options.insert("modifiedBefore".to_string(), modified_before.clone());
        }

        if let Some(modified_after) = &self.modified_after {
            options.insert("modifiedAfter".to_string(), modified_after.clone());
        }

        if let Some(ignore_corrupt_files) = self.ignore_corrupt_files {
            options.insert(
                "ignoreCorruptFiles".to_string(),
                ignore_corrupt_files.to_string(),
            );
        }

        if let Some(ignore_missing_files) = self.ignore_missing_files {
            options.insert(
                "ignoreMissingFiles".to_string(),
                ignore_missing_files.to_string(),
            );
        }

        options
    }
}

/// A struct that represents options for configuring CSV file parsing.
///
/// `CsvOptions` provides various settings to customize the reading of CSV files.
/// It allows users to define the format, schema inference, handling of null values,
/// and many other parsing behaviors. These options are used by the Spark DataFrame reader
/// to correctly interpret and load CSV files into a DataFrame.
///
/// # Fields
/// - `path`: Specifies the file path or directory path to the CSV file(s).
/// - `schema`: Defines the schema for the CSV data. If not provided, schema will be inferred based on the data.
/// - `sep`: Character used to separate fields in the CSV file. Default is a comma (`,`).
/// - `delimiter`: Alternative character used to separate fields in the CSV file.
/// - `encoding`: Character encoding used for the CSV file. Default is `UTF-8`.
/// - `quote`: Character used for quoting strings. Default is a double quote (`"`).
/// - `quote_all`: Whether to quote all fields or only those containing special characters.
/// - `escape`: Character used to escape quotes inside quoted strings. Default is a backslash (`\`).
/// - `comment`: Character that denotes the start of a comment line in the file.
/// - `header`: Whether the first line of the CSV file is a header that contains column names. Default is `false`.
/// - `infer_schema`: Whether to infer the schema from the CSV data. Default is `false`.
/// - `ignore_leading_white_space`: Whether to ignore leading white space in fields. Default is `false`.
/// - `ignore_trailing_white_space`: Whether to ignore trailing white space in fields. Default is `false`.
/// - `null_value`: String representation of a null value in the CSV file.
/// - `nan_value`: String representation of a NaN value in the CSV file.
/// - `positive_inf`: String representation of positive infinity in the CSV file.
/// - `negative_inf`: String representation of negative infinity in the CSV file.
/// - `date_format`: Format for parsing date fields in the CSV file.
/// - `timestamp_format`: Format for parsing timestamp fields in the CSV file.
/// - `timestamp_ntz_format`: Format for parsing timestamp fields without timezone information.
/// - `enable_datetime_parsing_fallback`: Whether to enable fallback parsing for date and time formats.
/// - `max_columns`: Maximum number of columns allowed in the CSV file.
/// - `max_chars_per_column`: Maximum number of characters allowed per column.
/// - `max_malformed_log_per_partition`: Maximum number of malformed rows logged per partition.
/// - `mode`: Handling mode for corrupt/malformed records. Options are "PERMISSIVE", "DROPMALFORMED", and "FAILFAST".
/// - `column_name_of_corrupt_record`: Name of the column to store malformed records.
/// - `multi_line`: Whether to treat a row as spanning multiple lines. Default is `false`.
/// - `char_to_escape_quote_escaping`: Sets a character for escaping quotes inside a quoted field.
/// - `sampling_ratio`: Fraction of rows used for schema inference.
/// - `enforce_schema`: Whether to force schema on the CSV file.
/// - `empty_value`: Representation of an empty value in the CSV file.
/// - `locale`: Locale of the CSV file, used for number formatting.
/// - `line_sep`: Line separator character in the CSV file.
/// - `unescaped_quote_handling`: How to handle unescaped quotes in quoted fields. Options are "STOP_AT_CLOSING_QUOTE" and "BACK_TO_DELIMITER".
/// - `common` - Common file options that are shared across multiple file formats.
#[derive(Debug, Clone, Default)]
pub struct CsvOptions {
    pub schema: Option<String>,
    pub sep: Option<String>,
    pub delimiter: Option<String>,
    pub encoding: Option<String>,
    pub quote: Option<String>,
    pub quote_all: Option<bool>,
    pub escape: Option<String>,
    pub escape_quotes: Option<bool>,
    pub comment: Option<String>,
    pub header: Option<bool>,
    pub infer_schema: Option<bool>,
    pub ignore_leading_white_space: Option<bool>,
    pub ignore_trailing_white_space: Option<bool>,
    pub null_value: Option<String>,
    pub nan_value: Option<String>,
    pub positive_inf: Option<String>,
    pub negative_inf: Option<String>,
    pub date_format: Option<String>,
    pub timestamp_format: Option<String>,
    pub timestamp_ntz_format: Option<String>,
    pub enable_datetime_parsing_fallback: Option<bool>,
    pub max_columns: Option<i32>,
    pub max_chars_per_column: Option<i32>,
    pub max_malformed_log_per_partition: Option<i32>,
    pub mode: Option<String>,
    pub column_name_of_corrupt_record: Option<String>,
    pub multi_line: Option<bool>,
    pub char_to_escape_quote_escaping: Option<String>,
    pub sampling_ratio: Option<f64>,
    pub prefer_date: Option<bool>,
    pub enforce_schema: Option<bool>,
    pub empty_value: Option<String>,
    pub locale: Option<String>,
    pub line_sep: Option<String>,
    pub unescaped_quote_handling: Option<String>,
    pub common: CommonFileOptions,
}

impl CsvOptions {
    pub fn schema(mut self, value: &str) -> Self {
        self.schema = Some(value.to_string());
        self
    }

    pub fn sep(mut self, value: &str) -> Self {
        self.sep = Some(value.to_string());
        self
    }

    pub fn delimiter(mut self, value: &str) -> Self {
        self.delimiter = Some(value.to_string());
        self
    }

    pub fn encoding(mut self, value: &str) -> Self {
        self.encoding = Some(value.to_string());
        self
    }

    pub fn quote(mut self, value: &str) -> Self {
        self.quote = Some(value.to_string());
        self
    }

    pub fn quote_all(mut self, value: bool) -> Self {
        self.quote_all = Some(value);
        self
    }

    pub fn escape(mut self, value: &str) -> Self {
        self.escape = Some(value.to_string());
        self
    }

    pub fn comment(mut self, value: &str) -> Self {
        self.comment = Some(value.to_string());
        self
    }

    pub fn header(mut self, value: bool) -> Self {
        self.header = Some(value);
        self
    }

    pub fn infer_schema(mut self, value: bool) -> Self {
        self.infer_schema = Some(value);
        self
    }

    pub fn ignore_leading_white_space(mut self, value: bool) -> Self {
        self.ignore_leading_white_space = Some(value);
        self
    }

    pub fn ignore_trailing_white_space(mut self, value: bool) -> Self {
        self.ignore_trailing_white_space = Some(value);
        self
    }

    pub fn null_value(mut self, value: &str) -> Self {
        self.null_value = Some(value.to_string());
        self
    }

    pub fn nan_value(mut self, value: &str) -> Self {
        self.nan_value = Some(value.to_string());
        self
    }

    pub fn positive_inf(mut self, value: &str) -> Self {
        self.positive_inf = Some(value.to_string());
        self
    }

    pub fn negative_inf(mut self, value: &str) -> Self {
        self.negative_inf = Some(value.to_string());
        self
    }

    pub fn date_format(mut self, value: &str) -> Self {
        self.date_format = Some(value.to_string());
        self
    }

    pub fn timestamp_format(mut self, value: &str) -> Self {
        self.timestamp_format = Some(value.to_string());
        self
    }

    pub fn timestamp_ntz_format(mut self, value: &str) -> Self {
        self.timestamp_ntz_format = Some(value.to_string());
        self
    }

    pub fn enable_datetime_parsing_fallback(mut self, value: bool) -> Self {
        self.enable_datetime_parsing_fallback = Some(value);
        self
    }

    pub fn max_columns(mut self, value: i32) -> Self {
        self.max_columns = Some(value);
        self
    }

    pub fn max_chars_per_column(mut self, value: i32) -> Self {
        self.max_chars_per_column = Some(value);
        self
    }

    pub fn max_malformed_log_per_partition(mut self, value: i32) -> Self {
        self.max_malformed_log_per_partition = Some(value);
        self
    }

    pub fn mode(mut self, value: &str) -> Self {
        self.mode = Some(value.to_string());
        self
    }

    pub fn column_name_of_corrupt_record(mut self, value: &str) -> Self {
        self.column_name_of_corrupt_record = Some(value.to_string());
        self
    }

    pub fn multi_line(mut self, value: bool) -> Self {
        self.multi_line = Some(value);
        self
    }

    pub fn char_to_escape_quote_escaping(mut self, value: &str) -> Self {
        self.char_to_escape_quote_escaping = Some(value.to_string());
        self
    }

    pub fn sampling_ratio(mut self, value: f64) -> Self {
        self.sampling_ratio = Some(value);
        self
    }

    pub fn prefer_date(mut self, value: bool) -> Self {
        self.prefer_date = Some(value);
        self
    }

    pub fn enforce_schema(mut self, value: bool) -> Self {
        self.enforce_schema = Some(value);
        self
    }

    pub fn empty_value(mut self, value: &str) -> Self {
        self.empty_value = Some(value.to_string());
        self
    }

    pub fn locale(mut self, value: &str) -> Self {
        self.locale = Some(value.to_string());
        self
    }

    pub fn line_sep(mut self, value: &str) -> Self {
        self.line_sep = Some(value.to_string());
        self
    }

    pub fn unescaped_quote_handling(mut self, value: &str) -> Self {
        self.unescaped_quote_handling = Some(value.to_string());
        self
    }

    pub fn escape_quotes(mut self, value: bool) -> Self {
        self.escape_quotes = Some(value);
        self
    }
}

impl ConfigOpts for CsvOptions {
    fn to_options(&self) -> HashMap<String, String> {
        let mut options: HashMap<String, String> = HashMap::new();

        if let Some(schema) = &self.schema {
            options.insert("schema".to_string(), schema.to_string());
        }

        if let Some(sep) = &self.sep {
            options.insert("sep".to_string(), sep.to_string());
        }

        if let Some(delimiter) = &self.delimiter {
            options.insert("delimiter".to_string(), delimiter.to_string());
        }

        if let Some(encoding) = &self.encoding {
            options.insert("encoding".to_string(), encoding.to_string());
        }

        if let Some(quote) = &self.quote {
            options.insert("quote".to_string(), quote.to_string());
        }

        if let Some(quote_all) = self.quote_all {
            options.insert("quoteAll".to_string(), quote_all.to_string());
        }

        if let Some(escape) = &self.escape {
            options.insert("escape".to_string(), escape.to_string());
        }

        if let Some(comment) = &self.comment {
            options.insert("comment".to_string(), comment.to_string());
        }

        if let Some(header) = self.header {
            options.insert("header".to_string(), header.to_string());
        }

        if let Some(infer_schema) = self.infer_schema {
            options.insert("inferSchema".to_string(), infer_schema.to_string());
        }

        if let Some(ignore_leading_white_space) = self.ignore_leading_white_space {
            options.insert(
                "ignoreLeadingWhiteSpace".to_string(),
                ignore_leading_white_space.to_string(),
            );
        }

        if let Some(ignore_trailing_white_space) = self.ignore_trailing_white_space {
            options.insert(
                "ignoreTrailingWhiteSpace".to_string(),
                ignore_trailing_white_space.to_string(),
            );
        }

        if let Some(null_value) = &self.null_value {
            options.insert("nullValue".to_string(), null_value.to_string());
        }

        if let Some(nan_value) = &self.nan_value {
            options.insert("nanValue".to_string(), nan_value.to_string());
        }

        if let Some(positive_inf) = &self.positive_inf {
            options.insert("positiveInf".to_string(), positive_inf.to_string());
        }

        if let Some(negative_inf) = &self.negative_inf {
            options.insert("negativeInf".to_string(), negative_inf.to_string());
        }

        if let Some(date_format) = &self.date_format {
            options.insert("dateFormat".to_string(), date_format.to_string());
        }

        if let Some(timestamp_format) = &self.timestamp_format {
            options.insert("timestampFormat".to_string(), timestamp_format.to_string());
        }

        if let Some(timestamp_ntz_format) = &self.timestamp_ntz_format {
            options.insert(
                "timestampNTZFormat".to_string(),
                timestamp_ntz_format.to_string(),
            );
        }

        if let Some(enable_datetime_parsing_fallback) = self.enable_datetime_parsing_fallback {
            options.insert(
                "enableDatetimeParsingFallback".to_string(),
                enable_datetime_parsing_fallback.to_string(),
            );
        }

        if let Some(max_columns) = self.max_columns {
            options.insert("maxColumns".to_string(), max_columns.to_string());
        }

        if let Some(max_chars_per_column) = self.max_chars_per_column {
            options.insert(
                "maxCharsPerColumn".to_string(),
                max_chars_per_column.to_string(),
            );
        }

        if let Some(max_malformed_log_per_partition) = self.max_malformed_log_per_partition {
            options.insert(
                "maxMalformedLogPerPartition".to_string(),
                max_malformed_log_per_partition.to_string(),
            );
        }

        if let Some(mode) = &self.mode {
            options.insert("mode".to_string(), mode.to_string());
        }

        if let Some(column_name_of_corrupt_record) = &self.column_name_of_corrupt_record {
            options.insert(
                "columnNameOfCorruptRecord".to_string(),
                column_name_of_corrupt_record.to_string(),
            );
        }

        if let Some(multi_line) = self.multi_line {
            options.insert("multiLine".to_string(), multi_line.to_string());
        }

        if let Some(char_to_escape_quote_escaping) = &self.char_to_escape_quote_escaping {
            options.insert(
                "charToEscapeQuoteEscaping".to_string(),
                char_to_escape_quote_escaping.to_string(),
            );
        }

        if let Some(sampling_ratio) = self.sampling_ratio {
            options.insert("samplingRatio".to_string(), sampling_ratio.to_string());
        }

        if let Some(prefer_date) = self.prefer_date {
            options.insert("preferDate".to_string(), prefer_date.to_string());
        }

        if let Some(enforce_schema) = self.enforce_schema {
            options.insert("enforceSchema".to_string(), enforce_schema.to_string());
        }

        if let Some(empty_value) = &self.empty_value {
            options.insert("emptyValue".to_string(), empty_value.to_string());
        }

        if let Some(locale) = &self.locale {
            options.insert("locale".to_string(), locale.to_string());
        }

        if let Some(line_sep) = &self.line_sep {
            options.insert("lineSep".to_string(), line_sep.to_string());
        }

        if let Some(unescaped_quote_handling) = &self.unescaped_quote_handling {
            options.insert(
                "unescapedQuoteHandling".to_string(),
                unescaped_quote_handling.to_string(),
            );
        }

        if let Some(escape_quotes) = self.escape_quotes {
            options.insert("escapeQuotes".to_string(), escape_quotes.to_string());
        }

        options.extend(self.common.to_options());

        options
    }
}

/// A struct that represents options for configuring JSON file parsing.
///
/// By default, this supports `JSON Lines` (newline-delimited JSON).
/// For single-record-per-file JSON, set the `multi_line` option to `true`.
///
/// If the `schema` option is not specified, the input schema is inferred from the data.
///
/// # Options
///
/// - `schema`: An optional schema for the JSON data, either as a `StructType` or a DDL string.
/// - `compression`: Compression codec to use when reading JSON files (e.g., `gzip`, `bzip2`).
/// - `primitives_as_string`: Treat primitive types (e.g., integers, booleans) as strings.
/// - `prefers_decimal`: Prefer parsing numbers as decimals rather than floating points.
/// - `allow_comments`: Allow comments in JSON files (e.g., lines starting with `//` or `/* */`).
/// - `allow_unquoted_field_names`: Allow field names without quotes.
/// - `allow_single_quotes`: Allow the use of single quotes instead of double quotes for strings.
/// - `allow_numeric_leading_zeros`: Allow numbers to have leading zeros (e.g., `007`).
/// - `allow_backslash_escaping_any_character`: Allow backslashes to escape any character.
/// - `mode`: The parsing mode (e.g., `PERMISSIVE`, `DROPMALFORMED`, `FAILFAST`).
/// - `column_name_of_corrupt_record`: Name of the column where corrupted records are placed.
/// - `date_format`: Custom date format (e.g., `yyyy-MM-dd`).
/// - `timestamp_format`: Custom timestamp format (e.g., `yyyy-MM-dd HH:mm:ss`).
/// - `multi_line`: Read multiline JSON files (e.g., when a single JSON object spans multiple lines).
/// - `allow_unquoted_control_chars`: Allow unquoted control characters in JSON (e.g., ASCII control characters).
/// - `line_sep`: Custom line separator (default is `\n`).
/// - `sampling_ratio`: Fraction of the data used for schema inference (e.g., `0.1` for 10%).
/// - `drop_field_if_all_null`: Drop fields that are `NULL` in all rows.
/// - `encoding`: Character encoding (default is `UTF-8`).
/// - `locale`: Locale for parsing dates and numbers (e.g., `en-US`).
/// - `allow_non_numeric_numbers`: Allow special non-numeric numbers (e.g., `NaN`, `Infinity`).
/// - `time_zone`: Time zone used for parsing dates and timestamps (e.g., `UTC`, `America/Los_Angeles`).
/// - `timestamp_ntz_format`: Format for parsing timestamp without time zone (NTZ) values (e.g., `yyyy-MM-dd'T'HH:mm:ss`).
/// - `enable_datetime_parsing_fallback`: Enable fallback mechanism for datetime parsing if the initial parsing fails.
/// - `ignore_null_fields`: Ignore `NULL` fields in the JSON structure, treating them as absent.
/// - `common` - Common file options that are shared across multiple file formats.
///
/// # Example
/// ```
/// let options = JsonOptions::new()
///     .schema("name STRING, salary INT")
///     .multi_line(true)
///     .allow_comments(true)
///     .encoding("UTF-8")
///     .time_zone("UTC")
///     .compression("gzip");
///
/// let df = spark.read().json(["/path/to/json"], options)?;
/// ```
#[derive(Debug, Clone)]
pub struct JsonOptions {
    pub schema: Option<String>,
    pub compression: Option<String>,
    pub primitives_as_string: Option<bool>,
    pub prefers_decimal: Option<bool>,
    pub allow_comments: Option<bool>,
    pub allow_unquoted_field_names: Option<bool>,
    pub allow_single_quotes: Option<bool>,
    pub allow_numeric_leading_zeros: Option<bool>,
    pub allow_backslash_escaping_any_character: Option<bool>,
    pub mode: Option<String>,
    pub column_name_of_corrupt_record: Option<String>,
    pub date_format: Option<String>,
    pub timestamp_format: Option<String>,
    pub multi_line: Option<bool>,
    pub allow_unquoted_control_chars: Option<bool>,
    pub line_sep: Option<String>,
    pub sampling_ratio: Option<f64>,
    pub drop_field_if_all_null: Option<bool>,
    pub encoding: Option<String>,
    pub locale: Option<String>,
    pub allow_non_numeric_numbers: Option<bool>,
    pub time_zone: Option<String>,
    pub timestamp_ntz_format: Option<String>,
    pub enable_datetime_parsing_fallback: Option<bool>,
    pub ignore_null_fields: Option<bool>,
    pub common: CommonFileOptions,
}

impl Default for JsonOptions {
    fn default() -> Self {
        Self {
            schema: None,
            compression: Some("gzip".to_string()),
            primitives_as_string: None,
            prefers_decimal: None,
            allow_comments: None,
            allow_unquoted_field_names: None,
            allow_single_quotes: None,
            allow_numeric_leading_zeros: None,
            allow_backslash_escaping_any_character: None,
            mode: None,
            column_name_of_corrupt_record: None,
            date_format: None,
            timestamp_format: None,
            multi_line: None,
            allow_unquoted_control_chars: None,
            line_sep: None,
            sampling_ratio: None,
            drop_field_if_all_null: None,
            encoding: None,
            locale: None,
            allow_non_numeric_numbers: None,
            time_zone: None,
            timestamp_ntz_format: None,
            enable_datetime_parsing_fallback: None,
            ignore_null_fields: None,
            common: CommonFileOptions::default(),
        }
    }
}

impl JsonOptions {
    pub fn schema(mut self, value: &str) -> Self {
        self.schema = Some(value.to_string());
        self
    }

    pub fn primitives_as_string(mut self, value: bool) -> Self {
        self.primitives_as_string = Some(value);
        self
    }

    pub fn prefers_decimal(mut self, value: bool) -> Self {
        self.prefers_decimal = Some(value);
        self
    }

    pub fn allow_comments(mut self, value: bool) -> Self {
        self.allow_comments = Some(value);
        self
    }

    pub fn allow_unquoted_field_names(mut self, value: bool) -> Self {
        self.allow_unquoted_field_names = Some(value);
        self
    }

    pub fn allow_single_quotes(mut self, value: bool) -> Self {
        self.allow_single_quotes = Some(value);
        self
    }

    pub fn allow_numeric_leading_zeros(mut self, value: bool) -> Self {
        self.allow_numeric_leading_zeros = Some(value);
        self
    }

    pub fn allow_backslash_escaping_any_character(mut self, value: bool) -> Self {
        self.allow_backslash_escaping_any_character = Some(value);
        self
    }

    pub fn mode(mut self, value: &str) -> Self {
        self.mode = Some(value.to_string());
        self
    }

    pub fn column_name_of_corrupt_record(mut self, value: &str) -> Self {
        self.column_name_of_corrupt_record = Some(value.to_string());
        self
    }

    pub fn date_format(mut self, value: &str) -> Self {
        self.date_format = Some(value.to_string());
        self
    }

    pub fn timestamp_format(mut self, value: &str) -> Self {
        self.timestamp_format = Some(value.to_string());
        self
    }

    pub fn multi_line(mut self, value: bool) -> Self {
        self.multi_line = Some(value);
        self
    }

    pub fn allow_unquoted_control_chars(mut self, value: bool) -> Self {
        self.allow_unquoted_control_chars = Some(value);
        self
    }

    pub fn line_sep(mut self, value: &str) -> Self {
        self.line_sep = Some(value.to_string());
        self
    }

    pub fn sampling_ratio(mut self, value: f64) -> Self {
        self.sampling_ratio = Some(value);
        self
    }

    pub fn drop_field_if_all_null(mut self, value: bool) -> Self {
        self.drop_field_if_all_null = Some(value);
        self
    }

    pub fn encoding(mut self, value: &str) -> Self {
        self.encoding = Some(value.to_string());
        self
    }

    pub fn locale(mut self, value: &str) -> Self {
        self.locale = Some(value.to_string());
        self
    }

    pub fn allow_non_numeric_numbers(mut self, value: bool) -> Self {
        self.allow_non_numeric_numbers = Some(value);
        self
    }

    pub fn time_zone(mut self, value: &str) -> Self {
        self.time_zone = Some(value.to_string());
        self
    }

    pub fn timestamp_ntz_format(mut self, value: &str) -> Self {
        self.timestamp_ntz_format = Some(value.to_string());
        self
    }

    pub fn enable_datetime_parsing_fallback(mut self, value: bool) -> Self {
        self.enable_datetime_parsing_fallback = Some(value);
        self
    }

    pub fn compression(mut self, value: &str) -> Self {
        self.compression = Some(value.to_string());
        self
    }

    pub fn ignore_null_fields(mut self, value: bool) -> Self {
        self.ignore_null_fields = Some(value);
        self
    }
}

impl ConfigOpts for JsonOptions {
    fn to_options(&self) -> HashMap<String, String> {
        let mut options: HashMap<String, String> = HashMap::new();

        if let Some(schema) = &self.schema {
            options.insert("schema".to_string(), schema.to_string());
        }

        if let Some(compression) = &self.compression {
            options.insert("compression".to_string(), compression.clone());
        }

        if let Some(primitives_as_string) = self.primitives_as_string {
            options.insert(
                "primitivesAsString".to_string(),
                primitives_as_string.to_string(),
            );
        }

        if let Some(prefers_decimal) = self.prefers_decimal {
            options.insert("prefersDecimal".to_string(), prefers_decimal.to_string());
        }

        if let Some(allow_comments) = self.allow_comments {
            options.insert("allowComments".to_string(), allow_comments.to_string());
        }

        if let Some(allow_unquoted_field_names) = self.allow_unquoted_field_names {
            options.insert(
                "allowUnquotedFieldNames".to_string(),
                allow_unquoted_field_names.to_string(),
            );
        }

        if let Some(allow_single_quotes) = self.allow_single_quotes {
            options.insert(
                "allowSingleQuotes".to_string(),
                allow_single_quotes.to_string(),
            );
        }

        if let Some(allow_numeric_leading_zeros) = self.allow_numeric_leading_zeros {
            options.insert(
                "allowNumericLeadingZero".to_string(),
                allow_numeric_leading_zeros.to_string(),
            );
        }

        if let Some(allow_backslash_escaping_any_character) =
            self.allow_backslash_escaping_any_character
        {
            options.insert(
                "allowBackslashEscapingAnyCharacter".to_string(),
                allow_backslash_escaping_any_character.to_string(),
            );
        }

        if let Some(mode) = &self.mode {
            options.insert("mode".to_string(), mode.clone());
        }

        if let Some(column_name_of_corrupt_record) = &self.column_name_of_corrupt_record {
            options.insert(
                "columnNameOfCorruptRecord".to_string(),
                column_name_of_corrupt_record.clone(),
            );
        }

        if let Some(date_format) = &self.date_format {
            options.insert("dateFormat".to_string(), date_format.clone());
        }

        if let Some(timestamp_format) = &self.timestamp_format {
            options.insert("timestampFormat".to_string(), timestamp_format.clone());
        }

        if let Some(multi_line) = self.multi_line {
            options.insert("multiLine".to_string(), multi_line.to_string());
        }

        if let Some(allow_unquoted_control_chars) = self.allow_unquoted_control_chars {
            options.insert(
                "allowUnquotedControlChars".to_string(),
                allow_unquoted_control_chars.to_string(),
            );
        }

        if let Some(line_sep) = &self.line_sep {
            options.insert("lineSep".to_string(), line_sep.clone());
        }

        if let Some(sampling_ratio) = self.sampling_ratio {
            options.insert("samplingRatio".to_string(), sampling_ratio.to_string());
        }

        if let Some(drop_field_if_all_null) = self.drop_field_if_all_null {
            options.insert(
                "dropFieldIfAllNull".to_string(),
                drop_field_if_all_null.to_string(),
            );
        }

        if let Some(encoding) = &self.encoding {
            options.insert("encoding".to_string(), encoding.clone());
        }

        if let Some(locale) = &self.locale {
            options.insert("locale".to_string(), locale.clone());
        }

        if let Some(allow_non_numeric_numbers) = self.allow_non_numeric_numbers {
            options.insert(
                "allowNonNumericNumbers".to_string(),
                allow_non_numeric_numbers.to_string(),
            );
        }

        if let Some(time_zone) = &self.time_zone {
            options.insert("timeZone".to_string(), time_zone.clone());
        }

        if let Some(timestamp_ntz_format) = &self.timestamp_ntz_format {
            options.insert(
                "timestampNTZFormat".to_string(),
                timestamp_ntz_format.clone(),
            );
        }

        if let Some(enable_datetime_parsing_fallback) = self.enable_datetime_parsing_fallback {
            options.insert(
                "enableDatetimeParsingFallback".to_string(),
                enable_datetime_parsing_fallback.to_string(),
            );
        }

        if let Some(ignore_null_fields) = self.ignore_null_fields {
            options.insert(
                "ignoreNullFields".to_string(),
                ignore_null_fields.to_string(),
            );
        }

        options.extend(self.common.to_options());

        options
    }
}

/// A struct that represents options for configuring ORC file parsing.
///
/// # Options
///
/// - `merge_schema`: Merge schemas from all ORC files.
/// - `path_glob_filter`: A glob pattern to filter files by their path.
/// - `common` - Common file options that are shared across multiple file formats.
///
/// # Example
/// ```
/// let options = OrcOptions::new()
///     .merge_schema(true)
///     .common.path_glob_filter("*.orc".to_string())
///     .common.recursive_file_lookup(true);
///
/// let df = spark.read().orc(["/path/to/orc"], options)?;
/// ```
#[derive(Debug, Clone)]
pub struct OrcOptions {
    pub compression: Option<String>,
    pub merge_schema: Option<bool>,
    pub common: CommonFileOptions,
}

impl Default for OrcOptions {
    fn default() -> Self {
        OrcOptions {
            compression: Some("snappy".to_string()),
            merge_schema: None,
            common: CommonFileOptions::default(),
        }
    }
}

impl OrcOptions {
    pub fn compression(mut self, value: &str) -> Self {
        self.compression = Some(value.to_string());
        self
    }

    pub fn merge_schema(mut self, value: bool) -> Self {
        self.merge_schema = Some(value);
        self
    }
}

impl ConfigOpts for OrcOptions {
    fn to_options(&self) -> HashMap<String, String> {
        let mut options: HashMap<String, String> = HashMap::new();

        if let Some(compression) = &self.compression {
            options.insert("compression".to_string(), compression.to_string());
        }

        if let Some(merge_schema) = self.merge_schema {
            options.insert("mergeSchema".to_string(), merge_schema.to_string());
        }

        options.extend(self.common.to_options());

        options
    }
}

/// A struct that represents options for configuring Parquet file parsing.
///
/// # Options
///
/// - `merge_schema`: Merge schemas from all Parquet files.
/// - `datetime_rebase_mode`: Controls how Spark handles rebase of datetime fields.
/// - `int96_rebase_mode`: Controls how Spark handles rebase of INT96 fields.
/// - `common`: Common file options that are shared across multiple file formats.
///
/// # Example
/// ```
/// let options = ParquetOptions::new()
///     .merge_schema(true)
///     .common.path_glob_filter("*.parquet".to_string())
///     .common.recursive_file_lookup(true);
///
/// let df = spark.read().parquet(["/path/to/parquet"], options)?;
/// ```
#[derive(Debug, Clone)]
pub struct ParquetOptions {
    pub compression: Option<String>,
    pub merge_schema: Option<bool>,
    pub datetime_rebase_mode: Option<String>,
    pub int96_rebase_mode: Option<String>,
    pub common: CommonFileOptions,
}

impl Default for ParquetOptions {
    fn default() -> Self {
        Self {
            compression: Some("snappy".to_string()),
            merge_schema: None,
            datetime_rebase_mode: None,
            int96_rebase_mode: None,
            common: CommonFileOptions::default(),
        }
    }
}

impl ParquetOptions {
    pub fn compression(mut self, value: &str) -> Self {
        self.compression = Some(value.to_string());
        self
    }

    pub fn merge_schema(mut self, value: bool) -> Self {
        self.merge_schema = Some(value);
        self
    }

    pub fn datetime_rebase_mode(mut self, value: &str) -> Self {
        self.datetime_rebase_mode = Some(value.to_string());
        self
    }

    pub fn int96_rebase_mode(mut self, value: &str) -> Self {
        self.int96_rebase_mode = Some(value.to_string());
        self
    }
}

impl ConfigOpts for ParquetOptions {
    fn to_options(&self) -> HashMap<String, String> {
        let mut options: HashMap<String, String> = HashMap::new();

        if let Some(compression) = &self.compression {
            options.insert("compression".to_string(), compression.to_string());
        }

        if let Some(merge_schema) = self.merge_schema {
            options.insert("mergeSchema".to_string(), merge_schema.to_string());
        }

        if let Some(datetime_rebase_mode) = &self.datetime_rebase_mode {
            options.insert(
                "datetimeRebaseMode".to_string(),
                datetime_rebase_mode.to_string(),
            );
        }

        if let Some(int96_rebase_mode) = &self.int96_rebase_mode {
            options.insert("int96RebaseMode".to_string(), int96_rebase_mode.to_string());
        }

        options.extend(self.common.to_options());

        options
    }
}

/// A struct that represents options for configuring text file parsing.
///
/// # Options
///
/// - `whole_text`: Read the entire file as a single string.
/// - `line_sep`: Define the line separator (default is `\n`).
/// - `common` - Common file options that are shared across multiple file formats.
///
/// # Example
/// ```
/// let options = TextOptions::new()
///     .whole_text(true)
///     .line_sep("\n".to_string())
///     .common.path_glob_filter("*.txt".to_string());
///
/// let df = spark.read().text(["/path/to/text"], options)?;
/// ```
#[derive(Debug, Clone, Default)]
pub struct TextOptions {
    pub whole_text: Option<bool>,
    pub line_sep: Option<String>,
    pub common: CommonFileOptions,
}

impl TextOptions {
    pub fn whole_text(mut self, value: bool) -> Self {
        self.whole_text = Some(value);
        self
    }

    pub fn line_sep(mut self, value: &str) -> Self {
        self.line_sep = Some(value.to_string());
        self
    }
}

impl ConfigOpts for TextOptions {
    fn to_options(&self) -> HashMap<String, String> {
        let mut options: HashMap<String, String> = HashMap::new();

        if let Some(whole_text) = self.whole_text {
            options.insert("wholeText".to_string(), whole_text.to_string());
        }

        if let Some(line_sep) = &self.line_sep {
            options.insert("lineSep".to_string(), line_sep.to_string());
        }

        options.extend(self.common.to_options());

        options
    }
}

/// DataFrameReader represents the entrypoint to create a DataFrame
/// from a specific file format.
#[derive(Clone, Debug)]
pub struct DataFrameReader {
    spark_session: SparkSession,
    format: Option<String>,
    schema: Option<String>,
    read_options: HashMap<String, String>,
}

impl DataFrameReader {
    /// Create a new DataFrameReader with a [SparkSession]
    pub fn new(spark_session: SparkSession) -> Self {
        Self {
            spark_session,
            format: None,
            schema: None,
            read_options: HashMap::new(),
        }
    }

    /// Specifies the input data source format
    pub fn format(mut self, format: &str) -> Self {
        self.format = Some(format.to_string());
        self
    }

    /// Add an input option for the underlying data source
    pub fn option(mut self, key: &str, value: &str) -> Self {
        self.read_options.insert(key.to_string(), value.to_string());
        self
    }

    pub fn schema<T: ToSchema>(mut self, schema: T) -> Self {
        self.schema = Some(schema.to_schema());
        self
    }

    /// Set many input options based on an iterator of (key/value pairs) for the underlying data source
    pub fn options<I, K, V>(mut self, options: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: AsRef<str>,
        V: AsRef<str>,
    {
        self.read_options = options
            .into_iter()
            .map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
            .collect();

        self
    }

    /// Loads data from a data source and returns it as a [DataFrame]
    ///
    /// Example:
    /// ```rust
    /// let path = vec!["some/dir/path/on/the/remote/cluster/"];
    ///
    /// // returns a DataFrame from a csv file with a header from a the specific path
    /// let mut df = spark.read().format("csv").option("header", "true").load(path);
    /// ```
    pub fn load<'a, I>(self, paths: I) -> Result<DataFrame, SparkError>
    where
        I: IntoIterator<Item = &'a str>,
    {
        let read_type = Some(spark::relation::RelType::Read(spark::Read {
            is_streaming: false,
            read_type: Some(spark::read::ReadType::DataSource(spark::read::DataSource {
                format: self.format,
                schema: self.schema,
                options: self.read_options,
                paths: paths.into_iter().map(|p| p.to_string()).collect(),
                predicates: vec![],
            })),
        }));

        let relation = spark::Relation {
            common: Some(spark::RelationCommon {
                source_info: "NA".to_string(),
                plan_id: Some(1),
            }),
            rel_type: read_type,
        };

        let logical_plan = LogicalPlanBuilder::new(relation);

        Ok(DataFrame::new(self.spark_session, logical_plan))
    }

    /// Returns the specific table as a [DataFrame]
    ///
    /// # Arguments:
    /// * `table_name`: &str of the table name
    /// * `options`: (optional Hashmap) contains additional read options for a table
    ///
    pub fn table(
        self,
        table_name: &str,
        options: Option<HashMap<String, String>>,
    ) -> Result<DataFrame, SparkError> {
        let read_type = Some(spark::relation::RelType::Read(spark::Read {
            is_streaming: false,
            read_type: Some(spark::read::ReadType::NamedTable(spark::read::NamedTable {
                unparsed_identifier: table_name.to_string(),
                options: options.unwrap_or(self.read_options),
            })),
        }));

        let relation = spark::Relation {
            common: Some(spark::RelationCommon {
                source_info: "NA".to_string(),
                plan_id: Some(1),
            }),
            rel_type: read_type,
        };

        let logical_plan = LogicalPlanBuilder::new(relation);

        Ok(DataFrame::new(self.spark_session, logical_plan))
    }

    /// Reads data from CSV files with the specified options.
    pub fn csv<'a, C, I>(mut self, paths: I, config: C) -> Result<DataFrame, SparkError>
    where
        C: ConfigOpts,
        I: IntoIterator<Item = &'a str>,
    {
        self.format = Some("csv".to_string());
        self.read_options.extend(config.to_options());
        self.load(paths)
    }

    /// Reads data from JSON files with the specified options.
    pub fn json<'a, C, I>(mut self, paths: I, config: C) -> Result<DataFrame, SparkError>
    where
        C: ConfigOpts,
        I: IntoIterator<Item = &'a str>,
    {
        self.format = Some("json".to_string());
        self.read_options.extend(config.to_options());
        self.load(paths)
    }

    /// Reads data from ORC files with the specified options.
    pub fn orc<'a, C, I>(mut self, paths: I, config: C) -> Result<DataFrame, SparkError>
    where
        C: ConfigOpts,
        I: IntoIterator<Item = &'a str>,
    {
        self.format = Some("orc".to_string());
        self.read_options.extend(config.to_options());
        self.load(paths)
    }

    /// Reads data from Parquet files with the specified options.
    pub fn parquet<'a, C, I>(mut self, paths: I, config: C) -> Result<DataFrame, SparkError>
    where
        C: ConfigOpts,
        I: IntoIterator<Item = &'a str>,
    {
        self.format = Some("parquet".to_string());
        self.read_options.extend(config.to_options());
        self.load(paths)
    }

    /// Reads data from text files with the specified options.
    pub fn text<'a, C, I>(mut self, paths: I, config: C) -> Result<DataFrame, SparkError>
    where
        C: ConfigOpts,
        I: IntoIterator<Item = &'a str>,
    {
        self.format = Some("text".to_string());
        self.read_options.extend(config.to_options());
        self.load(paths)
    }
}

/// DataFrameWriter provides the ability to output a [DataFrame]
/// to a specific file format supported by Spark
pub struct DataFrameWriter {
    dataframe: DataFrame,
    format: Option<String>,
    mode: SaveMode,
    bucket_by: Option<spark::write_operation::BucketBy>,
    partition_by: Vec<String>,
    sort_by: Vec<String>,
    write_options: HashMap<String, String>,
}

impl DataFrameWriter {
    /// Create a new DataFrameWriter from a provided [DataFrame]
    ///
    /// # Defaults
    /// - `format`: None,
    /// - `mode`: [SaveMode::Overwrite],
    /// - `bucket_by`: None,
    /// - `partition_by`: vec![],
    /// - `sort_by`: vec![],
    /// - `write_options`: HashMap::new()
    ///
    pub fn new(dataframe: DataFrame) -> Self {
        Self {
            dataframe,
            format: None,
            mode: SaveMode::Overwrite,
            bucket_by: None,
            partition_by: vec![],
            sort_by: vec![],
            write_options: HashMap::new(),
        }
    }

    /// Target format to output the [DataFrame]
    pub fn format(mut self, format: &str) -> Self {
        self.format = Some(format.to_string());
        self
    }

    /// Specifies the behavior when data or table already exists
    ///
    /// # Arguments:
    /// - `mode`: [SaveMode] enum from the protobuf
    ///
    pub fn mode(mut self, mode: SaveMode) -> Self {
        self.mode = mode;
        self
    }

    /// Buckets the output by the given columns.
    /// If specified, the output is laid out on the file system
    /// similar to Hive’s bucketing scheme.
    pub fn bucket_by<'a, I>(mut self, num_buckets: i32, buckets: I) -> Self
    where
        I: IntoIterator<Item = &'a str>,
    {
        self.bucket_by = Some(spark::write_operation::BucketBy {
            bucket_column_names: buckets.into_iter().map(|b| b.to_string()).collect(),
            num_buckets,
        });
        self
    }

    /// Sorts the output in each bucket by the given columns on the file system
    pub fn sort_by<'a, I>(mut self, cols: I) -> Self
    where
        I: IntoIterator<Item = &'a str>,
    {
        self.sort_by = cols.into_iter().map(|col| col.to_string()).collect();
        self
    }

    /// Partitions the output by the given columns on the file system
    pub fn partition_by<'a, I>(mut self, cols: I) -> Self
    where
        I: IntoIterator<Item = &'a str>,
    {
        self.sort_by = cols.into_iter().map(|col| col.to_string()).collect();
        self
    }

    /// Add an input option for the underlying data source
    pub fn option(mut self, key: &str, value: &str) -> Self {
        self.write_options
            .insert(key.to_string(), value.to_string());
        self
    }

    /// Set many input options based on an iterator of (key/value pairs) for the underlying data source
    pub fn options<I, K, V>(mut self, options: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: AsRef<str>,
        V: AsRef<str>,
    {
        self.write_options = options
            .into_iter()
            .map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
            .collect();
        self
    }

    /// Save the contents of the [DataFrame] to a data source.
    ///
    /// The data source is specified by the `format` and a set of `options`.
    pub async fn save(self, path: &str) -> Result<(), SparkError> {
        let write_command = spark::command::CommandType::WriteOperation(spark::WriteOperation {
            input: Some(self.dataframe.plan.clone().relation()),
            source: self.format,
            mode: self.mode.into(),
            sort_column_names: self.sort_by,
            partitioning_columns: self.partition_by,
            bucket_by: self.bucket_by,
            options: self.write_options,
            save_type: Some(spark::write_operation::SaveType::Path(path.to_string())),
        });

        let plan = LogicalPlanBuilder::plan_cmd(write_command);

        self.dataframe
            .spark_session
            .client()
            .execute_command(plan)
            .await
    }

    async fn save_table(self, table_name: &str, save_method: i32) -> Result<(), SparkError> {
        let write_command = spark::command::CommandType::WriteOperation(spark::WriteOperation {
            input: Some(self.dataframe.plan.relation()),
            source: self.format,
            mode: self.mode.into(),
            sort_column_names: self.sort_by,
            partitioning_columns: self.partition_by,
            bucket_by: self.bucket_by,
            options: self.write_options,
            save_type: Some(spark::write_operation::SaveType::Table(
                spark::write_operation::SaveTable {
                    table_name: table_name.to_string(),
                    save_method,
                },
            )),
        });

        let plan = LogicalPlanBuilder::plan_cmd(write_command);

        self.dataframe
            .spark_session
            .client()
            .execute_command(plan)
            .await
    }

    /// Saves the context of the [DataFrame] as the specified table.
    pub async fn save_as_table(self, table_name: &str) -> Result<(), SparkError> {
        self.save_table(table_name, 1).await
    }

    /// Inserts the content of the [DataFrame] to the specified table.
    ///
    /// It requires that the schema of the [DataFrame] is the same as the
    /// schema of the target table.
    ///
    /// Unlike `saveAsTable()`, this method ignores the column names and just uses
    /// position-based resolution
    pub async fn insert_into(self, table_name: &str) -> Result<(), SparkError> {
        self.save_table(table_name, 2).await
    }

    /// Writes the DataFrame to a CSV file with the specified options.
    pub async fn csv<C: ConfigOpts>(mut self, path: &str, config: C) -> Result<(), SparkError> {
        self.format = Some("csv".to_string());
        self.write_options.extend(config.to_options());
        self.save(path).await
    }

    /// Writes the DataFrame to a JSON file with the specified options.
    pub async fn json<C: ConfigOpts>(mut self, path: &str, config: C) -> Result<(), SparkError> {
        self.format = Some("json".to_string());
        self.write_options.extend(config.to_options());
        self.save(path).await
    }

    /// Writes the DataFrame to an ORC file with the specified options.
    pub async fn orc<C: ConfigOpts>(mut self, path: &str, config: C) -> Result<(), SparkError> {
        self.format = Some("orc".to_string());
        self.write_options.extend(config.to_options());
        self.save(path).await
    }

    /// Writes the DataFrame to a Parquet file with the specified options.
    pub async fn parquet<C: ConfigOpts>(mut self, path: &str, config: C) -> Result<(), SparkError> {
        self.format = Some("parquet".to_string());
        self.write_options.extend(config.to_options());
        self.save(path).await
    }

    /// Writes the DataFrame to a text file with the specified options.
    pub async fn text<C: ConfigOpts>(mut self, path: &str, config: C) -> Result<(), SparkError> {
        self.format = Some("text".to_string());
        self.write_options.extend(config.to_options());
        self.save(path).await
    }
}

pub struct DataFrameWriterV2 {
    dataframe: DataFrame,
    table: String,
    provider: Option<String>,
    options: HashMap<String, String>,
    properties: HashMap<String, String>,
    partitioning: Vec<Expression>,
    overwrite_condition: Option<Expression>,
}

impl DataFrameWriterV2 {
    pub fn new(dataframe: DataFrame, table: &str) -> Self {
        Self {
            dataframe,
            table: table.to_string(),
            provider: None,
            options: HashMap::new(),
            properties: HashMap::new(),
            partitioning: vec![],
            overwrite_condition: None,
        }
    }

    pub fn using(mut self, provider: &str) -> Self {
        self.provider.replace(provider.to_string());
        self
    }

    pub fn option(mut self, key: &str, value: &str) -> Self {
        self.options.insert(key.to_string(), value.to_string());
        self
    }

    pub fn options(mut self, provider: HashMap<String, String>) -> Self {
        self.options.extend(provider);
        self
    }

    pub fn table_property(mut self, property: &str, value: &str) -> Self {
        self.properties
            .insert(property.to_string(), value.to_string());
        self
    }

    pub fn partition_by<I, S>(mut self, columns: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<Column>,
    {
        self.partitioning = VecExpression::from_iter(columns).expr;
        self
    }

    pub async fn create(self) -> Result<(), SparkError> {
        self.execute_write(Mode::Create).await
    }

    pub async fn replace(self) -> Result<(), SparkError> {
        self.execute_write(Mode::Replace).await
    }

    pub async fn create_or_replace(self) -> Result<(), SparkError> {
        self.execute_write(Mode::CreateOrReplace).await
    }

    pub async fn append(self) -> Result<(), SparkError> {
        self.execute_write(Mode::Append).await
    }

    pub async fn overwrite(self) -> Result<(), SparkError> {
        self.execute_write(Mode::Overwrite).await
    }

    pub async fn overwrite_partitions(self) -> Result<(), SparkError> {
        self.execute_write(Mode::OverwritePartitions).await
    }

    async fn execute_write(self, mode: Mode) -> Result<(), SparkError> {
        let mut builder = spark::WriteOperationV2 {
            input: Some(self.dataframe.plan.relation()),
            table_name: self.table,
            provider: self.provider,
            partitioning_columns: self.partitioning,
            options: self.options,
            table_properties: self.properties,
            mode: 0,
            overwrite_condition: self.overwrite_condition,
        };

        builder.set_mode(mode);

        let cmd = spark::command::CommandType::WriteOperationV2(builder);
        let plan = LogicalPlanBuilder::plan_cmd(cmd);

        self.dataframe
            .spark_session
            .client()
            .execute_command(plan)
            .await
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    use std::sync::Arc;

    use crate::errors::SparkError;
    use crate::functions::*;
    use crate::types::{DataType, StructField, StructType};
    use crate::SparkSessionBuilder;

    use arrow::{
        array::{ArrayRef, StringArray},
        record_batch::RecordBatch,
    };

    async fn setup() -> SparkSession {
        println!("SparkSession Setup");

        let connection = "sc://127.0.0.1:15002/;user_id=rust_write;session_id=32c39012-896c-42fa-b487-969ee50e253b";

        SparkSessionBuilder::remote(connection)
            .build()
            .await
            .unwrap()
    }

    #[tokio::test]
    async fn test_dataframe_read() -> Result<(), SparkError> {
        let spark = setup().await;

        let path = ["/opt/spark/work-dir/datasets/people.csv"];

        let df = spark
            .read()
            .format("csv")
            .option("header", "True")
            .option("delimiter", ";")
            .load(path)?;

        let rows = df.collect().await?;

        assert_eq!(rows.num_rows(), 2);
        Ok(())
    }

    #[tokio::test]
    async fn test_dataframe_read_csv_with_options() -> Result<(), SparkError> {
        let spark = setup().await;

        let path = ["/opt/spark/work-dir/datasets/people.csv"];

        let mut opts = CsvOptions::default();

        opts.header = Some(true);
        opts.null_value = Some("NULL".to_string());
        opts.sep = Some(";".to_string());
        opts.infer_schema = Some(true);
        opts.encoding = Some("UTF-8".to_string());
        opts.quote = Some("\"".to_string());
        opts.escape = Some("\\".to_string());
        opts.multi_line = Some(false);
        opts.date_format = Some("yyyy-MM-dd".to_string());
        opts.timestamp_format = Some("yyyy-MM-dd'T'HH:mm:ss".to_string());
        opts.ignore_leading_white_space = Some(true);
        opts.ignore_trailing_white_space = Some(true);
        opts.mode = Some("DROPMALFORMED".to_string());

        let df = spark.read().csv(path, opts)?;

        let rows = df.clone().collect().await?;

        assert_eq!(rows.num_rows(), 2);
        Ok(())
    }

    #[tokio::test]
    async fn test_dataframe_read_json_with_options() -> Result<(), SparkError> {
        let spark = setup().await;

        let path = ["/opt/spark/work-dir/datasets/employees.json"];

        let mut opts = JsonOptions::default();

        opts.schema = Some("name STRING, salary INT".to_string());
        opts.multi_line = Some(false);
        opts.allow_comments = Some(false);
        opts.allow_unquoted_field_names = Some(false);
        opts.primitives_as_string = Some(false);
        opts.compression = Some("gzip".to_string());
        opts.ignore_null_fields = Some(true);

        let df = spark.read().json(path, opts)?;

        let rows = df.clone().collect().await?;

        assert_eq!(rows.num_rows(), 4);
        Ok(())
    }

    #[tokio::test]
    async fn test_dataframe_read_orc_with_options() -> Result<(), SparkError> {
        let spark = setup().await;

        let path = ["/opt/spark/work-dir/datasets/users.orc"];

        let mut opts = OrcOptions::default();

        opts.compression = Some("snappy".to_string());
        opts.merge_schema = Some(true);
        opts.common.path_glob_filter = Some("*.orc".to_string());
        opts.common.recursive_file_lookup = Some(true);

        let df = spark.read().orc(path, opts)?;

        let rows = df.clone().collect().await?;

        assert_eq!(rows.num_rows(), 2);
        Ok(())
    }

    #[tokio::test]
    async fn test_dataframe_read_parquet_with_options() -> Result<(), SparkError> {
        let spark = setup().await;

        let path = ["/opt/spark/work-dir/datasets/users.parquet"];

        let mut opts = ParquetOptions::default();

        opts.compression = Some("snappy".to_string());
        opts.common.path_glob_filter = Some("*.parquet".to_string());
        opts.common.recursive_file_lookup = Some(true);

        let df = spark.read().parquet(path, opts)?;

        let rows = df.clone().collect().await?;

        assert_eq!(rows.num_rows(), 2);
        Ok(())
    }

    #[tokio::test]
    async fn test_dataframe_read_text_with_options() -> Result<(), SparkError> {
        let spark = setup().await;

        let path = ["/opt/spark/work-dir/datasets/people.txt"];

        let mut opts = TextOptions::default();

        // If true, read each file from input path(s) as a single row.
        opts.whole_text = Some(false);
        opts.line_sep = Some("\n".to_string());
        opts.common.path_glob_filter = Some("*.txt".to_string());
        opts.common.recursive_file_lookup = Some(true);

        let df = spark.read().text(path, opts)?;

        let rows = df.clone().collect().await?;

        assert_eq!(rows.num_rows(), 3);
        Ok(())
    }

    #[tokio::test]
    async fn test_dataframe_read_schema() -> Result<(), SparkError> {
        let spark = setup().await;

        let path = ["/opt/spark/work-dir/datasets/people.csv"];

        let schema = StructType::new(vec![
            StructField {
                name: "name",
                data_type: DataType::String,
                nullable: false,
                metadata: None,
            },
            StructField {
                name: "age",
                data_type: DataType::Short,
                nullable: true,
                metadata: None,
            },
        ]);

        let df = spark.read().format("json").schema(schema).load(path)?;

        let schema_datatype = df.print_schema(None).await?;

        let df = spark
            .read()
            .format("json")
            .schema("name string, age short")
            .load(path)?;

        let schema_ddl = df.print_schema(None).await?;

        assert_eq!(schema_datatype, schema_ddl);
        Ok(())
    }

    #[tokio::test]
    async fn test_dataframe_write() -> Result<(), SparkError> {
        let spark = setup().await;

        let df = spark
            .range(None, 1000, 1, Some(16))
            .select_expr(vec!["id AS range_id"]);

        let path = "/tmp/range_id/";

        df.write()
            .mode(SaveMode::Overwrite)
            .format("csv")
            .option("header", "true")
            .save(path)
            .await?;

        let df = spark
            .read()
            .format("csv")
            .option("header", "true")
            .load([path])?;

        let records = df.select(vec![col("range_id")]).collect().await?;

        assert_eq!(records.num_rows(), 1000);
        Ok(())
    }

    #[tokio::test]
    async fn test_dataframe_write_table() -> Result<(), SparkError> {
        let spark = setup().await;

        let df = spark
            .range(None, 1000, 1, Some(16))
            .select_expr(vec!["id AS range_id"]);

        df.write()
            .mode(SaveMode::Overwrite)
            .save_as_table("test_table")
            .await?;

        let df = spark.read().table("test_table", None)?;

        let records = df.select(vec![col("range_id")]).collect().await?;

        assert_eq!(records.num_rows(), 1000);
        Ok(())
    }

    #[tokio::test]
    async fn test_dataframev2_write() -> Result<(), SparkError> {
        let spark = setup().await;

        let df = spark
            .range(None, 1000, 1, Some(16))
            .select_expr(vec!["id AS range_id"]);

        let table = "employees";

        df.write_to(table).using("csv").create().await?;

        let df = spark.table(table)?;

        let records = df.select(vec![col("range_id")]).collect().await?;

        assert_eq!(records.num_rows(), 1000);
        Ok(())
    }

    #[tokio::test]
    async fn test_dataframe_write_csv_with_options() -> Result<(), SparkError> {
        let spark = setup().await;

        let df = spark
            .range(None, 1000, 1, Some(16))
            .select_expr(vec!["id AS range_id"]);

        let path = "/tmp/csv_with_options_rande_id/";

        let mut write_opts = CsvOptions::default();

        write_opts.header = Some(true);
        write_opts.null_value = Some("NULL".to_string());

        let _ = df
            .write()
            .mode(SaveMode::Overwrite)
            .csv(path, write_opts)
            .await;

        let path = ["/tmp/csv_with_options_rande_id/"];

        let mut read_opts = CsvOptions::default();

        read_opts.header = Some(true);

        let df = spark.read().csv(path, read_opts)?;

        let records = df.select(vec![col("range_id")]).collect().await?;

        assert_eq!(records.num_rows(), 1000);
        Ok(())
    }

    #[tokio::test]
    async fn test_dataframe_write_json_with_options() -> Result<(), SparkError> {
        let spark = setup().await;

        let df = spark
            .range(None, 1000, 1, Some(16))
            .select_expr(vec!["id AS range_id"]);

        let path = "/tmp/json_with_options_rande_id/";

        let mut write_opts = JsonOptions::default();

        write_opts.multi_line = Some(true);
        write_opts.allow_comments = Some(false);
        write_opts.allow_unquoted_field_names = Some(false);
        write_opts.primitives_as_string = Some(false);

        let _ = df
            .write()
            .mode(SaveMode::Overwrite)
            .json(path, write_opts)
            .await;

        let path = ["/tmp/json_with_options_rande_id/"];

        let read_opts = JsonOptions::default();

        let df = spark.read().json(path, read_opts)?;

        let records = df.select(vec![col("range_id")]).collect().await?;

        assert_eq!(records.num_rows(), 1000);
        Ok(())
    }

    #[tokio::test]
    async fn test_dataframe_write_orc_with_options() -> Result<(), SparkError> {
        let spark = setup().await;

        let df = spark
            .range(None, 1000, 1, Some(16))
            .select_expr(vec!["id AS range_id"]);

        let path = "/tmp/orc_with_options_rande_id/";

        let write_opts = OrcOptions::default();

        let _ = df
            .write()
            .mode(SaveMode::Overwrite)
            .orc(path, write_opts)
            .await;

        let path = ["/tmp/orc_with_options_rande_id/"];

        let mut read_opts = OrcOptions::default();

        read_opts.merge_schema = Some(true);
        read_opts.common.path_glob_filter = Some("*.orc".to_string());
        read_opts.common.recursive_file_lookup = Some(true);

        let df = spark.read().orc(path, read_opts)?;

        let records = df.select(vec![col("range_id")]).collect().await?;

        assert_eq!(records.num_rows(), 1000);
        Ok(())
    }

    #[tokio::test]
    async fn test_dataframe_write_parquet_with_options() -> Result<(), SparkError> {
        let spark = setup().await;

        let df = spark
            .range(None, 1000, 1, Some(16))
            .select_expr(vec!["id AS range_id"]);

        let path = "/tmp/parquet_with_options_rande_id/";

        let mut write_opts = ParquetOptions::default();

        // Configure datetime rebase mode (options could be "EXCEPTION", "LEGACY", or "CORRECTED").
        write_opts.datetime_rebase_mode = Some("CORRECTED".to_string());

        // Configure int96 rebase mode (options could be "EXCEPTION", "LEGACY", or "CORRECTED").
        write_opts.int96_rebase_mode = Some("LEGACY".to_string());

        let _ = df
            .write()
            .mode(SaveMode::Overwrite)
            .parquet(path, write_opts)
            .await;

        let path = ["/tmp/parquet_with_options_rande_id/"];

        let mut read_opts = ParquetOptions::default();

        read_opts.merge_schema = Some(false);
        read_opts.common.path_glob_filter = Some("*.parquet".to_string());
        read_opts.common.recursive_file_lookup = Some(true);

        let df = spark.read().parquet(path, read_opts)?;

        let records = df.select(vec![col("range_id")]).collect().await?;

        assert_eq!(records.num_rows(), 1000);
        Ok(())
    }

    #[tokio::test]
    async fn test_dataframe_write_text_with_options() -> Result<(), SparkError> {
        let spark = setup().await;

        let names: ArrayRef = Arc::new(StringArray::from(vec![
            Some("Michael"),
            Some("Andy"),
            Some("Justin"),
        ]));

        let data = RecordBatch::try_from_iter(vec![("names", names)])?;

        let df = spark.create_dataframe(&data)?;

        let path = "/tmp/text_with_options_rande_id/";

        let mut write_opts = TextOptions::default();

        write_opts.whole_text = Some(true);

        // Note that, in order to use write.text(), the dataframe
        // must have only one column else it will throw error.
        // Hence you need to covert all columns into single column.
        let _ = df
            .write()
            .mode(SaveMode::Overwrite)
            .text(path, write_opts)
            .await;

        let path = ["/tmp/text_with_options_rande_id/"];

        let mut read_opts = TextOptions::default();

        read_opts.whole_text = Some(true);
        read_opts.line_sep = Some("\n".to_string());
        read_opts.common.path_glob_filter = Some("*.txt".to_string());
        read_opts.common.recursive_file_lookup = Some(true);

        let df = spark.read().text(path, read_opts)?;

        let rows = df.clone().collect().await?;

        assert_eq!(rows.num_rows(), 3);
        Ok(())
    }
}