Skip to content

register_spark_column_namespace

laktory.api.register_spark_column_namespace(name) ยค

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

The decorated class receives a native PySpark Column as its first __init__ argument. Use this to build reusable column expressions that can be referenced from func_args strings when DATAFRAME_API=NATIVE.

.. note:: This namespace is NATIVE-only. A Narwhals Expr bridge is not provided because Narwhals expressions and PySpark Columns have incompatible semantics.

PARAMETER DESCRIPTION
name

Namespace name, accessible as col("x").name.method() inside evaluated func_args strings.

TYPE: str

Examples:

import laktory as lk


@lk.api.register_spark_column_namespace("custom")
class CustomColOps:
    def __init__(self, _col):
        self._col = _col

    def double(self):
        return self._col * 2

Use in a pipeline YAML:

transformer:
  nodes:
    - func_name: withColumn
      func_args:
        - x2
        - "col('x1').custom.double()"
      dataframe_api: NATIVE
References
Source code in laktory/api/namespace.py
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
def register_spark_column_namespace(name: str):
    """
    Decorator for registering a custom namespace on PySpark's ``Column`` class.

    The decorated class receives a **native PySpark Column** as its first
    ``__init__`` argument. Use this to build reusable column expressions that
    can be referenced from ``func_args`` strings when ``DATAFRAME_API=NATIVE``.

    .. note::
        This namespace is NATIVE-only. A Narwhals ``Expr`` bridge is not
        provided because Narwhals expressions and PySpark Columns have
        incompatible semantics.

    Parameters
    ----------
    name:
        Namespace name, accessible as ``col("x").name.method()`` inside
        evaluated ``func_args`` strings.

    Examples
    --------
    ```py
    import laktory as lk


    @lk.api.register_spark_column_namespace("custom")
    class CustomColOps:
        def __init__(self, _col):
            self._col = _col

        def double(self):
            return self._col * 2
    ```

    Use in a pipeline YAML:

    ```yaml
    transformer:
      nodes:
        - func_name: withColumn
          func_args:
            - x2
            - "col('x1').custom.double()"
          dataframe_api: NATIVE
    ```

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

    def wrapper(ns_cls: type):
        from pyspark.sql.column import Column

        setattr(Column, name, SparkNameSpace(name, ns_cls))
        try:
            from pyspark.sql.connect.column import Column as ConnectColumn

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

    return wrapper