Skip to content

register_spark_dataframe_namespace

laktory.api.register_spark_dataframe_namespace(name) ยค

Decorator for registering a custom namespace on PySpark's DataFrame class.

The decorated class receives the native PySpark DataFrame as its first __init__ argument, so its methods are written in pure PySpark without any Narwhals knowledge.

Use with DATAFRAME_API=NATIVE in pipeline YAML.

PARAMETER DESCRIPTION
name

Namespace name, used as func_name: "<name>.<method>" in YAML.

TYPE: str

Examples:

import pyspark.sql.functions as F
import laktory as lk


@lk.api.register_spark_dataframe_namespace("custom")
class CustomOps:
    def __init__(self, _df):
        self._df = _df

    def with_x2(self):
        return self._df.withColumn("x2", F.col("x1") * 2)
transformer:
  nodes:
    - func_name: custom.with_x2
      dataframe_api: NATIVE
References
Source code in laktory/api/namespace.py
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
def register_spark_dataframe_namespace(name: str):
    """
    Decorator for registering a custom namespace on PySpark's ``DataFrame`` class.

    The decorated class receives the **native PySpark DataFrame** as its first
    ``__init__`` argument, so its methods are written in pure PySpark without
    any Narwhals knowledge.

    Use with ``DATAFRAME_API=NATIVE`` in pipeline YAML.

    Parameters
    ----------
    name:
        Namespace name, used as ``func_name: "<name>.<method>"`` in YAML.

    Examples
    --------
    ```py
    import pyspark.sql.functions as F
    import laktory as lk


    @lk.api.register_spark_dataframe_namespace("custom")
    class CustomOps:
        def __init__(self, _df):
            self._df = _df

        def with_x2(self):
            return self._df.withColumn("x2", F.col("x1") * 2)
    ```

    ```yaml
    transformer:
      nodes:
        - func_name: custom.with_x2
          dataframe_api: NATIVE
    ```

    References
    ----------
    * [Spark Extension](https://www.laktory.ai/concepts/extension_custom/)
    """

    def wrapper(ns_cls: type):
        from pyspark.sql import DataFrame as SparkDataFrame

        setattr(SparkDataFrame, name, SparkNameSpace(name, ns_cls))
        try:
            from pyspark.sql.connect.dataframe import DataFrame as ConnectDataFrame

            setattr(ConnectDataFrame, name, SparkNameSpace(name, ns_cls))
        except ImportError:
            pass
        return ns_cls

    return wrapper