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
"""
Unit tests for issue #270: tuple-based DataFrame creation.

Tests verify that createDataFrame correctly converts tuple-based data
to dictionaries when a StructType schema is provided, ensuring all
downstream operations work correctly.
"""

import pytest
from sparkless.testing import get_imports


class TestIssue270TupleDataFrame:
    """Test tuple-based DataFrame creation (Issue #270)."""

    def test_tuple_data_with_structtype_schema(self, spark):
        """Test that tuple data with StructType schema converts to dicts."""
        imports = get_imports()
        T = imports

        data = [("Alice", 1), ("Bob", 2)]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)

        # Verify show() works (the original failing operation)
        df.show()  # Should not raise AttributeError

        # Verify collect() (expectations match PySpark; use subscript for Row/dict compatibility)
        rows = df.collect()
        assert len(rows) == 2
        assert rows[0]["Name"] == "Alice"
        assert rows[0]["Value"] == 1
        assert rows[1]["Name"] == "Bob"
        assert rows[1]["Value"] == 2

    def test_tuple_data_show_works(self, spark):
        """Test that show() works with tuple-based data."""
        imports = get_imports()
        T = imports

        data = [("Alice", 1), ("Bob", 2)]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)

        # This should work without AttributeError
        try:
            df.show()
        except AttributeError as e:
            if "'tuple' object has no attribute" in str(e):
                pytest.fail(f"show() failed with tuple error: {e}")

    def test_tuple_data_unionByName_works(self, spark):
        """Test that unionByName() works with tuple-based data."""
        imports = get_imports()
        T = imports

        data1 = [("Alice", 1), ("Bob", 2)]
        data2 = [("Charlie", 3)]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
            ]
        )

        df1 = spark.createDataFrame(data=data1, schema=schema)
        df2 = spark.createDataFrame(data=data2, schema=schema)

        # This should work without AttributeError
        result = df1.unionByName(df2)
        assert result.count() == 3

    def test_tuple_data_operations_work(self, spark):
        """Test various operations that use .get(), .items(), .copy() work."""
        imports = get_imports()
        T = imports

        data = [("Alice", 1, "IT"), ("Bob", 2, "HR")]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
                T.StructField(name="Dept", dataType=T.StringType()),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)

        # Test fillna (uses .copy())
        df_filled = df.fillna({"Value": 0})
        assert df_filled.count() == 2

        # Test replace (uses .items())
        df_replaced = df.replace({"IT": "Engineering"})
        assert df_replaced.count() == 2

        # Test select (uses .get() indirectly)
        result = df.select("Name", "Value").collect()
        assert len(result) == 2

    def test_mixed_tuple_and_dict_data(self, spark):
        """Test mixed tuple and dict data with StructType schema."""
        imports = get_imports()
        T = imports

        data = [("Alice", 1), {"Name": "Bob", "Value": 2}]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)
        assert df.count() == 2

    def test_tuple_data_single_column(self, spark):
        """Test tuple data with single column schema."""
        imports = get_imports()
        T = imports

        data = [("Alice",), ("Bob",)]
        schema = T.StructType([T.StructField(name="Name", dataType=T.StringType())])

        df = spark.createDataFrame(data=data, schema=schema)
        assert df.count() == 2
        df.show()  # Should work

    def test_tuple_data_mismatched_length(self, spark):
        """Test tuple data where tuple length doesn't match schema.

        PySpark raises PySparkValueError for mismatched lengths.
        Sparkless should match this behavior.
        """
        imports = get_imports()
        T = imports

        data = [("Alice",), ("Bob", 2)]  # First tuple missing a value
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
            ]
        )

        # Expect PySpark-style error (LENGTH_SHOULD_BE_THE_SAME or "length" in message)
        with pytest.raises(Exception) as exc_info:
            spark.createDataFrame(data=data, schema=schema)
        assert (
            "LENGTH_SHOULD_BE_THE_SAME" in str(exc_info.value)
            or "length" in str(exc_info.value).lower()
        )

    def test_tuple_data_empty_schema(self, spark):
        """Test tuple data with empty schema.

        PySpark raises PySparkValueError for mismatched lengths.
        Robin raises with LENGTH_SHOULD_BE_THE_SAME (default backend).
        """
        imports = get_imports()
        T = imports

        data = [("Alice", 1), ("Bob", 2)]
        schema = T.StructType([])

        with pytest.raises(Exception) as exc_info:
            spark.createDataFrame(data=data, schema=schema)
        assert (
            "LENGTH_SHOULD_BE_THE_SAME" in str(exc_info.value)
            or "length" in str(exc_info.value).lower()
        )

    def test_list_data_with_structtype_schema(self, spark):
        """Test that list data (not just tuples) also converts correctly."""
        imports = get_imports()
        T = imports

        data = [["Alice", 1], ["Bob", 2]]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)
        df.show()  # Should work

    def test_pyspark_parity_exact_example(self, spark):
        """Test the exact example from issue #270."""
        imports = get_imports()
        T = imports

        data = [("Alice", 1), ("Bob", 2)]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)

        # This should work exactly as PySpark does
        df.show()

        # Verify structure (expectations match PySpark; subscript works for Row/dict)
        rows = df.collect()
        assert len(rows) == 2
        assert rows[0]["Name"] == "Alice"
        assert rows[0]["Value"] == 1
        assert rows[1]["Name"] == "Bob"
        assert rows[1]["Value"] == 2

    def test_tuple_with_none_values(self, spark):
        """Test tuple data with None values."""
        imports = get_imports()
        T = imports

        data = [("Alice", None), ("Bob", 2), (None, 3)]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)
        assert df.count() == 3
        df.show()  # Should work with None values

        # Verify None values are preserved (subscript for Row/dict compatibility)
        rows = df.collect()
        assert rows[0]["Value"] is None
        assert rows[2]["Name"] is None

    def test_tuple_with_different_data_types(self, spark):
        """Test tuple data with various data types."""
        imports = get_imports()
        T = imports

        data = [
            ("Alice", 25, 75000.50, True, "2024-01-01"),
            ("Bob", 30, 80000.75, False, "2024-02-01"),
        ]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Age", dataType=T.IntegerType()),
                T.StructField(name="Salary", dataType=T.DoubleType()),
                T.StructField(name="Active", dataType=T.BooleanType()),
                T.StructField(name="Date", dataType=T.StringType()),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)
        assert df.count() == 2
        df.show()

        # Verify all operations work
        df.select("Name", "Salary").show()
        df.filter(df["Age"] > 25).show()
        df.withColumn("SalaryK", df["Salary"] / 1000).show()

    def test_tuple_data_with_long_schema(self, spark):
        """Test tuple data with many columns."""
        imports = get_imports()
        T = imports

        data = [tuple(range(10)), tuple(range(10, 20))]
        schema = T.StructType(
            [
                T.StructField(name=f"col_{i}", dataType=T.IntegerType())
                for i in range(10)
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)
        assert df.count() == 2
        assert len(df.columns) == 10
        df.show()

    def test_tuple_data_single_row(self, spark):
        """Test tuple data with single row."""
        imports = get_imports()
        T = imports

        data = [("Alice", 1)]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)
        assert df.count() == 1
        df.show()

    def test_tuple_data_empty_dataframe_with_schema(self, spark):
        """Test empty DataFrame with schema from tuple format."""
        imports = get_imports()
        T = imports

        data = []
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)
        assert df.count() == 0
        assert len(df.columns) == 2
        df.show()  # Should show empty DataFrame

    def test_tuple_data_mixed_with_row_objects(self, spark):
        """Test mixed tuple data and Row objects with named fields.

        Note: Row objects need named fields when used with positional data.
        """
        imports = get_imports()
        T = imports
        Row = imports.Row

        # Row objects need to use named fields (like dicts) or match tuple structure
        # Create Row objects with named fields for compatibility
        data = [("Alice", 1), Row(Name="Bob", Value=2), ("Charlie", 3)]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)
        assert df.count() == 3
        df.show()

    def test_tuple_data_operations_comprehensive(self, spark):
        """Test comprehensive operations on tuple-based DataFrame."""
        imports = get_imports()
        T = imports

        data = [("Alice", 1, "IT"), ("Bob", 2, "HR"), ("Charlie", 3, "IT")]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
                T.StructField(name="Dept", dataType=T.StringType()),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)

        # Test various operations that use .get(), .items(), .copy()
        # fillna with subset
        df_filled = df.fillna({"Value": 0}, subset=["Value"])
        assert df_filled.count() == 3

        # replace
        df_replaced = df.replace({"IT": "Engineering"})
        assert df_replaced.count() == 3

        # dropna with subset
        df_with_nulls = spark.createDataFrame(
            [("Alice", None, "IT"), ("Bob", 2, "HR")], schema
        )
        df_dropped = df_with_nulls.dropna(subset=["Value"])
        assert df_dropped.count() == 1

        # groupBy
        grouped = df.groupBy("Dept").count()
        assert grouped.count() == 2

        # orderBy
        ordered = df.orderBy("Value")
        assert ordered.count() == 3

        # distinct
        distinct_depts = df.select("Dept").distinct()
        assert distinct_depts.count() == 2

    def test_tuple_data_union_operations(self, spark):
        """Test union operations with tuple-based DataFrames."""
        imports = get_imports()
        T = imports

        data1 = [("Alice", 1), ("Bob", 2)]
        data2 = [("Charlie", 3), ("Diana", 4)]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
            ]
        )

        df1 = spark.createDataFrame(data=data1, schema=schema)
        df2 = spark.createDataFrame(data=data2, schema=schema)

        # unionByName
        unioned = df1.unionByName(df2)
        assert unioned.count() == 4

        # union (should also work)
        unioned2 = df1.union(df2)
        assert unioned2.count() == 4

    def test_tuple_data_join_operations(self, spark):
        """Test join operations with tuple-based DataFrames."""
        imports = get_imports()
        T = imports

        employees = [("Alice", 1, "IT"), ("Bob", 2, "HR")]
        departments = [("IT", "Engineering"), ("HR", "Human Resources")]
        emp_schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Id", dataType=T.IntegerType()),
                T.StructField(name="Dept", dataType=T.StringType()),
            ]
        )
        dept_schema = T.StructType(
            [
                T.StructField(name="Dept", dataType=T.StringType()),
                T.StructField(name="Name", dataType=T.StringType()),
            ]
        )

        df_emp = spark.createDataFrame(data=employees, schema=emp_schema)
        df_dept = spark.createDataFrame(data=departments, schema=dept_schema)

        # inner join
        joined = df_emp.join(df_dept, "Dept", "inner")
        assert joined.count() == 2

    def test_tuple_data_error_message_matches_pyspark(self, spark):
        """Test that error messages match PySpark exactly."""
        imports = get_imports()
        T = imports

        # Test mismatched length - first tuple too short
        data = [("Alice",), ("Bob", 2)]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
            ]
        )

        with pytest.raises(Exception) as exc_info:
            spark.createDataFrame(data=data, schema=schema)

        error_msg = str(exc_info.value)
        assert "LENGTH_SHOULD_BE_THE_SAME" in error_msg or "length" in error_msg.lower()
        assert "1" in error_msg  # Got 1 element
        assert "2" in error_msg  # Expected 2 fields

        # Test mismatched length - tuple too long
        data2 = [("Alice", 1, 100), ("Bob", 2)]
        with pytest.raises(Exception) as exc_info2:
            spark.createDataFrame(data=data2, schema=schema)

        error_msg2 = str(exc_info2.value)
        assert (
            "LENGTH_SHOULD_BE_THE_SAME" in error_msg2 or "length" in error_msg2.lower()
        )
        assert "3" in error_msg2  # Got 3 elements
        assert "2" in error_msg2  # Expected 2 fields

    def test_tuple_data_all_operations_from_issue(self, spark):
        """Test all operations mentioned in issue #270."""
        imports = get_imports()
        T = imports

        data = [("Alice", 1), ("Bob", 2)]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Value", dataType=T.IntegerType()),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)

        # All operations that were failing in the issue should now work:
        # .show() - uses .keys()
        df.show()

        # .unionByName() - uses .get(), .items()
        df2 = spark.createDataFrame([("Charlie", 3)], schema)
        unioned = df.unionByName(df2)
        assert unioned.count() == 3

        # Any operation using .get(), .items(), .copy() should work
        result = df.select("Name").collect()
        assert len(result) == 2

    def test_tuple_data_with_array_type(self, spark):
        """Test tuple data with ArrayType in schema."""
        imports = get_imports()
        T = imports

        data = [("Alice", [1, 2, 3]), ("Bob", [4, 5])]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Values", dataType=T.ArrayType(T.IntegerType())),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)
        assert df.count() == 2
        df.show()

    def test_tuple_data_preserves_order(self, spark):
        """Test that tuple-to-dict conversion preserves field order."""
        imports = get_imports()
        T = imports

        data = [("Alice", 1, "IT")]
        schema = T.StructType(
            [
                T.StructField(name="Name", dataType=T.StringType()),
                T.StructField(name="Id", dataType=T.IntegerType()),
                T.StructField(name="Dept", dataType=T.StringType()),
            ]
        )

        df = spark.createDataFrame(data=data, schema=schema)

        # Verify column order matches schema (expectations match PySpark)
        assert df.columns == ["Name", "Id", "Dept"]