-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plumb pylibcudf strings
contains_re
through cudf_polars (#15918)
This PR adds cudf-polars code for evaluating the `StringFunction.Contains` expression node. Depends on #15880 Authors: - https://github.com/brandon-b-miller - Lawrence Mitchell (https://github.com/wence-) Approvers: - Lawrence Mitchell (https://github.com/wence-) URL: #15918
- Loading branch information
1 parent
af09d3e
commit 246d017
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from __future__ import annotations | ||
|
||
from functools import partial | ||
|
||
import pytest | ||
|
||
import polars as pl | ||
|
||
from cudf_polars.callback import execute_with_cudf | ||
from cudf_polars.testing.asserts import assert_gpu_result_equal | ||
|
||
|
||
@pytest.fixture | ||
def ldf(): | ||
return pl.DataFrame( | ||
{"a": ["AbC", "de", "FGHI", "j", "kLm", "nOPq", None, "RsT", None, "uVw"]} | ||
).lazy() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"substr", | ||
[ | ||
"A", | ||
"de", | ||
".*", | ||
"^a", | ||
"^A", | ||
"[^a-z]", | ||
"[a-z]{3,}", | ||
"^[A-Z]{2,}", | ||
"j|u", | ||
], | ||
) | ||
def test_contains_regex(ldf, substr): | ||
query = ldf.select(pl.col("a").str.contains(substr)) | ||
assert_gpu_result_equal(query) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"literal", ["A", "de", "FGHI", "j", "kLm", "nOPq", "RsT", "uVw"] | ||
) | ||
def test_contains_literal(ldf, literal): | ||
query = ldf.select(pl.col("a").str.contains(pl.lit(literal), literal=True)) | ||
assert_gpu_result_equal(query) | ||
|
||
|
||
def test_contains_column(ldf): | ||
query = ldf.select(pl.col("a").str.contains(pl.col("a"), literal=True)) | ||
assert_gpu_result_equal(query) | ||
|
||
|
||
@pytest.mark.parametrize("pat", ["["]) | ||
def test_contains_invalid(ldf, pat): | ||
query = ldf.select(pl.col("a").str.contains(pat)) | ||
|
||
with pytest.raises(pl.exceptions.ComputeError): | ||
query.collect() | ||
with pytest.raises(pl.exceptions.ComputeError): | ||
query.collect(post_opt_callback=partial(execute_with_cudf, raise_on_fail=True)) |