robin-sparkless 4.4.0

PySpark-like DataFrame API in Rust on Polars; no JVM.
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
"""JDBC read/write tests with SQLite.

These tests use an in-memory SQLite database so no external server is required.
They verify PySpark-compatible JDBC options work correctly.
"""

from __future__ import annotations

import tempfile
import os

import pytest

from tests.sql.conftest import jdbc_available

pytestmark = pytest.mark.skipif(
    not jdbc_available(),
    reason="JDBC/SQLite support not enabled in this build",
)


def test_read_jdbc_sqlite_basic(spark) -> None:
    """spark.read.jdbc() works with SQLite file database."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE test_table (id INTEGER PRIMARY KEY, name TEXT)")
        conn.execute("INSERT INTO test_table (id, name) VALUES (1, 'Alice')")
        conn.execute("INSERT INTO test_table (id, name) VALUES (2, 'Bob')")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = spark.read.jdbc(url=url, table="test_table", properties={})
        rows = df.collect()
        assert len(rows) == 2
        names = {r["name"] for r in rows}
        assert names == {"Alice", "Bob"}
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_read_jdbc_with_options_api(spark) -> None:
    """spark.read.option().jdbc() passes options correctly."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT)")
        conn.execute("INSERT INTO users (id, email) VALUES (1, 'a@test.com')")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = spark.read.option("fetchsize", "100").jdbc(
            url=url, table="users", properties={}
        )
        rows = df.collect()
        assert len(rows) == 1
        assert rows[0]["email"] == "a@test.com"
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_write_jdbc_sqlite_append(spark) -> None:
    """df.write.jdbc(..., mode='append') appends rows to SQLite."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE append_test (id INTEGER, value TEXT)")
        conn.execute("INSERT INTO append_test VALUES (1, 'initial')")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"

        df = spark.createDataFrame([(2, "appended")], schema="id int, value string")
        df.write.jdbc(url=url, table="append_test", properties={}, mode="append")

        read_df = spark.read.jdbc(url=url, table="append_test", properties={})
        rows = read_df.collect()
        assert len(rows) == 2
        values = {r["value"] for r in rows}
        assert values == {"initial", "appended"}
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_write_jdbc_sqlite_overwrite(spark) -> None:
    """df.write.jdbc(..., mode='overwrite') replaces existing data."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE overwrite_test (id INTEGER, value TEXT)")
        conn.execute("INSERT INTO overwrite_test VALUES (1, 'old')")
        conn.execute("INSERT INTO overwrite_test VALUES (2, 'data')")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"

        df = spark.createDataFrame([(100, "new")], schema="id int, value string")
        df.write.jdbc(url=url, table="overwrite_test", properties={}, mode="overwrite")

        read_df = spark.read.jdbc(url=url, table="overwrite_test", properties={})
        rows = read_df.collect()
        assert len(rows) == 1
        assert rows[0]["value"] == "new"
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_format_jdbc_load(spark) -> None:
    """spark.read.format('jdbc').option('url', ...).option('dbtable', ...).load() works."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE format_test (id INTEGER, name TEXT)")
        conn.execute("INSERT INTO format_test VALUES (1, 'test')")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = (
            spark.read.format("jdbc")
            .option("url", url)
            .option("dbtable", "format_test")
            .load(".")
        )
        rows = df.collect()
        assert len(rows) == 1
        assert rows[0]["name"] == "test"
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_with_query_option(spark) -> None:
    """Using query option instead of dbtable executes custom SQL."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute(
            "CREATE TABLE query_test (id INTEGER, category TEXT, amount INTEGER)"
        )
        conn.execute("INSERT INTO query_test VALUES (1, 'A', 100)")
        conn.execute("INSERT INTO query_test VALUES (2, 'B', 200)")
        conn.execute("INSERT INTO query_test VALUES (3, 'A', 150)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = (
            spark.read.format("jdbc")
            .option("url", url)
            .option(
                "query",
                "SELECT category, SUM(amount) as total FROM query_test GROUP BY category",
            )
            .load(".")
        )
        rows = df.collect()
        assert len(rows) == 2
        totals = {r["category"]: r["total"] for r in rows}
        assert totals["A"] == 250
        assert totals["B"] == 200
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_session_init_statement(spark) -> None:
    """sessionInitStatement executes SQL after connection is opened."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE session_init_test (id INTEGER, value TEXT)")
        conn.execute("INSERT INTO session_init_test VALUES (1, 'original')")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = (
            spark.read.format("jdbc")
            .option("url", url)
            .option("dbtable", "session_init_test")
            .option("sessionInitStatement", "SELECT 1")
            .load(".")
        )
        rows = df.collect()
        assert len(rows) == 1
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_batchsize_write(spark) -> None:
    """batchsize option controls write batching."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE batch_test (id INTEGER, value TEXT)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"

        data = [(i, f"row_{i}") for i in range(100)]
        df = spark.createDataFrame(data, schema="id int, value string")
        df.write.jdbc(
            url=url, table="batch_test", properties={"batchsize": "10"}, mode="append"
        )

        read_df = spark.read.jdbc(url=url, table="batch_test", properties={})
        assert read_df.count() == 100
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_truncate_option(spark) -> None:
    """truncate=true uses TRUNCATE (or DELETE for SQLite) before overwrite."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE truncate_test (id INTEGER, name TEXT)")
        conn.execute("INSERT INTO truncate_test VALUES (1, 'old')")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"

        df = spark.createDataFrame([(2, "new")], schema="id int, name string")
        df.write.jdbc(
            url=url,
            table="truncate_test",
            properties={"truncate": "true"},
            mode="overwrite",
        )

        read_df = spark.read.jdbc(url=url, table="truncate_test", properties={})
        rows = read_df.collect()
        assert len(rows) == 1
        assert rows[0]["name"] == "new"
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_error_if_exists_mode(spark) -> None:
    """mode='error' raises exception if table has data."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE error_test (id INTEGER, name TEXT)")
        conn.execute("INSERT INTO error_test VALUES (1, 'exists')")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"

        df = spark.createDataFrame([(2, "new")], schema="id int, name string")
        with pytest.raises(Exception) as exc_info:
            df.write.jdbc(url=url, table="error_test", properties={}, mode="error")

        err_msg = str(exc_info.value).lower()
        assert "exists" in err_msg or "not empty" in err_msg or "data" in err_msg
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_ignore_mode(spark) -> None:
    """mode='ignore' does nothing if table has data."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE ignore_test (id INTEGER, name TEXT)")
        conn.execute("INSERT INTO ignore_test VALUES (1, 'original')")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"

        df = spark.createDataFrame([(2, "new")], schema="id int, name string")
        df.write.jdbc(url=url, table="ignore_test", properties={}, mode="ignore")

        read_df = spark.read.jdbc(url=url, table="ignore_test", properties={})
        rows = read_df.collect()
        assert len(rows) == 1
        assert rows[0]["name"] == "original"
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_prepare_query(spark) -> None:
    """prepareQuery option executes SQL before the main query."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE prepare_source (id INTEGER, value INTEGER)")
        conn.execute("INSERT INTO prepare_source VALUES (1, 100)")
        conn.execute("INSERT INTO prepare_source VALUES (2, 200)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = (
            spark.read.format("jdbc")
            .option("url", url)
            .option(
                "prepareQuery",
                "CREATE TEMP VIEW prepare_view AS SELECT id, value * 2 as doubled FROM prepare_source",
            )
            .option("dbtable", "prepare_view")
            .load(".")
        )
        rows = df.collect()
        assert len(rows) == 2
        doubled_values = {r["doubled"] for r in rows}
        assert doubled_values == {200, 400}
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_memory_database(spark) -> None:
    """Using memory database URL works for ephemeral testing."""
    url = "jdbc:sqlite::memory:"

    df = spark.createDataFrame([(1, "test")], schema="id int, name string")

    # Memory database is per-connection so write then read within same session
    # may not work as expected - this test mainly verifies no crash occurs
    try:
        df.write.jdbc(url=url, table="mem_test", properties={}, mode="overwrite")
    except Exception:
        # Memory DB may not persist between operations - that's expected
        pass


