Skip to content

cudf-polars string slicing - #16082

Merged
rapids-bot[bot] merged 31 commits into
NVIDIA:branch-24.08from
brandon-b-miller:cudf-polars-str-slice
Jul 3, 2024
Merged

cudf-polars string slicing#16082
rapids-bot[bot] merged 31 commits into
NVIDIA:branch-24.08from
brandon-b-miller:cudf-polars-str-slice

Conversation

@brandon-b-miller

Copy link
Copy Markdown
Contributor

This PR plumbs the libcudf/pylibcudf slice_strings function through to cudf-polars. Depends on #15988

@brandon-b-miller brandon-b-miller added feature request New feature or request Python Affects Python cuDF API. non-breaking Non-breaking change cudf-polars Issues specific to cudf-polars labels Jun 25, 2024
@brandon-b-miller brandon-b-miller self-assigned this Jun 25, 2024
@brandon-b-miller
brandon-b-miller requested a review from a team as a code owner June 25, 2024 14:40
@github-actions github-actions Bot added CMake CMake build issue pylibcudf Issues specific to the pylibcudf package labels Jun 25, 2024

# libcudf slices via [start,stop).
# polars slices with offset + length where start == offset
# stop = start + length. Do this math on the host

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# stop = start + length. Do this math on the host
# stop = start + length. Do this maths on the host

;)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please check if the polars logic for slicing strings is the same as "dataframe" slicing, as implemented in

https://github.com/rapidsai/cudf/blob/ac0f79a92a77ed15d03124f2a37fb5d4364e45db/python/cudf_polars/cudf_polars/containers/dataframe.py#L212-L220

Comment thread python/cudf_polars/cudf_polars/dsl/expr.py Outdated
Comment on lines +724 to +731
stop = Literal(
expr_length.dtype,
pa.scalar(
expr_start.value.as_py() + expr_length.value.as_py(),
type=pa.int32(),
),
).evaluate(df, context=context, mapping=mapping)
start = expr_start.evaluate(df, context=context, mapping=mapping)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you know you're making scalars, you can just do plc.interop.from_arrow on the pyarrow scalar. for the one you computed.

@github-actions github-actions Bot removed the CMake CMake build issue label Jun 26, 2024
@github-actions github-actions Bot added the pylibcudf Issues specific to the pylibcudf package label Jul 1, 2024
@github-actions github-actions Bot removed the pylibcudf Issues specific to the pylibcudf package label Jul 1, 2024
@wence-

wence- commented Jul 1, 2024

Copy link
Copy Markdown
Contributor

(please merge trunk so that the cudf_polars test suite runs on this PR)

@brandon-b-miller

Copy link
Copy Markdown
Contributor Author

(please merge trunk so that the cudf_polars test suite runs on this PR)

I see some test failures here, but they seem unrelated to these changes - is there a blocking PR needed thats needed to pass things here?

polars.exceptions.ComputeError: 'cuda' conversion failed: TypeError: argument 'node': 'list' object cannot be interpreted as an integer

@vyasr

vyasr commented Jul 1, 2024

Copy link
Copy Markdown
Contributor

Possibly needs #16149?

@vyasr

vyasr commented Jul 1, 2024

Copy link
Copy Markdown
Contributor

Yeah I'm seeing all of the same failures in a completely unrelated PR #15904

@wence-

wence- commented Jul 1, 2024

Copy link
Copy Markdown
Contributor

Yeah I'm seeing all of the same failures in a completely unrelated PR #15904

This was why I didn't want these tests to block everything. But I guess that touches pylibcudf

@brandon-b-miller
brandon-b-miller requested a review from wence- July 2, 2024 13:37

@wence- wence- left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Some very minor nits

Comment thread python/cudf_polars/tests/expressions/test_stringfunction.py Outdated
Comment thread python/cudf_polars/cudf_polars/dsl/expr.py Outdated
Comment thread python/cudf_polars/cudf_polars/dsl/expr.py Outdated
Comment thread python/cudf_polars/tests/expressions/test_stringfunction.py Outdated
(-2, 2),
(-100, 3),
(0, 0),
(0, 1000),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a case where the computed start is negative and the computed stop is positive to ensure that does the right thing? e.g. suppose you have (-3, 4), I think that will produce a start stop pair of [-3, 1) which will be empty for long strings, whereas in polars it would slice from strlen - 3 to the end of the string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some logic for this.



@pytest.mark.parametrize(
"offset,length", [(1, 3), (0, 3), (0, 0), (-3, 1), (-100, 5), (1, 1), (100, 100)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these parametrizations different to those with columns above? As there, can we particularly add a case where the computed start is negative but the computed stop is non-negative (e.g. (-3, 4) or (-2, 2)? I think those will not do the right thing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged these

Comment thread python/cudf_polars/cudf_polars/dsl/expr.py Outdated
Comment thread python/cudf_polars/tests/expressions/test_stringfunction.py Outdated

@wence- wence- left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One fencepost error, and then I think we're good to go!

brandon-b-miller and others added 2 commits July 3, 2024 11:24
Co-authored-by: Lawrence Mitchell <wence@gmx.li>

@wence- wence- left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Brandon!

@wence-

wence- commented Jul 3, 2024

Copy link
Copy Markdown
Contributor

/merge

@rapids-bot
rapids-bot Bot merged commit 3aedeea into NVIDIA:branch-24.08 Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cudf-polars Issues specific to cudf-polars feature request New feature or request non-breaking Non-breaking change Python Affects Python cuDF API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants