Skip to content

Add most functions to the Expr class so that they're chainable. #1064

Open
@deanm0000

Description

@deanm0000

Is your feature request related to a problem or challenge? Please describe what you are trying to do.
Instead of doing

df.select(
    F.abs(
        col("id1")
        )
    .alias("id1")
    )

It would be nicer to do

df.select(
    col("id1")
    .abs()
    .alias("id1")
    )

Describe the solution you'd like
This is already partly there, for example, alias is already in the Expr class. It would be a bit tedious but easy to add under class Expr, for example:

def abs(self) -> datafusion.Expr:
    """Return the absolute value of a given number.

    Returns:
    --------
    Expr
        A new expression representing the absolute value of the input expression.
    """
    return F.abs(self)

Describe alternatives you've considered
if it weren't for the type hinter, monkey patching.

Additional context
There will still be functions that don't make sense to chain off of a call to col such as when since it doesn't return an Expr. But, even functions that take multiple inputs can have this for instance col("a").atan2("b"). Additionally, this is completely backwards compatible since I'm not proposing eliminating the functions module.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions