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
"""
Tests for DataFrame.na.fill() syntax support.

This module tests that sparkless correctly supports the .na.fill() syntax
for filling null values in DataFrames, matching PySpark behavior.

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

import pytest

from sparkless.testing import get_imports

# Get imports from fixture (same logic for both backends)
imports = get_imports()
SparkSession = imports.SparkSession
StringType = imports.StringType
IntegerType = imports.IntegerType
StructType = imports.StructType
StructField = imports.StructField
F = imports.F


class TestNaFill:
    """Test .na.fill() syntax for DataFrame."""

    def test_na_fill_scalar(self, spark):
        """Test .na.fill() with scalar value fills all nulls."""
        # Use strings to avoid type compatibility issues
        df = spark.createDataFrame(
            [
                {"key": "A", "value": "1"},
                {"key": None, "value": "2"},
                {"key": "C", "value": None},
            ]
        )

        result = df.na.fill("0")

        rows = result.collect()
        assert len(rows) == 3
        assert rows[0]["key"] == "A"
        assert rows[0]["value"] == "1"
        assert rows[1]["key"] == "0"  # Was null, now filled
        assert rows[1]["value"] == "2"
        assert rows[2]["key"] == "C"
        assert rows[2]["value"] == "0"  # Was null, now filled

    def test_na_fill_dict(self, spark):
        """Test .na.fill() with dict mapping columns to values."""
        df = spark.createDataFrame(
            [
                {"col1": None, "col2": "X", "col3": None},
                {"col1": "A", "col2": None, "col3": "Y"},
            ]
        )

        result = df.na.fill({"col1": "DEFAULT1", "col3": "DEFAULT3"})

        rows = result.collect()
        assert len(rows) == 2
        assert rows[0]["col1"] == "DEFAULT1"  # Was null, now filled
        assert rows[0]["col2"] == "X"  # Not in dict, unchanged
        assert rows[0]["col3"] == "DEFAULT3"  # Was null, now filled
        assert rows[1]["col1"] == "A"  # Not null, unchanged
        assert rows[1]["col2"] is None  # Not in dict, unchanged
        assert rows[1]["col3"] == "Y"  # Not null, unchanged

    def test_na_fill_subset(self, spark):
        """Test .na.fill() with subset parameter."""
        # Provide explicit schema since some columns are all null
        StructType = imports.StructType
        StructField = imports.StructField
        StringType = imports.StringType

        schema = StructType(
            [
                StructField("key", StringType(), nullable=True),
                StructField("value", StringType(), nullable=True),
                StructField("other", StringType(), nullable=True),
            ]
        )
        df = spark.createDataFrame(
            [
                {"key": None, "value": None, "other": "X"},
                {"key": "A", "value": None, "other": None},
            ],
            schema=schema,
        )

        result = df.na.fill("FILLED", subset=["key", "value"])

        rows = result.collect()
        assert len(rows) == 2
        assert rows[0]["key"] == "FILLED"  # Was null, now filled
        assert rows[0]["value"] == "FILLED"  # Was null, now filled
        assert rows[0]["other"] == "X"  # Not in subset, unchanged (was "X")
        assert rows[1]["key"] == "A"  # Not null, unchanged
        assert rows[1]["value"] == "FILLED"  # Was null, now filled
        assert rows[1]["other"] is None  # Not in subset, unchanged

    def test_na_fill_after_join(self, spark):
        """Test .na.fill() after join operation (exact scenario from issue #245)."""
        # Use integers for the exact scenario from issue #245
        # Note: PySpark accepts integers for filling LongType columns
        df_left = spark.createDataFrame(
            [
                {"key": "123", "value_left": 1},
                {
                    "key": "456",
                    "value_left": None,
                },  # <- null value that should be filled
            ]
        )

        df_right = spark.createDataFrame(
            [
                {
                    "key": "123",
                    "value_right": None,
                },  # <- null value that should be filled
                {"key": "456", "value_right": 2},
            ]
        )

        # This is the exact code from issue #245
        df = df_left.join(df_right, on="key", how="inner").na.fill(0)

        rows = df.collect()
        assert len(rows) == 2

        # Row 1: key="123", value_left=1, value_right should be filled
        row1 = next((r for r in rows if r["key"] == "123"), None)
        assert row1 is not None
        assert row1["value_left"] == 1
        # value_right was null, should be filled with 0 (or may remain None if type incompatible)
        # PySpark allows int to fill LongType, so expect 0
        assert row1["value_right"] == 0  # Was null, now filled

        # Row 2: key="456", value_left should be filled, value_right=2
        row2 = next((r for r in rows if r["key"] == "456"), None)
        assert row2 is not None
        # value_left was null, should be filled with 0
        assert row2["value_left"] == 0  # Was null, now filled
        assert row2["value_right"] == 2

    def test_na_fill_nonexistent_column(self, spark):
        """Test .na.fill() error handling for non-existent columns."""
        StructType = imports.StructType
        StructField = imports.StructField
        StringType = imports.StringType

        schema = StructType(
            [
                StructField("col1", StringType(), nullable=True),
                StructField("col2", StringType()),
            ]
        )
        df = spark.createDataFrame([{"col1": None, "col2": "X"}], schema=schema)

        # Test with subset containing non-existent column
        with pytest.raises(Exception):
            df.na.fill("FILLED", subset=["col1", "nonexistent"])

        # Test with dict containing non-existent column
        with pytest.raises(Exception):
            df.na.fill({"col1": "FILLED", "nonexistent": "VALUE"})

    def test_na_fill_different_types(self, spark):
        """Test .na.fill() with different data types."""
        from sparkless.testing import get_imports

        imports = get_imports()
        StructType = imports.StructType
        StructField = imports.StructField
        StringType = imports.StringType
        IntegerType = imports.IntegerType
        DoubleType = imports.DoubleType
        BooleanType = imports.BooleanType

        # Test with integers
        schema_int = StructType(
            [
                StructField("key", IntegerType()),
                StructField("value", IntegerType(), nullable=True),
            ]
        )
        df_int = spark.createDataFrame([{"key": 1, "value": None}], schema=schema_int)
        result_int = df_int.na.fill(0)
        rows_int = result_int.collect()
        assert rows_int[0]["value"] == 0

        # Test with strings
        schema_str = StructType(
            [
                StructField("key", StringType()),
                StructField("value", StringType(), nullable=True),
            ]
        )
        df_str = spark.createDataFrame([{"key": "A", "value": None}], schema=schema_str)
        result_str = df_str.na.fill("DEFAULT")
        rows_str = result_str.collect()
        assert rows_str[0]["value"] == "DEFAULT"

        # Test with floats
        schema_float = StructType(
            [
                StructField("key", DoubleType()),
                StructField("value", DoubleType(), nullable=True),
            ]
        )
        df_float = spark.createDataFrame(
            [{"key": 1.5, "value": None}], schema=schema_float
        )
        result_float = df_float.na.fill(0.0)
        rows_float = result_float.collect()
        assert rows_float[0]["value"] == 0.0

        # Test with booleans
        schema_bool = StructType(
            [
                StructField("key", BooleanType()),
                StructField("value", BooleanType(), nullable=True),
            ]
        )
        df_bool = spark.createDataFrame(
            [{"key": True, "value": None}], schema=schema_bool
        )
        result_bool = df_bool.na.fill(False)
        rows_bool = result_bool.collect()
        assert rows_bool[0]["value"] is False

    def test_na_fill_chained_operations(self, spark):
        """Test chaining .na.fill() with other operations."""
        df = spark.createDataFrame(
            [
                {"name": None, "age": 25, "city": None},
                {"name": "Bob", "age": None, "city": "NYC"},
            ]
        )

        result = df.na.fill("UNKNOWN", subset=["name"]).na.fill("N/A", subset=["city"])

        rows = result.collect()
        assert len(rows) == 2
        # First fillna should fill name, second should fill city
        assert rows[0]["name"] == "UNKNOWN"  # Filled by first na.fill
        assert rows[0]["age"] == 25  # Not filled
        assert rows[0]["city"] == "N/A"  # Filled by second na.fill
        assert rows[1]["name"] == "Bob"  # Not null, unchanged
        assert rows[1]["age"] is None  # Not filled
        assert rows[1]["city"] == "NYC"  # Not null, unchanged

    def test_na_fill_pyspark_parity(self, spark):
        """Test .na.fill() matches PySpark behavior exactly."""
        df = spark.createDataFrame(
            [
                {"key": "123", "value_left": 1},
                {"key": "456", "value_left": None},
            ]
        )

        # This should work exactly like PySpark
        result = df.na.fill(0)

        rows = result.collect()
        assert len(rows) == 2
        assert rows[0]["key"] == "123"
        assert rows[0]["value_left"] == 1
        assert rows[1]["key"] == "456"
        assert rows[1]["value_left"] == 0  # Was null, now filled

        # Verify schema (expectations match PySpark)
        schema = result.schema
        value_left_field = next(
            (f for f in schema.fields if f.name == "value_left"), None
        )
        assert value_left_field is not None

    def test_na_fill_empty_dataframe(self, spark):
        """Test .na.fill() on empty DataFrame."""
        from sparkless.testing import get_imports

        imports = get_imports()
        StructType = imports.StructType
        StructField = imports.StructField
        StringType = imports.StringType

        schema = StructType(
            [
                StructField("col1", StringType()),
                StructField("col2", StringType()),
            ]
        )
        df = spark.createDataFrame([], schema=schema)

        result = df.na.fill("DEFAULT")

        rows = result.collect()
        assert len(rows) == 0
        # Schema should be preserved
        assert len(result.schema.fields) == 2

    def test_na_fill_no_nulls(self, spark):
        """Test .na.fill() when DataFrame has no nulls."""
        df = spark.createDataFrame(
            [
                {"key": "A", "value": 1},
                {"key": "B", "value": 2},
            ]
        )

        result = df.na.fill(0)

        rows = result.collect()
        assert len(rows) == 2
        # All values should remain unchanged
        assert rows[0]["key"] == "A"
        assert rows[0]["value"] == 1
        assert rows[1]["key"] == "B"
        assert rows[1]["value"] == 2

    def test_na_fill_equivalence_with_fillna(self, spark):
        """Test that .na.fill() produces identical results to .fillna()."""
        df = spark.createDataFrame(
            [
                {"col1": None, "col2": "X", "col3": None},
                {"col1": "A", "col2": None, "col3": "Y"},
            ]
        )

        result_na = df.na.fill("FILLED")
        result_fillna = df.fillna("FILLED")

        rows_na = result_na.collect()
        rows_fillna = result_fillna.collect()

        assert len(rows_na) == len(rows_fillna)
        for i, row_na in enumerate(rows_na):
            row_fillna = rows_fillna[i]
            assert row_na["col1"] == row_fillna["col1"]
            assert row_na["col2"] == row_fillna["col2"]
            assert row_na["col3"] == row_fillna["col3"]

    def test_na_fill_subset_string(self, spark):
        """Test .na.fill() with subset as string (single column)."""
        from sparkless.testing import get_imports

        imports = get_imports()
        StructType = imports.StructType
        StructField = imports.StructField
        StringType = imports.StringType

        schema = StructType(
            [
                StructField("key", StringType(), nullable=True),
                StructField("value", StringType(), nullable=True),
            ]
        )
        df = spark.createDataFrame(
            [
                {"key": None, "value": None},
                {"key": "A", "value": None},
            ],
            schema=schema,
        )

        result = df.na.fill("FILLED", subset="key")

        rows = result.collect()
        assert rows[0]["key"] == "FILLED"  # Was null, now filled
        assert rows[0]["value"] is None  # Not in subset, unchanged
        assert rows[1]["key"] == "A"  # Not null, unchanged
        assert rows[1]["value"] is None  # Not in subset, unchanged

    def test_na_fill_subset_tuple(self, spark):
        """Test .na.fill() with subset as tuple."""
        from sparkless.testing import get_imports

        imports = get_imports()
        StructType = imports.StructType
        StructField = imports.StructField
        StringType = imports.StringType

        schema = StructType(
            [
                StructField("col1", StringType(), nullable=True),
                StructField("col2", StringType(), nullable=True),
                StructField("col3", StringType()),
            ]
        )
        df = spark.createDataFrame(
            [
                {"col1": None, "col2": None, "col3": "X"},
            ],
            schema=schema,
        )

        result = df.na.fill("FILLED", subset=("col1", "col2"))

        rows = result.collect()
        assert rows[0]["col1"] == "FILLED"  # Was null, now filled
        assert rows[0]["col2"] == "FILLED"  # Was null, now filled
        assert rows[0]["col3"] == "X"  # Not in subset, unchanged

    def test_na_fill_all_nulls(self, spark):
        """Test .na.fill() when all values are null."""
        from sparkless.testing import get_imports

        imports = get_imports()
        StructType = imports.StructType
        StructField = imports.StructField
        StringType = imports.StringType

        schema = StructType(
            [
                StructField("col1", StringType()),
                StructField("col2", StringType()),
            ]
        )
        df = spark.createDataFrame(
            [
                {"col1": None, "col2": None},
                {"col1": None, "col2": None},
            ],
            schema=schema,
        )

        result = df.na.fill("ALL_FILLED")

        rows = result.collect()
        assert len(rows) == 2
        assert rows[0]["col1"] == "ALL_FILLED"
        assert rows[0]["col2"] == "ALL_FILLED"
        assert rows[1]["col1"] == "ALL_FILLED"
        assert rows[1]["col2"] == "ALL_FILLED"