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
"""
Tests for Column.substr() method.

These tests ensure that:
1. The substr method works correctly on Column objects
2. Behavior matches F.substring() function
3. Edge cases are handled properly (nulls, out of bounds, etc.)
4. Integration with DataFrame operations works
5. Behavior matches PySpark exactly

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

from sparkless.testing import get_imports

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


class TestColumnSubstr:
    """Test Column.substr() method."""

    def test_basic_substr(self, spark):
        """Test basic substr usage: F.col("name").substr(1, 2)."""
        schema = StructType([StructField("name", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"name": "Alice"},
                {"name": "Bob"},
                {"name": "Charlie"},
            ],
            schema=schema,
        )

        result = df.select(F.col("name").substr(1, 2).alias("partial_name"))
        rows = result.collect()

        assert len(rows) == 3
        assert rows[0]["partial_name"] == "Al"  # "Alice"[0:2] = "Al"
        assert rows[1]["partial_name"] == "Bo"  # "Bob"[0:2] = "Bo"
        assert rows[2]["partial_name"] == "Ch"  # "Charlie"[0:2] = "Ch"

    def test_substr_from_second_position(self, spark):
        """Test substr starting from position 2."""
        schema = StructType([StructField("name", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"name": "Alice"},
                {"name": "Bob"},
            ],
            schema=schema,
        )

        # substr requires length parameter, use large length to get rest of string
        result = df.select(F.col("name").substr(2, 100).alias("from_second"))
        rows = result.collect()

        assert len(rows) == 2
        # substr(2, 100) should return from position 2 to end (or up to length)
        assert rows[0]["from_second"] == "lice"  # "Alice"[1:] = "lice"
        assert rows[1]["from_second"] == "ob"  # "Bob"[1:] = "ob"

    def test_substr_issue_238_example(self, spark):
        """Test the exact example from issue #238."""
        schema = StructType([StructField("name", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"name": "Alice"},
                {"name": "Bob"},
            ],
            schema=schema,
        )

        result = df.withColumn("partial_name", F.col("name").substr(1, 2))
        rows = result.collect()

        assert len(rows) == 2
        assert rows[0]["partial_name"] == "Al"
        assert rows[1]["partial_name"] == "Bo"

    def test_substr_start_at_one(self, spark):
        """Test substr starting at position 1."""
        schema = StructType([StructField("text", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"text": "Hello"},
                {"text": "World"},
            ],
            schema=schema,
        )

        result = df.select(F.col("text").substr(1, 3).alias("first_three"))
        rows = result.collect()

        assert len(rows) == 2
        assert rows[0]["first_three"] == "Hel"  # "Hello"[0:3] = "Hel"
        assert rows[1]["first_three"] == "Wor"  # "World"[0:3] = "Wor"

    def test_substr_start_beyond_length(self, spark):
        """Test substr with start position beyond string length."""
        schema = StructType([StructField("text", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"text": "Hi"},
                {"text": "No"},
            ],
            schema=schema,
        )

        result = df.select(F.col("text").substr(10, 5).alias("beyond"))
        rows = result.collect()

        assert len(rows) == 2
        # When start is beyond length, should return empty string
        assert rows[0]["beyond"] == ""
        assert rows[1]["beyond"] == ""

    def test_substr_with_null(self, spark):
        """Test substr with null values."""
        schema = StructType([StructField("name", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"name": "Alice"},
                {"name": None},
                {"name": "Bob"},
            ],
            schema=schema,
        )

        result = df.select(F.col("name").substr(1, 2).alias("partial"))
        rows = result.collect()

        assert len(rows) == 3
        assert rows[0]["partial"] == "Al"
        assert rows[1]["partial"] is None  # Null input should return null
        assert rows[2]["partial"] == "Bo"

    def test_substr_length_zero(self, spark):
        """Test substr with length=0."""
        schema = StructType([StructField("text", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"text": "Hello"},
            ],
            schema=schema,
        )

        result = df.select(F.col("text").substr(1, 0).alias("empty"))
        rows = result.collect()

        assert len(rows) == 1
        assert rows[0]["empty"] == ""  # Length 0 should return empty string

    def test_substr_length_exceeds_remaining(self, spark):
        """Test substr with length exceeding remaining characters."""
        schema = StructType([StructField("text", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"text": "Hi"},
            ],
            schema=schema,
        )

        result = df.select(F.col("text").substr(1, 100).alias("long"))
        rows = result.collect()

        assert len(rows) == 1
        # Should return all remaining characters, not error
        assert rows[0]["long"] == "Hi"

    def test_substr_in_select(self, spark):
        """Test substr in select operation."""
        schema = StructType([StructField("name", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"name": "Alice"},
                {"name": "Bob"},
            ],
            schema=schema,
        )

        result = df.select(
            F.col("name"),
            F.col("name").substr(1, 2).alias("first_two"),
            F.col("name")
            .substr(3, 100)
            .alias("from_third"),  # Use large length to get rest
        )
        rows = result.collect()

        assert len(rows) == 2
        assert rows[0]["name"] == "Alice"
        assert rows[0]["first_two"] == "Al"
        assert rows[0]["from_third"] == "ice"
        assert rows[1]["name"] == "Bob"
        assert rows[1]["first_two"] == "Bo"
        assert rows[1]["from_third"] == "b"

    def test_substr_in_withColumn(self, spark):
        """Test substr in withColumn operation."""
        schema = StructType([StructField("name", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"name": "Alice"},
                {"name": "Bob"},
            ],
            schema=schema,
        )

        result = df.withColumn("first_char", F.col("name").substr(1, 1))
        rows = result.collect()

        assert len(rows) == 2
        assert rows[0]["first_char"] == "A"
        assert rows[1]["first_char"] == "B"

    def test_substr_in_filter(self, spark):
        """Test substr in filter condition."""
        schema = StructType([StructField("name", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"name": "Alice"},
                {"name": "Bob"},
                {"name": "Charlie"},
            ],
            schema=schema,
        )

        # Filter where first character is 'A'
        result = df.filter(F.col("name").substr(1, 1) == "A")
        rows = result.collect()

        assert len(rows) == 1
        assert rows[0]["name"] == "Alice"

    def test_substr_in_orderBy(self, spark):
        """Test substr in orderBy operation."""
        schema = StructType([StructField("name", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"name": "Charlie"},
                {"name": "Alice"},
                {"name": "Bob"},
            ],
            schema=schema,
        )

        # Order by first character
        result = df.orderBy(F.col("name").substr(1, 1))
        rows = result.collect()

        assert len(rows) == 3
        assert rows[0]["name"] == "Alice"  # 'A'
        assert rows[1]["name"] == "Bob"  # 'B'
        assert rows[2]["name"] == "Charlie"  # 'C'

    def test_substr_equals_substring_function(self, spark):
        """Test that substr method produces same results as substring function when both have length."""
        schema = StructType([StructField("name", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"name": "Alice"},
                {"name": "Bob"},
            ],
            schema=schema,
        )

        result_substr = df.select(F.col("name").substr(1, 2).alias("partial"))
        result_substring = df.select(F.substring(F.col("name"), 1, 2).alias("partial"))

        rows_substr = result_substr.collect()
        rows_substring = result_substring.collect()

        assert len(rows_substr) == len(rows_substring)
        assert rows_substr[0]["partial"] == rows_substring[0]["partial"]
        assert rows_substr[1]["partial"] == rows_substring[1]["partial"]

    def test_substr_chained_operations(self, spark):
        """Test substr with chained column operations."""
        schema = StructType([StructField("name", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"name": "Alice"},
                {"name": "Bob"},
            ],
            schema=schema,
        )

        # Chain substr with upper
        result = df.select(F.col("name").substr(1, 2).alias("partial"))
        rows = result.collect()

        assert len(rows) == 2
        assert rows[0]["partial"] == "Al"
        assert rows[1]["partial"] == "Bo"

    def test_substr_empty_string(self, spark):
        """Test substr on empty string."""
        schema = StructType([StructField("text", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"text": ""},
                {"text": "Hello"},
            ],
            schema=schema,
        )

        result = df.select(F.col("text").substr(1, 2).alias("partial"))
        rows = result.collect()

        assert len(rows) == 2
        assert rows[0]["partial"] == ""  # Empty string substr returns empty
        assert rows[1]["partial"] == "He"

    def test_substr_unicode(self, spark):
        """Test substr with unicode characters."""
        schema = StructType([StructField("text", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"text": "Hello世界"},
                {"text": "测试"},
            ],
            schema=schema,
        )

        result = df.select(F.col("text").substr(1, 5).alias("partial"))
        rows = result.collect()

        assert len(rows) == 2
        # Unicode handling - should work correctly
        assert rows[0]["partial"] == "Hello"  # First 5 characters
        assert rows[1]["partial"] == "测试"  # First 2 unicode characters

    def test_substr_negative_start(self, spark):
        """Test substr with negative start positions (PySpark counts from end)."""
        schema = StructType([StructField("text", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"text": "Hello"},
            ],
            schema=schema,
        )

        # Test various negative start positions
        # PySpark: negative start counts from end (-1 = last char, -2 = second-to-last, etc.)
        test_cases = [
            (-5, 3, "Hel"),  # -5 from end of "Hello" (len=5) = position 0
            (-4, 3, "ell"),  # -4 from end = position 1
            (-3, 3, "llo"),  # -3 from end = position 2
            (-2, 3, "lo"),  # -2 from end = position 3, but only 2 chars remain
            (-1, 3, "o"),  # -1 from end = position 4 (last char), only 1 char
        ]

        for start, length, expected in test_cases:
            result = df.select(F.col("text").substr(start, length).alias("partial"))
            rows = result.collect()
            assert rows[0]["partial"] == expected, f"substr({start}, {length}) failed"

    def test_substr_zero_start(self, spark):
        """Test substr with start=0 (PySpark treats as start=1)."""
        schema = StructType([StructField("text", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"text": "Hello"},
            ],
            schema=schema,
        )

        result = df.select(F.col("text").substr(0, 3).alias("partial"))
        rows = result.collect()

        assert len(rows) == 1
        # PySpark treats start=0 as start=1
        assert rows[0]["partial"] == "Hel"

    def test_substr_with_alias(self, spark):
        """Test substr with alias."""
        schema = StructType([StructField("name", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"name": "Alice"},
            ],
            schema=schema,
        )

        result = df.select(F.col("name").substr(1, 2).alias("first_two_chars"))
        rows = result.collect()

        assert len(rows) == 1
        assert "first_two_chars" in rows[0]
        assert rows[0]["first_two_chars"] == "Al"

    def test_substr_pyspark_parity_comprehensive(self, spark):
        """Comprehensive test to verify substr matches PySpark behavior for common cases."""
        schema = StructType([StructField("text", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"text": "Hello"},
                {"text": "World"},
                {"text": "Test"},
                {"text": ""},
                {"text": None},
            ],
            schema=schema,
        )

        # Test cases: (start, length, expected_results)
        # Expected results based on actual PySpark behavior (verified with real PySpark)
        # Note: Very negative starts have complex behavior, so we test common cases
        test_cases = [
            (1, 3, ["Hel", "Wor", "Tes", "", None]),
            (1, 0, ["", "", "", "", None]),
            (1, 100, ["Hello", "World", "Test", "", None]),
            (2, 3, ["ell", "orl", "est", "", None]),
            (0, 3, ["Hel", "Wor", "Tes", "", None]),  # start=0 treated as start=1
            (
                -1,
                3,
                ["o", "d", "t", "", None],
            ),  # negative start from end (-1 = last char)
            (
                -2,
                3,
                ["lo", "ld", "st", "", None],
            ),  # negative start from end (-2 = second-to-last)
        ]

        for start, length, expected in test_cases:
            result = df.select(F.col("text").substr(start, length).alias("result"))
            rows = result.collect()
            for i, row in enumerate(rows):
                assert row["result"] == expected[i], (
                    f"substr({start}, {length}) failed for row {i} (text={df.collect()[i]['text']!r}): "
                    f"expected {expected[i]!r}, got {row['result']!r}"
                )

    def test_substr_in_groupBy(self, spark):
        """Test substr in groupBy aggregation."""
        schema = StructType(
            [
                StructField("name", StringType(), True),
                StructField("value", StringType(), True),
            ]
        )
        df = spark.createDataFrame(
            [
                {"name": "Alice", "value": "A1"},
                {"name": "Alice", "value": "A2"},
                {"name": "Bob", "value": "B1"},
            ],
            schema=schema,
        )

        # Group by first character of name - create a column first
        df_with_first_char = df.withColumn("first_char", F.col("name").substr(1, 1))
        result = df_with_first_char.groupBy("first_char").agg(
            F.count("*").alias("count")
        )
        rows = result.collect()

        assert len(rows) == 2
        # Should have one row for 'A' and one for 'B'
        first_chars = {row["first_char"]: row["count"] for row in rows}
        assert first_chars.get("A") == 2
        assert first_chars.get("B") == 1

    def test_substr_chained_with_other_operations(self, spark):
        """Test substr chained with other string operations."""
        schema = StructType([StructField("name", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"name": "Alice"},
                {"name": "Bob"},
            ],
            schema=schema,
        )

        # Chain substr with upper
        result = df.select(F.upper(F.col("name").substr(1, 2)).alias("upper_partial"))
        rows = result.collect()

        assert len(rows) == 2
        assert rows[0]["upper_partial"] == "AL"
        assert rows[1]["upper_partial"] == "BO"

    def test_substr_very_long_string(self, spark):
        """Test substr on very long strings."""
        long_string = "A" * 1000 + "B" * 1000
        schema = StructType([StructField("text", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"text": long_string},
            ],
            schema=schema,
        )

        result = df.select(F.col("text").substr(1, 100).alias("first_100"))
        rows = result.collect()

        assert len(rows) == 1
        assert rows[0]["first_100"] == "A" * 100

    def test_substr_start_exceeds_length(self, spark):
        """Test substr when start position exceeds string length."""
        schema = StructType([StructField("text", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"text": "Hi"},
            ],
            schema=schema,
        )

        result = df.select(F.col("text").substr(10, 5).alias("beyond"))
        rows = result.collect()

        assert len(rows) == 1
        # When start exceeds length, should return empty string
        assert rows[0]["beyond"] == ""

    def test_substr_negative_start_exceeds_length(self, spark):
        """Test substr with negative start that exceeds string length."""
        schema = StructType([StructField("text", StringType(), True)])
        df = spark.createDataFrame(
            [
                {"text": "Hi"},
            ],
            schema=schema,
        )

        # -10 from end of "Hi" (len=2) would be negative, should clamp to 0
        result = df.select(F.col("text").substr(-10, 3).alias("result"))
        rows = result.collect()

        assert len(rows) == 1
        # Should return from start (clamped to 0)
        assert rows[0]["result"] == "Hi"[:3] if len("Hi") >= 3 else "Hi"