Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-48593][PYTHON][CONNECT] Fix the string representation of lambda function #46948

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions python/pyspark/sql/connect/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ def to_plan(self, session: "SparkConnectClient") -> proto.Expression:
return expr

def __repr__(self) -> str:
return f"(UnresolvedNamedLambdaVariable({', '.join(self._name_parts)})"
return ", ".join(self._name_parts)

@staticmethod
def fresh_var_name(name: str) -> str:
Expand Down Expand Up @@ -959,7 +959,10 @@ def to_plan(self, session: "SparkConnectClient") -> proto.Expression:
return expr

def __repr__(self) -> str:
return f"(LambdaFunction({str(self._function)}, {', '.join(self._arguments)})"
return (
f"LambdaFunction({str(self._function)}, "
+ f"{', '.join([str(arg) for arg in self._arguments])})"
)


class WindowExpression(Expression):
Expand Down
22 changes: 22 additions & 0 deletions python/pyspark/sql/tests/connect/test_connect_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,28 @@ def test_distributed_sequence_id(self):
expected.collect(),
)

def test_lambda_str_representation(self):
from pyspark.sql.connect.expressions import UnresolvedNamedLambdaVariable

# forcely clear the internal increasing id,
# otherwise the string representation varies with this id
UnresolvedNamedLambdaVariable._nextVarNameId = 0

c = CF.array_sort(
"data",
lambda x, y: CF.when(x.isNull() | y.isNull(), CF.lit(0)).otherwise(
CF.length(y) - CF.length(x)
),
)

self.assertEqual(
str(c),
(
"""Column<'array_sort(data, LambdaFunction(CASE WHEN or(isNull(x_0), """
"""isNull(y_1)) THEN 0 ELSE -(length(y_1), length(x_0)) END, x_0, y_1))'>"""
),
)


if __name__ == "__main__":
import unittest
Expand Down