robin-sparkless 4.0.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
"""
Tests for chained arithmetic operations with reverse operators.

These tests ensure that:
1. Literals can appear on the left side of arithmetic operations (e.g., `2 * F.col("col")`)
2. Chained arithmetic operations work correctly (e.g., `F.col("a") + 2 * F.col("b") + 0.01`)
3. All arithmetic operations support reverse operators
4. Behavior matches PySpark exactly

These tests work with both sparkless (mock) and PySpark backends.
Set MOCK_SPARK_TEST_BACKEND=pyspark to run with real PySpark.
"""

from tests.fixtures.spark_imports import get_spark_imports

# Get imports based on backend
imports = get_spark_imports()
SparkSession = imports.SparkSession
DoubleType = imports.DoubleType
IntegerType = imports.IntegerType
StringType = imports.StringType
StructType = imports.StructType
StructField = imports.StructField
F = imports.F  # Functions module for backend-appropriate F.col() etc.


class TestChainedArithmetic:
    """Test chained arithmetic operations with reverse operators."""

    def test_reverse_multiplication(self, spark):
        """Test reverse multiplication: `2 * F.col("col")`."""
        schema = StructType([StructField("number_2", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"number_2": 1.0},
                {"number_2": 2.0},
                {"number_2": 3.0},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 2 * F.col("number_2"))

        rows = result.collect()
        assert len(rows) == 3
        assert rows[0]["result"] == 2.0  # 2 * 1.0
        assert rows[1]["result"] == 4.0  # 2 * 2.0
        assert rows[2]["result"] == 6.0  # 2 * 3.0

    def test_reverse_addition(self, spark):
        """Test reverse addition: `2 + F.col("col")`."""
        schema = StructType([StructField("number_2", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"number_2": 1.0},
                {"number_2": 2.0},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 2 + F.col("number_2"))

        rows = result.collect()
        assert len(rows) == 2
        assert rows[0]["result"] == 3.0  # 2 + 1.0
        assert rows[1]["result"] == 4.0  # 2 + 2.0

    def test_reverse_subtraction(self, spark):
        """Test reverse subtraction: `2 - F.col("col")`."""
        schema = StructType([StructField("number_2", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"number_2": 1.0},
                {"number_2": 2.0},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 2 - F.col("number_2"))

        rows = result.collect()
        assert len(rows) == 2
        assert rows[0]["result"] == 1.0  # 2 - 1.0
        assert rows[1]["result"] == 0.0  # 2 - 2.0

    def test_reverse_division(self, spark):
        """Test reverse division: `2 / F.col("col")`."""
        schema = StructType([StructField("number_2", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"number_2": 1.0},
                {"number_2": 2.0},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 2 / F.col("number_2"))

        rows = result.collect()
        assert len(rows) == 2
        assert rows[0]["result"] == 2.0  # 2 / 1.0
        assert rows[1]["result"] == 1.0  # 2 / 2.0

    def test_reverse_modulo(self, spark):
        """Test reverse modulo: `2 % F.col("col")`."""
        schema = StructType([StructField("number_2", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"number_2": 3.0},
                {"number_2": 2.0},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 2 % F.col("number_2"))

        rows = result.collect()
        assert len(rows) == 2
        assert rows[0]["result"] == 2.0  # 2 % 3.0
        assert rows[1]["result"] == 0.0  # 2 % 2.0

    def test_chained_arithmetic_issue_237_example(self, spark):
        """Test the exact example from issue #237."""
        schema = StructType(
            [
                StructField("number_1", DoubleType(), True),
                StructField("number_2", DoubleType(), True),
            ]
        )
        df = spark.createDataFrame(
            [
                {"number_1": 1.0, "number_2": 1.0},
                {"number_1": 2.0, "number_2": 2.0},
                {"number_1": 3.0, "number_2": 3.0},
            ],
            schema=schema,
        )

        result = df.withColumn(
            "result", F.col("number_1") + 2 * F.col("number_2") + 0.01
        )

        rows = result.collect()
        assert len(rows) == 3
        assert rows[0]["result"] == 3.01  # 1.0 + 2 * 1.0 + 0.01 = 1.0 + 2.0 + 0.01
        assert rows[1]["result"] == 6.01  # 2.0 + 2 * 2.0 + 0.01 = 2.0 + 4.0 + 0.01
        assert rows[2]["result"] == 9.01  # 3.0 + 2 * 3.0 + 0.01 = 3.0 + 6.0 + 0.01

    def test_complex_chained_operations(self, spark):
        """Test complex chained operations with multiple literals and columns."""
        schema = StructType(
            [
                StructField("a", DoubleType(), True),
                StructField("b", DoubleType(), True),
            ]
        )
        df = spark.createDataFrame(
            [
                {"a": 10.0, "b": 5.0},
                {"a": 20.0, "b": 10.0},
            ],
            schema=schema,
        )

        result = df.withColumn("result", F.col("a") * 3 - F.col("b") / 2 + 1.5)

        rows = result.collect()
        assert len(rows) == 2
        # 10.0 * 3 - 5.0 / 2 + 1.5 = 30.0 - 2.5 + 1.5 = 29.0
        assert rows[0]["result"] == 29.0
        # 20.0 * 3 - 10.0 / 2 + 1.5 = 60.0 - 5.0 + 1.5 = 56.5
        assert rows[1]["result"] == 56.5

    def test_all_reverse_operations(self, spark):
        """Test all reverse operations in one expression."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 2.0},
            ],
            schema=schema,
        )

        # Test all reverse operations
        result = (
            df.withColumn("add", 5 + F.col("col"))
            .withColumn("sub", 5 - F.col("col"))
            .withColumn("mul", 5 * F.col("col"))
            .withColumn("div", 5 / F.col("col"))
            .withColumn("mod", 5 % F.col("col"))
        )

        rows = result.collect()
        assert rows[0]["add"] == 7.0  # 5 + 2.0
        assert rows[0]["sub"] == 3.0  # 5 - 2.0
        assert rows[0]["mul"] == 10.0  # 5 * 2.0
        assert rows[0]["div"] == 2.5  # 5 / 2.0
        assert rows[0]["mod"] == 1.0  # 5 % 2.0

    def test_reverse_operations_with_integers(self, spark):
        """Test reverse operations with integer literals."""
        schema = StructType([StructField("col", IntegerType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 3},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 10 * F.col("col"))

        rows = result.collect()
        assert rows[0]["result"] == 30  # 10 * 3

    def test_reverse_operations_with_floats(self, spark):
        """Test reverse operations with float literals."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 2.0},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 3.5 * F.col("col"))

        rows = result.collect()
        assert rows[0]["result"] == 7.0  # 3.5 * 2.0

    def test_nested_chained_operations(self, spark):
        """Test deeply nested chained operations."""
        schema = StructType(
            [
                StructField("a", DoubleType(), True),
                StructField("b", DoubleType(), True),
                StructField("c", DoubleType(), True),
            ]
        )
        df = spark.createDataFrame(
            [
                {"a": 1.0, "b": 2.0, "c": 3.0},
            ],
            schema=schema,
        )

        result = df.withColumn(
            "result", 2 * F.col("a") + 3 * F.col("b") - 4 * F.col("c") + 1.0
        )

        rows = result.collect()
        # 2 * 1.0 + 3 * 2.0 - 4 * 3.0 + 1.0 = 2.0 + 6.0 - 12.0 + 1.0 = -3.0
        assert rows[0]["result"] == -3.0

    def test_reverse_operations_in_select(self, spark):
        """Test reverse operations in select statements."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 5.0},
            ],
            schema=schema,
        )

        result = df.select((10 * F.col("col")).alias("result"))

        rows = result.collect()
        assert rows[0]["result"] == 50.0  # 10 * 5.0

    def test_reverse_operations_in_filter(self, spark):
        """Test reverse operations in filter conditions."""
        schema = StructType([StructField("value", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"value": 5.0},
                {"value": 10.0},
                {"value": 15.0},
            ],
            schema=schema,
        )

        # Filter where 2 * value > 10
        result = df.filter(2 * F.col("value") > 10)

        rows = result.collect()
        assert len(rows) == 2  # 2 * 10.0 = 20 > 10, 2 * 15.0 = 30 > 10
        assert rows[0]["value"] == 10.0
        assert rows[1]["value"] == 15.0

    def test_mixed_forward_and_reverse_operations(self, spark):
        """Test mixing forward and reverse operations."""
        schema = StructType(
            [
                StructField("a", DoubleType(), True),
                StructField("b", DoubleType(), True),
            ]
        )
        df = spark.createDataFrame(
            [
                {"a": 2.0, "b": 3.0},
            ],
            schema=schema,
        )

        # Mix: col * literal and literal * col
        result = df.withColumn("result", F.col("a") * 5 + 10 * F.col("b"))

        rows = result.collect()
        assert rows[0]["result"] == 40.0  # 2.0 * 5 + 10 * 3.0 = 10.0 + 30.0

    def test_reverse_operations_with_null_values(self, spark):
        """Test reverse operations with null column values."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 2.0},
                {"col": None},
                {"col": 4.0},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 10 * F.col("col"))

        rows = result.collect()
        assert rows[0]["result"] == 20.0  # 10 * 2.0
        assert rows[1]["result"] is None  # 10 * None
        assert rows[2]["result"] == 40.0  # 10 * 4.0

    def test_reverse_operations_with_negative_numbers(self, spark):
        """Test reverse operations with negative literals."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 5.0},
            ],
            schema=schema,
        )

        result = df.withColumn("result", -2 * F.col("col"))

        rows = result.collect()
        assert rows[0]["result"] == -10.0  # -2 * 5.0

    def test_reverse_operations_chained_with_arithmetic(self, spark):
        """Test reverse operations in complex arithmetic chains."""
        schema = StructType(
            [
                StructField("x", DoubleType(), True),
                StructField("y", DoubleType(), True),
            ]
        )
        df = spark.createDataFrame(
            [
                {"x": 1.0, "y": 2.0},
            ],
            schema=schema,
        )

        # Complex: (2 * x) + (3 * y) - (4 * x) + 1
        result = df.withColumn(
            "result", 2 * F.col("x") + 3 * F.col("y") - 4 * F.col("x") + 1.0
        )

        rows = result.collect()
        # 2 * 1.0 + 3 * 2.0 - 4 * 1.0 + 1.0 = 2.0 + 6.0 - 4.0 + 1.0 = 5.0
        assert rows[0]["result"] == 5.0

    def test_operator_precedence(self, spark):
        """Test that operator precedence is correctly handled in chained operations."""
        schema = StructType([StructField("a", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"a": 2.0},
            ],
            schema=schema,
        )

        # Test precedence: multiplication before addition
        # 1 + 2 * a should be 1 + (2 * a) = 1 + 4 = 5, not (1 + 2) * a = 6
        result = df.withColumn("result", 1 + 2 * F.col("a"))

        rows = result.collect()
        assert rows[0]["result"] == 5.0  # 1 + (2 * 2.0) = 1 + 4.0 = 5.0

    def test_operator_precedence_with_parentheses_equivalent(self, spark):
        """Test that explicit parentheses work correctly."""
        schema = StructType([StructField("a", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"a": 2.0},
            ],
            schema=schema,
        )

        # Test that (1 + 2) * a gives different result than 1 + 2 * a
        result = df.withColumn("result", (1 + 2) * F.col("a"))

        rows = result.collect()
        assert rows[0]["result"] == 6.0  # (1 + 2) * 2.0 = 3 * 2.0 = 6.0

    def test_all_operators_in_single_expression(self, spark):
        """Test all arithmetic operators in a single chained expression."""
        schema = StructType([StructField("a", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"a": 3.0},
            ],
            schema=schema,
        )

        # Test: 10 + 2 * a - 5 / a + 3 % a
        # Order: 2 * 3 = 6, 5 / 3 = 1.67, 3 % 3 = 0
        # 10 + 6 - 1.67 + 0 = 14.33
        result = df.withColumn(
            "result", 10 + 2 * F.col("a") - 5 / F.col("a") + 3 % F.col("a")
        )

        rows = result.collect()
        # 10 + (2 * 3.0) - (5 / 3.0) + (3 % 3.0) = 10 + 6.0 - 1.67 + 0.0 = 14.33
        assert abs(rows[0]["result"] - 14.333333333333334) < 0.0001

    def test_reverse_operations_with_zero(self, spark):
        """Test reverse operations with zero literals."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 5.0},
            ],
            schema=schema,
        )

        result = (
            df.withColumn("mul", 0 * F.col("col"))
            .withColumn("add", 0 + F.col("col"))
            .withColumn("sub", 0 - F.col("col"))
        )

        rows = result.collect()
        assert rows[0]["mul"] == 0.0  # 0 * 5.0
        assert rows[0]["add"] == 5.0  # 0 + 5.0
        assert rows[0]["sub"] == -5.0  # 0 - 5.0

    def test_reverse_operations_with_one(self, spark):
        """Test reverse operations with one as literal."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 5.0},
            ],
            schema=schema,
        )

        result = df.withColumn("mul", 1 * F.col("col")).withColumn(
            "div", 1 / F.col("col")
        )

        rows = result.collect()
        assert rows[0]["mul"] == 5.0  # 1 * 5.0
        assert rows[0]["div"] == 0.2  # 1 / 5.0

    def test_reverse_operations_with_negative_literals(self, spark):
        """Test reverse operations with negative literals."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 5.0},
            ],
            schema=schema,
        )

        result = (
            df.withColumn("mul", -2 * F.col("col"))
            .withColumn("add", -3 + F.col("col"))
            .withColumn("sub", -4 - F.col("col"))
        )

        rows = result.collect()
        assert rows[0]["mul"] == -10.0  # -2 * 5.0
        assert rows[0]["add"] == 2.0  # -3 + 5.0
        assert rows[0]["sub"] == -9.0  # -4 - 5.0

    def test_reverse_operations_with_decimal_literals(self, spark):
        """Test reverse operations with decimal literals."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 4.0},
            ],
            schema=schema,
        )

        result = (
            df.withColumn("mul", 0.5 * F.col("col"))
            .withColumn("div", 0.5 / F.col("col"))
            .withColumn("add", 0.5 + F.col("col"))
        )

        rows = result.collect()
        assert rows[0]["mul"] == 2.0  # 0.5 * 4.0
        assert rows[0]["div"] == 0.125  # 0.5 / 4.0
        assert rows[0]["add"] == 4.5  # 0.5 + 4.0

    def test_chained_operations_with_mixed_types(self, spark):
        """Test chained operations mixing integers and floats."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 2.0},
            ],
            schema=schema,
        )

        # Mix int and float literals
        result = df.withColumn("result", 3 * F.col("col") + 1.5 * F.col("col"))

        rows = result.collect()
        assert rows[0]["result"] == 9.0  # 3 * 2.0 + 1.5 * 2.0 = 6.0 + 3.0

    def test_very_long_chained_expression(self, spark):
        """Test very long chained arithmetic expression."""
        schema = StructType([StructField("a", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"a": 2.0},
            ],
            schema=schema,
        )

        # Long chain: 1 + 2 * a + 3 * a - 4 * a + 5 * a
        result = df.withColumn(
            "result",
            1 + 2 * F.col("a") + 3 * F.col("a") - 4 * F.col("a") + 5 * F.col("a"),
        )

        rows = result.collect()
        # 1 + (2 * 2) + (3 * 2) - (4 * 2) + (5 * 2) = 1 + 4 + 6 - 8 + 10 = 13
        assert rows[0]["result"] == 13.0

    def test_reverse_operations_in_orderby(self, spark):
        """Test reverse operations in orderBy clauses."""
        schema = StructType([StructField("value", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"value": 3.0},
                {"value": 1.0},
                {"value": 2.0},
            ],
            schema=schema,
        )

        # Order by 2 * value
        result = df.orderBy(2 * F.col("value"))

        rows = result.collect()
        assert len(rows) == 3
        assert rows[0]["value"] == 1.0  # 2 * 1.0 = 2.0
        assert rows[1]["value"] == 2.0  # 2 * 2.0 = 4.0
        assert rows[2]["value"] == 3.0  # 2 * 3.0 = 6.0

    def test_reverse_operations_in_groupby_aggregation(self, spark):
        """Test reverse operations in groupBy aggregations."""
        schema = StructType(
            [
                StructField("category", StringType(), True),
                StructField("value", DoubleType(), True),
            ]
        )
        df = spark.createDataFrame(
            [
                {"category": "A", "value": 2.0},
                {"category": "A", "value": 3.0},
                {"category": "B", "value": 4.0},
            ],
            schema=schema,
        )

        # Group by category and sum 2 * value
        result = df.groupBy("category").agg(F.sum(2 * F.col("value")).alias("total"))

        rows = result.collect()
        assert len(rows) == 2
        row_a = next(r for r in rows if r["category"] == "A")
        row_b = next(r for r in rows if r["category"] == "B")
        assert row_a["total"] == 10.0  # 2 * 2.0 + 2 * 3.0 = 4.0 + 6.0
        assert row_b["total"] == 8.0  # 2 * 4.0

    def test_reverse_operations_with_when_otherwise(self, spark):
        """Test reverse operations in conditional expressions."""
        schema = StructType([StructField("value", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"value": 5.0},
                {"value": 15.0},
            ],
            schema=schema,
        )

        result = df.withColumn(
            "result",
            F.when(2 * F.col("value") > 10, 3 * F.col("value")).otherwise(
                1 * F.col("value")
            ),
        )

        rows = result.collect()
        assert rows[0]["result"] == 5.0  # 2 * 5.0 = 10, not > 10, so 1 * 5.0 = 5.0
        assert rows[1]["result"] == 45.0  # 2 * 15.0 = 30 > 10, so 3 * 15.0 = 45.0

    def test_reverse_operations_with_cast(self, spark):
        """Test reverse operations chained with cast operations."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 2.5},
            ],
            schema=schema,
        )

        # (2 * col) cast to int
        result = df.withColumn("result", (2 * F.col("col")).cast("int"))

        rows = result.collect()
        assert rows[0]["result"] == 5  # (2 * 2.5) = 5.0, cast to int = 5

    def test_reverse_operations_with_string_columns(self, spark):
        """Test reverse operations with string columns (should coerce to numeric)."""
        schema = StructType([StructField("string_col", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"string_col": "10.0"},
                {"string_col": "20"},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 2 * F.col("string_col"))

        rows = result.collect()
        assert rows[0]["result"] == 20.0  # 2 * 10.0
        assert rows[1]["result"] == 40.0  # 2 * 20

    def test_reverse_operations_division_by_zero(self, spark):
        """Test reverse division by zero."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 0.0},
                {"col": 5.0},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 10 / F.col("col"))

        rows = result.collect()
        # Division by zero should return None/null
        assert rows[0]["result"] is None  # 10 / 0.0
        assert rows[1]["result"] == 2.0  # 10 / 5.0

    def test_reverse_operations_modulo_by_zero(self, spark):
        """Test reverse modulo by zero."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 0.0},
                {"col": 3.0},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 10 % F.col("col"))

        rows = result.collect()
        # Modulo by zero should return None/null
        assert rows[0]["result"] is None  # 10 % 0.0
        assert rows[1]["result"] == 1.0  # 10 % 3.0

    def test_reverse_operations_with_null_literals(self, spark):
        """Test reverse operations when column has null values."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 5.0},
                {"col": None},
                {"col": 10.0},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 2 * F.col("col"))

        rows = result.collect()
        assert rows[0]["result"] == 10.0  # 2 * 5.0
        assert rows[1]["result"] is None  # 2 * None
        assert rows[2]["result"] == 20.0  # 2 * 10.0

    def test_very_complex_nested_expression(self, spark):
        """Test very complex nested expression with multiple reverse operations."""
        schema = StructType(
            [
                StructField("a", DoubleType(), True),
                StructField("b", DoubleType(), True),
                StructField("c", DoubleType(), True),
            ]
        )
        df = spark.createDataFrame(
            [
                {"a": 1.0, "b": 2.0, "c": 3.0},
            ],
            schema=schema,
        )

        # Very complex: 2 * a + 3 * b - 4 * c + 5 * a - 6 * b + 7 * c
        result = df.withColumn(
            "result",
            2 * F.col("a")
            + 3 * F.col("b")
            - 4 * F.col("c")
            + 5 * F.col("a")
            - 6 * F.col("b")
            + 7 * F.col("c"),
        )

        rows = result.collect()
        # 2*1 + 3*2 - 4*3 + 5*1 - 6*2 + 7*3 = 2 + 6 - 12 + 5 - 12 + 21 = 10
        assert rows[0]["result"] == 10.0

    def test_reverse_operations_with_select_multiple_columns(self, spark):
        """Test reverse operations in select with multiple computed columns."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 3.0},
            ],
            schema=schema,
        )

        result = df.select(
            (2 * F.col("col")).alias("double"),
            (3 + F.col("col")).alias("add"),
            (10 - F.col("col")).alias("sub"),
        )

        rows = result.collect()
        assert rows[0]["double"] == 6.0  # 2 * 3.0
        assert rows[0]["add"] == 6.0  # 3 + 3.0
        assert rows[0]["sub"] == 7.0  # 10 - 3.0

    def test_reverse_operations_preserve_precision(self, spark):
        """Test that reverse operations preserve decimal precision."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 0.1},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 3 * F.col("col"))

        rows = result.collect()
        # Floating point arithmetic - expect approximate value
        assert abs(rows[0]["result"] - 0.3) < 0.0001  # 3 * 0.1

    def test_reverse_operations_with_large_numbers(self, spark):
        """Test reverse operations with large numbers."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 1000000.0},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 2 * F.col("col"))

        rows = result.collect()
        assert rows[0]["result"] == 2000000.0  # 2 * 1000000.0

    def test_reverse_operations_with_small_numbers(self, spark):
        """Test reverse operations with very small numbers."""
        schema = StructType([StructField("col", DoubleType(), True)])
        df = spark.createDataFrame(
            [
                {"col": 0.0001},
            ],
            schema=schema,
        )

        result = df.withColumn("result", 1000 * F.col("col"))

        rows = result.collect()
        assert abs(rows[0]["result"] - 0.1) < 0.0001  # 1000 * 0.0001