def test_jdbc_integer_types_roundtrip(spark) -> None:
    """Integer values roundtrip correctly through SQLite."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE int_test (small_int INTEGER, big_int INTEGER)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"

        data = [
            (0, 0),
            (1, 100),
            (-1, -100),
            (32767, 2147483647),
            (-32768, -2147483648),
        ]
        df = spark.createDataFrame(data, schema="small_int int, big_int bigint")
        df.write.jdbc(url=url, table="int_test", properties={}, mode="append")

        read_df = spark.read.jdbc(url=url, table="int_test", properties={})
        rows = read_df.collect()
        assert len(rows) == 5

        small_vals = {r["small_int"] for r in rows}
        assert 0 in small_vals
        assert 1 in small_vals
        assert -1 in small_vals
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_float_types_roundtrip(spark) -> None:
    """Float/double values roundtrip correctly through SQLite."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE float_test (value REAL)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"

        data = [(0.0,), (1.5,), (-1.5,), (3.14159,), (1e10,)]
        df = spark.createDataFrame(data, schema="value double")
        df.write.jdbc(url=url, table="float_test", properties={}, mode="append")

        read_df = spark.read.jdbc(url=url, table="float_test", properties={})
        rows = read_df.collect()
        assert len(rows) == 5

        values = [r["value"] for r in rows]
        assert any(abs(v - 3.14159) < 0.0001 for v in values)
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_null_handling(spark) -> None:
    """NULL values are handled correctly."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE null_test (id INTEGER, name TEXT, value REAL)")
        conn.execute("INSERT INTO null_test VALUES (1, NULL, 1.0)")
        conn.execute("INSERT INTO null_test VALUES (2, 'present', NULL)")
        conn.execute("INSERT INTO null_test VALUES (NULL, 'no_id', 2.0)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = spark.read.jdbc(url=url, table="null_test", properties={})
        rows = df.collect()

        assert len(rows) == 3
        # Check that we can read rows with NULL values
        names = [r["name"] for r in rows]
        assert None in names or any(n is None for n in names)
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_special_characters_in_strings(spark) -> None:
    """Special characters in strings are preserved."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE special_chars (id INTEGER, text TEXT)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"

        special_strings = [
            (1, "Hello, World!"),
            (2, "Quote's test"),
            (3, 'Double "quotes"'),
            (4, "Line\nbreak"),
            (5, "Tab\there"),
            (6, "Unicode: 日本語 🎉"),
            (7, "Backslash: \\path\\to\\file"),
        ]
        df = spark.createDataFrame(special_strings, schema="id int, text string")
        df.write.jdbc(url=url, table="special_chars", properties={}, mode="append")

        read_df = spark.read.jdbc(url=url, table="special_chars", properties={})
        rows = read_df.collect()

        assert len(rows) == 7
        texts = {r["text"] for r in rows}
        assert "Hello, World!" in texts
        assert "Quote's test" in texts
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_options_dict_api(spark) -> None:
    """spark.read.options({...}) works with JDBC."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE options_test (id INTEGER, name TEXT)")
        conn.execute("INSERT INTO options_test VALUES (1, 'test')")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        opts = {
            "url": url,
            "dbtable": "options_test",
        }
        df = spark.read.format("jdbc").options(opts).load(".")
        rows = df.collect()

        assert len(rows) == 1
        assert rows[0]["name"] == "test"
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_schema_fields(spark) -> None:
    """DataFrame schema reflects database column names and types."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute(
            "CREATE TABLE schema_test (user_id INTEGER, user_name TEXT, balance REAL)"
        )
        conn.execute("INSERT INTO schema_test VALUES (1, 'Alice', 100.50)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = spark.read.jdbc(url=url, table="schema_test", properties={})

        field_names = [f.name for f in df.schema.fields]
        assert "user_id" in field_names
        assert "user_name" in field_names
        assert "balance" in field_names
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_count_operation(spark) -> None:
    """DataFrame.count() works on JDBC-sourced data."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE count_test (id INTEGER)")
        for i in range(50):
            conn.execute("INSERT INTO count_test VALUES (?)", (i,))
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = spark.read.jdbc(url=url, table="count_test", properties={})

        assert df.count() == 50
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_filter_after_read(spark) -> None:
    """DataFrame operations like filter work after JDBC read."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE filter_test (id INTEGER, category TEXT)")
        conn.execute("INSERT INTO filter_test VALUES (1, 'A')")
        conn.execute("INSERT INTO filter_test VALUES (2, 'B')")
        conn.execute("INSERT INTO filter_test VALUES (3, 'A')")
        conn.execute("INSERT INTO filter_test VALUES (4, 'C')")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = spark.read.jdbc(url=url, table="filter_test", properties={})

        filtered = df.filter(df["category"] == "A")
        rows = filtered.collect()

        assert len(rows) == 2
        assert all(r["category"] == "A" for r in rows)
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_select_columns(spark) -> None:
    """DataFrame.select() works after JDBC read."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE select_test (id INTEGER, name TEXT, email TEXT)")
        conn.execute("INSERT INTO select_test VALUES (1, 'Alice', 'alice@test.com')")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = spark.read.jdbc(url=url, table="select_test", properties={})

        selected = df.select("id", "name")
        rows = selected.collect()

        assert len(rows) == 1
        assert "id" in rows[0].asDict()
        assert "name" in rows[0].asDict()
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_multiple_writes(spark) -> None:
    """Multiple append writes accumulate data correctly."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE multi_write (id INTEGER, batch INTEGER)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"

        # Write batch 1
        df1 = spark.createDataFrame([(1, 1), (2, 1)], schema="id int, batch int")
        df1.write.jdbc(url=url, table="multi_write", properties={}, mode="append")

        # Write batch 2
        df2 = spark.createDataFrame([(3, 2), (4, 2)], schema="id int, batch int")
        df2.write.jdbc(url=url, table="multi_write", properties={}, mode="append")

        # Write batch 3
        df3 = spark.createDataFrame([(5, 3)], schema="id int, batch int")
        df3.write.jdbc(url=url, table="multi_write", properties={}, mode="append")

        read_df = spark.read.jdbc(url=url, table="multi_write", properties={})
        assert read_df.count() == 5

        rows = read_df.collect()
        batches = {r["batch"] for r in rows}
        assert batches == {1, 2, 3}
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_empty_table_read(spark) -> None:
    """Reading an empty table returns empty DataFrame."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE empty_table (id INTEGER, name TEXT)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = spark.read.jdbc(url=url, table="empty_table", properties={})

        assert df.count() == 0
        rows = df.collect()
        assert len(rows) == 0
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_properties_passed_via_jdbc_method(spark) -> None:
    """Properties dict passed to jdbc() method is used."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE props_test (id INTEGER)")
        conn.execute("INSERT INTO props_test VALUES (1)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        # Pass properties including fetchsize
        props = {"fetchsize": "500"}
        df = spark.read.jdbc(url=url, table="props_test", properties=props)

        rows = df.collect()
        assert len(rows) == 1
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_chained_options(spark) -> None:
    """Multiple chained .option() calls work correctly."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE chained_test (id INTEGER, val TEXT)")
        conn.execute("INSERT INTO chained_test VALUES (1, 'a')")
        conn.execute("INSERT INTO chained_test VALUES (2, 'b')")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = (
            spark.read.format("jdbc")
            .option("url", url)
            .option("dbtable", "chained_test")
            .option("fetchsize", "100")
            .option("sessionInitStatement", "SELECT 1")
            .load(".")
        )

        rows = df.collect()
        assert len(rows) == 2
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_write_mode_default_append(spark) -> None:
    """Default write mode without explicit mode works as append."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE default_mode (id INTEGER)")
        conn.execute("INSERT INTO default_mode VALUES (1)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"

        df = spark.createDataFrame([(2,)], schema="id int")
        # mode defaults to "error" in PySpark, but append is common
        df.write.jdbc(url=url, table="default_mode", properties={}, mode="append")

        read_df = spark.read.jdbc(url=url, table="default_mode", properties={})
        assert read_df.count() == 2
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_boolean_via_integer(spark) -> None:
    """Boolean values stored as integers (0/1) in SQLite roundtrip correctly."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE bool_test (id INTEGER, active INTEGER)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"

        # Write booleans as integers
        data = [(1, True), (2, False), (3, True)]
        df = spark.createDataFrame(data, schema="id int, active boolean")
        df.write.jdbc(url=url, table="bool_test", properties={}, mode="append")

        read_df = spark.read.jdbc(url=url, table="bool_test", properties={})
        rows = read_df.collect()

        assert len(rows) == 3
        # Values should be readable (as int or bool depending on implementation)
        active_vals = [r["active"] for r in rows]
        assert len(active_vals) == 3
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_large_string_values(spark) -> None:
    """Large string values are handled correctly."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE large_str (id INTEGER, content TEXT)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"

        # Create strings of various sizes
        large_string = "x" * 10000
        medium_string = "y" * 1000

        data = [(1, large_string), (2, medium_string), (3, "small")]
        df = spark.createDataFrame(data, schema="id int, content string")
        df.write.jdbc(url=url, table="large_str", properties={}, mode="append")

        read_df = spark.read.jdbc(url=url, table="large_str", properties={})
        rows = read_df.collect()

        assert len(rows) == 3
        contents = {r["id"]: r["content"] for r in rows}
        assert len(contents[1]) == 10000
        assert len(contents[2]) == 1000
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_groupby_after_read(spark) -> None:
    """GroupBy operations work after JDBC read."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE groupby_test (category TEXT, amount INTEGER)")
        conn.execute("INSERT INTO groupby_test VALUES ('A', 10)")
        conn.execute("INSERT INTO groupby_test VALUES ('A', 20)")
        conn.execute("INSERT INTO groupby_test VALUES ('B', 30)")
        conn.execute("INSERT INTO groupby_test VALUES ('B', 40)")
        conn.execute("INSERT INTO groupby_test VALUES ('B', 50)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = spark.read.jdbc(url=url, table="groupby_test", properties={})

        from sparkless.sql import functions as F

        grouped = df.groupBy("category").agg(F.sum("amount").alias("total"))
        rows = grouped.collect()

        assert len(rows) == 2
        totals = {r["category"]: r["total"] for r in rows}
        assert totals["A"] == 30
        assert totals["B"] == 120
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_orderby_after_read(spark) -> None:
    """OrderBy operations work after JDBC read."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE orderby_test (id INTEGER, name TEXT)")
        conn.execute("INSERT INTO orderby_test VALUES (3, 'Charlie')")
        conn.execute("INSERT INTO orderby_test VALUES (1, 'Alice')")
        conn.execute("INSERT INTO orderby_test VALUES (2, 'Bob')")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"
        df = spark.read.jdbc(url=url, table="orderby_test", properties={})

        ordered = df.orderBy("id")
        rows = ordered.collect()

        assert len(rows) == 3
        ids = [r["id"] for r in rows]
        assert ids == [1, 2, 3] or ids == sorted(ids)
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)


def test_jdbc_write_options_via_option_method(spark) -> None:
    """Write options passed via .option() on writer work."""
    with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
        db_path = f.name

    try:
        import sqlite3

        conn = sqlite3.connect(db_path)
        conn.execute("CREATE TABLE write_opts (id INTEGER, name TEXT)")
        conn.commit()
        conn.close()

        url = f"jdbc:sqlite:{db_path}"

        data = [(i, f"name_{i}") for i in range(20)]
        df = spark.createDataFrame(data, schema="id int, name string")

        # Use option on writer
        df.write.option("batchsize", "5").jdbc(
            url=url, table="write_opts", properties={}, mode="append"
        )

        read_df = spark.read.jdbc(url=url, table="write_opts", properties={})
        assert read_df.count() == 20
    finally:
        if os.path.exists(db_path):
            os.unlink(db_path)