robin-sparkless 0.8.2

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
#!/usr/bin/env python3
"""
Run common PySpark-idiomatic code against Sparkless and record failures (parity issues).

Usage:
  pip install sparkless  # if not already
  python scripts/sparkless_parity_check.py

Output: List of (check_name, exception_or_diff) for reporting to Sparkless repo.
Does not require PySpark or robin-sparkless.
"""

from __future__ import annotations

import sys
import warnings
from collections.abc import Callable
from typing import Any, List, Optional, Tuple

warnings.filterwarnings("ignore", message=".*LazyFrame.*")
warnings.filterwarnings("ignore", message=".*schema.*", module=".*materializer.*")

# (check_name, exception, note, result_or_none)
Result = Tuple[str, Optional[Exception], Optional[str], Any]


def _run(name: str, fn: Callable[[], Any]) -> Result:
    try:
        out = fn()
        return (name, None, None, out)
    except Exception as e:  # noqa: BLE001
        return (name, e, None, None)


def _rows_eq(a: Any, b: Any) -> bool:
    """Compare collect() results (list of Row or dict)."""
    if a is None and b is None:
        return True
    if a is None or b is None:
        return False
    if len(a) != len(b):
        return False
    for i, ra in enumerate(a):
        rb = b[i]
        da = (
            ra.asDict()
            if hasattr(ra, "asDict")
            else (ra if isinstance(ra, dict) else dict(ra))
        )
        db = (
            rb.asDict()
            if hasattr(rb, "asDict")
            else (rb if isinstance(rb, dict) else dict(rb))
        )
        if da != db:
            return False
    return True


