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
"""Test issue #326: format_string function support.

This test verifies that F.format_string() correctly formats strings.
Uses get_imports from fixture only.
"""

from sparkless.testing import get_imports

_imports = get_imports()
SparkSession = _imports.SparkSession
F = _imports.F
StructType = _imports.StructType
StructField = _imports.StructField
StringType = _imports.StringType


class TestIssue326FormatString:
    """Test format_string function."""

    def _get_unique_app_name(self, test_name: str) -> str:
        """Generate unique app name for parallel test execution."""
        import os
        import threading

        thread_id = threading.current_thread().ident
        process_id = os.getpid()
        return f"{test_name}_{process_id}_{thread_id}"

    def test_format_string_basic(self):
        """Test basic format_string functionality (issue example)."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            # Exact example from issue #326
            df = spark.createDataFrame(
                [
                    {"Name": "Alice", "StringValue": "abc", "IntegerValue": 123},
                    {"Name": "Bob", "StringValue": "def", "IntegerValue": 456},
                ]
            )

            df = df.withColumn(
                "NewValue",
                F.format_string("%s-%s", F.col("StringValue"), F.col("IntegerValue")),
            )

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

            # Find rows by Name to avoid order dependency
            row_alice = [r for r in rows if r["Name"] == "Alice"][0]
            row_bob = [r for r in rows if r["Name"] == "Bob"][0]

            # Expected: "abc-123" and "def-456"
            assert row_alice["NewValue"] == "abc-123", (
                f"Expected 'abc-123', got {row_alice['NewValue']}"
            )
            assert row_bob["NewValue"] == "def-456", (
                f"Expected 'def-456', got {row_bob['NewValue']}"
            )
        finally:
            spark.stop()

    def test_format_string_multiple_columns(self):
        """Test format_string with multiple columns."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            df = spark.createDataFrame(
                [
                    {"Name": "Alice", "Age": 25, "City": "NYC"},
                    {"Name": "Bob", "Age": 30, "City": "LA"},
                ]
            )

            df = df.withColumn(
                "Info",
                F.format_string(
                    "%s is %d years old and lives in %s",
                    F.col("Name"),
                    F.col("Age"),
                    F.col("City"),
                ),
            )

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

            row_alice = [r for r in rows if r["Name"] == "Alice"][0]
            row_bob = [r for r in rows if r["Name"] == "Bob"][0]

            assert row_alice["Info"] == "Alice is 25 years old and lives in NYC"
            assert row_bob["Info"] == "Bob is 30 years old and lives in LA"
        finally:
            spark.stop()

    def test_format_string_with_null_values(self):
        """Test format_string with null values."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            df = spark.createDataFrame(
                [
                    {"Name": "Alice", "Value": "abc", "Number": 123},
                    {"Name": "Bob", "Value": None, "Number": 456},
                    {"Name": "Charlie", "Value": "def", "Number": None},
                ]
            )

            df = df.withColumn(
                "NewValue",
                F.format_string("%s-%s", F.col("Value"), F.col("Number")),
            )

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

            row_alice = [r for r in rows if r["Name"] == "Alice"][0]
            row_bob = [r for r in rows if r["Name"] == "Bob"][0]
            row_charlie = [r for r in rows if r["Name"] == "Charlie"][0]

            # PySpark converts None to "null" string in format_string
            assert row_alice["NewValue"] == "abc-123"
            assert row_bob["NewValue"] == "null-456"  # None -> "null" string
            assert row_charlie["NewValue"] == "def-null"  # None -> "null" string
        finally:
            spark.stop()

    def test_format_string_in_select(self):
        """Test format_string in select context."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            df = spark.createDataFrame(
                [
                    {"Name": "Alice", "StringValue": "abc", "IntegerValue": 123},
                    {"Name": "Bob", "StringValue": "def", "IntegerValue": 456},
                ]
            )

            df = df.select(
                "Name",
                F.format_string(
                    "%s-%s", F.col("StringValue"), F.col("IntegerValue")
                ).alias("NewValue"),
            )

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

            row_alice = [r for r in rows if r["Name"] == "Alice"][0]
            row_bob = [r for r in rows if r["Name"] == "Bob"][0]

            assert row_alice["NewValue"] == "abc-123"
            assert row_bob["NewValue"] == "def-456"
        finally:
            spark.stop()

    def test_format_string_different_format_specifiers(self):
        """Test format_string with different format specifiers."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            df = spark.createDataFrame(
                [
                    {"Name": "Alice", "Age": 25, "Salary": 50000.5},
                    {"Name": "Bob", "Age": 30, "Salary": 75000.75},
                ]
            )

            # Test %s (string), %d (integer), %f (float)
            df = df.withColumn(
                "Info",
                F.format_string(
                    "Name: %s, Age: %d, Salary: %.2f",
                    F.col("Name"),
                    F.col("Age"),
                    F.col("Salary"),
                ),
            )

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

            row_alice = [r for r in rows if r["Name"] == "Alice"][0]
            row_bob = [r for r in rows if r["Name"] == "Bob"][0]

            # Note: Python % formatting behavior
            assert "Name: Alice" in row_alice["Info"]
            assert "Age: 25" in row_alice["Info"]
            assert "Name: Bob" in row_bob["Info"]
            assert "Age: 30" in row_bob["Info"]
        finally:
            spark.stop()

    def test_format_string_empty_strings(self):
        """Test format_string with empty strings."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            df = spark.createDataFrame(
                [
                    {"Name": "Alice", "Value": "", "Number": 123},
                    {"Name": "Bob", "Value": "def", "Number": 456},
                ]
            )

            df = df.withColumn(
                "NewValue",
                F.format_string("%s-%s", F.col("Value"), F.col("Number")),
            )

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

            row_alice = [r for r in rows if r["Name"] == "Alice"][0]
            row_bob = [r for r in rows if r["Name"] == "Bob"][0]

            assert (
                row_alice["NewValue"] == "-123"
            )  # Empty string (not None, so not "null")
            assert row_bob["NewValue"] == "def-456"
        finally:
            spark.stop()

    def test_format_string_many_columns(self):
        """Test format_string with many columns (5+)."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            df = spark.createDataFrame(
                [
                    {
                        "A": "one",
                        "B": 2,
                        "C": 3.0,
                        "D": "four",
                        "E": 5,
                        "F": "six",
                    },
                ]
            )

            df = df.withColumn(
                "Result",
                F.format_string(
                    "%s-%d-%.1f-%s-%d-%s",
                    F.col("A"),
                    F.col("B"),
                    F.col("C"),
                    F.col("D"),
                    F.col("E"),
                    F.col("F"),
                ),
            )

            rows = df.collect()
            assert len(rows) == 1
            assert rows[0]["Result"] == "one-2-3.0-four-5-six"
        finally:
            spark.stop()

    def test_format_string_numeric_edge_cases(self):
        """Test format_string with numeric edge cases (zero, negative, large numbers)."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            df = spark.createDataFrame(
                [
                    {
                        "Int": 0,
                        "Neg": -42,
                        "Large": 999999,
                        "Float": 0.0,
                        "NegFloat": -3.14,
                    },
                ]
            )

            df = df.withColumn(
                "Result",
                F.format_string(
                    "%d|%d|%d|%.2f|%.2f",
                    F.col("Int"),
                    F.col("Neg"),
                    F.col("Large"),
                    F.col("Float"),
                    F.col("NegFloat"),
                ),
            )

            rows = df.collect()
            assert len(rows) == 1
            result = rows[0]["Result"]
            assert "0|" in result
            assert "-42|" in result
            assert "999999|" in result
            assert "0.00|" in result
            assert "-3.14" in result
        finally:
            spark.stop()

    def test_format_string_unicode(self):
        """Test format_string with Unicode characters."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            df = spark.createDataFrame(
                [
                    {"Name": "José", "City": "São Paulo", "Emoji": "🎉"},
                ]
            )

            df = df.withColumn(
                "Result",
                F.format_string(
                    "%s from %s says %s",
                    F.col("Name"),
                    F.col("City"),
                    F.col("Emoji"),
                ),
            )

            rows = df.collect()
            assert len(rows) == 1
            result = rows[0]["Result"]
            assert "José" in result
            assert "São Paulo" in result
            assert "🎉" in result
        finally:
            spark.stop()

    def test_format_string_special_characters_in_format(self):
        """Test format_string with special characters in format string."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            df = spark.createDataFrame(
                [
                    {"A": "test", "B": 123},
                ]
            )

            df = df.withColumn(
                "Result",
                F.format_string(
                    "Value: %s | Number: %d | End",
                    F.col("A"),
                    F.col("B"),
                ),
            )

            rows = df.collect()
            assert len(rows) == 1
            result = rows[0]["Result"]
            assert result == "Value: test | Number: 123 | End"
        finally:
            spark.stop()

    def test_format_string_all_null(self):
        """Test format_string when all columns are null."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            # Provide explicit schema since all values are null
            schema = StructType(
                [
                    StructField("A", StringType(), True),
                    StructField("B", StringType(), True),
                    StructField("C", StringType(), True),
                ]
            )
            df = spark.createDataFrame(
                [
                    {"A": None, "B": None, "C": None},
                ],
                schema=schema,
            )

            df = df.withColumn(
                "Result",
                F.format_string(
                    "%s-%s-%s",
                    F.col("A"),
                    F.col("B"),
                    F.col("C"),
                ),
            )

            rows = df.collect()
            assert len(rows) == 1
            # All nulls should become "null" strings
            assert rows[0]["Result"] == "null-null-null"
        finally:
            spark.stop()

    def test_format_string_mixed_types(self):
        """Test format_string with mixed data types."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            df = spark.createDataFrame(
                [
                    {
                        "Str": "hello",
                        "Int": 42,
                        "Float": 3.14159,
                        "Bool": True,
                    },
                ]
            )

            df = df.withColumn(
                "Result",
                F.format_string(
                    "%s-%d-%.2f-%s",
                    F.col("Str"),
                    F.col("Int"),
                    F.col("Float"),
                    F.col("Bool"),
                ),
            )

            rows = df.collect()
            assert len(rows) == 1
            result = rows[0]["Result"]
            assert "hello-42-3.14" in result
            assert "True" in result or "true" in result
        finally:
            spark.stop()

    def test_format_string_format_specifiers(self):
        """Test format_string with various format specifiers (%x, %o, %e, etc.)."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            df = spark.createDataFrame(
                [
                    {"Dec": 255, "Hex": 255, "Oct": 64},
                ]
            )

            # Test hex and octal formatting
            df = df.withColumn(
                "HexResult",
                F.format_string("%x", F.col("Hex")),
            )
            df = df.withColumn(
                "OctResult",
                F.format_string("%o", F.col("Oct")),
            )

            rows = df.collect()
            assert len(rows) == 1
            # Hex: 255 = ff
            assert rows[0]["HexResult"] == "ff" or rows[0]["HexResult"] == "FF"
            # Oct: 64 = 100
            assert rows[0]["OctResult"] == "100"
        finally:
            spark.stop()

    def test_format_string_precision_formatting(self):
        """Test format_string with precision formatting (%.3f, %05d, etc.)."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            df = spark.createDataFrame(
                [
                    {"Num": 42, "Float": 3.14159265},
                ]
            )

            df = df.withColumn(
                "Padded",
                F.format_string("%05d", F.col("Num")),
            )
            df = df.withColumn(
                "Precise",
                F.format_string("%.3f", F.col("Float")),
            )

            rows = df.collect()
            assert len(rows) == 1
            assert rows[0]["Padded"] == "00042"
            assert rows[0]["Precise"] == "3.142"
        finally:
            spark.stop()

    def test_format_string_long_strings(self):
        """Test format_string with very long strings."""
        import inspect

        test_name = inspect.stack()[1].function
        spark = SparkSession.builder.appName(
            self._get_unique_app_name(test_name)
        ).getOrCreate()
        try:
            long_str = "x" * 1000
            df = spark.createDataFrame(
                [
                    {"Long": long_str, "Num": 123},
                ]
            )

            df = df.withColumn(
                "Result",
                F.format_string("%s-%d", F.col("Long"), F.col("Num")),
            )

            rows = df.collect()
            assert len(rows) == 1
            result = rows[0]["Result"]
            assert len(result) == 1004  # 1000 + "-" + "123"
            assert result.startswith("x" * 1000)
            assert result.endswith("-123")
        finally:
            spark.stop()