From 550debc0edf1ea3e8e80d9d54a545019b9e579b8 Mon Sep 17 00:00:00 2001 From: Lawrence Mitchell Date: Wed, 12 Jun 2024 16:24:53 +0000 Subject: [PATCH] Add tests of implemented StringFunctions Additionally, assert that we raise during translation for an unhandled function. --- python/cudf_polars/cudf_polars/dsl/expr.py | 4 +- .../tests/expressions/test_stringfunction.py | 41 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 python/cudf_polars/tests/expressions/test_stringfunction.py diff --git a/python/cudf_polars/cudf_polars/dsl/expr.py b/python/cudf_polars/cudf_polars/dsl/expr.py index a81cdcbf0c3..bac58f73f80 100644 --- a/python/cudf_polars/cudf_polars/dsl/expr.py +++ b/python/cudf_polars/cudf_polars/dsl/expr.py @@ -689,7 +689,9 @@ def do_evaluate( ) ) else: - raise NotImplementedError(f"StringFunction {self.name}") + raise NotImplementedError( + f"StringFunction {self.name}" + ) # pragma: no cover; handled by init raising class Sort(Expr): diff --git a/python/cudf_polars/tests/expressions/test_stringfunction.py b/python/cudf_polars/tests/expressions/test_stringfunction.py new file mode 100644 index 00000000000..198f35d376b --- /dev/null +++ b/python/cudf_polars/tests/expressions/test_stringfunction.py @@ -0,0 +1,41 @@ +# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. +# SPDX-License-Identifier: Apache-2.0 +from __future__ import annotations + +import pytest + +import polars as pl + +from cudf_polars import translate_ir +from cudf_polars.testing.asserts import assert_gpu_result_equal + + +def test_supported_stringfunction_expression(): + ldf = pl.LazyFrame( + { + "a": ["a", "b", "cdefg", "h", "Wıth ünιcοde"], # noqa: RUF001 + "b": [0, 3, 1, -1, None], + } + ) + + query = ldf.select( + pl.col("a").str.starts_with("Z"), + pl.col("a").str.ends_with("h").alias("endswith_h"), + pl.col("a").str.to_lowercase().alias("lower"), + pl.col("a").str.to_uppercase().alias("upper"), + ) + assert_gpu_result_equal(query) + + +def test_unsupported_stringfunction(): + ldf = pl.LazyFrame( + { + "a": ["a", "b", "cdefg", "h", "Wıth ünιcοde"], # noqa: RUF001 + "b": [0, 3, 1, -1, None], + } + ) + + q = ldf.select(pl.col("a").str.count_matches("e", literal=True)) + + with pytest.raises(NotImplementedError): + _ = translate_ir(q._ldf.visit())