def run_sparkless_checks() -> List[Result]:
    """Run Sparkless parity checks; return list of (name, exception, note, result)."""
    from sparkless.sql import SparkSession
    import sparkless.sql.functions as F

    results: List[Result] = []
    spark: Any = None

    def get_spark():
        nonlocal spark
        if spark is None:
            # Use attribute, not call (known workaround for builder())
            spark = SparkSession.builder.appName("parity_check").getOrCreate()
        return spark

    # --- Session / builder ---
    r = _run(
        "SparkSession.builder() callable",
        lambda: SparkSession.builder().appName("x").getOrCreate(),
    )
    results.append(r)

    # --- createDataFrame: list of tuples + column names ---
    def _create_and_collect():
        s = get_spark()
        data = [(1, 25, "alice"), (2, 30, "bob")]
        df = s.createDataFrame(data, ["id", "age", "name"])
        rows = df.collect()
        # Verify column order: first column should be id (value 1, 2)
        assert len(rows) == 2
        r0 = rows[0]
        d = r0.asDict() if hasattr(r0, "asDict") else dict(r0)
        if d.get("id") != 1 and d.get("age") == 1:
            raise AssertionError(
                f"createDataFrame column order wrong: first col should be id, got {list(d.keys())} -> {d}"
            )
        return rows

    results.append(
        _run("createDataFrame(tuples, names) column order", _create_and_collect)
    )

    # --- union (position-based) ---
    def _union_position():
        s = get_spark()
        data_a = [(1, 25, "a"), (2, 30, "b")]
        data_b = [(3, 22, "c"), (4, 28, "d")]
        df_a = s.createDataFrame(data_a, ["id", "age", "name"])
        df_b = s.createDataFrame(data_b, ["id", "age", "name"])
        return df_a.union(df_b).collect()

    results.append(_run("union() position-based", _union_position))

    # --- select with single str vs list ---
    def _select_single_str():
        s = get_spark()
        df = s.createDataFrame([(1, "x")], ["a", "b"])
        return df.select("a").collect()

    results.append(_run("select single column as str", _select_single_str))

    # --- filter with column expr ---
    def _filter_col():
        s = get_spark()
        df = s.createDataFrame([(1, 10), (2, 20), (3, 30)], ["id", "v"])
        return df.filter(F.col("v") >= 20).collect()

    results.append(_run("filter(F.col(...) >= literal)", _filter_col))

    # --- withColumn ---
    def _with_column():
        s = get_spark()
        df = s.createDataFrame([(1, 2)], ["a", "b"])
        return df.withColumn("c", F.col("a") + F.col("b")).collect()

    results.append(_run("withColumn expr", _with_column))

    # --- empty DataFrame count ---
    def _empty_count():
        s = get_spark()
        df = s.createDataFrame([], "a: int, b: string")
        return df.count()

    results.append(_run("createDataFrame([], schema_str) and count()", _empty_count))

    # --- limit(0) ---
    def _limit_zero():
        s = get_spark()
        df = s.createDataFrame([(1,), (2,)], ["x"])
        return df.limit(0).collect()

    results.append(_run("limit(0)", _limit_zero))

    # --- .schema and .columns ---
    def _schema_columns():
        s = get_spark()
        df = s.createDataFrame([(1, "y")], ["id", "name"])
        schema = df.schema
        cols = df.columns
        if cols != ["id", "name"]:
            raise AssertionError(
                f"df.columns order: expected ['id','name'], got {cols}"
            )
        return (str(schema), cols)

    results.append(_run("schema / columns order", _schema_columns))

    # --- createOrReplaceTempView + sql ---
    def _temp_view_sql():
        s = get_spark()
        df = s.createDataFrame([(1, "a"), (2, "b")], ["id", "label"])
        df.createOrReplaceTempView("t")
        out = s.sql("SELECT id, label FROM t WHERE id = 2").collect()
        return out

    results.append(_run("createOrReplaceTempView + sql", _temp_view_sql))

    # --- join with on as list ---
    def _join_on_list():
        s = get_spark()
        df1 = s.createDataFrame([(1, "x")], ["id", "a"])
        df2 = s.createDataFrame([(1, "y")], ["id", "b"])
        return df1.join(df2, ["id"], "inner").collect()

    results.append(_run("join(..., on=['id'], how='inner')", _join_on_list))

    # --- dropDuplicates() no args ---
    def _drop_duplicates():
        s = get_spark()
        df = s.createDataFrame([(1, 1), (1, 2), (2, 1)], ["a", "b"])
        return df.dropDuplicates().collect()

    results.append(_run("dropDuplicates() no args", _drop_duplicates))

    # --- orderBy(F.desc(...)) ---
    def _order_by_desc():
        s = get_spark()
        df = s.createDataFrame([(1, 10), (2, 30), (3, 20)], ["id", "v"])
        return df.orderBy(F.desc("v")).collect()

    results.append(_run("orderBy(F.desc('col'))", _order_by_desc))

    # --- first() on non-empty ---
    def _first():
        s = get_spark()
        df = s.createDataFrame([(1, "a")], ["id", "n"])
        return df.first()

    results.append(_run("first()", _first))

    # --- toPandas ---
    def _to_pandas():
        s = get_spark()
        df = s.createDataFrame([(1, "x")], ["a", "b"])
        return df.toPandas()

    results.append(_run("toPandas()", _to_pandas))

    # --- fillna ---
    def _fillna():
        s = get_spark()
        df = s.createDataFrame([(1, None), (2, "y")], ["id", "name"])
        return df.fillna("default").collect()

    results.append(_run("fillna(value)", _fillna))

    # --- replace ---
    def _replace():
        s = get_spark()
        df = s.createDataFrame([(1, "a"), (2, "b"), (3, "a")], ["id", "c"])
        return df.replace("a", "A").collect()

    results.append(_run("replace(old, new)", _replace))

    # --- distinct() ---
    def _distinct():
        s = get_spark()
        df = s.createDataFrame([(1,), (1,), (2,)], ["x"])
        return df.distinct().collect()

    results.append(_run("distinct()", _distinct))

    # --- createDataFrame from list of dicts ---
    def _create_from_dicts():
        s = get_spark()
        data = [{"id": 1, "name": "a"}, {"id": 2, "name": "b"}]
        df = s.createDataFrame(data)
        return df.collect()

    results.append(_run("createDataFrame(list of dicts)", _create_from_dicts))

    # --- groupBy().agg(F.sum(...)) ---
    def _groupby_agg():
        s = get_spark()
        df = s.createDataFrame([(1, 10), (1, 20), (2, 5)], ["k", "v"])
        return df.groupBy("k").agg(F.sum("v").alias("total")).collect()

    results.append(_run("groupBy().agg(F.sum().alias())", _groupby_agg))

    # --- Window + row_number ---
    def _window_row_number():
        from sparkless.sql import Window

        s = get_spark()
        df = s.createDataFrame([("a", 1), ("a", 2), ("b", 1)], ["g", "v"])
        w = Window.partitionBy("g").orderBy("v")
        return df.withColumn("rn", F.row_number().over(w)).collect()

    results.append(
        _run("Window.partitionBy().orderBy() + row_number()", _window_row_number)
    )

    # --- F.when / F.otherwise ---
    def _when_otherwise():
        s = get_spark()
        df = s.createDataFrame([(1,), (2,), (3,)], ["x"])
        return df.withColumn(
            "label", F.when(F.col("x") < 2, "low").otherwise("high")
        ).collect()

    results.append(_run("F.when().otherwise()", _when_otherwise))

    # --- coalesce (repartition) ---
    def _coalesce():
        s = get_spark()
        df = s.createDataFrame([(1,), (2,)], ["x"])
        return df.coalesce(1).count()

    results.append(_run("coalesce(1)", _coalesce))

    # --- df.write.format().mode().save (optional; may need path) ---
    def _write_parquet():
        import tempfile
        import os

        s = get_spark()
        df = s.createDataFrame([(1, "a")], ["id", "n"])
        with tempfile.TemporaryDirectory() as d:
            path = os.path.join(d, "out")
            df.write.format("parquet").mode("overwrite").save(path)
        return "ok"

    results.append(
        _run("write.format('parquet').mode('overwrite').save(path)", _write_parquet)
    )

    # --- StructType schema in createDataFrame ---
    def _create_struct_schema():
        try:
            from sparkless.sql import types as sparkless_types

            StructType = sparkless_types.StructType
            StructField = sparkless_types.StructField
            IntegerType = sparkless_types.IntegerType
            StringType = sparkless_types.StringType
        except (ImportError, AttributeError):
            from pyspark.sql.types import (
                StructType,
                StructField,
                IntegerType,
                StringType,
            )
        s = get_spark()
        schema = StructType(
            [StructField("id", IntegerType()), StructField("name", StringType())]
        )
        df = s.createDataFrame([(1, "x"), (2, "y")], schema)
        return df.collect()

    results.append(
        _run("createDataFrame with StructType schema", _create_struct_schema)
    )

    # --- F.col with cast ---
    def _col_cast():
        s = get_spark()
        df = s.createDataFrame([(1, "2"), (3, "4")], ["a", "b"])
        return df.select(F.col("a"), F.col("b").cast("int")).collect()

    results.append(_run("F.col().cast('int')", _col_cast))

    # --- describe / summary ---
    def _summary():
        s = get_spark()
        df = s.createDataFrame([(1, "a"), (2, "b")], ["id", "name"])
        return df.summary().collect()

    results.append(_run("summary()", _summary))

    # --- subtract (except) ---
    def _subtract():
        s = get_spark()
        a = s.createDataFrame([(1,), (2,), (3,)], ["x"])
        b = s.createDataFrame([(2,)], ["x"])
        return a.subtract(b).collect()

    results.append(_run("subtract(other)", _subtract))

    # --- intersect ---
    def _intersect():
        s = get_spark()
        a = s.createDataFrame([(1,), (2,)], ["x"])
        b = s.createDataFrame([(2,), (3,)], ["x"])
        return a.intersect(b).collect()

    results.append(_run("intersect(other)", _intersect))

    # --- crossJoin ---
    def _cross_join():
        s = get_spark()
        a = s.createDataFrame([(1,)], ["a"])
        b = s.createDataFrame([(10,), (20,)], ["b"])
        return a.crossJoin(b).collect()

    results.append(_run("crossJoin(other)", _cross_join))

    # --- withColumnRenamed ---
    def _with_column_renamed():
        s = get_spark()
        df = s.createDataFrame([(1, "x")], ["a", "b"])
        return df.withColumnRenamed("a", "id").columns

    results.append(_run("withColumnRenamed(old, new)", _with_column_renamed))

    # --- drop with list ---
    def _drop_columns():
        s = get_spark()
        df = s.createDataFrame([(1, 2, 3)], ["a", "b", "c"])
        return df.drop("b").columns

    results.append(_run("drop('col')", _drop_columns))

    # --- countDistinct ---
    def _count_distinct():
        s = get_spark()
        df = s.createDataFrame([(1,), (1,), (2,)], ["x"])
        return df.agg(F.countDistinct("x")).collect()

    results.append(_run("agg(F.countDistinct('col'))", _count_distinct))

    # --- alias in select ---
    def _select_alias():
        s = get_spark()
        df = s.createDataFrame([(1, 10)], ["a", "b"])
        return df.select(
            F.col("a").alias("id"), (F.col("b") * 2).alias("double_b")
        ).collect()

    results.append(_run("select with alias", _select_alias))

    # --- orderBy with list of columns ---
    def _order_by_list():
        s = get_spark()
        df = s.createDataFrame([(1, 2, 3), (1, 1, 4), (2, 0, 5)], ["a", "b", "c"])
        return df.orderBy(["a", "b"]).collect()

    results.append(_run("orderBy([list of columns])", _order_by_list))

    # --- na.drop ---
    def _na_drop():
        s = get_spark()
        df = s.createDataFrame([(1, None), (2, "y")], ["id", "name"])
        return df.na.drop().collect()

    results.append(_run("na.drop()", _na_drop))

    # --- F.struct ---
    def _struct():
        s = get_spark()
        df = s.createDataFrame([(1, "a", 10)], ["id", "n", "v"])
        return df.select(F.struct(F.col("id"), F.col("n")).alias("s")).collect()

    results.append(_run("F.struct(...)", _struct))

    # --- F.array ---
    def _array():
        s = get_spark()
        df = s.createDataFrame([(1, 2, 3)], ["a", "b", "c"])
        return df.select(F.array(F.col("a"), F.col("b")).alias("arr")).collect()

    results.append(_run("F.array(...)", _array))

    return results


def main() -> int:
    try:
        from sparkless.sql import SparkSession  # noqa: F401
    except ImportError:
        print("sparkless not installed. pip install sparkless", file=sys.stderr)
        return 1

    results: List[Result] = run_sparkless_checks()
    failed: List[Tuple[str, Exception]] = [
        (n, e) for n, e, _, _ in results if e is not None
    ]
    passed = [n for n, e, _, _ in results if e is None]

    print("--- Sparkless parity checks ---")
    print(f"Passed: {len(passed)}")
    print(f"Failed: {len(failed)}")
    if failed:
        print("\nFailed checks (parity issues):")
        for name, err in failed:
            print(f"  [{name}]")
            print(f"    {type(err).__name__}: {err}")
    if passed:
        print(
            "\nPassed:", ", ".join(passed[:15]) + (" ..." if len(passed) > 15 else "")
        )

    return 1 if failed else 0


if __name__ == "__main__":
    sys.exit(main())