8000 [PySpark] - Add `expr` function by mariotaddeucci · Pull Request #16468 · duckdb/duckdb · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

[PySpark] - Add expr function #16468

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

Merged
merged 2 commits into from
Mar 11, 2025
Merged
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
34 changes: 33 additions & 1 deletion tools/pythonpkg/duckdb/experimental/spark/sql/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
ConstantExpression,
Expression,
FunctionExpression,
LambdaExpression
LambdaExpression,
SQLExpression,
)
if TYPE_CHECKING:
from .dataframe import DataFrame
Expand Down Expand Up @@ -6056,6 +6057,37 @@ def instr(str: "ColumnOrName", substr: str) -> Column:
"""
return _invoke_function("instr", _to_column_expr(str), ConstantExpression(substr))

def expr(str: str) -> Column:
"""Parses the expression string into the column that it represents

.. versionadded:: 1.5.0

.. versionchanged:: 3.4.0
Supports Spark Connect.

Parameters
----------
str : str
expression defined in string.

Returns
-------
:class:`~pyspark.sql.Column`
column representing the expression.

Examples
--------
>>> df = spark.createDataFrame([["Alice"], ["Bob"]], ["name"])
>>> df.select("name", expr("length(name)")).show()
+-----+------------+
| name|length(name)|
+-----+------------+
|Alice| 5|
| Bob| 3|
+-----+------------+
"""
return Column(SQLExpression(str))

def broadcast(df: "DataFrame") -> "DataFrame":
"""
The broadcast function in Spark is used to optimize joins by broadcasting a smaller
Expand Down
16 changes: 16 additions & 0 deletions tools/pythonpkg/tests/fast/spark/test_spark_functions_expr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest
from spark_namespace.sql import functions as F
from spark_namespace.sql.types import Row

_ = pytest.importorskip("duckdb.experimental.spark")


class TestSparkFunctionsExpr(object):
def test_expr(self, spark):
df = spark.createDataFrame([["Alice"], ["Bob"]], ["name"])
res = df.select("name", F.expr("length(name)").alias("str_len")).collect()

assert res == [
Row(name="Alice", str_len=5),
Row(name="Bob", str_len=3),
]
Loading
0