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

Implement string join in cudf-polars #17755

Open
wants to merge 4 commits into
base: branch-25.02
Choose a base branch
from
Open
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
20 changes: 17 additions & 3 deletions python/cudf_polars/cudf_polars/dsl/expressions/string.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES.
# SPDX-License-Identifier: Apache-2.0
# TODO: remove need for this
# ruff: noqa: D101
Expand Down Expand Up @@ -111,6 +111,7 @@ def __init__(

def _validate_input(self):
if self.name not in (
StringFunction.Name.ConcatVertical,
StringFunction.Name.Contains,
StringFunction.Name.EndsWith,
StringFunction.Name.Lowercase,
Expand All @@ -124,7 +125,7 @@ def _validate_input(self):
StringFunction.Name.StripCharsEnd,
StringFunction.Name.Uppercase,
):
raise NotImplementedError(f"String function {self.name}")
raise NotImplementedError(f"String function {self.name!r}")
if self.name is StringFunction.Name.Contains:
literal, strict = self.options
if not literal:
Expand Down Expand Up @@ -204,7 +205,20 @@ def do_evaluate(
mapping: Mapping[Expr, Column] | None = None,
) -> Column:
"""Evaluate this expression given a dataframe for context."""
if self.name is StringFunction.Name.Contains:
if self.name is StringFunction.Name.ConcatVertical:
(child,) = self.children
column = child.evaluate(df, context=context, mapping=mapping)
delimiter, ignore_nulls = self.options
if column.obj.null_count() > 0 and not ignore_nulls:
return Column(plc.Column.all_null_like(column.obj, 1))
return Column(
plc.strings.combine.join_strings(
column.obj,
plc.interop.from_arrow(pa.scalar(delimiter, type=pa.string())),
plc.interop.from_arrow(pa.scalar(None, type=pa.string())),
)
)
elif self.name is StringFunction.Name.Contains:
child, arg = self.children
column = child.evaluate(df, context=context, mapping=mapping)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES.
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

Expand Down Expand Up @@ -454,3 +454,10 @@ def test_string_to_numeric_invalid(numeric_type):
polars_except=pl.exceptions.InvalidOperationError,
cudf_except=pl.exceptions.ComputeError,
)


@pytest.mark.parametrize("ignore_nulls", [False, True])
@pytest.mark.parametrize("delimiter", ["", "/"])
def test_string_join(ldf, ignore_nulls, delimiter):
q = ldf.select(pl.col("a").str.join(delimiter, ignore_nulls=ignore_nulls))
assert_gpu_result_equal(q)
Loading